i broke a bunch of stuff, but lets commit it anyway

This commit is contained in:
Jak Barnes 2019-03-03 00:34:11 +00:00
parent 44c2df3c12
commit 082a0c85ce
7 changed files with 770 additions and 578 deletions

View File

@ -21,10 +21,96 @@
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include "rnet.c"
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <string.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);
}
void NetworkConnect()
{
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;
}
}
}
void NetworkUpdate()
{
int active = CheckSockets(socket_set, 0);
if (active != 0) {
TraceLog(LOG_DEBUG,
"There are currently %d socket(s) with data to be processed.", active);
}
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) { pong = true; }
if (strcmp(recvBuffer, pongmsg) == 0) { ping = true; }
}
elapsed += GetFrameTime();
if (elapsed > delay) {
if (ping) {
ping = false;
SocketSend(client_res->socket, pingmsg, msglen);
} else if (pong) {
pong = false;
SocketSend(client_res->socket, pongmsg, msglen);
}
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() int main()
{ {
// Setup // Setup
@ -33,134 +119,40 @@ int main()
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(); InitNetwork();
// Create the server // Create the server
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .datagram = true, .nonblocking = true}; server_res = AllocSocketResult();
SocketResult* server_res = AllocSocketResult(); if (!SocketOpen(&server_cfg, server_res)) {
if (!SocketOpen(&server_cfg, server_res))
{
TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d\n", TraceLog(LOG_WARNING, "Failed to open server: status %d, errno %d\n",
server_res->status, server_res->socket->status); server_res->status, server_res->socket->status);
} }
// Create the client // Create the client
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .datagram = true, .nonblocking = true}; client_res = AllocSocketResult();
SocketResult* client_res = AllocSocketResult(); if (!SocketOpen(&client_cfg, client_res)) {
{
if (!SocketOpen(&client_cfg, client_res))
{
TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d\n", TraceLog(LOG_WARNING, "Failed to open client: status %d, errno %d\n",
client_res->status, client_res->socket->status); client_res->status, client_res->socket->status);
} }
}
char recvBuffer[512]; socket_set = CreateSocketSet(3);
msglen = strlen(pingmsg) + 1;
memset(recvBuffer, '\0', sizeof(recvBuffer)); 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;
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, server_res->socket);
AddSocket(socket_set, client_res->socket); AddSocket(socket_set, client_res->socket);
// Main game loop // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose()) {
{
// Draw
BeginDrawing(); BeginDrawing();
// Clear
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (connected) {
int active = CheckSockets(socket_set, 0); NetworkUpdate();
if (active != 0) } else {
{ NetworkConnect();
TraceLog(LOG_DEBUG,
"There are currently %d socket(s) with data to be processed.", active);
} }
// A valid connection will != -1
if (!connected)
{
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 = 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)
{
pong = true;
printf("%s\n", pingmsg);
}
if (strcmp(recvBuffer, pongmsg) == 0)
{
ping = true;
printf("%s\n", pongmsg);
}
}
elapsed += GetFrameTime();
if (elapsed > delay)
{
if (ping)
{
ping = false;
SocketSend(client_res->socket, pingmsg, msglen);
}
else if (pong)
{
pong = false;
SocketSend(client_res->socket, pongmsg, msglen);
}
elapsed = 0.0f;
}
}
// End draw
EndDrawing(); EndDrawing();
} }

View File

@ -21,6 +21,44 @@
#include "raylib.h" #include "raylib.h"
#define MAX_BUFFER_SIZE 512
char buffer[MAX_BUFFER_SIZE];
float elapsed = 0.0f;
float delay = 1.0f;
bool connected = false;
SocketConfig client_cfg = {.host = "127.0.0.1", .port = "8080", .nonblocking = true};
SocketResult* client_res = NULL;
SocketSet* socket_set = NULL;
void NetworkConnect()
{
if (!SocketConnect(&client_cfg, client_res)) {
TraceLog(LOG_WARNING,
"Failed to connect socket to server: status %d, errno %d",
client_res->status, client_res->socket->status);
} else {
connected = true;
}
}
void NetworkUpdate()
{
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) {
int bytesRecv = 0;
if (IsSocketReady(client_res->socket)) {
bytesRecv = SocketReceive(client_res->socket, buffer, MAX_BUFFER_SIZE, 0);
}
if (bytesRecv > 0) { TraceLog(LOG_INFO, "%s", buffer); }
}
}
int main() int main()
{ {
int screenWidth = 800; int screenWidth = 800;
@ -29,12 +67,35 @@ int main()
screenWidth, screenHeight, "raylib [core] example - basic window"); screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60); SetTargetFPS(60);
// Set log type
SetTraceLogLevel(LOG_DEBUG);
// Init the network layer
InitNetwork();
// Create the server
client_res = AllocSocketResult();
socket_set = AllocSocketSet(1);
if (!SocketCreate(&client_cfg, client_res)) {
TraceLog(LOG_WARNING, "Failed to create socket: status %d, errno %d",
client_res->status, client_res->socket->status);
} else {
AddSocket(socket_set, client_res->socket);
}
// Main game loop // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose()) {
{
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); if (connected) {
NetworkUpdate();
} else {
elapsed += GetFrameTime();
if (elapsed > delay) {
NetworkConnect();
elapsed = 0.0f;
}
}
EndDrawing(); EndDrawing();
} }
CloseWindow(); CloseWindow();

View File

@ -21,6 +21,44 @@
#include "raylib.h" #include "raylib.h"
bool connected = false;
bool sent = false;
float elapsed = 0.0f;
float delay = 1.0f;
SocketConfig server_cfg = {.host = "127.0.0.1", .port = "8080", .server = true, .nonblocking = true};
SocketConfig connection_cfg = {.nonblocking = true};
SocketResult* server_res = NULL;
Socket* connection = NULL;
SocketSet* socket_set = NULL;
void NetworkSend()
{
if (!sent) {
sent = true;
SocketSend(connection, "Hello, world!", 13);
}
}
void NetworkListen()
{
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) {
connection = SocketAccept(server_res->socket, &connection_cfg);
if (connection != NULL) {
AddSocket(socket_set, connection);
connected = true;
} else {
TraceLog(LOG_WARNING, "Failed to accept socket: status %d, errno %d",
server_res->status, server_res->socket->status);
}
}
}
int main() int main()
{ {
int screenWidth = 800; int screenWidth = 800;
@ -29,12 +67,40 @@ int main()
screenWidth, screenHeight, "raylib [core] example - basic window"); screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60); SetTargetFPS(60);
// Set log type
SetTraceLogLevel(LOG_DEBUG);
// Init the network layer
InitNetwork();
// Create the server
server_res = AllocSocketResult();
socket_set = AllocSocketSet(2);
if (!SocketCreate(&server_cfg, server_res)) {
TraceLog(LOG_WARNING, "Failed to create socket: status %d, errno %d",
server_res->status, server_res->socket->status);
} else {
AddSocket(socket_set, server_res->socket);
if (!SocketListen(&server_cfg, server_res)) {
TraceLog(LOG_WARNING,
"Failed to listen on socket: status %d, errno %d",
server_res->status, server_res->socket->status);
}
}
// Main game loop // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose()) {
{
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY); if (connected) {
NetworkSend();
} else {
elapsed += GetFrameTime();
if (elapsed > delay) {
NetworkListen();
elapsed = 0.0f;
}
}
EndDrawing(); EndDrawing();
} }
CloseWindow(); CloseWindow();

View File

@ -73,7 +73,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<OutDir>$(ProjectDir)$(ProjectName)\$(Configuration)\</OutDir> <OutDir>$(ProjectDir)$(ProjectName)\$(Configuration)\</OutDir>
<IntDir>$(ProjectDir)$(ProjectName)\$(Configuration)\temp</IntDir> <IntDir>$(ProjectDir)$(ProjectName)\$(Configuration)\temp\</IntDir>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug.DLL|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug.DLL|Win32'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>

View File

@ -72,6 +72,7 @@
#define RAYLIB_H #define RAYLIB_H
#include <stdarg.h> // Required for: va_list - Only used by TraceLogCallback #include <stdarg.h> // Required for: va_list - Only used by TraceLogCallback
#include <inttypes.h> // Required for rnet
#if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED) #if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED)
# define RLAPI \ # define RLAPI \
@ -203,7 +204,7 @@ typedef enum
#endif #endif
// Network typedefs // Network typedefs
typedef unsigned int SocketChannel; typedef int32_t SocketChannel;
// Vector2 type // Vector2 type
typedef struct Vector2 typedef struct Vector2
@ -576,6 +577,15 @@ typedef struct SocketResult
Socket *socket; Socket *socket;
} SocketResult; } SocketResult;
//
typedef struct Packet
{
uint32_t size; // The total size of bytes in data
uint32_t offs; // The offset to data access
uint32_t maxs; // The max size of data
uint8_t* data; // Data stored in network byte order
} Packet;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Enumerators Definition // Enumerators Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -1573,7 +1583,9 @@ extern "C"
RLAPI char *ResolveHost(const char *address, const char *port); RLAPI char *ResolveHost(const char *address, const char *port);
// Socket API // Socket API
RLAPI bool SocketOpen(SocketConfig *cfg, SocketResult *res); RLAPI bool SocketCreate(SocketConfig *cfg, SocketResult *res);
RLAPI bool SocketListen(SocketConfig *cfg, SocketResult *res);
RLAPI bool SocketConnect(SocketConfig *cfg, SocketResult *res);
RLAPI void SocketClose(SocketChannel socket); RLAPI void SocketClose(SocketChannel socket);
RLAPI Socket *SocketAccept(Socket *server, SocketConfig *cfg); RLAPI Socket *SocketAccept(Socket *server, SocketConfig *cfg);
RLAPI int SocketSend(Socket *socket, const void *datap, int len); RLAPI int SocketSend(Socket *socket, const void *datap, int len);
@ -1581,15 +1593,33 @@ extern "C"
// Socket set methods for async i/o // Socket set methods for async i/o
RLAPI bool IsSocketReady(Socket* sock); RLAPI bool IsSocketReady(Socket* sock);
RLAPI SocketSet* CreateSocketSet(int max); RLAPI SocketSet* AllocSocketSet(int max);
RLAPI void CleanupSocketSet(SocketSet* sockset); RLAPI void FreeSocketSet(SocketSet* sockset);
RLAPI int AddSocket(SocketSet* set, Socket* sock); RLAPI int AddSocket(SocketSet* set, Socket* sock);
RLAPI int RemoveSocket(SocketSet* set, Socket* sock); RLAPI int RemoveSocket(SocketSet* set, Socket* sock);
RLAPI int CheckSockets(SocketSet* set, unsigned int timeout); RLAPI int CheckSockets(SocketSet* set, unsigned int timeout);
// Creation and allocation // Creation and allocation
RLAPI SocketResult *AllocSocketResult(); RLAPI SocketResult *AllocSocketResult();
RLAPI void FreeSocketResult(SocketResult *result);
RLAPI Socket *AllocSocket(); RLAPI Socket *AllocSocket();
RLAPI void FreeSocket(Socket *sock);
// Data packets and helpers
Packet* AllocPacket(int size);
void FreePacket(Packet* packet);
void PacketSend(Packet* packet);
void PacketReceive(Packet* packet);
void PacketWrite8(Packet* packet, uint16_t value);
void PacketWrite16(Packet* packet, uint16_t value);
void PacketWrite32(Packet* packet, uint32_t value);
void PacketWrite64(Packet* packet, uint64_t value);
uint16_t PacketRead8(Packet* packet);
uint16_t PacketRead16(Packet* packet);
uint32_t PacketRead32(Packet* packet);
uint64_t PacketRead64(Packet* packet);
#if defined(__cplusplus) #if defined(__cplusplus)
} }

File diff suppressed because it is too large Load Diff

View File

@ -219,6 +219,7 @@ typedef long int int64;
#ifdef _WIN32 #ifdef _WIN32
# pragma comment(lib, "ws2_32.lib") # pragma comment(lib, "ws2_32.lib")
# define __USE_W32_SOCKETS # define __USE_W32_SOCKETS
# define WIN32_LEAN_AND_MEAN
# include <winsock2.h> # include <winsock2.h>
# include <Ws2tcpip.h> # include <Ws2tcpip.h>
# include <io.h> # include <io.h>