updated rnet api, fixed ping-pong example

This commit is contained in:
Jak Barnes 2019-03-16 00:48:42 +00:00
commit 38b77c7cc0
4 changed files with 347 additions and 272 deletions

View file

@ -27,50 +27,61 @@
#include <stdio.h>
#include <string.h>
char recvBuffer[512];
float elapsed = 0.0f;
float delay = 1.0f;
bool ping = false;
bool pong = false;
bool connected = false;
const char* pingmsg = "Ping!";
const char* pongmsg = "Pong!";
int msglen = 0;
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true};
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .nonblocking = true};
SocketConfig connection_cfg = {.nonblocking = true};
SocketResult* server_res = NULL;
SocketResult* client_res = NULL;
SocketSet* socket_set = NULL;
Socket* connection = NULL;
/*
** packshort() -- store a 16-bit int into a char buffer (like htons())
*/
void packshort(unsigned short s)
{
unsigned char* sptr = ((unsigned char*) &(s));
printf("before: %02X %02X\n", sptr[0], sptr[1]);
unsigned short ns = htons(s);
memcpy(packbuf + packlen, (unsigned char*) &(ns), sizeof(short));
printf("after: %02X %02X\n", (packbuf + packlen)[0], (packbuf + packlen)[1]);
packlen += sizeof(s);
}
float elapsed = 0.0f;
float delay = 1.0f;
bool ping = false;
bool pong = false;
bool connected = false;
bool client_connected = false;
const char * pingmsg = "Ping!";
const char * pongmsg = "Pong!";
int msglen = 0;
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .datagram = true, .server = true, .nonblocking = true};
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .datagram = true, .nonblocking = true};
SocketConfig connection_cfg = {.nonblocking = true};
SocketResult *server_res = NULL;
SocketResult *client_res = NULL;
SocketSet * socket_set = NULL;
Socket * connection = NULL;
char recvBuffer[512];
// Attempt to connect to the network (Either TCP, or UDP)
void NetworkConnect()
{
if (server_cfg.datagram) {
// If the server is configured as UDP, ignore connection requests
if (server_cfg.datagram == true && client_cfg.datagram == true) {
ping = true;
connected = true;
} else {
if ((connection = SocketAccept(server_res->socket, &connection_cfg)) != NULL) {
AddSocket(socket_set, connection);
ping = true;
connected = true;
// If the client is connected, run the server code to check for a connection
if (client_connected) {
int active = CheckSockets(socket_set, 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(server_res->socket, &connection_cfg)) != NULL) {
AddSocket(socket_set, connection);
ping = true;
connected = true;
}
}
} else {
// Check if we're connected every _delay_ seconds
elapsed += GetFrameTime();
if (elapsed > delay) {
if (IsSocketConnected(client_res->socket)) {
client_connected = 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.
void NetworkUpdate()
{
int active = CheckSockets(socket_set, 0);
@ -81,7 +92,7 @@ void NetworkUpdate()
int bytesRecv = 0;
if (server_cfg.datagram) {
if (IsSocketReady(server_res->socket)) {
if (IsSocketReady(client_res->socket)) {
bytesRecv = SocketReceive(server_res->socket, recvBuffer, msglen, 0);
}
} else {
@ -106,10 +117,6 @@ void NetworkUpdate()
elapsed = 0.0f;
}
}
#define VALUE_TO_STRING(x) #x
#define VALUE(x) VALUE_TO_STRING(x)
#define VAR_NAME_VALUE(var) #var "=" VALUE(var)
int main()
{
@ -122,23 +129,54 @@ int main()
SetTraceLogLevel(LOG_DEBUG);
// Networking
InitNetwork();
InitNetwork();
// Create the server
// Create the server
//
// Performs
// getaddrinfo
// socket
// setsockopt
// bind
// listen
server_res = AllocSocketResult();
if (!SocketOpen(&server_cfg, server_res)) {
TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d\n",
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);
} else {
if (!SocketListen(&server_cfg, server_res)) {
TraceLog(LOG_WARNING,
"Failed to start listen server: status %d, errno %d",
server_res->status, server_res->socket->status);
}
}
}
// Create the client
//
// Performs
// getaddrinfo
// socket
// setsockopt
// connect
client_res = AllocSocketResult();
if (!SocketOpen(&client_cfg, client_res)) {
TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d\n",
if (!SocketCreate(&client_cfg, client_res)) {
TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d",
client_res->status, client_res->socket->status);
} else {
if (!SocketConnect(&client_cfg, client_res)) {
TraceLog(LOG_WARNING,
"Failed to connect to server: status %d, errno %d",
client_res->status, client_res->socket->status);
}
}
socket_set = CreateSocketSet(3);
// Create & Add sockets to the socket set
socket_set = AllocSocketSet(3);
msglen = strlen(pingmsg) + 1;
memset(recvBuffer, '\0', sizeof(recvBuffer));
AddSocket(socket_set, server_res->socket);

View file

@ -67,24 +67,35 @@ void test_resolve_ip()
void test_resolve_host()
{
const char * address = "localhost";
const char * port = "80";
struct _AddressInformation **addr = AllocAddressList(3);
int count = ResolveHost(address, port, addr);
const char * address = "localhost";
const char * port = "80";
AddressInformation *addr = AllocAddressList(3);
int count = ResolveHost(address, port, 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 = AllocSocket();
SocketResult *server_res = AllocSocketResult();
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()
{
@ -96,10 +107,8 @@ int main()
// Run the tests
test_network_initialise();
// test_socket_result();
// test_socket();
// test_resolve_ip();
test_resolve_host();
// test_resolve_host();
test_socket_create();
// Main game loop
while (!WindowShouldClose()) {

View file

@ -1,24 +1,3 @@
/*******************************************************************************************
*
* 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()

View file

@ -1,24 +1,3 @@
/*******************************************************************************************
*
* 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()
@ -29,9 +8,13 @@ int main()
screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60);
// Initialise networking
InitNetwork();
//
// Main game loop
while (!WindowShouldClose())
{
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);