Network.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef CORE_NETWORK_H
  2. #define CORE_NETWORK_H
  3. #include <core/Buffer.h>
  4. #include <core/Types.h>
  5. typedef enum {
  6. PACKET_RELIABLE,
  7. PACKET_SEQUENCED,
  8. PACKET_UNSEQUENCED
  9. } PacketSendMode;
  10. typedef struct {
  11. const char* data;
  12. size_t size;
  13. size_t index;
  14. } InPacket;
  15. void initInPacket(InPacket* in, const void* data, size_t n);
  16. bool readInPacketU8(InPacket* in, u8* u);
  17. bool readInPacketU16(InPacket* in, u16* u);
  18. bool readInPacketU32(InPacket* in, u32* u);
  19. bool readInPacketI8(InPacket* in, i8* i);
  20. bool readInPacketI16(InPacket* in, i16* i);
  21. bool readInPacketI32(InPacket* in, i32* i);
  22. bool readInPacketFloat(InPacket* in, float* f);
  23. size_t readInPacketString(InPacket* in, char* buffer, size_t n);
  24. bool readInPacket(InPacket* in, void* buffer, size_t n);
  25. typedef struct {
  26. Buffer data;
  27. } OutPacket;
  28. void initOutPacket(OutPacket* out);
  29. void destroyOutPacket(OutPacket* out);
  30. void writeOutPacketU8(OutPacket* out, u8 u);
  31. void writeOutPacketU16(OutPacket* out, u16 u);
  32. void writeOutPacketU32(OutPacket* out, u32 u);
  33. void writeOutPacketI8(OutPacket* out, i8 i);
  34. void writeOutPacketI16(OutPacket* out, i16 i);
  35. void writeOutPacketI32(OutPacket* out, i32 i);
  36. void writeOutPacketFloat(OutPacket* out, float f);
  37. void writeOutPacketString(OutPacket* out, const char* buffer);
  38. void writeOutPacket(OutPacket* out, const void* buffer, size_t n);
  39. typedef u16 Port;
  40. typedef void (*OnServerConnect)(void);
  41. typedef void (*OnServerDisconnect)(void);
  42. typedef void (*OnServerPacket)(InPacket*);
  43. bool startClient(void);
  44. void stopClient(void);
  45. bool connectClient(const char* server, Port port, int timeoutTicks);
  46. void setClientTimeout(u32 timeout, u32 timeoutMin, u32 timeoutMax);
  47. void disconnectClient(int timeoutTicks);
  48. void sendClientPacket(const OutPacket* p, PacketSendMode mode);
  49. void tickClient(void);
  50. void setClientConnectHandler(OnServerConnect oc);
  51. void setClientDisconnectHandler(OnServerDisconnect od);
  52. void setClientPacketHandler(OnServerPacket op);
  53. void resetClientHandler(void);
  54. bool isClientConnecting(void);
  55. bool isClientConnected(void);
  56. typedef int Client;
  57. typedef void (*OnClientConnect)(Client);
  58. typedef void (*OnClientDisconnect)(Client);
  59. typedef void (*OnClientPacket)(Client, InPacket*);
  60. bool startServer(Port port, size_t maxClients);
  61. void stopServer(void);
  62. void tickServer(void);
  63. void sendServerPacketBroadcast(const OutPacket* p, PacketSendMode mode);
  64. void sendServerPacket(Client client, const OutPacket* p, PacketSendMode mode);
  65. void setServerTimeout(Client client, u32 timeout, u32 timeoutMin,
  66. u32 timeoutMax);
  67. void disconnectServerClient(Client client);
  68. void setServerConnectHandler(OnClientConnect oc);
  69. void setServerDisconnectHandler(OnClientDisconnect od);
  70. void setServerPacketHandler(OnClientPacket op);
  71. void resetServerHandler(void);
  72. #endif