|
|
@ -1,28 +1,24 @@ |
|
|
|
use std::time::Duration;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use futures::future::{Future, join_all, FutureExt};
|
|
|
|
use log::{trace, info, warn};
|
|
|
|
use futures::future::Future;
|
|
|
|
use log::{info, warn};
|
|
|
|
use async_std::sync::{Arc, Mutex};
|
|
|
|
use async_std::io::prelude::{ReadExt, WriteExt};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
use serde::Serialize;
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
|
|
|
|
use crate::common::serverstate::{RecvServerPacket, SendServerPacket, ServerState, OnConnect};
|
|
|
|
use crate::common::interserver::{ServerId, InterserverActor, LoginMessage, ShipMessage};
|
|
|
|
use crate::common::mainloop::client::client_accept_mainloop;
|
|
|
|
pub use crate::common::mainloop::client::NetworkError;
|
|
|
|
use crate::common::interserver::{ServerId, InterserverActor};
|
|
|
|
|
|
|
|
use crate::patch::patch::PatchServerState;
|
|
|
|
use crate::login::character::CharacterServerState;
|
|
|
|
use crate::ship::ship::ShipServerState;
|
|
|
|
use crate::entity::gateway::entitygateway::EntityGateway;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum MessageReceiverError {
|
|
|
|
InvalidSize,
|
|
|
|
//InvalidSize,
|
|
|
|
InvalidPayload,
|
|
|
|
NetworkError(std::io::Error),
|
|
|
|
//NetworkError(std::io::Error),
|
|
|
|
Disconnected,
|
|
|
|
}
|
|
|
|
|
|
|
@ -41,20 +37,16 @@ impl MessageReceiver { |
|
|
|
let mut size_buf = [0u8; 4];
|
|
|
|
self.socket.read_exact(&mut size_buf).await.map_err(|_| MessageReceiverError::Disconnected)?;
|
|
|
|
let size = u32::from_le_bytes(size_buf) as usize;
|
|
|
|
info!("expected len: {:?}", size);
|
|
|
|
|
|
|
|
let mut payload = vec![0u8; size];
|
|
|
|
self.socket.read_exact(&mut payload).await.map_err(|err| MessageReceiverError::Disconnected)?;
|
|
|
|
let payload = String::from_utf8(payload).map_err(|_| MessageReceiverError::InvalidPayload)?;
|
|
|
|
|
|
|
|
info!("payload: {:?}", payload);
|
|
|
|
let msg = serde_json::from_str(&payload).map_err(|_| MessageReceiverError::InvalidPayload)?;
|
|
|
|
info!("msg: {:?}", msg);
|
|
|
|
Ok(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum InterserverInputAction<S, R> {
|
|
|
|
NewConnection(ServerId, async_std::sync::Sender<S>),
|
|
|
@ -62,10 +54,6 @@ enum InterserverInputAction<S, R> { |
|
|
|
Disconnect(ServerId),
|
|
|
|
}
|
|
|
|
|
|
|
|
/*struct LoginOutputAction {
|
|
|
|
Message
|
|
|
|
}*/
|
|
|
|
|
|
|
|
async fn interserver_state_loop<A, S, R>(state: Arc<Mutex<A>>, action_receiver: async_std::sync::Receiver<InterserverInputAction<S, R>>)
|
|
|
|
where
|
|
|
|
A: InterserverActor<SendMessage=S, RecvMessage=R, Error=()> + Send + 'static,
|
|
|
@ -211,17 +199,12 @@ pub fn ship_connect_mainloop<EG: EntityGateway + 'static>(state: Arc<Mutex<ShipS |
|
|
|
interserver_state_loop(state, server_state_receiver).await;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
//let listener = async_std::net::TcpListener::bind(&std::net::SocketAddr::from((std::net::Ipv4Addr::new(0,0,0,0), port))).await.unwrap();
|
|
|
|
// TOOD: err check and loop with timeout
|
|
|
|
let socket = async_std::net::TcpStream::connect((ip, port)).await.unwrap();
|
|
|
|
//let (socket, addr) = listener.accept().await.unwrap();
|
|
|
|
info!("ship connected to login: {:?}", socket);
|
|
|
|
|
|
|
|
id += 1;
|
|
|
|
let server_id = crate::common::interserver::ServerId(id);
|
|
|
|
|
|
|
|
let (client_sender, client_receiver) = async_std::sync::channel(64);
|
|
|
|
|
|
|
|
info!("ship connected to login: {:?}", socket);
|
|
|
|
login_recv_loop(server_id, socket.clone(), server_state_sender.clone(), client_sender).await;
|
|
|
|
interserver_send_loop(server_id, socket.clone(), client_receiver).await;
|
|
|
|
loop {
|
|
|
|