Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lib.rs #4729

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 83 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,26 +1262,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 @@ -1746,32 +1756,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 @@ -1780,6 +1801,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 @@ -1834,6 +1878,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 @@ -1867,6 +1919,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
Loading