+ game protocol

This commit is contained in:
Wiktor Mazur
2019-08-01 18:46:52 +02:00
parent 761e088423
commit 6b7d1e2905
5 changed files with 82 additions and 4 deletions
Generated
+1
View File
@@ -36,6 +36,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "damage-meter" name = "damage-meter"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
"packet_sniffer 0.1.0", "packet_sniffer 0.1.0",
"protocol16 0.1.0", "protocol16 0.1.0",
] ]
+2 -1
View File
@@ -8,4 +8,5 @@ edition = "2018"
[dependencies] [dependencies]
packet_sniffer = { path = "./lib/packet-sniffer" } packet_sniffer = { path = "./lib/packet-sniffer" }
protocol16 = { path = "./lib/protocol16" } protocol16 = { path = "./lib/protocol16" }
bytes = "0.4"
+1 -1
View File
@@ -7,4 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bytes = "0.4.12" bytes = "0.4"
+15 -2
View File
@@ -1,16 +1,29 @@
extern crate bytes;
extern crate packet_sniffer; extern crate packet_sniffer;
extern crate protocol16;
use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc; use std::sync::mpsc;
use packet_sniffer::UdpPacket; use packet_sniffer::UdpPacket;
mod protocol;
fn main() { fn main() {
let (tx, rx): (Sender<UdpPacket>, Receiver<UdpPacket>) = mpsc::channel(); let (tx, rx): (Sender<UdpPacket>, Receiver<UdpPacket>) = mpsc::channel();
packet_sniffer::receive(tx); packet_sniffer::receive(tx);
loop { loop {
println!("{:?}", rx.recv()); if let Ok(packet) = rx.recv() {
if packet.destination_port != 5056 && packet.source_port != 5056 {
continue;
}
protocol::decode(&packet.payload);
}
} }
} }
+63
View File
@@ -0,0 +1,63 @@
use std::io::Cursor;
use bytes::Buf;
static CMD_HEADER_LENGTH: u32 = 12;
static SIGNIFIER_BYTE_LENGTH: usize = 1;
static LOG_OUT: u8 = 4;
static SEND_UNRELIABLE: u8 = 7;
static SEND_RELIABLE: u8 = 6;
static MSG_TYPE_REQUEST: u8 = 2;
static MSG_TYPE_RESPONSE: u8 = 3;
static MSG_TYPE_EVENT: u8 = 4;
pub fn decode(payload: &[u8])
{
let mut cursor = Cursor::new(payload);
cursor.advance(3);
let cmd_cnt = protocol16::deserialize_byte(&mut cursor).unwrap();
cursor.advance(8);
for _ in 0..cmd_cnt {
let cmd_type = protocol16::deserialize_byte(&mut cursor).unwrap();
cursor.advance(3);
let cmd_length = protocol16::deserialize_integer(&mut cursor).unwrap();
cursor.advance(4);
if cmd_type == LOG_OUT {
break;
} else if cmd_type == SEND_UNRELIABLE {
cursor.advance(4);
on_message(&mut cursor, cmd_length - 4);
} else if cmd_type == SEND_RELIABLE {
on_message(&mut cursor, cmd_length);
} else {
cursor.advance((cmd_length - CMD_HEADER_LENGTH) as usize);
}
}
}
fn on_message(cursor: &mut Cursor<&[u8]>, msg_len: u32) {
let init = cursor.bytes().len();
cursor.advance(SIGNIFIER_BYTE_LENGTH);
let msg_type = protocol16::deserialize_byte(cursor).unwrap();
let operation_length = msg_len - CMD_HEADER_LENGTH - 2;
let mut payload = Cursor::new(&cursor.bytes()[0..operation_length as usize]);
if msg_type == MSG_TYPE_EVENT {
if let Some(event_data) = protocol16::deserialize_event_data(&mut payload) {
if event_data.code != 2 {
println!("{:?}", event_data);
}
}
}
cursor.advance(operation_length as usize);
let last = cursor.bytes().len();
assert!(init - last == msg_len as usize - CMD_HEADER_LENGTH as usize);
}