From ffddc2187416cffff99d0b44d7723835e82bdf01 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Fri, 13 Sep 2024 09:45:23 +0800 Subject: [PATCH] internal_send function --- src/async_session.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/async_session.rs b/src/async_session.rs index af9047e..fcda093 100644 --- a/src/async_session.rs +++ b/src/async_session.rs @@ -85,6 +85,10 @@ impl AsyncSession { } pub async fn send(&self, buf: &[u8]) -> std::io::Result { + self.internal_send(buf) + } + + fn internal_send(&self, buf: &[u8]) -> std::io::Result { let packet = self.session.allocate_send_packet(buf.len() as _)?; packet.bytes.copy_from_slice(buf); self.session.send_packet(packet); @@ -122,8 +126,8 @@ 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)))); + use std::io::{Error, ErrorKind::Other}; + return Poll::Ready(Err(Error::new(Other, format!("Lock task failed: {}", e)))); } }; self.read_state = match Pin::new(&mut *task_guard).poll(cx) { @@ -143,10 +147,7 @@ impl AsyncRead for AsyncSession { impl AsyncWrite for AsyncSession { fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8]) -> Poll> { - 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> {