Network.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef GAMINGCORE_NETWORK_HPP
  2. #define GAMINGCORE_NETWORK_HPP
  3. #include <core/Buffer.hpp>
  4. #include <core/Types.hpp>
  5. #include <core/UniquePointer.hpp>
  6. namespace Core {
  7. enum class PacketSendMode { RELIABLE, SEQUENCED, UNSEQUENCED };
  8. class InPacket {
  9. const char* data;
  10. size_t size;
  11. size_t index;
  12. public:
  13. InPacket(const void* data, size_t n);
  14. InPacket(const InPacket&) = delete;
  15. InPacket(InPacket&&) = delete;
  16. InPacket& operator=(const InPacket&) = delete;
  17. InPacket& operator=(InPacket&&) = delete;
  18. bool readU8(u8& u);
  19. bool readU16(u16& u);
  20. bool readU32(u32& u);
  21. bool readI8(i8& i);
  22. bool readI16(i16& i);
  23. bool readI32(i32& i);
  24. bool readFloat(float& f);
  25. size_t readString(char* buffer, size_t n);
  26. bool read(void* buffer, size_t n);
  27. };
  28. struct OutPacket {
  29. Buffer data{};
  30. void writeU8(u8 u);
  31. void writeU16(u16 u);
  32. void writeU32(u32 u);
  33. void writeI8(i8 i);
  34. void writeI16(i16 i);
  35. void writeI32(i32 i);
  36. void writeFloat(float f);
  37. void writeString(const char* buffer);
  38. void write(const void* buffer, size_t n);
  39. };
  40. using Port = u16;
  41. using OnServerConnect = void (*)();
  42. using OnServerDisconnect = void (*)();
  43. using OnServerPacket = void (*)(InPacket&);
  44. struct Client final {
  45. Client();
  46. ~Client();
  47. bool start();
  48. void stop();
  49. bool connect(const char* server, Port port, int timeoutTicks);
  50. void setTimeout(u32 timeout, u32 timeoutMin, u32 timeoutMax);
  51. void disconnect(int timeoutTicks);
  52. void sendPacket(const OutPacket& p, PacketSendMode mode);
  53. void tick();
  54. void setConnectHandler(OnServerConnect oc);
  55. void setDisconnectHandler(OnServerDisconnect od);
  56. void setPacketHandler(OnServerPacket op);
  57. void resetHandler();
  58. bool isConnecting();
  59. bool isConnected();
  60. struct Data;
  61. private:
  62. UniquePointer<Data> data;
  63. };
  64. struct Server;
  65. using ClientHandle = int;
  66. using OnClientConnect = void (*)(Server&, ClientHandle);
  67. using OnClientDisconnect = void (*)(Server&, ClientHandle);
  68. using OnClientPacket = void (*)(Server&, ClientHandle, InPacket&);
  69. struct Server final {
  70. Server();
  71. ~Server();
  72. bool start(Port port, size_t maxClients);
  73. void stop();
  74. void tick();
  75. void sendPacketBroadcast(const OutPacket& p, PacketSendMode mode);
  76. void sendPacket(
  77. ClientHandle client, const OutPacket& p, PacketSendMode mode);
  78. void setTimeout(
  79. ClientHandle client, u32 timeout, u32 timeoutMin, u32 timeoutMax);
  80. void disconnectClient(ClientHandle client);
  81. void setConnectHandler(OnClientConnect oc);
  82. void setDisconnectHandler(OnClientDisconnect od);
  83. void setPacketHandler(OnClientPacket op);
  84. void resetHandler();
  85. struct Data;
  86. private:
  87. UniquePointer<Data> data;
  88. };
  89. }
  90. #endif