Add network link to ue-server implementation #13
@ -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
|
||||
// amount of bytes it received since the last update
|
||||
dkanus marked this conversation as resolved
Outdated
|
||||
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
|
||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Wtf Wtf
|
||||
// next JSON message from ue-server
|
||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Wtf Wtf
|
||||
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.
|
||||
/// Expected format is a sequence of either:
|
||||
/// 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]
|
||||
Ggg_123
commented
Maybe remove continue? Maybe remove continue?
|
||||
/// 2. [HEAD_UE_MESSAGE: marker byte | 1 byte]
|
||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
with_capactiy? with_capactiy?
Ggg_123
commented
+ // will be recreated with with_capacity in push()
|
||||
/// [LENGTH: length of the JSON message in utf8 encoding | 4 bytes: u32 BE]
|
||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
with_capactiy? with_capactiy?
|
||||
/// [PAYLOAD: utf8-encoded string | `LENGTH` bytes]
|
||||
@ -91,7 +91,7 @@ impl MessageReader {
|
||||
self.length_buffer[self.read_bytes] = input;
|
||||
Ggg_123
commented
Why is it a separate function, and not inside send/flush? Why is it a separate function, and not inside send/flush?
When should I call it?
|
||||
self.read_bytes += 1;
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -155,11 +155,15 @@ impl MessageReader {
|
||||
}
|
||||
}
|
||||
|
||||
fn array_of_u8_to_u32(bytes: [u8; 4]) -> u64 {
|
||||
(u64::from(bytes[0]) << 24)
|
||||
+ (u64::from(bytes[1]) << 16)
|
||||
+ (u64::from(bytes[2]) << 8)
|
||||
+ (u64::from(bytes[3]))
|
||||
fn array_of_u8_to_u16(bytes: [u8; 4]) -> u16 {
|
||||
(u16::from(bytes[0]) << 8) + u16::from(bytes[1])
|
||||
}
|
||||
|
||||
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]
|
||||
@ -201,21 +205,15 @@ fn received_push_byte() {
|
||||
let mut reader = MessageReader::new();
|
||||
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();
|
||||
assert_eq!(reader.ue_received_bytes(), 0xf3);
|
||||
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(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(231).unwrap();
|
||||
dkanus marked this conversation as resolved
Outdated
Ggg_123
commented
Replace with: Replace with:
reader.push_byte(0xf3).unwrap();
...
reader.push_byte(0x41).unwrap();
reader.push_byte(0x19).unwrap();
reader.push_byte(0xb2).unwrap();
reader.push_byte(0x04).unwrap();
assert_eq!(reader.ue_received_bytes(), 0x41_19_b2_f7); // 0xf7 = 0x04 + 0xf3
Because otherwise this is "magic numbers wtf".
dkanus
commented
Woa, that's neat. Agreed. Woa, that's neat. Agreed.
|
||||
reader.push_byte(34).unwrap();
|
||||
reader.push_byte(154).unwrap();
|
||||
assert_eq!(reader.ue_received_bytes(), 0x41_19_b2_f7);
|
||||
assert_eq!(reader.ue_received_bytes(), 0xb2_f7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -223,9 +221,7 @@ fn mixed_push_byte() {
|
||||
let mut reader = MessageReader::new();
|
||||
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(243).unwrap();
|
||||
reader.push_byte(0xf3).unwrap();
|
||||
reader.push_byte(HEAD_UE_MESSAGE).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'!').unwrap();
|
||||
reader.push_byte(HEAD_UE_RECEIVED).unwrap();
|
||||
reader.push_byte(65).unwrap();
|
||||
reader.push_byte(25).unwrap();
|
||||
reader.push_byte(178).unwrap();
|
||||
reader.push_byte(4).unwrap();
|
||||
assert_eq!(reader.ue_received_bytes(), 1092203255);
|
||||
reader.push_byte(0xb2).unwrap();
|
||||
reader.push_byte(0x04).unwrap();
|
||||
assert_eq!(reader.ue_received_bytes(), 0xb2_f7); // 0xf7 = 0x04 + 0xf3
|
||||
assert_eq!(reader.pop().unwrap(), "Yo!");
|
||||
assert_eq!(reader.pop(), None);
|
||||
}
|
||||
@ -251,9 +245,7 @@ fn pushing_many_bytes_at_once() {
|
||||
.push(&[
|
||||
HEAD_UE_RECEIVED,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
243,
|
||||
0xf3,
|
||||
HEAD_UE_MESSAGE,
|
||||
0,
|
||||
0,
|
||||
@ -263,13 +255,11 @@ fn pushing_many_bytes_at_once() {
|
||||
b'o',
|
||||
b'!',
|
||||
HEAD_UE_RECEIVED,
|
||||
65,
|
||||
25,
|
||||
178,
|
||||
4,
|
||||
0xb2,
|
||||
0x04,
|
||||
])
|
||||
.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(), None);
|
||||
}
|
||||
@ -279,9 +269,7 @@ fn generates_error_invalid_head() {
|
||||
let mut reader = MessageReader::new();
|
||||
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(243).unwrap();
|
||||
reader.push_byte(0xf3).unwrap();
|
||||
assert!(!reader.is_broken());
|
||||
reader
|
||||
.push_byte(25)
|
||||
|
Loading…
Reference in New Issue
Block a user
Wtf
replace with array[u8;4] and
pub 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]))
}