elseware/src/common/serverstate.rs

33 lines
1.1 KiB
Rust
Raw Normal View History

use libpso::PacketParseError;
use libpso::crypto::PSOCipher;
2019-09-14 11:43:02 -07:00
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct ClientId(pub usize);
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>)),
}
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]
pub trait ServerState {
type SendPacket: SendServerPacket;
type RecvPacket: RecvServerPacket;
type PacketError;
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>;
async fn on_disconnect(&mut self, id: ClientId) -> Vec<(ClientId, Self::SendPacket)>;
}