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

Add async send and recv functions to AsyncSession #2

Merged
merged 3 commits into from
Sep 13, 2024
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
51 changes: 44 additions & 7 deletions src/async_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,56 @@ impl AsyncSession {
e => panic!("WaitForMultipleObjects returned unexpected value {:?}", e),
}
}

pub async fn recv(&self, buf: &mut [u8]) -> std::io::Result<usize> {
loop {
match self.session.try_receive() {
Ok(Some(packet)) => {
let size = packet.bytes.len();
if buf.len() < size {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Buffer too small"));
}
buf[..size].copy_from_slice(&packet.bytes[..size]);
return Ok(size);
}
Ok(None) => {
let read_event = self.session.get_read_wait_event()?;
let shutdown_event = self.session.shutdown_event.get_handle();
match blocking::unblock(move || Self::wait_for_read(read_event, shutdown_event)).await {
WaitingStopReason::Shutdown => {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Shutdown"));
}
WaitingStopReason::Ready => continue,
}
}
Err(err) => return Err(std::io::Error::new(std::io::ErrorKind::Other, err)),
}
}
}

pub async fn send(&self, buf: &[u8]) -> std::io::Result<usize> {
self.internal_send(buf)
}

fn internal_send(&self, buf: &[u8]) -> std::io::Result<usize> {
let packet = self.session.allocate_send_packet(buf.len() as _)?;
packet.bytes.copy_from_slice(buf);
self.session.send_packet(packet);
Ok(buf.len())
}
}

impl AsyncRead for AsyncSession {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll<std::io::Result<usize>> {
use std::io::{Error, ErrorKind::Other};
loop {
match &mut self.read_state {
ReadState::Idle => match self.session.try_receive() {
Ok(Some(packet)) => {
let size = packet.bytes.len().min(buf.len());
let size = packet.bytes.len();
if buf.len() < size {
return Poll::Ready(Err(Error::new(Other, "Buffer too small")));
}
buf[..size].copy_from_slice(&packet.bytes[..size]);
return Poll::Ready(Ok(size));
}
Expand All @@ -89,8 +130,7 @@ impl AsyncRead for AsyncSession {
Ok(guard) => guard,
Err(e) => {
self.read_state = ReadState::Waiting(Some(task));
use std::io::{Error, ErrorKind};
return Poll::Ready(Err(Error::new(ErrorKind::Other, format!("Lock task failed: {}", e))));
return Poll::Ready(Err(Error::new(Other, format!("Lock task failed: {}", e))));
}
};
self.read_state = match Pin::new(&mut *task_guard).poll(cx) {
Expand All @@ -110,10 +150,7 @@ impl AsyncRead for AsyncSession {

impl AsyncWrite for AsyncSession {
fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
let packet = self.session.allocate_send_packet(buf.len() as _)?;
packet.bytes.copy_from_slice(buf);
self.session.send_packet(packet);
Poll::Ready(Ok(buf.len()))
Poll::Ready(Ok(self.internal_send(buf)?))
}

fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Expand Down