You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

5 years ago
  1. use libpso::PacketParseError;
  2. use libpso::crypto::PSOCipher;
  3. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
  4. pub struct ClientId(pub usize);
  5. pub enum OnConnect<S: SendServerPacket> {
  6. Packet(S),
  7. Cipher((Box<dyn PSOCipher + Send + Sync>, Box<dyn PSOCipher + Send + Sync>)),
  8. }
  9. pub trait RecvServerPacket: Sized + Sync {
  10. fn from_bytes(data: &[u8]) -> Result<Self, PacketParseError>;
  11. }
  12. pub trait SendServerPacket: Sized + Sync {
  13. fn as_bytes(&self) -> Vec<u8>;
  14. }
  15. // TODO: rename this trait, this isn't the state but the actionability of the state re: the client
  16. #[async_trait::async_trait]
  17. pub trait ServerState {
  18. type SendPacket: SendServerPacket;
  19. type RecvPacket: RecvServerPacket;
  20. type PacketError;
  21. async fn on_connect(&mut self, id: ClientId) -> Result<Vec<OnConnect<Self::SendPacket>>, Self::PacketError>;
  22. async fn handle(&mut self, id: ClientId, pkt: &Self::RecvPacket)
  23. -> Result<Box<dyn Iterator<Item = (ClientId, Self::SendPacket)> + Send>, Self::PacketError>;
  24. async fn on_disconnect(&mut self, id: ClientId) -> Result<Vec<(ClientId, Self::SendPacket)>, Self::PacketError>;
  25. }