[rnet] Review module formating

Align formating with other raylib modules coding style.
This commit is contained in:
Ray 2020-02-18 18:15:22 +01:00
parent 51db3b589d
commit 6d1cdf25a7
2 changed files with 495 additions and 659 deletions

File diff suppressed because it is too large Load diff

View file

@ -2,9 +2,6 @@
* *
* rnet - Provides cross-platform network defines, macros etc * rnet - Provides cross-platform network defines, macros etc
* *
* DEPENDENCIES:
* <limits.h> - Used for cross-platform type specifiers
*
* INSPIRED BY: * INSPIRED BY:
* SFML Sockets - https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Socket.php * SFML Sockets - https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Socket.php
* SDL_net - https://www.libsdl.org/projects/SDL_net/ * SDL_net - https://www.libsdl.org/projects/SDL_net/
@ -12,7 +9,6 @@
* BEEJ - https://beej.us/guide/bgnet/html/single/bgnet.html * BEEJ - https://beej.us/guide/bgnet/html/single/bgnet.html
* Winsock2 - https://docs.microsoft.com/en-us/windows/desktop/api/winsock2 * Winsock2 - https://docs.microsoft.com/en-us/windows/desktop/api/winsock2
* *
*
* CONTRIBUTORS: * CONTRIBUTORS:
* Jak Barnes (github: @syphonx) (Feb. 2019) - Initial version * Jak Barnes (github: @syphonx) (Feb. 2019) - Initial version
* *
@ -135,14 +131,14 @@ typedef int socklen_t;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Include system network headers // Include system network headers
#if defined(_WIN32) #if defined(_WIN32) // Windows
#define __USE_W32_SOCKETS #define __USE_W32_SOCKETS
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <io.h> #include <io.h>
#define IPTOS_LOWDELAY 0x10 #define IPTOS_LOWDELAY 0x10
#else // Unix #else // Unix
#include <sys/types.h> #include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -178,27 +174,34 @@ typedef int socklen_t;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Network connection related defines // Network connection related defines
#define SOCKET_MAX_SET_SIZE (32) // Maximum sockets in a set #define SOCKET_MAX_SET_SIZE 32 // Maximum sockets in a set
#define SOCKET_MAX_QUEUE_SIZE (16) // Maximum socket queue size #define SOCKET_MAX_QUEUE_SIZE 16 // Maximum socket queue size
#define SOCKET_MAX_SOCK_OPTS (4) // Maximum socket options #define SOCKET_MAX_SOCK_OPTS 4 // Maximum socket options
#define SOCKET_MAX_UDPCHANNELS (32) // Maximum UDP channels #define SOCKET_MAX_UDPCHANNELS 32 // Maximum UDP channels
#define SOCKET_MAX_UDPADDRESSES (4) // Maximum bound UDP addresses #define SOCKET_MAX_UDPADDRESSES 4 // Maximum bound UDP addresses
// Network address related defines // Network address related defines
#define ADDRESS_IPV4_ADDRSTRLEN (22) // IPv4 string length #define ADDRESS_IPV4_ADDRSTRLEN 22 // IPv4 string length
#define ADDRESS_IPV6_ADDRSTRLEN (65) // IPv6 string length #define ADDRESS_IPV6_ADDRSTRLEN 65 // IPv6 string length
#define ADDRESS_TYPE_ANY (0) // AF_UNSPEC #define ADDRESS_TYPE_ANY 0 // AF_UNSPEC
#define ADDRESS_TYPE_IPV4 (2) // AF_INET #define ADDRESS_TYPE_IPV4 2 // AF_INET
#define ADDRESS_TYPE_IPV6 (23) // AF_INET6 #define ADDRESS_TYPE_IPV6 23 // AF_INET6
#define ADDRESS_MAXHOST (1025) // Max size of a fully-qualified domain name #define ADDRESS_MAXHOST 1025 // Max size of a fully-qualified domain name
#define ADDRESS_MAXSERV (32) // Max size of a service name #define ADDRESS_MAXSERV 32 // Max size of a service name
// Network address related defines // Network address related defines
#define ADDRESS_ANY ((unsigned long) 0x00000000) #define ADDRESS_ANY (unsigned long)0x00000000
#define ADDRESS_LOOPBACK (0x7f000001) #define ADDRESS_LOOPBACK 0x7f000001
#define ADDRESS_BROADCAST ((unsigned long) 0xffffffff) #define ADDRESS_BROADCAST (unsigned long)0xffffffff
#define ADDRESS_NONE (0xffffffff) #define ADDRESS_NONE xffffffff
// Network resolution related defines
#define NAME_INFO_DEFAULT 0x00 // No flags set
#define NAME_INFO_NOFQDN 0x01 // Only return nodename portion for local hosts
#define NAME_INFO_NUMERICHOST 0x02 // Return numeric form of the host's address
#define NAME_INFO_NAMEREQD 0x04 // Error if the host's name not in DNS
#define NAME_INFO_NUMERICSERV 0x08 // Return numeric form of the service (port #)
#define NAME_INFO_DGRAM 0x10 // Service is a datagram service
// Address resolution related defines // Address resolution related defines
#if defined(_WIN32) #if defined(_WIN32)
@ -220,14 +223,6 @@ typedef int socklen_t;
#define ADDRESS_INFO_RESOLUTION_HANDLE (0x40000000) // Request resolution handle #define ADDRESS_INFO_RESOLUTION_HANDLE (0x40000000) // Request resolution handle
#endif #endif
// Network resolution related defines
#define NAME_INFO_DEFAULT (0x00) // No flags set
#define NAME_INFO_NOFQDN (0x01) // Only return nodename portion for local hosts
#define NAME_INFO_NUMERICHOST (0x02) // Return numeric form of the host's address
#define NAME_INFO_NAMEREQD (0x04) // Error if the host's name not in DNS
#define NAME_INFO_NUMERICSERV (0x08) // Return numeric form of the service (port #)
#define NAME_INFO_DGRAM (0x10) // Service is a datagram service
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -239,6 +234,11 @@ typedef int socklen_t;
typedef enum { false, true } bool; typedef enum { false, true } bool;
#endif #endif
typedef enum {
SOCKET_TCP = 0, // SOCK_STREAM
SOCKET_UDP = 1 // SOCK_DGRAM
} SocketType;
// Network typedefs // Network typedefs
typedef uint32_t SocketChannel; typedef uint32_t SocketChannel;
typedef struct _AddressInformation *AddressInformation; typedef struct _AddressInformation *AddressInformation;
@ -249,83 +249,78 @@ typedef struct _SocketAddressStorage *SocketAddressStorage;
// IPAddress definition (in network byte order) // IPAddress definition (in network byte order)
typedef struct IPAddress { typedef struct IPAddress {
unsigned long host; /* 32-bit IPv4 host address */ unsigned long host; // 32-bit IPv4 host address
unsigned short port; /* 16-bit protocol port */ unsigned short port; // 16-bit protocol port
} IPAddress; } IPAddress;
// An option ID, value, sizeof(value) tuple for setsockopt(2). // An option ID, value, sizeof(value) tuple for setsockopt(2).
typedef struct SocketOpt { typedef struct SocketOpt {
int id; int id; // Socked option id
void *value; int valueLen; // Socked option value len
int valueLen; void *value; // Socked option value data
} SocketOpt; } SocketOpt;
typedef enum {
SOCKET_TCP = 0, // SOCK_STREAM
SOCKET_UDP = 1 // SOCK_DGRAM
} SocketType;
typedef struct UDPChannel { typedef struct UDPChannel {
int numbound; // The total number of addresses this channel is bound to 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 IPAddress address[SOCKET_MAX_UDPADDRESSES]; // The list of remote addresses this channel is bound to
} UDPChannel; } UDPChannel;
typedef struct Socket { typedef struct Socket {
int ready; // Is the socket ready? i.e. has information int ready; // Is the socket ready? i.e. has information
int status; // The last status code to have occured using this socket 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) bool isServer; // Is this socket a server socket (i.e. TCP/UDP Listen Server)
SocketChannel channel; // The socket handle id SocketChannel channel; // The socket handle id
SocketType type; // Is this socket a TCP or UDP socket? SocketType type; // Is this socket a TCP or UDP socket?
bool isIPv6; // Is this socket address an ipv6 address? bool isIPv6; // Is this socket address an ipv6 address?
SocketAddressIPv4 addripv4; // The host/target IPv4 for this socket (in network byte order) 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) SocketAddressIPv6 addripv6; // The host/target IPv6 for this socket (in network byte order)
struct UDPChannel binding[SOCKET_MAX_UDPCHANNELS]; // The amount of channels (if UDP) this socket is bound to struct UDPChannel binding[SOCKET_MAX_UDPCHANNELS]; // The amount of channels (if UDP) this socket is bound to
} Socket; } Socket;
// Result from calling open with a given config
typedef struct SocketResult {
int status; // Socket result state
Socket *socket; // Socket ref
} SocketResult;
typedef struct SocketSet { typedef struct SocketSet {
int numsockets; int numsockets; // Socket set count
int maxsockets; int maxsockets; // Socket set max
struct Socket **sockets; struct Socket **sockets; // Sockets array
} SocketSet; } SocketSet;
typedef struct SocketDataPacket { typedef struct SocketDataPacket {
int channel; // The src/dst channel of the packet IPAddress address; // The source/dest address of an incoming/outgoing packet
unsigned char *data; // The packet data int channel; // The src/dst channel of the packet
int len; // The length of the packet data int maxlen; // The size of the data buffer
int maxlen; // The size of the data buffer int status; // Packet status after sending
int status; // packet status after sending unsigned int len; // The length of the packet data
IPAddress address; // The source/dest address of an incoming/outgoing packet unsigned char *data; // The packet data
} SocketDataPacket; } SocketDataPacket;
// Configuration for a socket. // Configuration for a socket
typedef struct SocketConfig { typedef struct SocketConfig {
char * host; // The host address in xxx.xxx.xxx.xxx form SocketType type; // The type of socket, TCP/UDP
char * port; // The target port/service in the form "http" or "25565" char *host; // The host address in xxx.xxx.xxx.xxx form
bool server; // Listen for incoming clients? char *port; // The target port/service in the form "http" or "25565"
SocketType type; // The type of socket, TCP/UDP bool server; // Listen for incoming clients?
bool nonblocking; // non-blocking operation? bool nonblocking; // non-blocking operation?
int backlog_size; // set a custom backlog size int backlog_size; // set a custom backlog size
SocketOpt sockopts[SOCKET_MAX_SOCK_OPTS]; SocketOpt sockopts[SOCKET_MAX_SOCK_OPTS];
} SocketConfig; } SocketConfig;
// Result from calling open with a given config.
typedef struct SocketResult {
int status;
Socket *socket;
} SocketResult;
// Packet type // Packet type
typedef struct Packet { typedef struct Packet {
uint32_t size; // The total size of bytes in data uint32_t size; // The total size of bytes in data
uint32_t offs; // The offset to data access uint32_t offs; // The offset to data access
uint32_t maxs; // The max size of data uint32_t maxs; // The max size of data
uint8_t *data; // Data stored in network byte order uint8_t *data; // Data stored in network byte order
} Packet; } Packet;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------