Browse Source

make clientid a stronger type

pbs
jake 5 years ago
parent
commit
709d0bae81
  1. 8
      src/common/clientpool.rs
  2. 3
      src/common/serverstate.rs
  3. 10
      src/login/character.rs
  4. 6
      src/login/login.rs

8
src/common/clientpool.rs

@ -61,7 +61,7 @@ pub struct ClientPool<S, R>{
client_ids: HashMap<Token, ClientId>,
clients: HashMap<ClientId, Client<S, R>>,
listener: TcpListener,
client_id_incr: ClientId,
client_id_incr: usize,
}
@ -90,13 +90,13 @@ impl<S, R> ClientPool<S, R> 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();
}

3
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<S: SendServerPacket> {
Packet(S),

10
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::<Vec<_>>();
let send = server.handle(ClientId(5), &RecvCharacterPacket::RequestSettings(RequestSettings {flag: 0})).collect::<Vec<_>>();
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::<Vec<_>>();
assert!(send.len() == 1);

6
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::<Vec<_>>();
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,

Loading…
Cancel
Save