1
1
use base64:: { engine:: general_purpose, Engine } ;
2
+ use bytes:: Bytes ;
2
3
use serde:: Serialize ;
3
4
4
5
use crate :: config:: EngineIoConfig ;
@@ -38,7 +39,7 @@ pub enum Packet {
38
39
/// Or to a websocket binary frame when using websocket connection
39
40
///
40
41
/// When receiving, it is only used with polling connection, websocket use binary frame
41
- Binary ( Vec < u8 > ) , // Not part of the protocol, used internally
42
+ Binary ( Bytes ) , // Not part of the protocol, used internally
42
43
43
44
/// Binary packet used to send binary data to the client
44
45
/// Converts to a String using base64 encoding when using polling connection
@@ -47,7 +48,7 @@ pub enum Packet {
47
48
/// When receiving, it is only used with polling connection, websocket use binary frame
48
49
///
49
50
/// This is a special packet, excepionally specific to the V3 protocol.
50
- BinaryV3 ( Vec < u8 > ) , // Not part of the protocol, used internally
51
+ BinaryV3 ( Bytes ) , // Not part of the protocol, used internally
51
52
}
52
53
53
54
impl Packet {
@@ -65,7 +66,7 @@ impl Packet {
65
66
}
66
67
67
68
/// If the packet is a binary packet, it returns the binary data
68
- pub ( crate ) fn into_binary ( self ) -> Vec < u8 > {
69
+ pub ( crate ) fn into_binary ( self ) -> Bytes {
69
70
match self {
70
71
Packet :: Binary ( data) => data,
71
72
Packet :: BinaryV3 ( data) => data,
@@ -159,10 +160,16 @@ impl TryFrom<&str> for Packet {
159
160
b'4' => Packet :: Message ( value[ 1 ..] . to_string ( ) ) ,
160
161
b'5' => Packet :: Upgrade ,
161
162
b'6' => Packet :: Noop ,
162
- b'b' if value. as_bytes ( ) . get ( 1 ) == Some ( & b'4' ) => {
163
- Packet :: BinaryV3 ( general_purpose:: STANDARD . decode ( value[ 2 ..] . as_bytes ( ) ) ?)
164
- }
165
- b'b' => Packet :: Binary ( general_purpose:: STANDARD . decode ( value[ 1 ..] . as_bytes ( ) ) ?) ,
163
+ b'b' if value. as_bytes ( ) . get ( 1 ) == Some ( & b'4' ) => Packet :: BinaryV3 (
164
+ general_purpose:: STANDARD
165
+ . decode ( value[ 2 ..] . as_bytes ( ) ) ?
166
+ . into ( ) ,
167
+ ) ,
168
+ b'b' => Packet :: Binary (
169
+ general_purpose:: STANDARD
170
+ . decode ( value[ 1 ..] . as_bytes ( ) ) ?
171
+ . into ( ) ,
172
+ ) ,
166
173
c => Err ( Error :: InvalidPacketType ( Some ( * c as char ) ) ) ?,
167
174
} ;
168
175
Ok ( res)
@@ -241,7 +248,7 @@ mod tests {
241
248
242
249
#[ test]
243
250
fn test_binary_packet ( ) {
244
- let packet = Packet :: Binary ( vec ! [ 1 , 2 , 3 ] ) ;
251
+ let packet = Packet :: Binary ( vec ! [ 1 , 2 , 3 ] . into ( ) ) ;
245
252
let packet_str: String = packet. try_into ( ) . unwrap ( ) ;
246
253
assert_eq ! ( packet_str, "bAQID" ) ;
247
254
}
@@ -250,12 +257,12 @@ mod tests {
250
257
fn test_binary_packet_deserialize ( ) {
251
258
let packet_str = "bAQID" . to_string ( ) ;
252
259
let packet: Packet = packet_str. try_into ( ) . unwrap ( ) ;
253
- assert_eq ! ( packet, Packet :: Binary ( vec![ 1 , 2 , 3 ] ) ) ;
260
+ assert_eq ! ( packet, Packet :: Binary ( vec![ 1 , 2 , 3 ] . into ( ) ) ) ;
254
261
}
255
262
256
263
#[ test]
257
264
fn test_binary_packet_v3 ( ) {
258
- let packet = Packet :: BinaryV3 ( vec ! [ 1 , 2 , 3 ] ) ;
265
+ let packet = Packet :: BinaryV3 ( vec ! [ 1 , 2 , 3 ] . into ( ) ) ;
259
266
let packet_str: String = packet. try_into ( ) . unwrap ( ) ;
260
267
assert_eq ! ( packet_str, "b4AQID" ) ;
261
268
}
@@ -264,7 +271,7 @@ mod tests {
264
271
fn test_binary_packet_v3_deserialize ( ) {
265
272
let packet_str = "b4AQID" . to_string ( ) ;
266
273
let packet: Packet = packet_str. try_into ( ) . unwrap ( ) ;
267
- assert_eq ! ( packet, Packet :: BinaryV3 ( vec![ 1 , 2 , 3 ] ) ) ;
274
+ assert_eq ! ( packet, Packet :: BinaryV3 ( vec![ 1 , 2 , 3 ] . into ( ) ) ) ;
268
275
}
269
276
270
277
#[ test]
@@ -310,11 +317,11 @@ mod tests {
310
317
let packet = Packet :: Noop ;
311
318
assert_eq ! ( packet. get_size_hint( false ) , 1 ) ;
312
319
313
- let packet = Packet :: Binary ( vec ! [ 1 , 2 , 3 ] ) ;
320
+ let packet = Packet :: Binary ( vec ! [ 1 , 2 , 3 ] . into ( ) ) ;
314
321
assert_eq ! ( packet. get_size_hint( false ) , 4 ) ;
315
322
assert_eq ! ( packet. get_size_hint( true ) , 5 ) ;
316
323
317
- let packet = Packet :: BinaryV3 ( vec ! [ 1 , 2 , 3 ] ) ;
324
+ let packet = Packet :: BinaryV3 ( vec ! [ 1 , 2 , 3 ] . into ( ) ) ;
318
325
assert_eq ! ( packet. get_size_hint( false ) , 4 ) ;
319
326
assert_eq ! ( packet. get_size_hint( true ) , 6 ) ;
320
327
}
0 commit comments