added ping-pong networking example
Signed-off-by: Jak Barnes <contact@jakbarnes.co.uk>
This commit is contained in:
parent
85967c9f36
commit
bb01a49398
3 changed files with 1993 additions and 1567 deletions
2800
src/raylib.h
2800
src/raylib.h
File diff suppressed because it is too large
Load diff
266
src/rnet.c
266
src/rnet.c
|
|
@ -37,109 +37,239 @@
|
|||
//----------------------------------------------------------------------------------
|
||||
// Check if config flags have been externally provided on compilation line
|
||||
//----------------------------------------------------------------------------------
|
||||
#if !defined(EXTERNAL_CONFIG_FLAGS)
|
||||
#include "config.h" // Defines module configuration flags
|
||||
#if !defined(EXTERNAL_CONFIG_FLAGS)
|
||||
# include "config.h" // Defines module configuration flags
|
||||
#endif
|
||||
|
||||
#include "sysnet.h"
|
||||
#include "raylib.h"
|
||||
|
||||
bool InitializeSockets()
|
||||
#include "raylib.h"
|
||||
#include "sysnet.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
bool InitNetwork()
|
||||
{
|
||||
#if PLATFORM == PLATFORM_WINDOWS
|
||||
WSADATA WsaData;
|
||||
return WSAStartup
|
||||
(
|
||||
MAKEWORD(2, 2),
|
||||
&WsaData
|
||||
) == NO_ERROR;
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR) {
|
||||
TraceLog(LOG_INFO, "WinSock initialised.");
|
||||
return true;
|
||||
} else {
|
||||
TraceLog(LOG_WARNING, "WinSock failed to initialise.");
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void ShutdownSockets()
|
||||
void CloseNetwork()
|
||||
{
|
||||
#if PLATFORM == PLATFORM_WINDOWS
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
int CreateUDPSocket()
|
||||
void CreateTCPListenServer(TCPSocket* tcpsock, const char* address, const char* port)
|
||||
{
|
||||
int handle = socket(AF_INET,
|
||||
SOCK_DGRAM,
|
||||
IPPROTO_UDP);
|
||||
// Variables
|
||||
int status; // Status value to return (0) is success
|
||||
struct addrinfo hints; // Address flags (IPV4, IPV6, UDP?)
|
||||
struct addrinfo* results; // A pointer to the resulting address list
|
||||
|
||||
if (handle <= 0)
|
||||
{
|
||||
printf("failed to create socket\n");
|
||||
return false;
|
||||
// Set the hints
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; // Either IPv4 or IPv6 (AF_INET, AF_INET6)
|
||||
hints.ai_socktype = SOCK_STREAM; // TCP (SOCK_STREAM), UDP (SOCK_DGRAM)
|
||||
|
||||
// Populate address information
|
||||
status = getaddrinfo(address, // e.g. "www.example.com" or IP
|
||||
port, // e.g. "http" or port number
|
||||
&hints, // e.g. SOCK_STREAM/SOCK_DGRAM
|
||||
&results // The struct to populate
|
||||
);
|
||||
|
||||
// Did we succeed?
|
||||
if (status == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to get resolve host %s:%s: %s", address, port, strerror(status));
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully resolved host %s:%s", address, port);
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
// Create our server socket
|
||||
int sockfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);
|
||||
|
||||
bool OpenSocket(int handle, unsigned short port)
|
||||
{
|
||||
struct sockaddr_in address;
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons((unsigned short)port);
|
||||
// Bind it to the port we passed in to getaddrinfo():
|
||||
status = bind(sockfd, results->ai_addr, results->ai_addrlen);
|
||||
|
||||
if (bind(handle, (const struct sockaddr*)&address, sizeof( struct sockaddr_in)) < 0)
|
||||
{
|
||||
printf("failed to bind socket\n");
|
||||
return false;
|
||||
// Did we succeed?
|
||||
if (status == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to get bind socket to port (%s): %s", port, strerror(errno));
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully bound %s to port (%s)", address, port);
|
||||
}
|
||||
|
||||
#if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
|
||||
// Listen on the bound port
|
||||
status = listen(sockfd, 5);
|
||||
|
||||
int nonBlocking = 1;
|
||||
if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1)
|
||||
{
|
||||
printf("failed to set non-blocking\n");
|
||||
return false;
|
||||
// Did we succeed?
|
||||
if (status == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to listen to socket: %s", strerror(errno));
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully started listen server.");
|
||||
}
|
||||
|
||||
#elif PLATFORM == PLATFORM_WINDOWS
|
||||
|
||||
DWORD nonBlocking = 1;
|
||||
if (ioctlsocket(handle, FIONBIO, &nonBlocking) != 0)
|
||||
{
|
||||
printf("failed to set non-blocking\n");
|
||||
return false;
|
||||
if (ioctlsocket(sockfd, FIONBIO, &nonBlocking) == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to set socket to non-blocking.");
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully set socket to non-blocking.");
|
||||
}
|
||||
|
||||
#endif
|
||||
// Free the linked-list, we're not using it anymore
|
||||
freeaddrinfo(results);
|
||||
|
||||
return true;
|
||||
// Finally, return our socket descriptor
|
||||
tcpsock->sockfd = sockfd;
|
||||
tcpsock->server = 1;
|
||||
tcpsock->ready = true;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool SendData(int handle, const Address* destination, const void* data, int size)
|
||||
void CreateTCPClient(TCPSocket* tcpsock, char* address, char* port)
|
||||
{
|
||||
int sent_bytes = sendto(handle, (const char*) data, size, 0, ( struct sockaddr*) &destination, sizeof(struct sockaddr_in));
|
||||
if (sent_bytes != size) {
|
||||
printf("failed to send packet\n");
|
||||
return false;
|
||||
int status;
|
||||
int sockfd;
|
||||
struct addrinfo hints;
|
||||
struct addrinfo* results; // Will point to the results
|
||||
|
||||
memset(&hints, 0, sizeof hints); // Make sure the struct is empty
|
||||
hints.ai_family = AF_UNSPEC; // Don't care IPv4 or IPv6
|
||||
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
|
||||
|
||||
// Get ready to connect
|
||||
status = getaddrinfo(address, port, &hints, &results);
|
||||
|
||||
// Did we succeed?
|
||||
if (status != 0) {
|
||||
TraceLog(LOG_WARNING, "Failed to get address information: %s", strerror(errno));
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully created TCP client on port (%s)", port);
|
||||
}
|
||||
|
||||
// Create our socket
|
||||
sockfd = socket(results->ai_family, results->ai_socktype, results->ai_protocol);
|
||||
|
||||
// Did it succeed?
|
||||
if (sockfd == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to create socket: %s", strerror(errno));
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully created socket");
|
||||
}
|
||||
|
||||
// Connect to the server
|
||||
status = connect(sockfd, results->ai_addr, results->ai_addrlen);
|
||||
|
||||
if (status == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to connect to server %s:%s", address, port);
|
||||
} else {
|
||||
TraceLog(LOG_INFO, "Successfully connected to %s:%s", address, port);
|
||||
}
|
||||
|
||||
freeaddrinfo(results);
|
||||
|
||||
// Finally, return our socket descriptor
|
||||
tcpsock->sockfd = sockfd;
|
||||
tcpsock->server = 0;
|
||||
tcpsock->ready = true;
|
||||
}
|
||||
|
||||
void AcceptIncomingConnections(TCPSocket* tcpsock, int sockfd)
|
||||
{
|
||||
struct sockaddr_storage their_addr;
|
||||
socklen_t addr_size;
|
||||
int new_fd;
|
||||
addr_size = sizeof their_addr;
|
||||
new_fd = accept(sockfd, (struct sockaddr*) &their_addr, &addr_size);
|
||||
|
||||
if (new_fd == -1) {
|
||||
TraceLog(LOG_DEBUG, "Failed to accept incoming connection: %s", strerror(errno));
|
||||
} else {
|
||||
tcpsock->sockfd = new_fd;
|
||||
tcpsock->server = false;
|
||||
tcpsock->ready = true;
|
||||
TraceLog(LOG_INFO, "Successfully accepted a new connection.");
|
||||
}
|
||||
}
|
||||
|
||||
int ReceiveData(int handle, Address* sender, void* data, int size)
|
||||
int SendTCP(int sockfd, const char* data, int len)
|
||||
{
|
||||
#if PLATFORM == PLATFORM_WINDOWS
|
||||
typedef int socklen_t;
|
||||
#endif
|
||||
|
||||
while (true) {
|
||||
socklen_t fromLength = sizeof(AddressToInt(*sender));
|
||||
int bytes = recvfrom(handle, (char*) data, size, 0, (struct sockaddr*) &sender, &fromLength);
|
||||
if (bytes <= 0) break;
|
||||
return bytes;
|
||||
int sentBytes = send(sockfd, data, len, 0);
|
||||
if (sentBytes == -1) {
|
||||
TraceLog(LOG_WARNING, "Failed to send data: %s", strerror(errno));
|
||||
} else {
|
||||
TraceLog(LOG_DEBUG, "Successfully sent %d bytes.", sentBytes);
|
||||
}
|
||||
}
|
||||
|
||||
int AddressToInt(Address address)
|
||||
{
|
||||
return (address.a << 24) | (address.b << 16) | (address.c << 8) | address.d;
|
||||
}
|
||||
}
|
||||
|
||||
int ReceiveTCP(int sockfd, const char* data, int len)
|
||||
{
|
||||
int receiveBytes = recv(sockfd, data, len, 0);
|
||||
if (receiveBytes == -1) {
|
||||
TraceLog(LOG_DEBUG, "Failed to receive data: %s", strerror(errno));
|
||||
} else if (receiveBytes == 0) {
|
||||
TraceLog(LOG_INFO, "Connection closed.");
|
||||
} else {
|
||||
TraceLog(LOG_DEBUG, "Successfully received %d bytes.", receiveBytes);
|
||||
}
|
||||
return receiveBytes;
|
||||
}
|
||||
|
||||
void ResetSocket(TCPSocket* socket)
|
||||
{
|
||||
socket->ready = false;
|
||||
socket->server = 0;
|
||||
socket->sockfd = -1;
|
||||
};
|
||||
|
||||
void ResolveHost(const char* hostname)
|
||||
{
|
||||
struct addrinfo hints, *res, *p;
|
||||
int status;
|
||||
char ipstr[INET6_ADDRSTRLEN];
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_INET; // AF_INET or AF_INET6 to force version
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if ((status = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
|
||||
TraceLog(LOG_WARNING, "getaddrinfo: %s", strerror(status));
|
||||
return 2;
|
||||
}
|
||||
|
||||
TraceLog(LOG_INFO, "IP addresses for %s:", hostname);
|
||||
|
||||
for (p = res; p != NULL; p = p->ai_next) {
|
||||
void* addr;
|
||||
char* ipver;
|
||||
|
||||
// get the pointer to the address itself,
|
||||
// different fields in IPv4 and IPv6:
|
||||
if (p->ai_family == AF_INET) { // IPv4
|
||||
struct sockaddr_in* ipv4 = (struct sockaddr_in*) p->ai_addr;
|
||||
addr = &(ipv4->sin_addr);
|
||||
ipver = "IPv4";
|
||||
} else { // IPv6
|
||||
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*) p->ai_addr;
|
||||
addr = &(ipv6->sin6_addr);
|
||||
ipver = "IPv6";
|
||||
}
|
||||
|
||||
// convert the IP to a string and print it:
|
||||
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
|
||||
TraceLog(LOG_INFO, "%s: %s", ipver, ipstr);
|
||||
}
|
||||
|
||||
freeaddrinfo(res); // free the linked list
|
||||
}
|
||||
|
|
|
|||
78
src/sysnet.h
78
src/sysnet.h
|
|
@ -45,36 +45,58 @@
|
|||
|
||||
#define MMNOSOUND
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Platform specific network includes
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#define PLATFORM_WINDOWS 1
|
||||
#define PLATFORM_MAC 2
|
||||
#define PLATFORM_UNIX 3
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define PLATFORM PLATFORM_WINDOWS
|
||||
#elif defined(__APPLE__)
|
||||
#define PLATFORM PLATFORM_MAC
|
||||
//----------------------------------------------------------------------------------
|
||||
// Platform defines
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#define PLATFORM_WINDOWS 1
|
||||
#define PLATFORM_UNIX 2
|
||||
|
||||
#if defined(__WIN32__) || defined(WIN32)
|
||||
#define PLATFORM PLATFORM_WINDOWS
|
||||
#else
|
||||
#define PLATFORM PLATFORM_UNIX
|
||||
#endif
|
||||
|
||||
#if PLATFORM == PLATFORM_WINDOWS
|
||||
// #define _INC_WINDOWS
|
||||
// #define WIN32_LEAN_AND_MEAN
|
||||
// #include <minwindef.h>
|
||||
#endif
|
||||
|
||||
#if PLATFORM == PLATFORM_WINDOWS
|
||||
#include <winsock2.h>
|
||||
#elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#if PLATFORM == PLATFORM_WINDOWS
|
||||
#pragma comment(lib, "wsock32.lib")
|
||||
#endif
|
||||
//----------------------------------------------------------------------------------
|
||||
// Platform specific network includes
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#if PLATFORM_WINDOWS
|
||||
#define __USE_W32_SOCKETS
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <iphlpapi.h>
|
||||
#else /* UNIX */
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <netdb.h>
|
||||
#endif /* WIN32 */
|
||||
|
||||
/* System-dependent definitions */
|
||||
#ifndef __USE_W32_SOCKETS
|
||||
#define closesocket close
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
#endif /* __USE_W32_SOCKETS */
|
||||
|
||||
#ifdef __USE_W32_SOCKETS
|
||||
#define RNet_GetLastError WSAGetLastError
|
||||
#define RNet_SetLastError WSASetLastError
|
||||
#ifndef EINTR
|
||||
#define EINTR WSAEINTR
|
||||
#endif
|
||||
#else
|
||||
int RNet_GetLastError(void);
|
||||
void RNet_SetLastError(int err);
|
||||
#endif
|
||||
Loading…
Reference in a new issue