Browse Source

shrink some structures, remove async from functions that didn't need it

pbs
jake 4 years ago
parent
commit
10458baea0
  1. 30
      src/bin/main.rs
  2. 16
      src/common/mainloop/client.rs
  3. 4
      src/common/mainloop/mod.rs
  4. 4
      src/login/character.rs
  5. 6
      src/patch/patch.rs
  6. 4
      src/ship/packet/handler/lobby.rs
  7. 10
      src/ship/ship.rs

30
src/bin/main.rs

@ -379,46 +379,46 @@ fn main() {
let patch_config = load_config(); let patch_config = load_config();
let patch_motd = load_motd(); let patch_motd = load_motd();
let (patch_file_tree, patch_file_lookup) = generate_patch_tree(patch_config.path.as_str()); 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(); let thread_entity_gateway = entity_gateway.clone();
info!("[auth] starting server"); 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(); let thread_entity_gateway = entity_gateway.clone();
info!("[character] starting server"); 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(); let thread_entity_gateway = entity_gateway.clone();
info!("[ship] starting server"); info!("[ship] starting server");
let ship_state = ShipServerStateBuilder::new()
let ship_state = Box::new(ShipServerStateBuilder::new()
.name("US/Sona-Nyl".into()) .name("US/Sona-Nyl".into())
.ip(Ipv4Addr::new(127,0,0,1)) .ip(Ipv4Addr::new(127,0,0,1))
.port(elseware::ship::ship::SHIP_PORT) .port(elseware::ship::ship::SHIP_PORT)
.gateway(thread_entity_gateway) .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 thread_entity_gateway = entity_gateway.clone();
let ship_state = ShipServerStateBuilder::new()
let ship_state = Box::new(ShipServerStateBuilder::new()
.name("EU/Dylath-Leen".into()) .name("EU/Dylath-Leen".into())
.ip(Ipv4Addr::new(127,0,0,1)) .ip(Ipv4Addr::new(127,0,0,1))
.port(elseware::ship::ship::SHIP_PORT+2000) .port(elseware::ship::ship::SHIP_PORT+2000)
.gateway(thread_entity_gateway) .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 thread_entity_gateway = entity_gateway.clone();
let ship_state = ShipServerStateBuilder::new()
let ship_state = Box::new(ShipServerStateBuilder::new()
.name("JP/Thalarion".into()) .name("JP/Thalarion".into())
.ip(Ipv4Addr::new(127,0,0,1)) .ip(Ipv4Addr::new(127,0,0,1))
.port(elseware::ship::ship::SHIP_PORT+3000) .port(elseware::ship::ship::SHIP_PORT+3000)
.gateway(thread_entity_gateway) .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; futures::future::join_all(vec![patch_loop, login_loop, character_loop, ship_loop, ship_loop2, ship_loop3]).await;
}); });

16
src/common/mainloop/client.rs

@ -139,7 +139,7 @@ enum ServerStateAction<S> {
Disconnect, Disconnect,
} }
async fn client_recv_loop<S, R>(client_id: ClientId,
fn client_recv_loop<S, R>(client_id: ClientId,
socket: Arc<async_std::net::TcpStream>, socket: Arc<async_std::net::TcpStream>,
cipher: Arc<Mutex<Box<dyn PSOCipher + Send>>>, cipher: Arc<Mutex<Box<dyn PSOCipher + Send>>>,
server_sender: async_std::sync::Sender<ClientAction<ServerStateAction<S>, R>>, server_sender: async_std::sync::Sender<ClientAction<ServerStateAction<S>, R>>,
@ -177,7 +177,7 @@ where
}); });
} }
async fn client_send_loop<S>(client_id: ClientId,
fn client_send_loop<S>(client_id: ClientId,
socket: Arc<async_std::net::TcpStream>, socket: Arc<async_std::net::TcpStream>,
cipher_in: Arc<Mutex<Box<dyn PSOCipher + Send>>>, cipher_in: Arc<Mutex<Box<dyn PSOCipher + Send>>>,
cipher_out: Arc<Mutex<Box<dyn PSOCipher + Send>>>, cipher_out: Arc<Mutex<Box<dyn PSOCipher + Send>>>,
@ -207,7 +207,7 @@ where
}); });
} }
async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
server_state_receiver: async_std::sync::Receiver<ClientAction<ServerStateAction<S>, R>>) where server_state_receiver: async_std::sync::Receiver<ClientAction<ServerStateAction<S>, R>>) where
STATE: ServerState<SendPacket=S, RecvPacket=R, PacketError=E> + Send + 'static, STATE: ServerState<SendPacket=S, RecvPacket=R, PacketError=E> + Send + 'static,
S: SendServerPacket + std::fmt::Debug + Send + 'static, S: SendServerPacket + std::fmt::Debug + Send + 'static,
@ -223,7 +223,6 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
match action { match action {
ClientAction::NewClient(client_id, sender) => { ClientAction::NewClient(client_id, sender) => {
clients.insert(client_id, sender.clone());
let actions = state.on_connect(client_id).await; let actions = state.on_connect(client_id).await;
match actions { match actions {
Ok(actions) => { Ok(actions) => {
@ -237,11 +236,12 @@ async fn state_client_loop<STATE, S, R, E>(state: Arc<Mutex<STATE>>,
} }
} }
} }
}
},
Err(err) => { Err(err) => {
warn!("[client {:?} state on_connect error] {:?}", client_id, err); warn!("[client {:?} state on_connect error] {:?}", client_id, err);
} }
} }
clients.insert(client_id, sender);
}, },
ClientAction::Packet(client_id, pkt) => { ClientAction::Packet(client_id, pkt) => {
let pkts = state.handle(client_id, &pkt).await; let pkts = state.handle(client_id, &pkt).await;
@ -295,7 +295,7 @@ where
let mut id = 0; let mut id = 0;
let (server_state_sender, server_state_receiver) = async_std::sync::channel(1024); 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 { loop {
let (sock, addr) = listener.accept().await.unwrap(); let (sock, addr) = listener.accept().await.unwrap();
@ -309,8 +309,8 @@ where
let cipher_in: Arc<Mutex<Box<dyn PSOCipher + Send>>> = Arc::new(Mutex::new(Box::new(NullCipher {}))); let cipher_in: Arc<Mutex<Box<dyn PSOCipher + Send>>> = Arc::new(Mutex::new(Box::new(NullCipher {})));
let cipher_out: Arc<Mutex<Box<dyn PSOCipher + Send>>> = Arc::new(Mutex::new(Box::new(NullCipher {}))); let cipher_out: Arc<Mutex<Box<dyn PSOCipher + Send>>> = 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);
} }
})) }))
} }

4
src/common/mainloop/mod.rs

@ -32,7 +32,7 @@ pub fn login_mainloop<EG: EntityGateway + 'static>(login_state: LoginServerState
pub fn character_mainloop<EG: EntityGateway + 'static>(character_state: CharacterServerState<EG>, character_port: u16, comm_port: u16) -> Pin<Box<dyn Future<Output = ()>>> { pub fn character_mainloop<EG: EntityGateway + 'static>(character_state: CharacterServerState<EG>, character_port: u16, comm_port: u16) -> Pin<Box<dyn Future<Output = ()>>> {
let character_state = Arc::new(Mutex::new(character_state)); let character_state = Arc::new(Mutex::new(character_state));
let client_mainloop = client_accept_mainloop(character_state.clone(), character_port); 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(|_| ())) Box::pin(join_all(vec![client_mainloop, ship_communication_mainloop]).map(|_| ()))
} }
@ -40,6 +40,6 @@ pub fn character_mainloop<EG: EntityGateway + 'static>(character_state: Characte
pub fn ship_mainloop<EG: EntityGateway + 'static>(ship_state: ShipServerState<EG>, ship_port: u16, comm_ip: std::net::Ipv4Addr, comm_port: u16) -> Pin<Box<dyn Future<Output = ()>>> { pub fn ship_mainloop<EG: EntityGateway + 'static>(ship_state: ShipServerState<EG>, ship_port: u16, comm_ip: std::net::Ipv4Addr, comm_port: u16) -> Pin<Box<dyn Future<Output = ()>>> {
let ship_state = Arc::new(Mutex::new(ship_state)); let ship_state = Arc::new(Mutex::new(ship_state));
let client_mainloop = client_accept_mainloop(ship_state.clone(), ship_port); 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(|_| ())) Box::pin(join_all(vec![client_mainloop, login_communication_mainloop]).map(|_| ()))
} }

4
src/login/character.rs

@ -88,7 +88,7 @@ pub enum SendCharacterPacket {
ChecksumAck(ChecksumAck), ChecksumAck(ChecksumAck),
CharacterPreview(CharacterPreview), CharacterPreview(CharacterPreview),
GuildcardDataHeader(GuildcardDataHeader), GuildcardDataHeader(GuildcardDataHeader),
GuildcardDataChunk(GuildcardDataChunk),
GuildcardDataChunk(Box<GuildcardDataChunk>),
ParamDataHeader(ParamDataHeader), ParamDataHeader(ParamDataHeader),
ParamDataChunk(ParamDataChunk), ParamDataChunk(ParamDataChunk),
Timestamp(Timestamp), Timestamp(Timestamp),
@ -412,7 +412,7 @@ impl<EG: EntityGateway> CharacterServerState<EG> {
let mut buf = [0u8; GUILD_CARD_CHUNK_SIZE as usize]; 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]); 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 { } else {
Vec::new() Vec::new()
}) })

6
src/patch/patch.rs

@ -103,7 +103,7 @@ pub enum SendPatchPacket {
ChangeDirectory(ChangeDirectory), ChangeDirectory(ChangeDirectory),
EndFileSend(EndFileSend), EndFileSend(EndFileSend),
FileInfo(FileInfo), FileInfo(FileInfo),
FileSend(FileSend),
FileSend(Box<FileSend>),
FilesToPatchMetadata(FilesToPatchMetadata), FilesToPatchMetadata(FilesToPatchMetadata),
FinalizePatching(FinalizePatching), FinalizePatching(FinalizePatching),
Message(Message), Message(Message),
@ -338,12 +338,12 @@ impl Iterator for SendFileIterator {
else { else {
let mut crc = crc32::Digest::new(crc32::IEEE); let mut crc = crc32::Digest::new(crc32::IEEE);
crc.write(&buf[0..len]); crc.write(&buf[0..len]);
let pkt = SendPatchPacket::FileSend(FileSend {
let pkt = SendPatchPacket::FileSend(Box::new(FileSend {
chunk_num: self.chunk_num, chunk_num: self.chunk_num,
checksum: crc.sum32(), checksum: crc.sum32(),
chunk_size: len as u32, chunk_size: len as u32,
buffer: buf, buffer: buf,
});
}));
self.chunk_num += 1; self.chunk_num += 1;
Some(pkt) Some(pkt)
} }

4
src/ship/packet/handler/lobby.rs

@ -38,9 +38,9 @@ pub fn block_selected(id: ClientId,
.build(); .build();
Ok(vec![ Ok(vec![
SendShipPacket::FullCharacter(FullCharacter {
SendShipPacket::FullCharacter(Box::new(FullCharacter {
character: fc, character: fc,
}),
})),
SendShipPacket::CharDataRequest(CharDataRequest {}), SendShipPacket::CharDataRequest(CharDataRequest {}),
SendShipPacket::LobbyList(LobbyList::new()), SendShipPacket::LobbyList(LobbyList::new()),
]) ])

10
src/ship/ship.rs

@ -145,7 +145,7 @@ pub enum SendShipPacket {
ShipWelcome(ShipWelcome), ShipWelcome(ShipWelcome),
LoginResponse(LoginResponse), LoginResponse(LoginResponse),
ShipBlockList(ShipBlockList), ShipBlockList(ShipBlockList),
FullCharacter(FullCharacter),
FullCharacter(Box<FullCharacter>),
CharDataRequest(CharDataRequest), CharDataRequest(CharDataRequest),
JoinLobby(JoinLobby), JoinLobby(JoinLobby),
AddToLobby(AddToLobby), AddToLobby(AddToLobby),
@ -341,10 +341,10 @@ impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
ShipServerState { ShipServerState {
entity_gateway: self.entity_gateway.unwrap(), entity_gateway: self.entity_gateway.unwrap(),
clients: HashMap::new(), clients: HashMap::new(),
client_location: ClientLocation::new(),
client_location: Box::new(ClientLocation::new()),
level_table: CharacterLevelTable::new(), level_table: CharacterLevelTable::new(),
name: self.name.unwrap_or("NAMENOTSET".into()), name: self.name.unwrap_or("NAMENOTSET".into()),
rooms: [None; MAX_ROOMS],
rooms: Box::new([None; MAX_ROOMS]),
item_manager: items::ItemManager::new(), item_manager: items::ItemManager::new(),
quests: quests::load_quests("data/quests.toml".into()).unwrap(), quests: quests::load_quests("data/quests.toml".into()).unwrap(),
ip: self.ip.unwrap_or(Ipv4Addr::new(127,0,0,1)), ip: self.ip.unwrap_or(Ipv4Addr::new(127,0,0,1)),
@ -358,10 +358,10 @@ impl<EG: EntityGateway> ShipServerStateBuilder<EG> {
pub struct ShipServerState<EG: EntityGateway> { pub struct ShipServerState<EG: EntityGateway> {
entity_gateway: EG, entity_gateway: EG,
pub clients: Clients, pub clients: Clients,
client_location: ClientLocation,
client_location: Box<ClientLocation>,
level_table: CharacterLevelTable, level_table: CharacterLevelTable,
name: String, name: String,
pub rooms: Rooms,
pub rooms: Box<Rooms>,
item_manager: items::ItemManager, item_manager: items::ItemManager,
quests: quests::QuestList, quests: quests::QuestList,
ip: Ipv4Addr, ip: Ipv4Addr,

Loading…
Cancel
Save