updated rnet api, fixed ping-pong example
This commit is contained in:
parent
22249b72cf
commit
38b77c7cc0
1 changed files with 347 additions and 272 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue