2
2
// This is the most common use case when working with websockets and is recommended due to the hand shaky nature of
3
3
// the protocol as well as the fact that an input buffer can contain multiple websocket frames or maybe only a fragment of one.
4
4
// This module allows you to work with discrete websocket frames rather than the multiple fragments you read off a stream.
5
- // NOTE: if you are using the standard library then you can use the built in Read and Write traits from std otherwise
6
- // you have to implement the Read and Write traits specified below
5
+ // NOTE: if you are using the standard library then you can use the built in compat:: Read and compat:: Write traits from std otherwise
6
+ // you have to implement the compat:: Read and compat:: Write traits specified below
7
7
8
8
use crate :: {
9
- WebSocket , WebSocketCloseStatusCode , WebSocketContext , WebSocketOptions ,
9
+ compat , WebSocket , WebSocketCloseStatusCode , WebSocketContext , WebSocketOptions ,
10
10
WebSocketReceiveMessageType , WebSocketSendMessageType , WebSocketState , WebSocketSubProtocol ,
11
11
WebSocketType ,
12
12
} ;
13
13
use core:: { cmp:: min, str:: Utf8Error } ;
14
14
use rand_core:: RngCore ;
15
15
16
- pub trait Read < E > {
17
- fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , E > ;
18
- }
19
-
20
- pub trait Write < E > {
21
- fn write_all ( & mut self , buf : & [ u8 ] ) -> Result < ( ) , E > ;
22
- }
23
-
24
- cfg_if:: cfg_if! {
25
- if #[ cfg( feature = "std" ) ] {
26
- impl Read <std:: io:: Error > for std:: net:: TcpStream {
27
- fn read( & mut self , buf: & mut [ u8 ] ) -> Result <usize , std:: io:: Error > {
28
- std:: io:: Read :: read( self , buf)
29
- }
30
- }
31
-
32
- impl Write <std:: io:: Error > for std:: net:: TcpStream {
33
- fn write_all( & mut self , buf: & [ u8 ] ) -> Result <( ) , std:: io:: Error > {
34
- std:: io:: Write :: write_all( self , buf)
35
- }
36
- }
37
- }
38
- }
39
-
40
- cfg_if:: cfg_if! {
41
- if #[ cfg( feature = "async" ) ] {
42
- #[ async_trait:: async_trait]
43
- pub trait AsyncRead <E > {
44
- async fn read( & mut self , buf: & mut [ u8 ] ) -> Result <usize , E >;
45
- }
46
-
47
- #[ async_trait:: async_trait]
48
- pub trait AsyncWrite <E > {
49
- async fn write_all( & mut self , buf: & [ u8 ] ) -> Result <( ) , E >;
50
- }
51
- }
52
- }
53
-
54
- cfg_if:: cfg_if! {
55
- if #[ cfg( feature = "tokio" ) ] {
56
- #[ async_trait:: async_trait]
57
- impl AsyncRead <std:: io:: Error > for tokio:: net:: TcpStream {
58
- async fn read( & mut self , buf: & mut [ u8 ] ) -> Result <usize , std:: io:: Error > {
59
- tokio:: io:: AsyncReadExt :: read( self , buf) . await
60
- }
61
- }
62
-
63
- #[ async_trait:: async_trait]
64
- impl AsyncWrite <std:: io:: Error > for tokio:: net:: TcpStream {
65
- async fn write_all( & mut self , buf: & [ u8 ] ) -> Result <( ) , std:: io:: Error > {
66
- tokio:: io:: AsyncWriteExt :: write_all( self , buf) . await
67
- }
68
- }
69
- } else if #[ cfg( feature = "smol" ) ] {
70
- #[ async_trait:: async_trait]
71
- impl AsyncRead <std:: io:: Error > for smol:: net:: TcpStream {
72
- async fn read( & mut self , buf: & mut [ u8 ] ) -> Result <usize , std:: io:: Error > {
73
- smol:: io:: AsyncReadExt :: read( self , buf) . await
74
- }
75
- }
76
-
77
- #[ async_trait:: async_trait]
78
- impl AsyncWrite <std:: io:: Error > for smol:: net:: TcpStream {
79
- async fn write_all( & mut self , buf: & [ u8 ] ) -> Result <( ) , std:: io:: Error > {
80
- smol:: io:: AsyncWriteExt :: write_all( self , buf) . await
81
- }
82
- }
83
- } else if #[ cfg( feature = "async-std" ) ] {
84
- #[ async_trait:: async_trait]
85
- impl AsyncRead <std:: io:: Error > for async_std:: net:: TcpStream {
86
- async fn read( & mut self , buf: & mut [ u8 ] ) -> Result <usize , std:: io:: Error > {
87
- async_std:: io:: ReadExt :: read( self , buf) . await
88
- }
89
- }
90
-
91
- #[ async_trait:: async_trait]
92
- impl AsyncWrite <std:: io:: Error > for async_std:: net:: TcpStream {
93
- async fn write_all( & mut self , buf: & [ u8 ] ) -> Result <( ) , std:: io:: Error > {
94
- async_std:: io:: WriteExt :: write_all( self , buf) . await
95
- }
96
- }
97
- }
98
- }
99
-
100
16
pub enum ReadResult < ' a > {
101
17
Binary ( & ' a [ u8 ] ) ,
102
18
Text ( & ' a str ) ,
132
48
{
133
49
pub fn connect < E > (
134
50
& mut self ,
135
- stream : & mut ( impl Read < E > + Write < E > ) ,
51
+ stream : & mut ( impl compat :: Read < E > + compat :: Write < E > ) ,
136
52
websocket_options : & WebSocketOptions ,
137
53
) -> Result < Option < WebSocketSubProtocol > , FramerError < E > > {
138
54
let ( len, web_socket_key) = self
179
95
{
180
96
pub fn accept < E > (
181
97
& mut self ,
182
- stream : & mut impl Write < E > ,
98
+ stream : & mut impl compat :: Write < E > ,
183
99
websocket_context : & WebSocketContext ,
184
100
) -> Result < ( ) , FramerError < E > > {
185
101
let len = self . accept_len ( & websocket_context) ?;
@@ -244,7 +160,7 @@ where
244
160
// calling close on a websocket that has already been closed by the other party has no effect
245
161
pub fn close < E > (
246
162
& mut self ,
247
- stream : & mut impl Write < E > ,
163
+ stream : & mut impl compat :: Write < E > ,
248
164
close_status : WebSocketCloseStatusCode ,
249
165
status_description : Option < & str > ,
250
166
) -> Result < ( ) , FramerError < E > > {
@@ -268,7 +184,7 @@ where
268
184
269
185
pub fn write < E > (
270
186
& mut self ,
271
- stream : & mut impl Write < E > ,
187
+ stream : & mut impl compat :: Write < E > ,
272
188
message_type : WebSocketSendMessageType ,
273
189
end_of_message : bool ,
274
190
frame_buf : & [ u8 ] ,
@@ -285,7 +201,7 @@ where
285
201
// It will wait until the last fragmented frame has arrived.
286
202
pub fn read < ' b , E > (
287
203
& mut self ,
288
- stream : & mut ( impl Read < E > + Write < E > ) ,
204
+ stream : & mut ( impl compat :: Read < E > + compat :: Write < E > ) ,
289
205
frame_buf : & ' b mut [ u8 ] ,
290
206
) -> Result < ReadResult < ' b > , FramerError < E > > {
291
207
loop {
@@ -365,7 +281,7 @@ where
365
281
366
282
fn send_back < E > (
367
283
& mut self ,
368
- stream : & mut impl Write < E > ,
284
+ stream : & mut impl compat :: Write < E > ,
369
285
frame_buf : & ' _ mut [ u8 ] ,
370
286
len_to : usize ,
371
287
send_message_type : WebSocketSendMessageType ,
@@ -396,7 +312,7 @@ impl<'a, TRng> Framer<'a, TRng, crate::Client>
396
312
where
397
313
TRng : RngCore ,
398
314
{
399
- pub async fn connect_async < S : AsyncRead < E > + AsyncWrite < E > + Unpin , E > (
315
+ pub async fn connect_async < S : compat :: AsyncRead < E > + compat :: AsyncWrite < E > + Unpin , E > (
400
316
& mut self ,
401
317
stream : & mut S ,
402
318
websocket_options : & ' a WebSocketOptions < ' a > ,
@@ -446,7 +362,7 @@ impl<'a, TRng> Framer<'a, TRng, crate::Server>
446
362
where
447
363
TRng : RngCore ,
448
364
{
449
- pub async fn accept_async < W : AsyncWrite < E > + Unpin , E > (
365
+ pub async fn accept_async < W : compat :: AsyncWrite < E > + Unpin , E > (
450
366
& mut self ,
451
367
stream : & mut W ,
452
368
websocket_context : & WebSocketContext ,
@@ -468,7 +384,7 @@ where
468
384
TWebSocketType : WebSocketType ,
469
385
{
470
386
// calling close on a websocket that has already been closed by the other party has no effect
471
- pub async fn close_async < W : AsyncWrite < E > + Unpin , E > (
387
+ pub async fn close_async < W : compat :: AsyncWrite < E > + Unpin , E > (
472
388
& mut self ,
473
389
stream : & mut W ,
474
390
close_status : WebSocketCloseStatusCode ,
@@ -482,7 +398,7 @@ where
482
398
Ok ( ( ) )
483
399
}
484
400
485
- pub async fn write_async < W : AsyncWrite < E > + Unpin , E > (
401
+ pub async fn write_async < W : compat :: AsyncWrite < E > + Unpin , E > (
486
402
& mut self ,
487
403
stream : & mut W ,
488
404
message_type : WebSocketSendMessageType ,
@@ -498,7 +414,7 @@ where
498
414
Ok ( ( ) )
499
415
}
500
416
501
- pub async fn read_async < ' b , S : AsyncWrite < E > + AsyncRead < E > + Unpin , E > (
417
+ pub async fn read_async < ' b , S : compat :: AsyncWrite < E > + compat :: AsyncRead < E > + Unpin , E > (
502
418
& mut self ,
503
419
stream : & mut S ,
504
420
frame_buf : & ' b mut [ u8 ] ,
@@ -580,7 +496,7 @@ where
580
496
}
581
497
}
582
498
583
- async fn send_back_async < W : AsyncWrite < E > + Unpin , E > (
499
+ async fn send_back_async < W : compat :: AsyncWrite < E > + Unpin , E > (
584
500
& mut self ,
585
501
stream : & mut W ,
586
502
frame_buf : & ' _ mut [ u8 ] ,
0 commit comments