Comments and examples

This commit is contained in:
Jak Barnes 2019-02-23 13:51:20 +00:00
parent 97d01fe36d
commit bde0b6cd75
3 changed files with 646 additions and 558 deletions

View File

@ -37,9 +37,9 @@ int main()
InitNetwork(); InitNetwork();
AddressInformation addr; AddressInformation addr;
ResolveHost(&addr, "www.google.com", NULL, SOCKET_TCP); ResolveHost("www.google.com", "80", &addr);
ResolveIP("8.8.8.8", NULL); // ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT);
ResolveIP("2001:4860:4860::8888", "80"); // ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV);
// Main game loop // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose())

View File

@ -100,13 +100,19 @@
// Network limits // Network limits
#define MAX_SOCKET_SET_SIZE 32 #define MAX_SOCKET_SET_SIZE 32
#define MAX_SOCKET_QUEUE_SIZE 16 #define MAX_SOCKET_QUEUE_SIZE 16
#define MAX_HOST_NAME_SIZE 1025
#define MAX_SERV_NAME_SIZE 32
#define MAX_IPV4_NAME_SIZE 22
#define MAX_IPV6_NAME_SIZE 65
#define MAX_SOCK_OPTS 4 #define MAX_SOCK_OPTS 4
// getnameinfo() 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 */
// NOTE: MSC C++ compiler does not support compound literals (C99 feature) // NOTE: MSC C++ compiler does not support compound literals (C99 feature)
// Plain structures in C++ (without constructors) can be initialized from { } initializers. // Plain structures in C++ (without constructors) can be initialized from { } initializers.
#if defined(__cplusplus) #if defined(__cplusplus)
@ -437,6 +443,7 @@ typedef struct IPAddress
unsigned char* port; /* 16-bit protocol port */ unsigned char* port; /* 16-bit protocol port */
} IPAddress; } IPAddress;
// Used by the getaddrinfo function to hold host address information.
typedef struct AddressInformation typedef struct AddressInformation
{ {
int flags; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST int flags; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
@ -449,31 +456,35 @@ typedef struct AddressInformation
struct AddressInformation *next; // Next structure in linked list struct AddressInformation *next; // Next structure in linked list
} AddressInformation; } AddressInformation;
// Used
//
// The sockaddr structure varies depending on the protocol selected.
// Except for the sin*_family parameter, sockaddr contents are expressed
// in network byte order.
typedef struct SocketAddress typedef struct SocketAddress
{ {
unsigned short family; // Address family. unsigned short family; // Address family.
char data[14]; // Up to 14 bytes of direct address. char data[14]; // Up to 14 bytes of direct address.
} SocketAddress; } SocketAddress;
/* 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 option_id; int id;
void *value; void *value;
int value_len; int valueLen;
} SocketOpt; } SocketOpt;
typedef struct Socket typedef struct Socket
{ {
int ready; int ready; // Is the socket ready? i.e. has information
SocketHandle handle; SocketHandle handle; // The socket handle id
IPAddress remoteAddress; IPAddress host; // The host/target ip for this socket
IPAddress localAddress; int sflag; // Is this socket a server socket (i.e. TCP/UDP Listen Server)
int sflag;
} Socket; } Socket;
/* Configuration for a socket. Not all of these fields need to // Configuration for a socket. Not all of these fields need to
* be set, and ones omitted from a C99-style "designated initializer" // be set, and ones omitted from a C99-style "designated initializer"
* struct literal will be zeroed out and replaced with defaults. */ // struct literal will be zeroed out and replaced with defaults.
typedef struct SocketConfig { typedef struct SocketConfig {
/* Hostname and port, for TCP or UDP sockets. */ /* Hostname and port, for TCP or UDP sockets. */
char *host; char *host;
@ -488,7 +499,7 @@ typedef struct SocketConfig {
char *IPv6; char *IPv6;
bool server; /* Listen for incoming clients? */ bool server; /* Listen for incoming clients? */
bool datagram; /* UDP or datagram Unix domain? */ bool datagram; /* UDP or datagram? */
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 */
@ -1510,12 +1521,15 @@ RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pit
// Network (Module: network) // Network (Module: network)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Initialisation and cleanup
RLAPI bool InitNetwork(void); RLAPI bool InitNetwork(void);
RLAPI void CloseNetwork(void); RLAPI void CloseNetwork(void);
// Resolution // Protocol-independent name resolution from an address to an ANSI host name and from a port number to the ANSI service name.
RLAPI void ResolveHost(AddressInformation *outaddr, const char *address, const char *port, SocketType socketType); RLAPI char* ResolveIP(const char *host, const char *port, int flags);
RLAPI char *ResolveIP(const char *host, const char *port);
// Protocol-independent translation from an ANSI host name to an address.
RLAPI char* ResolveHost(const char *address, const char *port, AddressInformation *outaddr);
// IP // IP
RLAPI void GetLocalAddresses(); RLAPI void GetLocalAddresses();
@ -1530,7 +1544,7 @@ RLAPI int SocketGetError(char *buf, int buf_size, SocketResult *res);
RLAPI void SocketPrintError(SocketResult *res); RLAPI void SocketPrintError(SocketResult *res);
RLAPI void SocketSetHints(SocketConfig *cfg, AddressInformation *hints); RLAPI void SocketSetHints(SocketConfig *cfg, AddressInformation *hints);
// Print methods // Utility print methods
RLAPI char *SocketAddressToString(SocketAddress *sockaddr, char buffer[]); RLAPI char *SocketAddressToString(SocketAddress *sockaddr, char buffer[]);
RLAPI void PrintSocket(SocketAddress *addr, const int family, const int socktype, const int protocol); RLAPI void PrintSocket(SocketAddress *addr, const int family, const int socktype, const int protocol);

1140
src/rnet.c

File diff suppressed because it is too large Load Diff