Explorar el Código

[rnet] Examples review -WIP-

pull/1113/head
Ray hace 4 años
padre
commit
19390eaf09
Se han modificado 5 ficheros con 232 adiciones y 181 borrados
  1. +46
    -27
      examples/network/network_ping_pong.c
  2. +42
    -37
      examples/network/network_resolve_host.c
  3. +53
    -40
      examples/network/network_test.c
  4. +42
    -34
      examples/network/network_udp_client.c
  5. +49
    -43
      examples/network/network_udp_server.c

+ 46
- 27
examples/network/network_ping_pong.c Ver fichero

@ -1,26 +1,17 @@
/*******************************************************************************************
*
* raylib [network] example - Client/Server ping-pong
*
* 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)
*
********************************************************************************************/
*
* 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 "rnet.h"
#include <assert.h>
@ -82,7 +73,7 @@ void NetworkConnect()
// Once connected to the network, check the sockets for pending information
// and when information is ready, send either a Ping or a Pong.
void NetworkUpdate()
void UpdateNetwork()
{
// CheckSockets
//
@ -139,18 +130,20 @@ void NetworkUpdate()
}
}
int main()
int main(kt">void)
{
// Setup
int screenWidth = 800;
int screenHeight = 450;
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - ping pong");
SetTargetFPS(60);
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
InitNetworkDevice();
// Create the server
//
@ -212,14 +205,40 @@ int main()
BeginDrawing();
ClearBackground(RAYWHITE);
if (connected) {
NetworkUpdate();
UpdateNetwork();
} else {
NetworkConnect();
}
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;
}

+ 42
- 37
examples/network/network_resolve_host.c Ver fichero

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

+ 53
- 40
examples/network/network_test.c Ver fichero

@ -1,33 +1,24 @@
/*******************************************************************************************
*
* raylib [network] example - Network Test
*
* 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 [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 "rnet.h"
#include <assert.h>
void test_network_initialise()
{
assert(InitNetwork() == true);
assert(InitNetworkDevice() == true);
}
void test_socket_result()
@ -50,8 +41,8 @@ void test_resolve_ip()
{
const char *host = "8.8.8.8";
const char *port = "8080";
char ip[ADDRESS_IPV6_ADDRSTRLEN];
char service[ADDRESS_MAXSERV];
char ip[ADDRESS_IPV6_ADDRSTRLEN];
char service[ADDRESS_MAXSERV];
memset(ip, '\0', ADDRESS_IPV6_ADDRSTRLEN);
ResolveIP(host, port, NAME_INFO_NUMERICHOST, ip, service);
@ -91,9 +82,9 @@ void test_resolve_ip()
void test_resolve_host()
{
const char * address = "localhost";
const char * port = "80";
AddressInformation *addr = AllocAddressList(3);
const char *address = "localhost";
const char *port = "80";
AddressInformation *addr = AllocAddressList(3);
int count = ResolveHost(address, port, ADDRESS_TYPE_ANY, 0, addr);
assert(GetAddressFamily(addr[0]) == ADDRESS_TYPE_IPV6);
@ -113,36 +104,58 @@ void test_address_list()
void test_socket_create()
{
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true};
Socket * socket = AllocSocket();
SocketConfig server_cfg = { .host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true };
Socket *socket = AllocSocket();
SocketResult *server_res = AllocSocketResult();
SocketSet * socket_set = AllocSocketSet(1);
SocketSet *socket_set = AllocSocketSet(1);
assert(SocketCreate(&server_cfg, server_res));
assert(AddSocket(socket_set, server_res->socket));
assert(SocketListen(&server_cfg, server_res));
}
int main()
int main(kt">void)
{
int screenWidth = 800;
int screenHeight = 450;
InitWindow(
screenWidth, screenHeight, "raylib [network] example - network test");
SetTargetFPS(60);
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [network] example - network test");
// Run the tests
test_network_initialise();
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
while (!WindowShouldClose()) {
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
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();
//----------------------------------------------------------------------------------
}
CloseWindow();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

+ 42
- 34
examples/network/network_udp_client.c Ver fichero

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

+ 49
- 43
examples/network/network_udp_server.c Ver fichero

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

Cargando…
Cancelar
Guardar