From 709d0bae8132ca48bece6a10d0649c49f06fb856 Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 14 Sep 2019 11:43:02 -0700 Subject: [PATCH] make clientid a stronger type --- src/common/clientpool.rs | 8 ++++---- src/common/serverstate.rs | 3 ++- src/login/character.rs | 10 +++++----- src/login/login.rs | 6 +++--- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/common/clientpool.rs b/src/common/clientpool.rs index dafd845..2f513d5 100644 --- a/src/common/clientpool.rs +++ b/src/common/clientpool.rs @@ -61,7 +61,7 @@ pub struct ClientPool{ client_ids: HashMap, clients: HashMap>, listener: TcpListener, - client_id_incr: ClientId, + client_id_incr: usize, } @@ -90,13 +90,13 @@ impl ClientPool where fn new_client(&mut self) { let (socket, _addr) = self.listener.accept().unwrap(); - let client_id = self.client_id_incr; + let client_id = ClientId(self.client_id_incr); self.client_id_incr += 1; - self.poll.register(&socket, Token(client_id), Ready::readable() | Ready::writable(), PollOpt::edge()).unwrap(); + self.poll.register(&socket, Token(client_id.0), Ready::readable() | Ready::writable(), PollOpt::edge()).unwrap(); let client = Client::new(client_id, socket); - self.client_ids.insert(Token(client_id), client_id); + self.client_ids.insert(Token(client_id.0), client_id); self.clients.insert(client_id, client); self.sender.send(ClientPoolAction::NewClient(client_id)).unwrap(); } diff --git a/src/common/serverstate.rs b/src/common/serverstate.rs index 3291c00..e47b189 100644 --- a/src/common/serverstate.rs +++ b/src/common/serverstate.rs @@ -1,7 +1,8 @@ use libpso::PacketParseError; use libpso::crypto::PSOCipher; -pub type ClientId = usize; +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub struct ClientId(pub usize); pub enum OnConnect { Packet(S), diff --git a/src/login/character.rs b/src/login/character.rs index be112cf..4714296 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -339,10 +339,10 @@ mod test { created_at: SystemTime::now(), }); - let send = server.handle(5, &RecvCharacterPacket::RequestSettings(RequestSettings {flag: 0})).collect::>(); + let send = server.handle(ClientId(5), &RecvCharacterPacket::RequestSettings(RequestSettings {flag: 0})).collect::>(); assert!(send.len() == 1); - assert!(send[0].0 == 5); + assert!(send[0].0 == ClientId(5)); let bytes = send[0].1.as_bytes(); assert!(bytes[2] == 0xE2); assert!(bytes.len() == 0xAFC); @@ -353,9 +353,9 @@ mod test { struct TestData; impl DataAccess for TestData {} let mut server = CharacterServerState::new(TestData {}); - let send = server.handle(1, &RecvCharacterPacket::Checksum(Checksum {flag: 0, - checksum: 1234, - padding: 0, + let send = server.handle(ClientId(1), &RecvCharacterPacket::Checksum(Checksum {flag: 0, + checksum: 1234, + padding: 0, })).collect::>(); assert!(send.len() == 1); diff --git a/src/login/login.rs b/src/login/login.rs index 3f6114c..b757685 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -174,7 +174,7 @@ mod test { let mut server = LoginServerState::new(TestData {}); - let send = server.handle(1, &RecvLoginPacket::Login(Login { + let send = server.handle(ClientId(1), &RecvLoginPacket::Login(Login { flag: 0, tag: 65536, guildcard: 0, @@ -194,7 +194,7 @@ mod test { })).collect::>(); assert!(send == vec![ - (1, SendLoginPacket::LoginResponse(LoginResponse { + (ClientId(1), SendLoginPacket::LoginResponse(LoginResponse { flag: 0, status: AccountStatus::Ok, tag: 65536, @@ -204,7 +204,7 @@ mod test { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], caps: 258 })), - (1, SendLoginPacket::RedirectClient(RedirectClient { + (ClientId(1), SendLoginPacket::RedirectClient(RedirectClient { flag: 0, ip: 16777343, port: 12001,