1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #ifndef CORE_NETWORK_H
- #define CORE_NETWORK_H
- #include <core/Buffer.h>
- #include <core/Types.h>
- typedef enum {
- PACKET_RELIABLE,
- PACKET_SEQUENCED,
- PACKET_UNSEQUENCED
- } PacketSendMode;
- typedef struct {
- const char* data;
- size_t size;
- size_t index;
- } InPacket;
- void initInPacket(InPacket* in, const void* data, size_t n);
- bool readInPacketU8(InPacket* in, u8* u);
- bool readInPacketU16(InPacket* in, u16* u);
- bool readInPacketU32(InPacket* in, u32* u);
- bool readInPacketI8(InPacket* in, i8* i);
- bool readInPacketI16(InPacket* in, i16* i);
- bool readInPacketI32(InPacket* in, i32* i);
- bool readInPacketFloat(InPacket* in, float* f);
- size_t readInPacketString(InPacket* in, char* buffer, size_t n);
- bool readInPacket(InPacket* in, void* buffer, size_t n);
- typedef struct {
- Buffer data;
- } OutPacket;
- void initOutPacket(OutPacket* out);
- void destroyOutPacket(OutPacket* out);
- void writeOutPacketU8(OutPacket* out, u8 u);
- void writeOutPacketU16(OutPacket* out, u16 u);
- void writeOutPacketU32(OutPacket* out, u32 u);
- void writeOutPacketI8(OutPacket* out, i8 i);
- void writeOutPacketI16(OutPacket* out, i16 i);
- void writeOutPacketI32(OutPacket* out, i32 i);
- void writeOutPacketFloat(OutPacket* out, float f);
- void writeOutPacketString(OutPacket* out, const char* buffer);
- void writeOutPacket(OutPacket* out, const void* buffer, size_t n);
- typedef u16 Port;
- typedef void (*OnServerConnect)(void);
- typedef void (*OnServerDisconnect)(void);
- typedef void (*OnServerPacket)(InPacket*);
- bool startClient(void);
- void stopClient(void);
- bool connectClient(const char* server, Port port, int timeoutTicks);
- void setClientTimeout(u32 timeout, u32 timeoutMin, u32 timeoutMax);
- void disconnectClient(int timeoutTicks);
- void sendClientPacket(const OutPacket* p, PacketSendMode mode);
- void tickClient(void);
- void setClientConnectHandler(OnServerConnect oc);
- void setClientDisconnectHandler(OnServerDisconnect od);
- void setClientPacketHandler(OnServerPacket op);
- void resetClientHandler(void);
- bool isClientConnecting(void);
- bool isClientConnected(void);
- typedef int Client;
- typedef void (*OnClientConnect)(Client);
- typedef void (*OnClientDisconnect)(Client);
- typedef void (*OnClientPacket)(Client, InPacket*);
- bool startServer(Port port, size_t maxClients);
- void stopServer(void);
- void tickServer(void);
- void sendServerPacketBroadcast(const OutPacket* p, PacketSendMode mode);
- void sendServerPacket(Client client, const OutPacket* p, PacketSendMode mode);
- void setServerTimeout(Client client, u32 timeout, u32 timeoutMin,
- u32 timeoutMax);
- void disconnectServerClient(Client client);
- void setServerConnectHandler(OnClientConnect oc);
- void setServerDisconnectHandler(OnClientDisconnect od);
- void setServerPacketHandler(OnClientPacket op);
- void resetServerHandler(void);
- #endif
|