2019-08-24 14:18:07 -07:00
|
|
|
use libpso::PacketParseError;
|
2019-07-17 23:17:57 -07:00
|
|
|
use libpso::crypto::PSOCipher;
|
|
|
|
|
2019-09-14 11:43:02 -07:00
|
|
|
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
|
|
|
|
pub struct ClientId(pub usize);
|
2019-07-17 23:17:57 -07:00
|
|
|
|
2019-08-24 14:18:07 -07:00
|
|
|
pub enum OnConnect<S: SendServerPacket> {
|
|
|
|
Packet(S),
|
2020-01-23 18:00:34 -08:00
|
|
|
Cipher((Box<dyn PSOCipher + Send + Sync>, Box<dyn PSOCipher + Send + Sync>)),
|
2019-07-17 23:17:57 -07:00
|
|
|
}
|
|
|
|
|
2020-06-02 18:51:18 -06:00
|
|
|
pub trait RecvServerPacket: Sized + Sync {
|
2019-09-04 09:17:22 -07:00
|
|
|
fn from_bytes(data: &[u8]) -> Result<Self, PacketParseError>;
|
2019-07-17 23:17:57 -07:00
|
|
|
}
|
|
|
|
|
2020-06-02 18:51:18 -06:00
|
|
|
pub trait SendServerPacket: Sized + Sync {
|
2019-08-24 14:18:07 -07:00
|
|
|
fn as_bytes(&self) -> Vec<u8>;
|
|
|
|
}
|
|
|
|
|
2020-08-19 21:21:09 -06:00
|
|
|
// TODO: rename this trait, this isn't the state but the actionability of the state re: the client
|
2020-06-02 18:51:18 -06:00
|
|
|
#[async_trait::async_trait]
|
2019-07-17 23:17:57 -07:00
|
|
|
pub trait ServerState {
|
2019-08-24 14:18:07 -07:00
|
|
|
type SendPacket: SendServerPacket;
|
|
|
|
type RecvPacket: RecvServerPacket;
|
2019-07-17 23:17:57 -07:00
|
|
|
type PacketError;
|
|
|
|
|
2020-10-27 22:27:12 -06:00
|
|
|
async fn on_connect(&mut self, id: ClientId) -> Vec<OnConnect<Self::SendPacket>>;
|
2020-06-02 18:51:18 -06:00
|
|
|
async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket)
|
|
|
|
-> Result<Box<dyn Iterator<Item = (ClientId, Self::SendPacket)> + Send>, Self::PacketError>;
|
2020-10-27 22:27:12 -06:00
|
|
|
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, Self::SendPacket)>;
|
2019-07-17 23:17:57 -07:00
|
|
|
}
|
|
|
|
|