Network.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef CORE_NETWORK_H
  2. #define CORE_NETWORK_H
  3. #include "core/Buffer.h"
  4. #include "core/Types.h"
  5. typedef enum {
  6. CORE_RELIABLE,
  7. CORE_SEQUENCED,
  8. CORE_UNSEQUENCED
  9. } CorePacketSendMode;
  10. typedef struct {
  11. const char* data;
  12. size_t size;
  13. size_t index;
  14. } CoreInPacket;
  15. void coreInitInPacket(CoreInPacket* in, const void* data, size_t n);
  16. bool coreInPacketReadU8(CoreInPacket* in, u8* u);
  17. bool coreInPacketReadU16(CoreInPacket* in, u16* u);
  18. bool coreInPacketReadU32(CoreInPacket* in, u32* u);
  19. bool coreInPacketReadI8(CoreInPacket* in, i8* i);
  20. bool coreInPacketReadI16(CoreInPacket* in, i16* i);
  21. bool coreInPacketReadI32(CoreInPacket* in, i32* i);
  22. bool coreInPacketReadFloat(CoreInPacket* in, float* f);
  23. size_t coreInPacketReadString(CoreInPacket* in, char* buffer, size_t n);
  24. bool coreInPacketRead(CoreInPacket* in, void* buffer, size_t n);
  25. typedef struct {
  26. CoreBuffer data;
  27. } CoreOutPacket;
  28. void coreInitOutPacket(CoreOutPacket* out);
  29. void coreDestroyOutPacket(CoreOutPacket* out);
  30. CoreOutPacket* coreOutPacketWriteU8(CoreOutPacket* out, u8 u);
  31. CoreOutPacket* coreOutPacketWriteU16(CoreOutPacket* out, u16 u);
  32. CoreOutPacket* coreOutPacketWriteU32(CoreOutPacket* out, u32 u);
  33. CoreOutPacket* coreOutPacketWriteI8(CoreOutPacket* out, i8 i);
  34. CoreOutPacket* coreOutPacketWriteI16(CoreOutPacket* out, i16 i);
  35. CoreOutPacket* coreOutPacketWriteI32(CoreOutPacket* out, i32 i);
  36. CoreOutPacket* coreOutPacketWriteFloat(CoreOutPacket* out, float f);
  37. CoreOutPacket* coreOutPacketWriteString(CoreOutPacket* out, const char* buffer,
  38. size_t n);
  39. CoreOutPacket* coreOutPacketWrite(CoreOutPacket* out, const void* buffer,
  40. size_t n);
  41. /*namespace Client {
  42. typedef uint16 Port;
  43. typedef void (*OnConnect)();
  44. typedef void (*OnDisconnect)();
  45. typedef void (*OnPacket)(InPacket&);
  46. Error start();
  47. void stop();
  48. Error connect(const char* server, Port port, int timeoutTicks);
  49. void disconnect(int timeoutTicks);
  50. void tick();
  51. void send(OutPacket& p, PacketSendMode mode);
  52. void setConnectHandler(OnConnect oc);
  53. void setDisconnectHandler(OnDisconnect od);
  54. void setPacketHandler(OnPacket op);
  55. void resetHandler();
  56. bool isConnecting();
  57. bool isConnected();
  58. }
  59. namespace Server {
  60. typedef uint16 Port;
  61. typedef int Client;
  62. typedef void (*OnConnect)(Client);
  63. typedef void (*OnDisconnect)(Client);
  64. typedef void (*OnPacket)(Client, InPacket&);
  65. Error start(Port port, int maxClients);
  66. void stop();
  67. void tick();
  68. void send(const OutPacket& p, PacketSendMode mode);
  69. void send(Client client, const OutPacket& p, PacketSendMode mode);
  70. void disconnect(Client client);
  71. void setConnectHandler(OnConnect oc);
  72. void setDisconnectHandler(OnDisconnect od);
  73. void setPacketHandler(OnPacket op);
  74. void resetHandler();
  75. }*/
  76. #endif