From 10458baea0766fd03eb077f2cb68af527f0c28fa Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 9 Nov 2020 16:04:42 -0700 Subject: [PATCH] shrink some structures, remove async from functions that didn't need it --- src/bin/main.rs | 30 +++++++++++++++--------------- src/common/mainloop/client.rs | 16 ++++++++-------- src/common/mainloop/mod.rs | 4 ++-- src/login/character.rs | 4 ++-- src/patch/patch.rs | 6 +++--- src/ship/packet/handler/lobby.rs | 4 ++-- src/ship/ship.rs | 10 +++++----- 7 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 7a91eab..ad797c0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -379,46 +379,46 @@ fn main() { let patch_config = load_config(); let patch_motd = load_motd(); let (patch_file_tree, patch_file_lookup) = generate_patch_tree(patch_config.path.as_str()); - let patch_state = PatchServerState::new(patch_file_tree, patch_file_lookup, patch_motd); - let patch_loop = patch_mainloop(patch_state, patch_config.port); + let patch_state = Box::new(PatchServerState::new(patch_file_tree, patch_file_lookup, patch_motd)); + let patch_loop = patch_mainloop(*patch_state, patch_config.port); let thread_entity_gateway = entity_gateway.clone(); info!("[auth] starting server"); - let login_state = LoginServerState::new(thread_entity_gateway, "127.0.0.1".parse().unwrap()); - let login_loop = login_mainloop(login_state, elseware::login::login::LOGIN_PORT); + let login_state = Box::new(LoginServerState::new(thread_entity_gateway, "127.0.0.1".parse().unwrap())); + let login_loop = login_mainloop(*login_state, elseware::login::login::LOGIN_PORT); let thread_entity_gateway = entity_gateway.clone(); info!("[character] starting server"); - let char_state = CharacterServerState::new(thread_entity_gateway, AuthToken("".into())); - let character_loop = character_mainloop(char_state, elseware::login::character::CHARACTER_PORT, elseware::login::login::COMMUNICATION_PORT); + let char_state = Box::new(CharacterServerState::new(thread_entity_gateway, AuthToken("".into()))); + let character_loop = character_mainloop(*char_state, elseware::login::character::CHARACTER_PORT, elseware::login::login::COMMUNICATION_PORT); let thread_entity_gateway = entity_gateway.clone(); info!("[ship] starting server"); - let ship_state = ShipServerStateBuilder::new() + let ship_state = Box::new(ShipServerStateBuilder::new() .name("US/Sona-Nyl".into()) .ip(Ipv4Addr::new(127,0,0,1)) .port(elseware::ship::ship::SHIP_PORT) .gateway(thread_entity_gateway) - .build(); - let ship_loop = ship_mainloop(ship_state, elseware::ship::ship::SHIP_PORT, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT); + .build()); + let ship_loop = ship_mainloop(*ship_state, elseware::ship::ship::SHIP_PORT, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT); let thread_entity_gateway = entity_gateway.clone(); - let ship_state = ShipServerStateBuilder::new() + let ship_state = Box::new(ShipServerStateBuilder::new() .name("EU/Dylath-Leen".into()) .ip(Ipv4Addr::new(127,0,0,1)) .port(elseware::ship::ship::SHIP_PORT+2000) .gateway(thread_entity_gateway) - .build(); - let ship_loop2 = ship_mainloop(ship_state, elseware::ship::ship::SHIP_PORT+2000, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT); + .build()); + let ship_loop2 = ship_mainloop(*ship_state, elseware::ship::ship::SHIP_PORT+2000, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT); let thread_entity_gateway = entity_gateway.clone(); - let ship_state = ShipServerStateBuilder::new() + let ship_state = Box::new(ShipServerStateBuilder::new() .name("JP/Thalarion".into()) .ip(Ipv4Addr::new(127,0,0,1)) .port(elseware::ship::ship::SHIP_PORT+3000) .gateway(thread_entity_gateway) - .build(); - let ship_loop3 = ship_mainloop(ship_state, elseware::ship::ship::SHIP_PORT+3000, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT); + .build()); + let ship_loop3 = ship_mainloop(*ship_state, elseware::ship::ship::SHIP_PORT+3000, std::net::Ipv4Addr::new(127, 0, 0, 1), elseware::login::login::COMMUNICATION_PORT); futures::future::join_all(vec![patch_loop, login_loop, character_loop, ship_loop, ship_loop2, ship_loop3]).await; }); diff --git a/src/common/mainloop/client.rs b/src/common/mainloop/client.rs index 383f0a6..fff2a4f 100644 --- a/src/common/mainloop/client.rs +++ b/src/common/mainloop/client.rs @@ -139,7 +139,7 @@ enum ServerStateAction { Disconnect, } -async fn client_recv_loop(client_id: ClientId, +fn client_recv_loop(client_id: ClientId, socket: Arc, cipher: Arc>>, server_sender: async_std::sync::Sender, R>>, @@ -177,7 +177,7 @@ where }); } -async fn client_send_loop(client_id: ClientId, +fn client_send_loop(client_id: ClientId, socket: Arc, cipher_in: Arc>>, cipher_out: Arc>>, @@ -207,7 +207,7 @@ where }); } -async fn state_client_loop(state: Arc>, +fn state_client_loop(state: Arc>, server_state_receiver: async_std::sync::Receiver, R>>) where STATE: ServerState + Send + 'static, S: SendServerPacket + std::fmt::Debug + Send + 'static, @@ -223,7 +223,6 @@ async fn state_client_loop(state: Arc>, match action { ClientAction::NewClient(client_id, sender) => { - clients.insert(client_id, sender.clone()); let actions = state.on_connect(client_id).await; match actions { Ok(actions) => { @@ -237,11 +236,12 @@ async fn state_client_loop(state: Arc>, } } } - } + }, Err(err) => { warn!("[client {:?} state on_connect error] {:?}", client_id, err); } } + clients.insert(client_id, sender); }, ClientAction::Packet(client_id, pkt) => { let pkts = state.handle(client_id, &pkt).await; @@ -295,7 +295,7 @@ where let mut id = 0; let (server_state_sender, server_state_receiver) = async_std::sync::channel(1024); - state_client_loop(state, server_state_receiver).await; + state_client_loop(state, server_state_receiver); loop { let (sock, addr) = listener.accept().await.unwrap(); @@ -309,8 +309,8 @@ where let cipher_in: Arc>> = Arc::new(Mutex::new(Box::new(NullCipher {}))); let cipher_out: Arc>> = Arc::new(Mutex::new(Box::new(NullCipher {}))); - client_recv_loop(client_id, socket.clone(), cipher_in.clone(), server_state_sender.clone(), client_sender).await; - client_send_loop(client_id, socket.clone(), cipher_in.clone(), cipher_out.clone(), client_receiver).await; + client_recv_loop(client_id, socket.clone(), cipher_in.clone(), server_state_sender.clone(), client_sender); + client_send_loop(client_id, socket.clone(), cipher_in.clone(), cipher_out.clone(), client_receiver); } })) } diff --git a/src/common/mainloop/mod.rs b/src/common/mainloop/mod.rs index 7214ae5..b0e207b 100644 --- a/src/common/mainloop/mod.rs +++ b/src/common/mainloop/mod.rs @@ -32,7 +32,7 @@ pub fn login_mainloop(login_state: LoginServerState pub fn character_mainloop(character_state: CharacterServerState, character_port: u16, comm_port: u16) -> Pin>> { let character_state = Arc::new(Mutex::new(character_state)); let client_mainloop = client_accept_mainloop(character_state.clone(), character_port); - let ship_communication_mainloop = login_listen_mainloop(character_state.clone(), comm_port); + let ship_communication_mainloop = login_listen_mainloop(character_state, comm_port); Box::pin(join_all(vec![client_mainloop, ship_communication_mainloop]).map(|_| ())) } @@ -40,6 +40,6 @@ pub fn character_mainloop(character_state: Characte pub fn ship_mainloop(ship_state: ShipServerState, ship_port: u16, comm_ip: std::net::Ipv4Addr, comm_port: u16) -> Pin>> { let ship_state = Arc::new(Mutex::new(ship_state)); let client_mainloop = client_accept_mainloop(ship_state.clone(), ship_port); - let login_communication_mainloop = ship_connect_mainloop(ship_state.clone(), comm_ip, comm_port); + let login_communication_mainloop = ship_connect_mainloop(ship_state, comm_ip, comm_port); Box::pin(join_all(vec![client_mainloop, login_communication_mainloop]).map(|_| ())) } diff --git a/src/login/character.rs b/src/login/character.rs index 3ec42b8..a991ed9 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -88,7 +88,7 @@ pub enum SendCharacterPacket { ChecksumAck(ChecksumAck), CharacterPreview(CharacterPreview), GuildcardDataHeader(GuildcardDataHeader), - GuildcardDataChunk(GuildcardDataChunk), + GuildcardDataChunk(Box), ParamDataHeader(ParamDataHeader), ParamDataChunk(ParamDataChunk), Timestamp(Timestamp), @@ -412,7 +412,7 @@ impl CharacterServerState { let mut buf = [0u8; GUILD_CARD_CHUNK_SIZE as usize]; buf[..len as usize].copy_from_slice(&client.guildcard_data_buffer.as_ref().unwrap()[start..end]); - vec![SendCharacterPacket::GuildcardDataChunk(GuildcardDataChunk::new(chunk, buf, len))] + vec![SendCharacterPacket::GuildcardDataChunk(Box::new(GuildcardDataChunk::new(chunk, buf, len)))] } else { Vec::new() }) diff --git a/src/patch/patch.rs b/src/patch/patch.rs index 65cbc3f..9d71f10 100644 --- a/src/patch/patch.rs +++ b/src/patch/patch.rs @@ -103,7 +103,7 @@ pub enum SendPatchPacket { ChangeDirectory(ChangeDirectory), EndFileSend(EndFileSend), FileInfo(FileInfo), - FileSend(FileSend), + FileSend(Box), FilesToPatchMetadata(FilesToPatchMetadata), FinalizePatching(FinalizePatching), Message(Message), @@ -338,12 +338,12 @@ impl Iterator for SendFileIterator { else { let mut crc = crc32::Digest::new(crc32::IEEE); crc.write(&buf[0..len]); - let pkt = SendPatchPacket::FileSend(FileSend { + let pkt = SendPatchPacket::FileSend(Box::new(FileSend { chunk_num: self.chunk_num, checksum: crc.sum32(), chunk_size: len as u32, buffer: buf, - }); + })); self.chunk_num += 1; Some(pkt) } diff --git a/src/ship/packet/handler/lobby.rs b/src/ship/packet/handler/lobby.rs index af9dcbb..cf4f024 100644 --- a/src/ship/packet/handler/lobby.rs +++ b/src/ship/packet/handler/lobby.rs @@ -38,9 +38,9 @@ pub fn block_selected(id: ClientId, .build(); Ok(vec![ - SendShipPacket::FullCharacter(FullCharacter { + SendShipPacket::FullCharacter(Box::new(FullCharacter { character: fc, - }), + })), SendShipPacket::CharDataRequest(CharDataRequest {}), SendShipPacket::LobbyList(LobbyList::new()), ]) diff --git a/src/ship/ship.rs b/src/ship/ship.rs index a717acb..a0aa5d3 100644 --- a/src/ship/ship.rs +++ b/src/ship/ship.rs @@ -145,7 +145,7 @@ pub enum SendShipPacket { ShipWelcome(ShipWelcome), LoginResponse(LoginResponse), ShipBlockList(ShipBlockList), - FullCharacter(FullCharacter), + FullCharacter(Box), CharDataRequest(CharDataRequest), JoinLobby(JoinLobby), AddToLobby(AddToLobby), @@ -341,10 +341,10 @@ impl ShipServerStateBuilder { ShipServerState { entity_gateway: self.entity_gateway.unwrap(), clients: HashMap::new(), - client_location: ClientLocation::new(), + client_location: Box::new(ClientLocation::new()), level_table: CharacterLevelTable::new(), name: self.name.unwrap_or("NAMENOTSET".into()), - rooms: [None; MAX_ROOMS], + rooms: Box::new([None; MAX_ROOMS]), item_manager: items::ItemManager::new(), quests: quests::load_quests("data/quests.toml".into()).unwrap(), ip: self.ip.unwrap_or(Ipv4Addr::new(127,0,0,1)), @@ -358,10 +358,10 @@ impl ShipServerStateBuilder { pub struct ShipServerState { entity_gateway: EG, pub clients: Clients, - client_location: ClientLocation, + client_location: Box, level_table: CharacterLevelTable, name: String, - pub rooms: Rooms, + pub rooms: Box, item_manager: items::ItemManager, quests: quests::QuestList, ip: Ipv4Addr,