34 lines
1.3 KiB
Rust
Raw Normal View History

use libpso::PacketParseError;
use libpso::crypto::PSOCipher;
2021-12-10 23:41:17 -07:00
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, derive_more::Display)]
2019-09-14 11:43:02 -07:00
pub struct ClientId(pub usize);
2022-10-18 04:46:21 -06:00
pub enum OnConnect<S: SendServerPacket, C: PSOCipher> {
Packet(S),
2022-10-18 04:46:21 -06:00
Cipher(C, C),
//Cipher((Box<dyn PSOCipher + Send + Sync>, Box<dyn PSOCipher + Send + Sync>)),
}
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>;
}
2020-06-02 18:51:18 -06:00
pub trait SendServerPacket: Sized + Sync {
fn as_bytes(&self) -> Vec<u8>;
}
// 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]
2022-10-18 04:46:21 -06:00
pub trait ServerState: Clone {
type SendPacket: SendServerPacket;
type RecvPacket: RecvServerPacket;
2022-10-18 04:46:21 -06:00
type Cipher: PSOCipher;
type PacketError;
2022-10-18 04:46:21 -06:00
async fn on_connect(&mut self, id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket, Self::Cipher>>, Self::PacketError>;
async fn handle(&mut self, id: ClientId, pkt: Self::RecvPacket) -> Result<Vec<(ClientId, Self::SendPacket)>, Self::PacketError>;
//-> Result<Box<dyn Iterator<Item = (ClientId, Self::SendPacket)>>, Self::PacketError>;
async fn on_disconnect(&mut self, id: ClientId) -> Result<Vec<(ClientId, Self::SendPacket)>, Self::PacketError>;
}