@ -6,7 +6,7 @@ use custom_error::custom_error;
// Defines how many bytes is used to encode "AMOUNT" field in the response from ue-server about
// Defines how many bytes is used to encode "AMOUNT" field in the response from ue-server about
// amount of bytes it received since the last update
// amount of bytes it received since the last update
const UE_RECEIVED_FIELD_SIZE : usize = 4 ;
const UE_RECEIVED_FIELD_SIZE : usize = 2 ;
// Defines how many bytes is used to encode "LENGTH" field, describing length of
// Defines how many bytes is used to encode "LENGTH" field, describing length of
// next JSON message from ue-server
// next JSON message from ue-server
const UE_LENGTH_FIELD_SIZE : usize = 4 ;
const UE_LENGTH_FIELD_SIZE : usize = 4 ;
@ -49,7 +49,7 @@ pub struct MessageReader {
/// For converting byte stream that is expected from the ue-server into actual messages.
/// For converting byte stream that is expected from the ue-server into actual messages.
/// Expected format is a sequence of either:
/// Expected format is a sequence of either:
/// 1. [HEAD_UE_RECEIVED: marker byte | 1 byte]
/// 1. [HEAD_UE_RECEIVED: marker byte | 1 byte]
/// [AMOUNT: amount of bytes received by ue-server since last update | 4 bytes: u32 BE]
/// [AMOUNT: amount of bytes received by ue-server since last update | 2 bytes: u16 BE]
/// 2. [HEAD_UE_MESSAGE: marker byte | 1 byte]
/// 2. [HEAD_UE_MESSAGE: marker byte | 1 byte]
/// [LENGTH: length of the JSON message in utf8 encoding | 4 bytes: u32 BE]
/// [LENGTH: length of the JSON message in utf8 encoding | 4 bytes: u32 BE]
/// [PAYLOAD: utf8-encoded string | `LENGTH` bytes]
/// [PAYLOAD: utf8-encoded string | `LENGTH` bytes]
@ -91,7 +91,7 @@ impl MessageReader {
self . length_buffer [ self . read_bytes ] = input ;
self . length_buffer [ self . read_bytes ] = input ;
self . read_bytes + = 1 ;
self . read_bytes + = 1 ;
if self . read_bytes > = UE_RECEIVED_FIELD_SIZE {
if self . read_bytes > = UE_RECEIVED_FIELD_SIZE {
self . ue_received_bytes + = array_of_u8_to_u32 ( self . length_buffer ) as u64 ;
self . ue_received_bytes + = array_of_u8_to_u16 ( self . length_buffer ) as u64 ;
self . change_state ( ReadingState ::Head ) ;
self . change_state ( ReadingState ::Head ) ;
}
}
}
}
@ -155,11 +155,15 @@ impl MessageReader {
}
}
}
}
fn array_of_u8_to_u32 ( bytes : [ u8 ; 4 ] ) -> u64 {
fn array_of_u8_to_u16 ( bytes : [ u8 ; 4 ] ) -> u16 {
( u64 ::from ( bytes [ 0 ] ) < < 24 )
( u16 ::from ( bytes [ 0 ] ) < < 8 ) + u16 ::from ( bytes [ 1 ] )
+ ( u64 ::from ( bytes [ 1 ] ) < < 16 )
}
+ ( u64 ::from ( bytes [ 2 ] ) < < 8 )
+ ( u64 ::from ( bytes [ 3 ] ) )
fn array_of_u8_to_u32 ( bytes : [ u8 ; 4 ] ) -> u32 {
( u32 ::from ( bytes [ 0 ] ) < < 24 )
+ ( u32 ::from ( bytes [ 1 ] ) < < 16 )
+ ( u32 ::from ( bytes [ 2 ] ) < < 8 )
+ ( u32 ::from ( bytes [ 3 ] ) )
}
}
#[ test ]
#[ test ]
@ -201,21 +205,15 @@ fn received_push_byte() {
let mut reader = MessageReader ::new ( ) ;
let mut reader = MessageReader ::new ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0xf3 ) . unwrap ( ) ;
reader . push_byte ( 0xf3 ) . unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0xf3 ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0xf3 ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( 0x41 ) . unwrap ( ) ;
reader . push_byte ( 0x19 ) . unwrap ( ) ;
reader . push_byte ( 0xb2 ) . unwrap ( ) ;
reader . push_byte ( 0xb2 ) . unwrap ( ) ;
reader . push_byte ( 0x04 ) . unwrap ( ) ;
reader . push_byte ( 0x04 ) . unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0x41_19_ b2_f7 ) ; // 0xf7 = 0x04 + 0xf3
assert_eq! ( reader . ue_received_bytes ( ) , 0xb2_f7 ) ; // 0xf7 = 0x04 + 0xf3
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( 231 ) . unwrap ( ) ;
reader . push_byte ( 231 ) . unwrap ( ) ;
reader . push_byte ( 34 ) . unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0xb2_f7 ) ;
reader . push_byte ( 154 ) . unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0x41_19_b2_f7 ) ;
}
}
#[ test ]
#[ test ]
@ -223,9 +221,7 @@ fn mixed_push_byte() {
let mut reader = MessageReader ::new ( ) ;
let mut reader = MessageReader ::new ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0xf3 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 243 ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_MESSAGE ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_MESSAGE ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
@ -235,11 +231,9 @@ fn mixed_push_byte() {
reader . push_byte ( b'o' ) . unwrap ( ) ;
reader . push_byte ( b'o' ) . unwrap ( ) ;
reader . push_byte ( b'!' ) . unwrap ( ) ;
reader . push_byte ( b'!' ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( 65 ) . unwrap ( ) ;
reader . push_byte ( 0xb2 ) . unwrap ( ) ;
reader . push_byte ( 25 ) . unwrap ( ) ;
reader . push_byte ( 0x04 ) . unwrap ( ) ;
reader . push_byte ( 178 ) . unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0xb2_f7 ) ; // 0xf7 = 0x04 + 0xf3
reader . push_byte ( 4 ) . unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 1092203255 ) ;
assert_eq! ( reader . pop ( ) . unwrap ( ) , "Yo!" ) ;
assert_eq! ( reader . pop ( ) . unwrap ( ) , "Yo!" ) ;
assert_eq! ( reader . pop ( ) , None ) ;
assert_eq! ( reader . pop ( ) , None ) ;
}
}
@ -251,9 +245,7 @@ fn pushing_many_bytes_at_once() {
. push ( & [
. push ( & [
HEAD_UE_RECEIVED ,
HEAD_UE_RECEIVED ,
0 ,
0 ,
0 ,
0xf3 ,
0 ,
243 ,
HEAD_UE_MESSAGE ,
HEAD_UE_MESSAGE ,
0 ,
0 ,
0 ,
0 ,
@ -263,13 +255,11 @@ fn pushing_many_bytes_at_once() {
b'o' ,
b'o' ,
b'!' ,
b'!' ,
HEAD_UE_RECEIVED ,
HEAD_UE_RECEIVED ,
65 ,
0xb2 ,
25 ,
0x04 ,
178 ,
4 ,
] )
] )
. unwrap ( ) ;
. unwrap ( ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 1092203255 ) ;
assert_eq! ( reader . ue_received_bytes ( ) , 0xb2_f7 ) ;
assert_eq! ( reader . pop ( ) . unwrap ( ) , "Yo!" ) ;
assert_eq! ( reader . pop ( ) . unwrap ( ) , "Yo!" ) ;
assert_eq! ( reader . pop ( ) , None ) ;
assert_eq! ( reader . pop ( ) , None ) ;
}
}
@ -279,9 +269,7 @@ fn generates_error_invalid_head() {
let mut reader = MessageReader ::new ( ) ;
let mut reader = MessageReader ::new ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( HEAD_UE_RECEIVED ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 0xf3 ) . unwrap ( ) ;
reader . push_byte ( 0 ) . unwrap ( ) ;
reader . push_byte ( 243 ) . unwrap ( ) ;
assert! ( ! reader . is_broken ( ) ) ;
assert! ( ! reader . is_broken ( ) ) ;
reader
reader
. push_byte ( 25 )
. push_byte ( 25 )