REMOVED: Network examples
They were not working
This commit is contained in:
parent
1aaa55c38e
commit
c3d32c156c
16 changed files with 0 additions and 3871 deletions
|
@ -188,22 +188,6 @@ Examples showing physics functionality with raylib. This functionality is provid
|
|||
| 110 | [physics_restitution](physics/physics_restitution.c) | <img src="physics/physics_restitution.png" alt="physics_restitution" width="200"> | [Victor Fisac](https://github.com/victorfisac) | |
|
||||
| 111 | [physics_shatter](physics/physics_shatter.c) | <img src="physics/physics_shatter.png" alt="physics_shatter" width="200"> | [Victor Fisac](https://github.com/victorfisac) | |
|
||||
|
||||
### category: network
|
||||
|
||||
Examples showing raylib network functionality. This functionality is provided by [rnet](../src/rnet.h) module.
|
||||
|
||||
**Note that rnet module is under development and not ready yet.**
|
||||
|
||||
| ## | example | image | developer | new |
|
||||
|----|----------|--------|:----------:|:---:|
|
||||
| 112 | [network_ping_pong](network/network_ping_pong.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
| 113 | [network_resolve_host](network/network_resolve_host.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
| 114 | [network_tcp_client](network/network_tcp_client.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
| 115 | [network_tcp_server](network/network_tcp_server.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
| 116 | [network_test](network/network_test.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
| 117 | [network_udp_client](network/network_udp_client.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
| 118 | [network_udp_server](network/network_udp_server.c) | | [Jak Barnes](https://github.com/syphonx) | |
|
||||
|
||||
### category: others
|
||||
|
||||
Examples showing raylib misc functionality that does not fit in other categories, like standalone modules usage or examples integrating external libraries.
|
||||
|
|
|
@ -1,212 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Client/Server ping-pong
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
float elapsed = 0.0f;
|
||||
float delay = 1.0f;
|
||||
bool ping = false;
|
||||
bool pong = false;
|
||||
bool connected = false;
|
||||
bool clientConnected = false;
|
||||
const char *pingmsg = "Ping!";
|
||||
const char *pongmsg = "Pong!";
|
||||
int msglen = 0;
|
||||
SocketConfig serverConfig = { .host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .server = true, .nonblocking = true };
|
||||
SocketConfig clientConfig = { .host = "127.0.0.1", .port = "4950", .type = SOCKET_TCP, .nonblocking = true };
|
||||
SocketConfig connectionConfig = { .nonblocking = true };
|
||||
SocketResult *serverResult = NULL;
|
||||
SocketResult *clientResult = NULL;
|
||||
SocketSet *socketSet = NULL;
|
||||
Socket *connection = NULL;
|
||||
char receiveBuffer[512] = { 0 };
|
||||
|
||||
// Attempt to connect to the network (Either TCP, or UDP)
|
||||
static void NetworkConnect(void)
|
||||
{
|
||||
// If the server is configured as UDP, ignore connection requests
|
||||
if ((serverConfig.type == SOCKET_UDP) && (clientConfig.type == SOCKET_UDP))
|
||||
{
|
||||
ping = true;
|
||||
connected = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the client is connected, run the server code to check for a connection
|
||||
if (clientConnected)
|
||||
{
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
if (active > 0)
|
||||
{
|
||||
if ((connection = SocketAccept(serverResult->socket, &connectionConfig)) != NULL)
|
||||
{
|
||||
AddSocket(socketSet, connection);
|
||||
connected = true;
|
||||
ping = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if we're connected every _delay_ seconds
|
||||
elapsed += GetFrameTime();
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (IsSocketConnected(clientResult->socket)) clientConnected = true;
|
||||
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Once connected to the network, check the sockets for pending information
|
||||
// and when information is ready, send either a Ping or a Pong.
|
||||
static void UpdateNetwork(void)
|
||||
{
|
||||
// CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
|
||||
// then mark the socket as being ready. You can check this with IsSocketReady(clientResult->socket)
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_DEBUG, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
// IsSocketReady, if the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if ((serverConfig.type == SOCKET_UDP) && (clientConfig.type == SOCKET_UDP))
|
||||
{
|
||||
if (IsSocketReady(clientResult->socket)) bytesRecv = SocketReceive(clientResult->socket, receiveBuffer, msglen);
|
||||
if (IsSocketReady(serverResult->socket)) bytesRecv = SocketReceive(serverResult->socket, receiveBuffer, msglen);
|
||||
}
|
||||
else if (IsSocketReady(connection)) bytesRecv = SocketReceive(connection, receiveBuffer, msglen);
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
|
||||
if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
|
||||
}
|
||||
|
||||
// After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
|
||||
elapsed += GetFrameTime();
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
if (serverConfig.type == SOCKET_UDP && clientConfig.type == SOCKET_UDP) SocketSend(clientResult->socket, pingmsg, msglen);
|
||||
else SocketSend(clientResult->socket, pingmsg, msglen);
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
if (serverConfig.type == SOCKET_UDP && clientConfig.type == SOCKET_UDP) SocketSend(clientResult->socket, pongmsg, msglen);
|
||||
else SocketSend(clientResult->socket, pongmsg, msglen);
|
||||
}
|
||||
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - ping pong");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
// Create the server: getaddrinfo + socket + setsockopt + bind + listen
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(serverConfig.type == SOCKET_UDP))
|
||||
{
|
||||
if (!SocketListen(&serverConfig, serverResult))
|
||||
{
|
||||
TraceLog(LOG_WARNING, "Failed to start listen server: status %d, errno %d", serverResult->status, serverResult->socket->status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create the client: getaddrinfo + socket + setsockopt + connect (TCP only)
|
||||
clientResult = LoadSocketResult();
|
||||
if (!SocketCreate(&clientConfig, clientResult))
|
||||
{
|
||||
TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(clientConfig.type == SOCKET_UDP))
|
||||
{
|
||||
if (!SocketConnect(&clientConfig, clientResult))
|
||||
{
|
||||
TraceLog(LOG_WARNING, "Failed to connect to server: status %d, errno %d", clientResult->status, clientResult->socket->status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create and add sockets to the socket set
|
||||
socketSet = LoadSocketSet(3);
|
||||
|
||||
AddSocket(socketSet, serverResult->socket);
|
||||
AddSocket(socketSet, clientResult->socket);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (connected) UpdateNetwork();
|
||||
//else NetworkConnect();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Resolve Host
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - resolve host");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
char buffer[ADDRESS_IPV6_ADDRSTRLEN];
|
||||
unsigned short port = 0;
|
||||
|
||||
AddressInformation *address = LoadAddressList(1);
|
||||
|
||||
// Address info flags
|
||||
// ADDRESS_INFO_NUMERICHOST // or try them in conjunction to
|
||||
// ADDRESS_INFO_NUMERICSERV // specify custom behaviour from
|
||||
// ADDRESS_INFO_DNS_ONLY // the function getaddrinfo()
|
||||
// ADDRESS_INFO_ALL //
|
||||
// ADDRESS_INFO_FQDN // e.g. ADDRESS_INFO_CANONNAME | ADDRESS_INFO_NUMERICSERV
|
||||
int count = ResolveHost(NULL, "5210", ADDRESS_TYPE_IPV4, 0, address);
|
||||
|
||||
if (count > 0)
|
||||
{
|
||||
GetAddressHostAndPort(address[0], buffer, &port);
|
||||
TraceLog(LOG_INFO, "Resolved to ip %s::%d", buffer, port);
|
||||
}
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,142 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - TCP Client
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - tcp client");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
const char *pingmsg = "Ping!";
|
||||
const char *pongmsg = "Pong!";
|
||||
|
||||
bool ping = false;
|
||||
bool pong = false;
|
||||
float elapsed = 0.0f;
|
||||
float delay = 1.0f;
|
||||
bool connected = false;
|
||||
|
||||
SocketConfig clientConfig = {
|
||||
.host = "127.0.0.1",
|
||||
.port = "4950",
|
||||
.type = SOCKET_TCP,
|
||||
.nonblocking = true
|
||||
};
|
||||
|
||||
SocketSet *socketSet = NULL;
|
||||
SocketResult *clientResult = NULL;
|
||||
char receiveBuffer[512] = { 0 };
|
||||
|
||||
// Create the client: getaddrinfo + socket + setsockopt + connect (TCP only)
|
||||
clientResult = LoadSocketResult();
|
||||
if (!SocketCreate(&clientConfig, clientResult)) TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d", clientResult->status, clientResult->socket->status);
|
||||
else
|
||||
{
|
||||
if (!(clientConfig.type == SOCKET_UDP))
|
||||
{
|
||||
if (!SocketConnect(&clientConfig, clientResult)) TraceLog(LOG_WARNING, "Failed to connect to server: status %d, errno %d", clientResult->status, clientResult->socket->status);
|
||||
}
|
||||
}
|
||||
|
||||
// Create and add sockets to the socket set
|
||||
socketSet = LoadSocketSet(1);
|
||||
AddSocket(socketSet, clientResult->socket);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (connected)
|
||||
{
|
||||
// Once connected to the network, check the sockets for pending information
|
||||
// and when information is ready, send either a Ping or a Pong.
|
||||
|
||||
// CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
|
||||
// then mark the socket as being ready. You can check this with IsSocketReady(clientResult->socket)
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_DEBUG, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
// IsSocketReady, if the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if (IsSocketReady(clientResult->socket)) bytesRecv = SocketReceive(clientResult->socket, receiveBuffer, (int)strlen(pingmsg) + 1);
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
|
||||
if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
|
||||
}
|
||||
|
||||
// After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
|
||||
elapsed += GetFrameTime();
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
SocketSend(clientResult->socket, pingmsg, (int)strlen(pingmsg) + 1);
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
SocketSend(clientResult->socket, pongmsg, (int)strlen(pingmsg) + 1);
|
||||
}
|
||||
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if we're connected every delay seconds
|
||||
elapsed += GetFrameTime();
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (IsSocketConnected(clientResult->socket)) { connected = true; }
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,162 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - TCP Server
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - tcp server");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
const char *pingmsg = "Ping!";
|
||||
const char *pongmsg = "Pong!";
|
||||
|
||||
bool ping = false;
|
||||
bool pong = false;
|
||||
float elapsed = 0.0f;
|
||||
float delay = 1.0f;
|
||||
bool connected = false;
|
||||
|
||||
SocketConfig serverConfig = {
|
||||
.host = "127.0.0.1",
|
||||
.port = "4950",
|
||||
.type = SOCKET_TCP,
|
||||
.server = true,
|
||||
.nonblocking = true
|
||||
};
|
||||
|
||||
SocketConfig connectionConfig = { .nonblocking = true };
|
||||
|
||||
Socket *connection = NULL;
|
||||
SocketSet *socketSet = NULL;
|
||||
SocketResult *serverResult = NULL;
|
||||
char receiveBuffer[512] = { 0 };
|
||||
|
||||
// Create the server: getaddrinfo + socket + setsockopt + bind + listen
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(serverConfig.type == SOCKET_UDP))
|
||||
{
|
||||
if (!SocketListen(&serverConfig, serverResult)) TraceLog(LOG_WARNING, "Failed to start listen server: status %d, errno %d", serverResult->status, serverResult->socket->status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create and add sockets to the socket set
|
||||
socketSet = LoadSocketSet(2);
|
||||
AddSocket(socketSet, serverResult->socket);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (connected)
|
||||
{
|
||||
// Once connected to the network, check the sockets for pending information
|
||||
// and when information is ready, send either a Ping or a Pong.
|
||||
|
||||
// CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
|
||||
// then mark the socket as being ready. You can check this with IsSocketReady(client_res->socket)
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_DEBUG, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
// IsSocketReady, if the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if (IsSocketReady(connection)) bytesRecv = SocketReceive(connection, receiveBuffer, (int)strlen(pingmsg) + 1);
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
|
||||
if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
|
||||
}
|
||||
|
||||
// After each delay has expired, send a response "Ping!" for a "Pong!" and vice versa
|
||||
elapsed += GetFrameTime();
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
SocketSend(connection, pingmsg, (int)strlen(pingmsg) + 1);
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
SocketSend(connection, pongmsg, (int)strlen(pingmsg) + 1);
|
||||
}
|
||||
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Attempt to connect to the network (Either TCP, or UDP)
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_DEBUG, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
if (active > 0)
|
||||
{
|
||||
if ((connection = SocketAccept(serverResult->socket, &connectionConfig)) != NULL)
|
||||
{
|
||||
AddSocket(socketSet, connection);
|
||||
connected = true;
|
||||
ping = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,164 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - Network Test
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
void test_network_initialise()
|
||||
{
|
||||
assert(InitNetworkDevice() == true);
|
||||
}
|
||||
|
||||
void test_socket_result()
|
||||
{
|
||||
SocketResult *result = LoadSocketResult();
|
||||
assert(result != NULL);
|
||||
UnloadSocketResult(&result);
|
||||
assert(result == NULL);
|
||||
}
|
||||
|
||||
void test_socket()
|
||||
{
|
||||
Socket *socket = LoadSocket();
|
||||
assert(socket != NULL);
|
||||
UnloadSocket(&socket);
|
||||
assert(socket == NULL);
|
||||
}
|
||||
|
||||
void test_resolve_ip()
|
||||
{
|
||||
const char *host = "8.8.8.8";
|
||||
const char *port = "8080";
|
||||
char ip[ADDRESS_IPV6_ADDRSTRLEN];
|
||||
char service[ADDRESS_MAXSERV];
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "8.8.8.8") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_DEFAULT, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NOFQDN, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "8.8.8.8") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NAMEREQD, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_NUMERICSERV, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
|
||||
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
|
||||
ResolveIP(host, port, NAME_INFO_DGRAM, ip, service);
|
||||
TraceLog(LOG_INFO, "Resolved %s to %s", host, ip);
|
||||
assert(strcmp(ip, "google-public-dns-a.google.com") == 0);
|
||||
}
|
||||
|
||||
void test_resolve_host()
|
||||
{
|
||||
const char *address = "localhost";
|
||||
const char *port = "80";
|
||||
AddressInformation *addr = LoadAddressList(3);
|
||||
int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr);
|
||||
|
||||
assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6);
|
||||
assert(GetAddressFamily(addr[1]) == ADDRESS_TYPE_IPV4);
|
||||
assert(GetAddressSocketType(addr[0]) == 0);
|
||||
assert(GetAddressProtocol(addr[0]) == 0);
|
||||
// for (size_t i = 0; i < count; i++) { PrintAddressInfo(addr[i]); }
|
||||
}
|
||||
|
||||
void test_address()
|
||||
{
|
||||
}
|
||||
|
||||
void test_address_list()
|
||||
{
|
||||
}
|
||||
|
||||
void test_socket_create()
|
||||
{
|
||||
SocketConfig server_cfg = { .host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true };
|
||||
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));
|
||||
assert(SocketListen(&server_cfg, server_res));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - network test");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
// Run some tests
|
||||
test_resolve_host();
|
||||
//test_socket_create();
|
||||
//test_resolve_ip();
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,124 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - UDP Client
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - udp client");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
const char *pingmsg = "Ping!";
|
||||
const char *pongmsg = "Pong!";
|
||||
|
||||
bool ping = true;
|
||||
bool pong = false;
|
||||
float elapsed = 0.0f;
|
||||
float delay = 1.0f;
|
||||
|
||||
SocketConfig clientConfig = {
|
||||
.host = "127.0.0.1",
|
||||
.port = "4950",
|
||||
.type = SOCKET_UDP,
|
||||
.nonblocking = true
|
||||
};
|
||||
|
||||
SocketResult *clientResult = NULL;
|
||||
SocketSet *socketSet = NULL;
|
||||
char receiveBuffer[512] = { 0 };
|
||||
|
||||
// Create the client: getaddrinfo + socket + setsockopt + connect (TCP only)
|
||||
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 = LoadSocketSet(1);
|
||||
AddSocket(socketSet, clientResult->socket);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Once connected to the network, check the sockets for pending information
|
||||
// and when information is ready, send either a Ping or a Pong.
|
||||
|
||||
// CheckSockets, if any of the sockets in the socketSet are pending (received data, or requests)
|
||||
// then mark the socket as being ready. You can check this with IsSocketReady(clientResult->socket)
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
// IsSocketReady, if the socket is ready, attempt to receive data from the socket
|
||||
int bytesRecv = 0;
|
||||
if (IsSocketReady(clientResult->socket)) bytesRecv = SocketReceive(clientResult->socket, receiveBuffer, (int)strlen(pingmsg) + 1);
|
||||
|
||||
// If we received data, was that data a "Ping!" or a "Pong!"
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
|
||||
if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
|
||||
}
|
||||
|
||||
// After each delay has expired, send a response "Ping!" for a "Pong!" and vice-versa
|
||||
elapsed += GetFrameTime();
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
SocketSend(clientResult->socket, pingmsg, (int)strlen(pingmsg) + 1);
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
SocketSend(clientResult->socket, pongmsg, (int)strlen(pongmsg) + 1);
|
||||
}
|
||||
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,128 +0,0 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [network] example - UDP Server
|
||||
*
|
||||
* This example has been created using raylib 3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define RNET_IMPLEMENTATION
|
||||
#include "extras/rnet.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [network] example - udp server");
|
||||
|
||||
InitNetworkDevice(); // Init network communications
|
||||
|
||||
const char *pingmsg = "Ping!";
|
||||
const char *pongmsg = "Pong!";
|
||||
|
||||
bool ping = false;
|
||||
bool pong = false;
|
||||
float elapsed = 0.0f;
|
||||
float delay = 1.0f;
|
||||
|
||||
SocketConfig serverConfig = {
|
||||
.host = "127.0.0.1",
|
||||
.port = "4950",
|
||||
.server = true,
|
||||
.type = SOCKET_UDP,
|
||||
.nonblocking = true
|
||||
};
|
||||
|
||||
SocketResult *serverResult = NULL;
|
||||
SocketSet *socketSet = NULL;
|
||||
char receiveBuffer[512] = { 0 };
|
||||
|
||||
// Create the server: getaddrinfo + socket + setsockopt + bind + listen
|
||||
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 = LoadSocketSet(1);
|
||||
AddSocket(socketSet, serverResult->socket);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Once connected to the network, check the sockets for pending information
|
||||
// and when information is ready, send either a Ping or a Pong.
|
||||
|
||||
// CheckSockets, if any of the sockets in the set are pending (received data, or requests)
|
||||
// then mark the socket as being ready. You can check this with IsSocketReady(client_res->socket)
|
||||
int active = CheckSockets(socketSet, 0);
|
||||
if (active != 0) TraceLog(LOG_INFO, "There are currently %d socket(s) with data to be processed.", active);
|
||||
|
||||
// IsSocketReady, if the socket is ready, attempt to receive data from the socket
|
||||
// int bytesRecv = 0;
|
||||
// if (IsSocketReady(serverResult->socket)) {
|
||||
// bytesRecv = SocketReceive(serverResult->socket, receiveBuffer, msglen);
|
||||
// }
|
||||
int bytesRecv = SocketReceive(serverResult->socket, receiveBuffer, (int)strlen(pingmsg) + 1);
|
||||
|
||||
// If we received data, is that data a "Ping!" or a "Pong!"?
|
||||
if (bytesRecv > 0)
|
||||
{
|
||||
if (strcmp(receiveBuffer, pingmsg) == 0) { pong = true; }
|
||||
if (strcmp(receiveBuffer, pongmsg) == 0) { ping = true; }
|
||||
}
|
||||
|
||||
// After each delay has expired, send a response "Ping!" for a "Pong!" and vice-versa
|
||||
elapsed += GetFrameTime();
|
||||
|
||||
if (elapsed > delay)
|
||||
{
|
||||
if (ping)
|
||||
{
|
||||
ping = false;
|
||||
SocketSend(serverResult->socket, pingmsg, (int)strlen(pingmsg) + 1);
|
||||
}
|
||||
else if (pong)
|
||||
{
|
||||
pong = false;
|
||||
SocketSend(serverResult->socket, pongmsg, (int)strlen(pongmsg) + 1);
|
||||
}
|
||||
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// TODO: Draw relevant connection info
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseNetworkDevice(); // Close network communication
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue