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.

33 lines
1.3 KiB

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