diff --git a/examples/network/network_ping_pong.c b/examples/network/network_ping_pong.c index a56cafe8..22aaa69e 100644 --- a/examples/network/network_ping_pong.c +++ b/examples/network/network_ping_pong.c @@ -131,7 +131,7 @@ int main(void) InitNetworkDevice(); // Init network communications // Create the server: getaddrinfo + socket + setsockopt + bind + listen - serverResult = AllocSocketResult(); + serverResult = LoadSocketResult(); if (!SocketCreate(&serverConfig, serverResult)) { TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", serverResult->status, serverResult->socket->status); @@ -155,7 +155,7 @@ int main(void) } // Create the client: getaddrinfo + socket + setsockopt + connect (TCP only) - clientResult = AllocSocketResult(); + clientResult = LoadSocketResult(); if (!SocketCreate(&clientConfig, clientResult)) { TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status); @@ -172,7 +172,7 @@ int main(void) } // Create and add sockets to the socket set - socketSet = AllocSocketSet(3); + socketSet = LoadSocketSet(3); AddSocket(socketSet, serverResult->socket); AddSocket(socketSet, clientResult->socket); diff --git a/examples/network/network_resolve_host.c b/examples/network/network_resolve_host.c index c06f1e35..e2e3f721 100644 --- a/examples/network/network_resolve_host.c +++ b/examples/network/network_resolve_host.c @@ -28,7 +28,7 @@ int main(void) char buffer[ADDRESS_IPV6_ADDRSTRLEN]; unsigned short port = 0; - AddressInformation *address = AllocAddressList(1); + AddressInformation *address = LoadAddressList(1); // Address info flags // ADDRESS_INFO_NUMERICHOST // or try them in conjunction to diff --git a/examples/network/network_tcp_client.c b/examples/network/network_tcp_client.c index 91b57156..4259e25d 100644 --- a/examples/network/network_tcp_client.c +++ b/examples/network/network_tcp_client.c @@ -46,7 +46,7 @@ int main(void) char receiveBuffer[512] = { 0 }; // Create the client: getaddrinfo + socket + setsockopt + connect (TCP only) - clientResult = AllocSocketResult(); + clientResult = LoadSocketResult(); if (!SocketCreate(&clientConfig, clientResult)) TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status); else { @@ -57,7 +57,7 @@ int main(void) } // Create and add sockets to the socket set - socketSet = AllocSocketSet(1); + socketSet = LoadSocketSet(1); AddSocket(socketSet, clientResult->socket); SetTargetFPS(60); // Set our game to run at 60 frames-per-second diff --git a/examples/network/network_tcp_server.c b/examples/network/network_tcp_server.c index e47e12cf..ee64acfc 100644 --- a/examples/network/network_tcp_server.c +++ b/examples/network/network_tcp_server.c @@ -50,7 +50,7 @@ int main(void) char receiveBuffer[512] = { 0 }; // Create the server: getaddrinfo + socket + setsockopt + bind + listen - serverResult = AllocSocketResult(); + serverResult = LoadSocketResult(); if (!SocketCreate(&serverConfig, serverResult)) { TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", serverResult->status, serverResult->socket->status); @@ -71,7 +71,7 @@ int main(void) } // Create and add sockets to the socket set - socketSet = AllocSocketSet(2); + socketSet = LoadSocketSet(2); AddSocket(socketSet, serverResult->socket); SetTargetFPS(60); // Set our game to run at 60 frames-per-second diff --git a/examples/network/network_test.c b/examples/network/network_test.c index 2307410e..b756c2cd 100644 --- a/examples/network/network_test.c +++ b/examples/network/network_test.c @@ -23,17 +23,17 @@ void test_network_initialise() void test_socket_result() { - SocketResult *result = AllocSocketResult(); + SocketResult *result = LoadSocketResult(); assert(result != NULL); - FreeSocketResult(&result); + UnloadSocketResult(&result); assert(result == NULL); } void test_socket() { - Socket *socket = AllocSocket(); + Socket *socket = LoadSocket(); assert(socket != NULL); - FreeSocket(&socket); + UnloadSocket(&socket); assert(socket == NULL); } @@ -84,7 +84,7 @@ void test_resolve_host() { const char *address = "localhost"; const char *port = "80"; - AddressInformation *addr = AllocAddressList(3); + AddressInformation *addr = LoadAddressList(3); int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr); assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6); @@ -105,9 +105,9 @@ void test_address_list() void test_socket_create() { SocketConfig server_cfg = { .host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true }; - Socket *socket = AllocSocket(); - SocketResult *server_res = AllocSocketResult(); - SocketSet *socket_set = AllocSocketSet(1); + Socket *socket = LoadSocket(); + SocketResult *server_res = LoadSocketResult(); + SocketSet *socket_set = LoadSocketSet(1); assert(SocketCreate(&server_cfg, server_res)); assert(AddSocket(socket_set, server_res->socket)); diff --git a/examples/network/network_udp_client.c b/examples/network/network_udp_client.c index 38a4f0e9..74a241b6 100644 --- a/examples/network/network_udp_client.c +++ b/examples/network/network_udp_client.c @@ -45,14 +45,14 @@ int main(void) char receiveBuffer[512] = { 0 }; // Create the client: getaddrinfo + socket + setsockopt + connect (TCP only) - clientResult = AllocSocketResult(); + clientResult = LoadSocketResult(); if (!SocketCreate(&clientConfig, clientResult)) { TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status); } // Create and add sockets to the socket set - socketSet = AllocSocketSet(1); + socketSet = LoadSocketSet(1); AddSocket(socketSet, clientResult->socket); SetTargetFPS(60); // Set our game to run at 60 frames-per-second diff --git a/examples/network/network_udp_server.c b/examples/network/network_udp_server.c index 6eee2dad..5d5c425e 100644 --- a/examples/network/network_udp_server.c +++ b/examples/network/network_udp_server.c @@ -46,13 +46,13 @@ int main(void) char receiveBuffer[512] = { 0 }; // Create the server: getaddrinfo + socket + setsockopt + bind + listen - serverResult = AllocSocketResult(); + serverResult = LoadSocketResult(); if (!SocketCreate(&serverConfig, serverResult)) TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", serverResult->status, serverResult->socket->status); else if (!SocketBind(&serverConfig, serverResult)) TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d", serverResult->status, serverResult->socket->status); // Create and add sockets to the socket set - socketSet = AllocSocketSet(1); + socketSet = LoadSocketSet(1); AddSocket(socketSet, serverResult->socket); SetTargetFPS(60); // Set our game to run at 60 frames-per-second diff --git a/src/rnet.h b/src/rnet.h index 0e11f518..1145df01 100644 --- a/src/rnet.h +++ b/src/rnet.h @@ -77,15 +77,15 @@ #define NOMSG // typedef MSG and associated routines #define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_* #define NOSCROLL // SB_* and scrolling routines -#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc. -#define NOSOUND // Sound driver routines +#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc. +#define NOSOUND // Sound driver routines #define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines #define NOWH // SetWindowsHook and WH_* #define NOWINOFFSETS // GWL_*, GCL_*, associated routines -#define NOCOMM // COMM driver routines -#define NOKANJI // Kanji support stuff. -#define NOHELP // Help engine interface. -#define NOPROFILER // Profiler interface. +#define NOCOMM // COMM driver routines +#define NOKANJI // Kanji support stuff. +#define NOHELP // Help engine interface. +#define NOPROFILER // Profiler interface. #define NODEFERWINDOWPOS // DeferWindowPos routines #define NOMCX // Modem Configuration Extensions #define MMNOSOUND @@ -256,6 +256,11 @@ typedef struct IPAddress { unsigned short port; // 16-bit protocol port } IPAddress; +typedef struct UDPChannel { + int numbound; // The total number of addresses this channel is bound to + IPAddress address[SOCKET_MAX_UDPADDRESSES]; // The list of remote addresses this channel is bound to +} UDPChannel; + // An option ID, value, sizeof(value) tuple for setsockopt(2). typedef struct SocketOpt { int id; // Socked option id @@ -263,17 +268,13 @@ typedef struct SocketOpt { void *value; // Socked option value data } SocketOpt; -typedef struct UDPChannel { - int numbound; // The total number of addresses this channel is bound to - IPAddress address[SOCKET_MAX_UDPADDRESSES]; // The list of remote addresses this channel is bound to -} UDPChannel; - typedef struct Socket { int ready; // Is the socket ready? i.e. has information int status; // The last status code to have occured using this socket bool isServer; // Is this socket a server socket (i.e. TCP/UDP Listen Server) SocketChannel channel; // The socket handle id SocketType type; // Is this socket a TCP or UDP socket? + bool isIPv6; // Is this socket address an ipv6 address? SocketAddressIPv4 addripv4; // The host/target IPv4 for this socket (in network byte order) SocketAddressIPv6 addripv6; // The host/target IPv6 for this socket (in network byte order) @@ -281,6 +282,26 @@ typedef struct Socket { struct UDPChannel binding[SOCKET_MAX_UDPCHANNELS]; // The amount of channels (if UDP) this socket is bound to } Socket; +// Configuration for a socket +typedef struct SocketConfig { + SocketType type; // The type of socket, TCP/UDP + char *host; // The host address in xxx.xxx.xxx.xxx form + char *port; // The target port/service in the form "http" or "25565" + bool server; // Listen for incoming clients? + bool nonblocking; // non-blocking operation? + int backlog_size; // set a custom backlog size + SocketOpt sockopts[SOCKET_MAX_SOCK_OPTS]; +} SocketConfig; + +typedef struct SocketDataPacket { + IPAddress address; // The source/dest address of an incoming/outgoing packet + int channel; // The src/dst channel of the packet + int maxlen; // The size of the data buffer + int status; // Packet status after sending + unsigned int len; // The length of the packet data + unsigned char *data; // The packet data +} SocketDataPacket; + // Result from calling open with a given config typedef struct SocketResult { int status; // Socket result state @@ -293,26 +314,6 @@ typedef struct SocketSet { struct Socket **sockets; // Sockets array } SocketSet; -typedef struct SocketDataPacket { - IPAddress address; // The source/dest address of an incoming/outgoing packet - int channel; // The src/dst channel of the packet - int maxlen; // The size of the data buffer - int status; // Packet status after sending - unsigned int len; // The length of the packet data - unsigned char *data; // The packet data -} SocketDataPacket; - -// Configuration for a socket -typedef struct SocketConfig { - SocketType type; // The type of socket, TCP/UDP - char *host; // The host address in xxx.xxx.xxx.xxx form - char *port; // The target port/service in the form "http" or "25565" - bool server; // Listen for incoming clients? - bool nonblocking; // non-blocking operation? - int backlog_size; // set a custom backlog size - SocketOpt sockopts[SOCKET_MAX_SOCK_OPTS]; -} SocketConfig; - // Packet type typedef struct Packet { uint32_t size; // The total size of bytes in data @@ -347,12 +348,11 @@ int GetAddressSocketType(AddressInformation address); int GetAddressProtocol(AddressInformation address); char *GetAddressCanonName(AddressInformation address); char *GetAddressHostAndPort(AddressInformation address, char *outhost, int *outport); -void PrintAddressInfo(AddressInformation address); // Address Memory API -AddressInformation AllocAddress(); -void FreeAddress(AddressInformation *addressInfo); -AddressInformation *AllocAddressList(int size); +AddressInformation LoadAddress(void); +void UnloadAddress(AddressInformation *addressInfo); +AddressInformation *LoadAddressList(int size); // Socket API bool SocketCreate(SocketConfig *config, SocketResult *result); @@ -361,6 +361,14 @@ bool SocketListen(SocketConfig *config, SocketResult *result); bool SocketConnect(SocketConfig *config, SocketResult *result); Socket *SocketAccept(Socket *server, SocketConfig *config); +// General Socket API +int SocketSend(Socket *sock, const void *datap, int len); +int SocketReceive(Socket *sock, void *data, int maxlen); +SocketAddressStorage SocketGetPeerAddress(Socket *sock); +char *GetSocketAddressHost(SocketAddressStorage storage); +short GetSocketAddressPort(SocketAddressStorage storage); +void SocketClose(Socket *sock); + // UDP Socket API int SocketSetChannel(Socket *socket, int channel, const IPAddress *address); void SocketUnsetChannel(Socket *socket, int channel); @@ -372,21 +380,13 @@ void FreePacket(SocketDataPacket *packet); SocketDataPacket **AllocPacketList(int count, int size); void FreePacketList(SocketDataPacket **packets); -// General Socket API -int SocketSend(Socket *sock, const void *datap, int len); -int SocketReceive(Socket *sock, void *data, int maxlen); -void SocketClose(Socket *sock); -SocketAddressStorage SocketGetPeerAddress(Socket *sock); -char* GetSocketAddressHost(SocketAddressStorage storage); -short GetSocketAddressPort(SocketAddressStorage storage); - // Socket Memory API -Socket *AllocSocket(); -void FreeSocket(Socket **sock); -SocketResult *AllocSocketResult(); -void FreeSocketResult(SocketResult **result); -SocketSet *AllocSocketSet(int max); -void FreeSocketSet(SocketSet *sockset); +Socket *LoadSocket(void); +void UnloadSocket(Socket **sock); +SocketResult *LoadSocketResult(void); +void UnloadSocketResult(SocketResult **result); +SocketSet *LoadSocketSet(int max); +void UnloadSocketSet(SocketSet *sockset); // Socket I/O API bool IsSocketReady(Socket *sock); @@ -1120,7 +1120,7 @@ int ResolveHost(const char *address, const char *service, int addressType, int f int i; for (i = 0; i < size; ++i) { - outAddr[i] = AllocAddress(); + outAddr[i] = LoadAddress(); if (outAddr[i] == NULL) { break; @@ -1475,7 +1475,7 @@ Socket *SocketAccept(Socket *server, SocketConfig *config) struct sockaddr_storage sock_addr; socklen_t sock_alen; - Socket *sock = AllocSocket(); + Socket *sock = LoadSocket(); server->ready = 0; sock_alen = sizeof(sock_addr); sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr, &sock_alen); @@ -1863,14 +1863,14 @@ bool IsSocketConnected(Socket *sock) } // Allocate and return a SocketResult struct -SocketResult *AllocSocketResult(void) +SocketResult *LoadSocketResult(void) { struct SocketResult *res = (struct SocketResult *)RNET_MALLOC(sizeof(*res)); if (res != NULL) { memset(res, 0, sizeof(*res)); - if ((res->socket = AllocSocket()) == NULL) + if ((res->socket = LoadSocket()) == NULL) { RNET_FREE(res); res = NULL; @@ -1881,11 +1881,11 @@ SocketResult *AllocSocketResult(void) } // Free an allocated SocketResult -void FreeSocketResult(SocketResult **result) +void UnloadSocketResult(SocketResult **result) { if (*result != NULL) { - if ((*result)->socket != NULL) FreeSocket(&((*result)->socket)); + if ((*result)->socket != NULL) UnloadSocket(&((*result)->socket)); RNET_FREE(*result); *result = NULL; @@ -1893,7 +1893,7 @@ void FreeSocketResult(SocketResult **result) } // Allocate a Socket -Socket *AllocSocket(void) +Socket *LoadSocket(void) { struct Socket *sock; sock = (Socket *)RNET_MALLOC(sizeof(*sock)); @@ -1911,7 +1911,7 @@ Socket *AllocSocket(void) } // Free an allocated Socket -void FreeSocket(Socket **sock) +void UnloadSocket(Socket **sock) { if (*sock != NULL) { @@ -1921,7 +1921,7 @@ void FreeSocket(Socket **sock) } // Allocate a SocketSet -SocketSet *AllocSocketSet(int max) +SocketSet *LoadSocketSet(int max) { struct SocketSet *set = (struct SocketSet *)RNET_MALLOC(sizeof(*set)); @@ -1945,7 +1945,7 @@ SocketSet *AllocSocketSet(int max) } // Free an allocated SocketSet -void FreeSocketSet(SocketSet *set) +void UnloadSocketSet(SocketSet *set) { if (set) { @@ -2053,10 +2053,10 @@ int CheckSockets(SocketSet *set, unsigned int timeout) } // Allocate an AddressInformation -AddressInformation AllocAddress(void) +AddressInformation LoadAddress(void) { AddressInformation addressInfo = NULL; - addressInfo = (AddressInformation) RNET_CALLOC(1, sizeof(*addressInfo)); + addressInfo = (AddressInformation)RNET_CALLOC(1, sizeof(*addressInfo)); if (addressInfo != NULL) { @@ -2069,7 +2069,7 @@ AddressInformation AllocAddress(void) } // Free an AddressInformation struct -void FreeAddress(AddressInformation *addressInfo) +void UnloadAddress(AddressInformation *addressInfo) { if (*addressInfo != NULL) { @@ -2085,7 +2085,7 @@ void FreeAddress(AddressInformation *addressInfo) } // Allocate a list of AddressInformation -AddressInformation *AllocAddressList(int size) +AddressInformation *LoadAddressList(int size) { AddressInformation *addr; addr = (AddressInformation *)RNET_MALLOC(size * sizeof(AddressInformation));