[rnet] Examples review -WIP-

This commit is contained in:
Ray 2020-02-19 18:16:20 +01:00
parent faff512140
commit 19390eaf09
5 changed files with 232 additions and 181 deletions

View file

@ -1,26 +1,17 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [network] example - Client/Server ping-pong * raylib [network] example - Client/Server ping-pong
* *
* Welcome to raylib! * 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)
* 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 * Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
* *
* 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)
*
********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#define RNET_IMPLEMENTATION
#include "rnet.h" #include "rnet.h"
#include <assert.h> #include <assert.h>
@ -82,7 +73,7 @@ void NetworkConnect()
// Once connected to the network, check the sockets for pending information // Once connected to the network, check the sockets for pending information
// and when information is ready, send either a Ping or a Pong. // and when information is ready, send either a Ping or a Pong.
void NetworkUpdate() void UpdateNetwork()
{ {
// CheckSockets // CheckSockets
// //
@ -139,18 +130,20 @@ void NetworkUpdate()
} }
} }
int main() int main(void)
{ {
// Setup // Initialization
int screenWidth = 800; //--------------------------------------------------------------------------------------
int screenHeight = 450; const int screenWidth = 800;
const int screenHeight = 450;
InitWindow( InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong"); screenWidth, screenHeight, "raylib [network] example - ping pong");
SetTargetFPS(60); SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG); SetTraceLogLevel(LOG_DEBUG);
// Networking // Networking
InitNetwork(); InitNetworkDevice();
// Create the server // Create the server
// //
@ -212,14 +205,40 @@ int main()
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (connected) { if (connected) {
NetworkUpdate(); UpdateNetwork();
} else { } else {
NetworkConnect(); NetworkConnect();
} }
EndDrawing(); EndDrawing();
} }
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);
if (connected) UpdateNetwork();
else NetworkConnect();
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
// Cleanup
CloseWindow();
return 0; return 0;
} }

View file

@ -1,44 +1,36 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [network] example - Resolve Host * raylib [network] example - Resolve Host
* *
* Welcome to raylib! * 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)
* 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 * Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
* *
* 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)
*
********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#define RNET_IMPLEMENTATION
#include "rnet.h" #include "rnet.h"
char buffer[ADDRESS_IPV6_ADDRSTRLEN]; int main(void)
uint16_t port = 0;
int main()
{ {
// Setup // Initialization
int screenWidth = 800; //--------------------------------------------------------------------------------------
int screenHeight = 450; const int screenWidth = 800;
InitWindow( const int screenHeight = 450;
screenWidth, screenHeight, "raylib [network] example - ping pong");
SetTargetFPS(60);
InitWindow(screenWidth, screenHeight, "raylib [network] example - ping pong");
char buffer[ADDRESS_IPV6_ADDRSTRLEN];
uint16_t port = 0;
SetTraceLogLevel(LOG_DEBUG); SetTraceLogLevel(LOG_DEBUG);
// Networking // Networking
InitNetwork(); InitNetworkDevice();
AddressInformation* addr = AllocAddressList(1); AddressInformation* addr = AllocAddressList(1);
int count = ResolveHost( int count = ResolveHost(
@ -58,23 +50,36 @@ int main()
if (count > 0) if (count > 0)
{ {
GetAddressHostAndPort(addr[0], buffer, &port); GetAddressHostAndPort(addr[0], buffer, &port);
TraceLog(LOG_INFO, "Resolved to ip %s::%d\n", 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 // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw // Draw
//----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
// Clear ClearBackground(RAYWHITE);
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
// End draw
EndDrawing(); EndDrawing();
//----------------------------------------------------------------------------------
} }
// Cleanup // De-Initialization
CloseWindow(); //--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0; return 0;
} }

View file

@ -1,33 +1,24 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [network] example - Network Test * raylib [network] example - Network Test
* *
* Welcome to raylib! * 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)
* 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 * Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
* *
* 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)
*
********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#define RNET_IMPLEMENTATION
#include "rnet.h" #include "rnet.h"
#include <assert.h> #include <assert.h>
void test_network_initialise() void test_network_initialise()
{ {
assert(InitNetwork() == true); assert(InitNetworkDevice() == true);
} }
void test_socket_result() void test_socket_result()
@ -50,8 +41,8 @@ void test_resolve_ip()
{ {
const char *host = "8.8.8.8"; const char *host = "8.8.8.8";
const char *port = "8080"; const char *port = "8080";
char ip[ADDRESS_IPV6_ADDRSTRLEN]; char ip[ADDRESS_IPV6_ADDRSTRLEN];
char service[ADDRESS_MAXSERV]; char service[ADDRESS_MAXSERV];
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN); memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service); ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
@ -91,9 +82,9 @@ void test_resolve_ip()
void test_resolve_host() void test_resolve_host()
{ {
const char * address = "localhost"; const char *address = "localhost";
const char * port = "80"; const char *port = "80";
AddressInformation *addr = AllocAddressList(3); AddressInformation *addr = AllocAddressList(3);
int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr); int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr);
assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6); assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6);
@ -113,36 +104,58 @@ void test_address_list()
void test_socket_create() void test_socket_create()
{ {
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true}; SocketConfig server_cfg = { .host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true };
Socket * socket = AllocSocket(); Socket *socket = AllocSocket();
SocketResult *server_res = AllocSocketResult(); SocketResult *server_res = AllocSocketResult();
SocketSet * socket_set = AllocSocketSet(1); SocketSet *socket_set = AllocSocketSet(1);
assert(SocketCreate(&server_cfg, server_res)); assert(SocketCreate(&server_cfg, server_res));
assert(AddSocket(socket_set, server_res->socket)); assert(AddSocket(socket_set, server_res->socket));
assert(SocketListen(&server_cfg, server_res)); assert(SocketListen(&server_cfg, server_res));
} }
int main() int main(void)
{ {
int screenWidth = 800; // Initialization
int screenHeight = 450; //--------------------------------------------------------------------------------------
InitWindow( const int screenWidth = 800;
screenWidth, screenHeight, "raylib [network] example - network test"); const int screenHeight = 450;
SetTargetFPS(60);
InitWindow(screenWidth, screenHeight, "raylib [network] example - network test");
// Run the tests // Run the tests
test_network_initialise(); test_network_initialise();
test_resolve_host(); test_resolve_host();
// test_socket_create(); //test_socket_create();
test_resolve_ip();
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) { while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing(); EndDrawing();
//----------------------------------------------------------------------------------
} }
CloseWindow();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0; return 0;
} }

View file

@ -1,28 +1,20 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [network] example - UDP Client * raylib [network] example - UDP Client
* *
* Welcome to raylib! * 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)
* 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 * Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
* *
* 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)
*
********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#define RNET_IMPLEMENTATION
#include "rnet.h" #include "rnet.h"
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -41,7 +33,7 @@ char recvBuffer[512];
// Once connected to the network, check the sockets for pending information // Once connected to the network, check the sockets for pending information
// and when information is ready, send either a Ping or a Pong. // and when information is ready, send either a Ping or a Pong.
void NetworkUpdate() void UpdateNetwork()
{ {
// CheckSockets // CheckSockets
// //
@ -81,18 +73,16 @@ void NetworkUpdate()
} }
} }
int main() int main(void)
{ {
// Setup // Initialization
int screenWidth = 800; //--------------------------------------------------------------------------------------
int screenHeight = 450; const int screenWidth = 800;
InitWindow( const int screenHeight = 450;
screenWidth, screenHeight, "raylib [network] example - udp client");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking InitWindow(screenWidth, screenHeight, "raylib [network] example - udp client");
InitNetwork();
InitNetworkDevice();
// Create the client // Create the client
// //
@ -114,15 +104,33 @@ int main()
memset(recvBuffer, '\0', sizeof(recvBuffer)); memset(recvBuffer, '\0', sizeof(recvBuffer));
AddSocket(socket_set, client_res->socket); AddSocket(socket_set, client_res->socket);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) { while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateNetwork();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE);
NetworkUpdate(); ClearBackground(RAYWHITE);
EndDrawing(); EndDrawing();
//----------------------------------------------------------------------------------
} }
// Cleanup // De-Initialization
CloseWindow(); //--------------------------------------------------------------------------------------
CloseNetworkDevice(); // Close network
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0; return 0;
} }

View file

@ -1,28 +1,20 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [network] example - UDP Server * raylib [network] example - UDP Server
* *
* Welcome to raylib! * 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)
* 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 * Copyright (c) 2019-2020 Jak Barnes (@syphonx) and Ramon Santamaria (@raysan5)
* *
* 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)
*
********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#define RNET_IMPLEMENTATION
#include "rnet.h" #include "rnet.h"
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -41,7 +33,7 @@ char recvBuffer[512];
// Once connected to the network, check the sockets for pending information // Once connected to the network, check the sockets for pending information
// and when information is ready, send either a Ping or a Pong. // and when information is ready, send either a Ping or a Pong.
void NetworkUpdate() void UpdateNetwork()
{ {
// CheckSockets // CheckSockets
// //
@ -82,18 +74,16 @@ void NetworkUpdate()
} }
} }
int main() int main(void)
{ {
// Setup // Initialization
int screenWidth = 800; //--------------------------------------------------------------------------------------
int screenHeight = 450; const int screenWidth = 800;
InitWindow( const int screenHeight = 450;
screenWidth, screenHeight, "raylib [network] example - udp server");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking InitWindow(screenWidth, screenHeight, "raylib [network] example - udp server");
InitNetwork();
InitNetworkDevice();
// Create the server // Create the server
// //
@ -104,31 +94,47 @@ int main()
// bind // bind
// listen // listen
server_res = AllocSocketResult(); server_res = AllocSocketResult();
if (!SocketCreate(&server_cfg, server_res)) { if (!SocketCreate(&server_cfg, server_res))
TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", {
server_res->status, server_res->socket->status); TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d", server_res->status, server_res->socket->status);
} else { } else
if (!SocketBind(&server_cfg, server_res)) { {
TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d", if (!SocketBind(&server_cfg, server_res)) TraceLog(LOG_WARNING, "Failed to bind server: status %d, errno %d", server_res->status, server_res->socket->status);
server_res->status, server_res->socket->status);
}
} }
// Create & Add sockets to the socket set // Create & Add sockets to the socket set
socket_set = AllocSocketSet(1); socket_set = AllocSocketSet(1);
msglen = strlen(pingmsg) + 1; msglen = strlen(pingmsg) + 1;
memset(recvBuffer, '\0', sizeof(recvBuffer)); memset(recvBuffer, '\0', sizeof(recvBuffer));
AddSocket(socket_set, server_res->socket); AddSocket(socket_set, server_res->socket);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) { while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateNetwork();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE);
NetworkUpdate(); ClearBackground(RAYWHITE);
EndDrawing(); EndDrawing();
//----------------------------------------------------------------------------------
} }
// Cleanup // De-Initialization
CloseWindow(); //--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0; return 0;
} }