Skip to content

Commit

Permalink
Refactor lib.rs (#4729)
Browse files Browse the repository at this point in the history
* Implement default for Listener and enhance its methods for better functionality

* Add shutdown and parameter retrieval methods to Stream; clean up Listener methods

* Implement Sync and Send traits for QUIC structures
  • Loading branch information
masa-koz authored Jan 2, 2025
1 parent 38469af commit 453fb17
Showing 1 changed file with 83 additions and 10 deletions.
93 changes: 83 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,26 +1367,36 @@ pub struct Api {
pub struct Registration {
handle: Handle,
}
unsafe impl Sync for Registration {}
unsafe impl Send for Registration {}

/// Specifies how to configure a connection.
pub struct Configuration {
handle: Handle,
}
unsafe impl Sync for Configuration {}
unsafe impl Send for Configuration {}

/// A single QUIC connection.
pub struct Connection {
handle: Handle,
}
unsafe impl Sync for Connection {}
unsafe impl Send for Connection {}

/// A single server listener
pub struct Listener {
handle: Handle,
}
unsafe impl Sync for Listener {}
unsafe impl Send for Listener {}

/// A single QUIC stream on a parent connection.
pub struct Stream {
handle: Handle,
}
unsafe impl Sync for Stream {}
unsafe impl Send for Stream {}

impl From<&str> for Buffer {
fn from(data: &str) -> Buffer {
Expand Down Expand Up @@ -1857,32 +1867,43 @@ impl Drop for Connection {
}
}

impl Default for Listener {
fn default() -> Self {
Self::new()
}
}

impl Listener {
pub fn new(
pub fn new() -> Listener {
Listener {
handle: ptr::null(),
}
}

pub fn open(
&self,
registration: &Registration,
handler: ListenerEventHandler,
context: *const c_void,
) -> Result<Listener, u32> {
let new_listener: Handle = ptr::null();
) -> Result<(), u32> {
let status = unsafe {
((*APITABLE).listener_open)(registration.handle, handler, context, &new_listener)
((*APITABLE).listener_open)(registration.handle, handler, context, &self.handle)
};
if Status::failed(status) {
return Err(status);
}

Ok(Listener {
handle: new_listener,
})
Ok(())
}

pub fn start(&self, alpn: &[Buffer], local_address: &Addr) -> Result<(), u32> {
pub fn start(&self, alpn: &[Buffer], local_address: Option<&Addr>) -> Result<(), u32> {
let status = unsafe {
((*APITABLE).listener_start)(
self.handle,
alpn.as_ptr(),
alpn.len() as u32,
local_address,
local_address
.map(|addr| addr as *const _)
.unwrap_or(ptr::null()),
)
};
if Status::failed(status) {
Expand All @@ -1891,6 +1912,29 @@ impl Listener {
Ok(())
}

pub fn stop(&self) {
unsafe {
((*APITABLE).listener_stop)(self.handle);
}
}

pub fn get_local_addr(&self) -> Result<Addr, u32> {
let mut addr_buffer: [u8; mem::size_of::<Addr>()] = [0; mem::size_of::<Addr>()];
let addr_size_mut = mem::size_of::<Addr>();
let status = unsafe {
((*APITABLE).get_param)(
self.handle,
PARAM_LISTENER_LOCAL_ADDRESS,
(&addr_size_mut) as *const usize as *const u32 as *mut u32,
addr_buffer.as_mut_ptr() as *const c_void,
)
};
if Status::failed(status) {
return Err(status);
}
Ok(unsafe { *(addr_buffer.as_ptr() as *const c_void as *const Addr) })
}

pub fn close(&self) {
unsafe {
((*APITABLE).listener_close)(self.handle);
Expand Down Expand Up @@ -1945,6 +1989,14 @@ impl Stream {
Ok(())
}

pub fn shutdown(&self, flags: StreamShutdownFlags, error_code: u62) -> Result<(), u32> {
let status = unsafe { ((*APITABLE).stream_shutdown)(self.handle, flags, error_code) };
if Status::failed(status) {
return Err(status);
}
Ok(())
}

pub fn close(&self) {
unsafe {
((*APITABLE).stream_close)(self.handle);
Expand Down Expand Up @@ -1978,6 +2030,27 @@ impl Stream {
((*APITABLE).set_callback_handler)(self.handle, handler as *const c_void, context)
};
}

pub fn get_param(
&self,
param: u32,
buffer_length: *mut u32,
buffer: *const c_void,
) -> Result<(), u32> {
let status = unsafe { ((*APITABLE).get_param)(self.handle, param, buffer_length, buffer) };
if Status::failed(status) {
return Err(status);
}
Ok(())
}

pub fn receive_complete(&self, buffer_length: u64) -> Result<(), u32> {
let status = unsafe { ((*APITABLE).stream_receive_complete)(self.handle, buffer_length) };
if Status::failed(status) {
return Err(status);
}
Ok(())
}
}

impl Drop for Stream {
Expand Down

0 comments on commit 453fb17

Please sign in to comment.