ping-pong example now supports udp

added socket sets (query for non-blocking sockets)
removed rpack.h
removed old-packing helpers

Signed-off-by: Jak Barnes <contact@jakbarnes.co.uk>
This commit is contained in:
Jak Barnes 2019-02-24 21:54:02 +00:00
parent ee85f59643
commit 1f11913d8c
13 changed files with 2165 additions and 2339 deletions

View File

@ -1,53 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Chat client
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
// Networking
InitNetwork();
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
CloseWindow();
return 0;
}

View File

@ -1,56 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Chat server
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#define MYPORT "4950"
#define MAXBUFLEN 100
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
// Networking
InitNetwork();
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
CloseWindow();
return 0;
}

View File

@ -22,6 +22,9 @@
#include "raylib.h"
#include <stdio.h>
#include <string.h>
int main()
{
// Setup
@ -31,56 +34,46 @@ int main()
screenWidth, screenHeight, "raylib [network] example - ping pong");
SetTargetFPS(60);
SetTraceLogLevel(LOG_INFO);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
// Create the server
SocketConfig server_cfg = {
.host = "127.0.0.1",
.port = "8080",
.server = true,
.nonblocking = true
};
SocketResult server_res;
memset(&server_res, 0, sizeof(SocketResult));
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .datagram = true, .nonblocking = true};
SocketResult* server_res = AllocSocketResult();
if (!SocketOpen(&server_cfg, server_res))
{
bool ok = SocketOpen(&server_cfg, &server_res);
if (!ok)
{
return false;
}
TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d\n",
server_res->status, server_res->socket->status);
}
// Create the client
SocketConfig client_cfg = {
.host = "127.0.0.1",
.port = "8080"
};
SocketResult client_res;
memset(&client_res, 0, sizeof(SocketResult));
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .datagram = true, .nonblocking = true};
SocketResult* client_res = AllocSocketResult();
{
bool ok = SocketOpen(&client_cfg, &client_res);
if (!ok)
if (!SocketOpen(&client_cfg, client_res))
{
printf("failed to open: status %d, errno %d\n", client_res.status,
client_res.socket.error);
return false;
TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d\n",
client_res->status, client_res->socket->status);
}
}
SocketResult connection;
memset(&connection, 0, sizeof(SocketResult));
float elapsed = 0.0f, delay = 1.0f; // ms
bool ping = false, pong = false;
char recvBuffer[512];
memset(recvBuffer, '\0', sizeof(recvBuffer));
Socket* connection = NULL;
SocketConfig connection_cfg = { .nonblocking = true,.datagram = true };
float elapsed = 0.0f;
float delay = 1.0f;
bool ping = false;
bool pong = false;
bool connected = false;
memset(&recvBuffer, 0, 8);
char pingmsg[6] = "Ping!";
char pongmsg[6] = "Pong!";
const char* pingmsg = "Ping!";
const char* pongmsg = "Pong!";
const int msglen = strlen(pingmsg) + 1;
SocketSet* socket_set = CreateSocketSet(3);
AddSocket(socket_set, server_res->socket);
AddSocket(socket_set, client_res->socket);
// Main game loop
while (!WindowShouldClose())
@ -91,20 +84,51 @@ int main()
// Clear
ClearBackground(RAYWHITE);
int active = CheckSockets(socket_set, 0);
if (active != 0)
{
TraceLog(LOG_DEBUG,
"There are currently %d socket(s) with data to be processed.", active);
}
// A valid connection will != -1
if (!connected)
{
if (SocketAccept(server_res.socket.handle, &connection))
if (server_cfg.datagram)
{
ping = true;
connected = true;
}
else
{
if ((connection = SocketAccept(server_res->socket, &connection_cfg)) != NULL)
{
AddSocket(socket_set, connection);
ping = true;
connected = true;
}
}
}
// Connected
if (connected)
{
int bytesRecv = SocketReceive(&connection.socket, recvBuffer, sizeof(pingmsg));
int bytesRecv = 0;
if (server_cfg.datagram)
{
if (IsSocketReady(server_res->socket))
{
bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen, 0);
}
}
else
{
if (IsSocketReady(connection))
{
bytesRecv = SocketReceive(connection, recvBuffer, msglen, 0);
}
}
if (bytesRecv > 0)
{
if (strcmp(recvBuffer, pingmsg) == 0)
@ -125,12 +149,12 @@ int main()
if (ping)
{
ping = false;
SocketSend(&client_res.socket, pingmsg, sizeof(pingmsg));
SocketSend(client_res->socket, pingmsg, msglen);
}
else if (pong)
{
pong = false;
SocketSend(&client_res.socket, pongmsg, sizeof(pongmsg));
SocketSend(client_res->socket, pongmsg, msglen);
}
elapsed = 0.0f;
}

View File

@ -36,8 +36,7 @@ int main()
// Networking
InitNetwork();
AddressInformation addr;
ResolveHost("www.google.com", "80", &addr);
ResolveHost("www.google.com", "80");
ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT);
ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV);

View File

@ -1,74 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Resolve host
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <string.h>
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
unsigned char buf[1024];
unsigned char magic;
int monkeycount;
long altitude;
double absurdityfactor;
char* s = "Great unmitigated Zot! You've found the Runestaff!";
char s2[96];
unsigned int packetsize, ps2;
packetsize = PackData(buf, "CHhlsd", 'B', 0, 37, -5, s, -3490.5);
packi16(buf + 1, packetsize); // store packet size in packet for kicks
printf("packet is %u bytes\n", packetsize);
UnpackData(buf, "CHhl96sd", &magic, &ps2, &monkeycount, &altitude, s2, &absurdityfactor);
printf("'%c' %hhu %u %ld \"%s\" %f\n", magic, ps2, monkeycount, altitude, s2, absurdityfactor);
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
CloseWindow();
return 0;
}

View File

@ -1,93 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Resolve host
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <string.h>
#define PORT "3490" // the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
int numbytes;
char buf[MAXDATASIZE];
// Create the client
SocketConfig client_cfg = {
.host = "127.0.0.1",
.port = "8080"
};
SocketResult client_res;
memset(&client_res, 0, sizeof(SocketResult));
{
bool ok = SocketOpen(&client_cfg, &client_res);
if (!ok)
{
printf("Failed to open: status %d, errno %d\n", client_res.status,
client_res.socket.error);
return false;
}
}
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// Receive bytes from the server
numbytes = SocketReceive(client_res.socket.handle, buf, MAXDATASIZE - 1);
if (numbytes == -1)
{
printf("Client: error recv '%s'\n", buf);
break;
}
buf[numbytes] = '\0';
printf("Client: received '%s'\n", buf);
break;
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
SocketClose(client_res.socket.handle);
CloseNetwork();
CloseWindow();
return 0;
}

View File

@ -1,100 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Resolve host
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <string.h>
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
// Create the server
SocketConfig server_cfg = {
.host = "127.0.0.1",
.port = "8080",
.server = true,
.nonblocking = true
};
SocketResult server_res;
memset(&server_res, 0, sizeof(SocketResult));
{
bool ok = SocketOpen(&server_cfg, &server_res);
if (!ok)
{
return false;
}
}
SocketResult connection;
memset(&connection, 0, sizeof(SocketResult));
char recvBuffer[512];
memset(&recvBuffer, 0, 8);
bool connected = false;
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// Wait for a valid connection
if (!connected)
{
if (SocketAccept(server_res.socket.handle, &connection))
{
connected = true;
}
}
if (connected)
{
SocketSend(&connection, "Hello, world!", 13);
}
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
CloseWindow();
return 0;
}

View File

@ -1,74 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Resolve host
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <string.h>
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
unsigned char buf[1024];
unsigned char magic;
int monkeycount;
long altitude;
double absurdityfactor;
char* s = "Great unmitigated Zot! You've found the Runestaff!";
char s2[96];
unsigned int packetsize, ps2;
packetsize = PackData(buf, "CHhlsd", 'B', 0, 37, -5, s, -3490.5);
packi16(buf + 1, packetsize); // store packet size in packet for kicks
printf("packet is %u bytes\n", packetsize);
UnpackData(buf, "CHhl96sd", &magic, &ps2, &monkeycount, &altitude, s2, &absurdityfactor);
printf("'%c' %hhu %u %ld \"%s\" %f\n", magic, ps2, monkeycount, altitude, s2, absurdityfactor);
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
CloseWindow();
return 0;
}

View File

@ -1,74 +1,43 @@
/*******************************************************************************************
*
* raylib [network] example - Resolve host
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h
*for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
*
* raylib [core] example - Basic window
*
* Welcome to raylib!
*
* To test examples, just press F6 and execute raylib_compile_execute script
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include <string.h>
int main()
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
unsigned char buf[1024];
unsigned char magic;
int monkeycount;
long altitude;
double absurdityfactor;
char* s = "Great unmitigated Zot! You've found the Runestaff!";
char s2[96];
unsigned int packetsize, ps2;
packetsize = PackData(buf, "CHhlsd", 'B', 0, 37, -5, s, -3490.5);
packi16(buf + 1, packetsize); // store packet size in packet for kicks
printf("packet is %u bytes\n", packetsize);
UnpackData(buf, "CHhl96sd", &magic, &ps2, &monkeycount, &altitude, s2, &absurdityfactor);
printf("'%c' %hhu %u %ld \"%s\" %f\n", magic, ps2, monkeycount, altitude, s2, absurdityfactor);
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
// Clear
ClearBackground(RAYWHITE);
// End draw
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
// Cleanup
CloseWindow();
return 0;
}

View File

@ -185,7 +185,6 @@
<ClInclude Include="..\..\..\src\raylib.h" />
<ClInclude Include="..\..\..\src\raymath.h" />
<ClInclude Include="..\..\..\src\rlgl.h" />
<ClInclude Include="..\..\..\src\rpack.h" />
<ClInclude Include="..\..\..\src\sysnet.h" />
<ClInclude Include="..\..\..\src\utils.h" />
</ItemGroup>

File diff suppressed because it is too large Load Diff

1214
src/rnet.c

File diff suppressed because it is too large Load Diff

View File

@ -186,6 +186,19 @@ typedef long int int64;
# define RESULT_FAILURE 1
#endif // RESULT_FAILURE
#ifndef INADDR_ANY
# define INADDR_ANY 0x00000000
#endif // INADDR_ANY
#ifndef INADDR_NONE
# define INADDR_NONE 0xFFFFFFFF
#endif // INADDR_NONE
#ifndef INADDR_LOOPBACK
# define INADDR_LOOPBACK 0x7f000001
#endif // INADDR_LOOPBACK
#ifndef INADDR_BROADCAST
# define INADDR_BROADCAST 0xFFFFFFFF
#endif // INADDR_BROADCAST
#ifndef htonll
# ifdef _BIG_ENDIAN
# define htonll(x) (x)
@ -206,9 +219,9 @@ typedef long int int64;
#ifdef _WIN32
# pragma comment(lib, "ws2_32.lib")
# define __USE_W32_SOCKETS
# include <winsock2.h>
# include <Ws2tcpip.h>
# include <io.h>
# include <winsock2.h>
# define IPTOS_LOWDELAY 0x10
#else /* UNIX */
# include <sys/types.h>