2828
2929//! Utility module for TCP client or server.
3030
31+ use bp3d_debug:: warning;
3132use std:: fmt:: { Debug , Display , Formatter } ;
3233use std:: io:: { Error , ErrorKind , IoSlice } ;
3334use std:: net:: SocketAddr ;
3435use std:: pin:: Pin ;
3536use std:: task:: { Context , Poll } ;
36- use bp3d_debug:: warning;
3737use tokio:: io:: { AsyncRead , AsyncWrite , Interest , ReadBuf } ;
3838use tokio:: net:: tcp:: { OwnedReadHalf , OwnedWriteHalf } ;
3939use tokio:: net:: TcpStream ;
4040use tokio:: sync:: mpsc;
4141
42- use tokio:: sync:: Semaphore ;
4342use crate :: tcp:: buffer:: Bytes ;
43+ use tokio:: sync:: Semaphore ;
4444
4545#[ derive( Clone , Debug ) ]
4646pub ( super ) struct DataMsg {
4747 pub ( super ) synchro : * const Semaphore ,
4848 pub ( super ) buffer : * const u8 ,
4949 pub ( super ) buffer_size : usize ,
5050 #[ allow( dead_code) ]
51- pub ( super ) net_id : usize
51+ pub ( super ) net_id : usize ,
5252}
5353
5454unsafe impl Send for DataMsg { }
@@ -60,14 +60,14 @@ pub enum SendError {
6060 IsExiting ,
6161
6262 /// The broadcast channel is already closed.
63- Closed
63+ Closed ,
6464}
6565
6666impl Display for SendError {
6767 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
6868 match self {
6969 SendError :: IsExiting => f. write_str ( "exit requested" ) ,
70- SendError :: Closed => f. write_str ( "channel closed" )
70+ SendError :: Closed => f. write_str ( "channel closed" ) ,
7171 }
7272 }
7373}
@@ -83,7 +83,7 @@ pub enum ReadyEvent {
8383 None ,
8484
8585 /// Data was submitted to the given ChannelBuffer channel.
86- Submitted
86+ Submitted ,
8787}
8888
8989/// Buffered reader/writer for a TCP stream.
@@ -93,7 +93,7 @@ pub struct Network {
9393 reader : OwnedReadHalf ,
9494 writer : OwnedWriteHalf ,
9595 addr : SocketAddr ,
96- id : usize
96+ id : usize ,
9797}
9898
9999impl Debug for Network {
@@ -141,8 +141,14 @@ impl Network {
141141 /// # Errors
142142 ///
143143 /// Returns an IO error if the operation failed.
144- pub async fn ready_read < const N : usize > ( & self , bytes_sender : & mpsc:: Sender < Bytes < N > > ) -> std:: io:: Result < ReadyEvent > {
145- let ev = self . reader . ready ( Interest :: ERROR | Interest :: READABLE ) . await ?;
144+ pub async fn ready_read < const N : usize > (
145+ & self ,
146+ bytes_sender : & mpsc:: Sender < Bytes < N > > ,
147+ ) -> std:: io:: Result < ReadyEvent > {
148+ let ev = self
149+ . reader
150+ . ready ( Interest :: ERROR | Interest :: READABLE )
151+ . await ?;
146152 if ev. is_write_closed ( ) || ev. is_read_closed ( ) || ev. is_error ( ) {
147153 return Ok ( ReadyEvent :: ConnectionLoss ) ;
148154 }
@@ -170,14 +176,25 @@ impl Network {
170176}
171177
172178impl AsyncRead for Network {
173- fn poll_read ( self : Pin < & mut Self > , cx : & mut Context < ' _ > , buf : & mut ReadBuf < ' _ > ) -> Poll < std:: io:: Result < ( ) > > {
179+ fn poll_read (
180+ self : Pin < & mut Self > ,
181+ cx : & mut Context < ' _ > ,
182+ buf : & mut ReadBuf < ' _ > ,
183+ ) -> Poll < std:: io:: Result < ( ) > > {
174184 unsafe { self . map_unchecked_mut ( |v| & mut v. reader ) . poll_read ( cx, buf) }
175185 }
176186}
177187
178188impl AsyncWrite for Network {
179- fn poll_write ( self : Pin < & mut Self > , cx : & mut Context < ' _ > , buf : & [ u8 ] ) -> Poll < Result < usize , Error > > {
180- unsafe { self . map_unchecked_mut ( |v| & mut v. writer ) . poll_write ( cx, buf) }
189+ fn poll_write (
190+ self : Pin < & mut Self > ,
191+ cx : & mut Context < ' _ > ,
192+ buf : & [ u8 ] ,
193+ ) -> Poll < Result < usize , Error > > {
194+ unsafe {
195+ self . map_unchecked_mut ( |v| & mut v. writer )
196+ . poll_write ( cx, buf)
197+ }
181198 }
182199
183200 fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Result < ( ) , Error > > {
@@ -188,8 +205,15 @@ impl AsyncWrite for Network {
188205 unsafe { self . map_unchecked_mut ( |v| & mut v. writer ) . poll_shutdown ( cx) }
189206 }
190207
191- fn poll_write_vectored ( self : Pin < & mut Self > , cx : & mut Context < ' _ > , bufs : & [ IoSlice < ' _ > ] ) -> Poll < Result < usize , Error > > {
192- unsafe { self . map_unchecked_mut ( |v| & mut v. writer ) . poll_write_vectored ( cx, bufs) }
208+ fn poll_write_vectored (
209+ self : Pin < & mut Self > ,
210+ cx : & mut Context < ' _ > ,
211+ bufs : & [ IoSlice < ' _ > ] ,
212+ ) -> Poll < Result < usize , Error > > {
213+ unsafe {
214+ self . map_unchecked_mut ( |v| & mut v. writer )
215+ . poll_write_vectored ( cx, bufs)
216+ }
193217 }
194218
195219 fn is_write_vectored ( & self ) -> bool {
0 commit comments