Browse Source

recive auf tcpstream umgeändert

master
Daniel Laptop 1 year ago
parent
commit
3698b82dd8
1 changed files with 24 additions and 12 deletions
  1. 24
    12
      src/bin/message.rs

+ 24
- 12
src/bin/message.rs View File

@@ -1,3 +1,8 @@
use std::{
io::{BufReader, Read},
net::TcpStream,
};

const KEYLENGTH: u8 = 32;

pub enum PayloadType {
@@ -41,34 +46,41 @@ impl Message {
msg
}

fn recive(input: Vec<u8>) -> Message {
let byte1 = input.get(0).expect("Fail to Read Byte 1");
let byte2 = input.get(1).expect("Fail to Read Byte 2");
let byte3 = input.get(2).expect("Fail to Read Byte 3");
let byte4 = input.get(3).expect("Fail to Read Byte 4");
fn recive(stream: TcpStream) -> Message {
let buffreader = BufReader::new(stream);
let mut byte_iterator = buffreader.bytes();
let byte1 = byte_iterator.next().unwrap().unwrap();
let byte2 = byte_iterator.next().unwrap().unwrap();
let byte3 = byte_iterator.next().unwrap().unwrap();
let byte4 = byte_iterator.next().unwrap().unwrap();

let src_id: u32 =
(byte1 << 24) as u32 | (byte2 << 16) as u32 | (byte3 << 8) as u32 | (byte4 << 0) as u32;

let byte5 = input.get(5).expect("Fail to Read Byte 5");
let byte6 = input.get(6).expect("Fail to Read Byte 6");
let byte7 = input.get(7).expect("Fail to Read Byte 7");
let byte8 = input.get(8).expect("Fail to Read Byte 8");
let byte5 = byte_iterator.next().unwrap().unwrap();
let byte6 = byte_iterator.next().unwrap().unwrap();
let byte7 = byte_iterator.next().unwrap().unwrap();
let byte8 = byte_iterator.next().unwrap().unwrap();

let dest_id: u32 =
(byte5 << 24) as u32 | (byte6 << 16) as u32 | (byte7 << 8) as u32 | (byte8 << 0) as u32;

let byte9 = input.get(9).expect("Fail to Read byte 9");
let byte10 = input.get(10).expect("Fail to Read byte 10");
let byte9 = byte_iterator.next().unwrap().unwrap();
let byte10 = byte_iterator.next().unwrap().unwrap();

let size = (byte9 << 8) as u16 | (byte10 << 0) as u16;
assert!(size > 0, "Ungültige size größe size = {}", size);

let mut payload = Vec::<u8>::new();
for i in 0..size {
payload.push(byte_iterator.next().unwrap().unwrap());
}

Message {
src_id: src_id,
dest_id: dest_id,
size: size,
payload: (&input[11..]).to_vec(),
payload: payload,
}
}
// Methoden der Klasse

Loading…
Cancel
Save