Skip to content

Commit faad7ba

Browse files
committed
feat: minor performance optimizations and API changes
1 parent 5b756d6 commit faad7ba

5 files changed

Lines changed: 39 additions & 17 deletions

File tree

mania-core/src/core/crypto/stream_sha1.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ use digest::{
66
};
77
use sha1::block_api::compress;
88

9-
#[derive(Default)]
109
struct StreamSha1Core {
11-
h: [u32; 5] = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
12-
block_len: u64 = 0,
10+
h: [u32; 5],
11+
block_len: u64,
1312
buffer: BlockBuffer<U64, Eager>,
1413
}
1514

15+
impl Default for StreamSha1Core {
16+
fn default() -> Self {
17+
Self {
18+
h: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
19+
block_len: 0,
20+
buffer: BlockBuffer::default(),
21+
}
22+
}
23+
}
24+
1625
impl StreamSha1Core {
1726
#[inline]
1827
fn hash(&self) -> [u8; 20] {
@@ -47,14 +56,24 @@ impl StreamSha1Core {
4756
}
4857
}
4958

50-
#[derive(Default)]
5159
pub struct StreamSha1 {
5260
hasher: StreamSha1Core,
53-
block_size: usize = 1024 * 1024,
54-
offset: usize = 0,
61+
block_size: usize,
62+
offset: usize,
5563
digests_stream: Vec<[u8; 20]>,
5664
}
5765

66+
impl Default for StreamSha1 {
67+
fn default() -> Self {
68+
Self {
69+
hasher: StreamSha1Core::default(),
70+
block_size: 1024 * 1024,
71+
offset: 0,
72+
digests_stream: Vec::new(),
73+
}
74+
}
75+
}
76+
5877
impl StreamSha1 {
5978
pub fn new() -> Self {
6079
Self::default()

mania-core/src/core/event.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::core::session::Session;
99
use bytes::Bytes;
1010
use once_cell::sync::Lazy;
1111
use std::any::Any;
12+
use std::borrow::Cow;
1213
use std::collections::HashMap;
1314
use std::fmt::Debug;
1415
use thiserror::Error;
@@ -72,9 +73,10 @@ static EVENT_MAP: Lazy<EventMap> = Lazy::new(|| {
7273

7374
pub fn resolve_event(packet: SsoPacket, session: &Session) -> CEParseResult {
7475
// Lagrange.Core.Internal.Context.ServiceContext.ResolveEventByPacket
75-
let payload = PacketReader::new(packet.payload()).section(|p| p.bytes());
76-
let Some(parse) = EVENT_MAP.get(packet.command()) else {
77-
return Err(EventError::UnsupportedEvent(packet.command().to_string()));
76+
let (_, cmd, _, data) = packet.into_parts();
77+
let payload = PacketReader::new(data.0).section(|p| p.bytes());
78+
let Some(parse) = EVENT_MAP.get(cmd.as_ref()) else {
79+
return Err(EventError::UnsupportedEvent(cmd));
7880
};
7981
let events = parse(payload, session)?;
8082
Ok(events)
@@ -101,7 +103,7 @@ pub fn downcast_mut_major_event<T: ServerEvent + 'static>(event: &mut CEParse) -
101103
#[derive(Debug, Error)]
102104
pub enum EventError {
103105
#[error("unsupported event, commend: {0}")]
104-
UnsupportedEvent(String),
106+
UnsupportedEvent(Cow<'static, str>),
105107

106108
#[error("TLV error occurred: {0}")]
107109
MissingTlv(#[from] crate::core::tlv::TlvError),

mania-core/src/core/packet.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl TryFrom<u32> for PacketType {
3636
}
3737
}
3838

39-
#[derive(Debug)]
39+
#[derive(Debug, Clone)]
4040
pub struct BinaryPacket(pub Bytes);
4141

4242
pub struct OidbPacket {
@@ -114,6 +114,7 @@ impl OidbPacket {
114114
}
115115
}
116116

117+
#[derive(Clone)]
117118
pub struct SsoPacket {
118119
packet_type: PacketType,
119120
command: Cow<'static, str>,
@@ -187,8 +188,12 @@ impl SsoPacket {
187188
self.sequence
188189
}
189190

190-
pub fn payload(&self) -> Bytes {
191-
self.payload.0.clone()
191+
pub fn payload(&self) -> &[u8] {
192+
self.payload.0.as_ref()
193+
}
194+
195+
pub fn into_parts(self) -> (PacketType, Cow<'static, str>, u32, BinaryPacket) {
196+
(self.packet_type, self.command, self.sequence, self.payload)
192197
}
193198

194199
pub fn build(&self, ctx: &Context) -> Vec<u8> {

mania-core/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![allow(dead_code)]
2-
#![feature(default_field_values)]
3-
42
pub mod core;
53
pub mod entity;
64
pub mod message;

mania/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![allow(dead_code)] // TODO: remove this after stable
2-
#![feature(default_field_values)]
3-
42
pub mod business;
53
pub mod cache;
64
pub mod connect;

0 commit comments

Comments
 (0)