Skip to content

Commit

Permalink
WIP removing serde stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGruber committed Dec 8, 2023
1 parent 26b8efa commit 8195dcb
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ path = "src/lib.rs"

[profile.release]
lto = true
strip = true
# strip = true
opt-level = 3

[package.metadata.wasm-pack.profile.release]
Expand Down
24 changes: 9 additions & 15 deletions benches/lib-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ fn soeprotocol_utils_benchmarks(c: &mut Criterion) {
]
.to_vec();
let wtr = vec![];
let mut data_packet = DataPacket {
data: data_to_pack,
sequence: 0,
};
let mut data_packet = DataPacket::new(data_to_pack, 0);

c.bench_function("write_packet_data_crc", |b| {
b.iter(|| write_packet_data(black_box(&mut wtr.to_owned()), black_box(&mut data_packet)))
Expand All @@ -31,10 +28,7 @@ fn soeprotocol_utils_benchmarks(c: &mut Criterion) {
]
.to_vec();
let wtr = vec![];
let mut data_packet = DataPacket {
data: data_to_pack,
sequence: 0,
};
let mut data_packet = DataPacket::new(data_to_pack, 0);
c.bench_function("write_packet_data", |b| {
b.iter(|| write_packet_data(black_box(&mut wtr.to_owned()), black_box(&mut data_packet)))
});
Expand Down Expand Up @@ -485,16 +479,16 @@ fn rc4_benchmark(c: &mut Criterion) {
}

fn criterion_benchmark(c: &mut Criterion) {
crc_legacy_benchmark(c);
crc_benchmark(c);
utils_benchmark(c);
jooat_benchmark(c);
rc4_benchmark(c);
// crc_legacy_benchmark(c);
// crc_benchmark(c);
// utils_benchmark(c);
// jooat_benchmark(c);
// rc4_benchmark(c);
soeprotocol_parse_benchmarks(c);
soeprotocol_pack_benchmarks(c);
soeprotocol_utils_benchmarks(c);
gatewayprotocol_parse_benchmarks(c);
gatewayprotocol_pack_benchmarks(c);
// gatewayprotocol_parse_benchmarks(c);
// gatewayprotocol_pack_benchmarks(c);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
30 changes: 16 additions & 14 deletions src/soeprotocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Soeprotocol {
.write_u32::<BigEndian>(packet.udp_length)
.unwrap_or_default();
self.wtr.append(&mut u8_from_str_nul_utf8_unchecked(
packet.protocol.as_str(),
packet.get_protocol().as_str(),
));
self.wtr.clone()
}
Expand Down Expand Up @@ -223,22 +223,27 @@ impl Soeprotocol {
serde_json::from_str(&packet_string)
}

pub fn group_packets(&mut self, opcode: u16, packets: Vec<Vec<u8>>) -> Vec<u8> {
pub fn group_packets(&mut self, opcode: u16, packets: &Vec<Vec<u8>>) -> Vec<u8> {
self.wtr.clear();
self.wtr.write_u16::<BigEndian>(opcode).unwrap_or_default();
for mut packet in packets {
for packet in packets {
write_data_length(&mut self.wtr, packet.len());
// FIXME: shitty clone
let mut packet = packet.clone();
self.wtr.append(&mut packet);
}
self.wtr.clone()
}

pub fn pack_group_object(&mut self, group_packet: SubBasePackets) -> Vec<u8> {
self.group_packets(SoeOpcode::Group as u16, group_packet.sub_packets)
self.group_packets(SoeOpcode::Group as u16, group_packet.get_sub_packets())
}

pub fn pack_multi_object(&mut self, multi_packet: SubBasePackets) -> Vec<u8> {
self.group_packets(SoeOpcode::MultiPacket as u16, multi_packet.sub_packets)
self.group_packets(
SoeOpcode::MultiPacket as u16,
multi_packet.get_sub_packets(),
)
}

pub fn get_data_object(
Expand Down Expand Up @@ -328,7 +333,7 @@ impl Soeprotocol {
}

pub fn pack_ordered_packet(&mut self, data: Vec<u8>, sequence: u16) -> Vec<u8> {
self.pack_ordered_object(DataPacket { data, sequence })
self.pack_ordered_object(DataPacket::new(data, sequence))
}

pub fn pack_session_request(&mut self, packet: String) -> Vec<u8> {
Expand All @@ -353,12 +358,9 @@ impl Soeprotocol {
udp_length: u32,
protocol: String,
) -> Vec<u8> {
self.pack_session_request_object(SessionRequestPacket {
session_id,
crc_length,
udp_length,
protocol,
})
self.pack_session_request_object(SessionRequestPacket::new(
session_id, crc_length, udp_length, protocol,
))
}

pub fn pack_session_reply(&mut self, packet: String) -> Vec<u8> {
Expand Down Expand Up @@ -463,7 +465,7 @@ impl Soeprotocol {
}

pub fn pack_data_packet(&mut self, data: Vec<u8>, sequence: u16) -> Vec<u8> {
self.pack_data_object(DataPacket { data, sequence })
self.pack_data_object(DataPacket::new(data, sequence))
}

pub fn pack_fragment_data(&mut self, packet: String) -> Vec<u8> {
Expand All @@ -481,7 +483,7 @@ impl Soeprotocol {
}

pub fn pack_fragment_data_packet(&mut self, data: Vec<u8>, sequence: u16) -> Vec<u8> {
self.pack_fragment_data_object(DataPacket { data, sequence })
self.pack_fragment_data_object(DataPacket::new(data, sequence))
}

pub fn pack_out_of_order(&mut self, packet: String) -> Vec<u8> {
Expand Down
7 changes: 2 additions & 5 deletions src/soeprotocol_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn extract_subpacket_data(
pub fn write_packet_data(wtr: &mut Vec<u8>, data_packet: &mut DataPacket) {
wtr.write_u16::<BigEndian>(data_packet.sequence)
.unwrap_or_default();
wtr.append(&mut data_packet.data);
wtr.append(data_packet.get_data());
}

#[cfg(test)]
Expand All @@ -104,10 +104,7 @@ mod tests {
]
.to_vec();
let mut wtr = vec![];
let mut data_packet = super::DataPacket {
data: data_to_pack,
sequence: 0,
};
let mut data_packet = super::DataPacket::new(data_to_pack, 0);
super::write_packet_data(&mut wtr, &mut data_packet);
assert_eq!(
wtr,
Expand Down
Loading

0 comments on commit 8195dcb

Please sign in to comment.