Added stub functions, fleshed out network includes

This commit is contained in:
Jak Barnes 2019-02-14 00:29:37 +00:00
commit d120683753
4 changed files with 558 additions and 71 deletions

View file

@ -418,6 +418,15 @@ typedef struct VrStereoConfig {
int eyeViewportLeft[4]; // VR stereo rendering left eye viewport [x, y, w, h]
} VrStereoConfig;
// Address struct in the form a.b.c.d where [a, b, c, d] are >= 0 && <= 255
typedef struct Address {
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
short port;
} Address;
//----------------------------------------------------------------------------------
// Enumerators Definition
//----------------------------------------------------------------------------------
@ -1375,6 +1384,15 @@ RLAPI void StopAudioStream(AudioStream stream); // Stop au
RLAPI void SetAudioStreamVolume(AudioStream stream, float volume); // Set volume for audio stream (1.0 is max level)
RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
// Network functions
RLAPI bool InitializeSockets(void);
RLAPI void ShutdownSockets(void);
RLAPI int CreateUDPSocket(void);
RLAPI bool OpenSocket(int handle, unsigned short port);
RLAPI bool SendData(int handle, const Address* destination, const void* data, int size);
RLAPI bool ReceiveData(int handle, Address* sender, void* data, int size);
RLAPI int AddressToInt(Address address);
#if defined(__cplusplus)
}
#endif

View file

@ -33,22 +33,113 @@
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#if defined(RNET_STANDALONE)
#include "raudio.h"
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
#else
#include "raylib.h" // Declares module functions
//----------------------------------------------------------------------------------
// Check if config flags have been externally provided on compilation line
#if !defined(EXTERNAL_CONFIG_FLAGS)
//----------------------------------------------------------------------------------
#if !defined(EXTERNAL_CONFIG_FLAGS)
#include "config.h" // Defines module configuration flags
#endif
#include "utils.h" // Required for: fopen() Android mapping
#include "sysnet.h"
#include "raylib.h"
bool InitializeSockets()
{
#if PLATFORM == PLATFORM_WINDOWS
WSADATA WsaData;
return WSAStartup
(
MAKEWORD(2, 2),
&WsaData
) == NO_ERROR;
#else
return true;
#endif
}
void ShutdownSockets()
{
#if PLATFORM == PLATFORM_WINDOWS
WSACleanup();
#endif
}
int CreateUDPSocket()
{
int handle = socket(AF_INET,
SOCK_DGRAM,
IPPROTO_UDP);
if (handle <= 0)
{
printf("failed to create socket\n");
return false;
}
return handle;
}
bool OpenSocket(int handle, unsigned short port)
{
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons((unsigned short)port);
if (bind(handle, (const struct sockaddr*)&address, sizeof( struct sockaddr_in)) < 0)
{
printf("failed to bind socket\n");
return false;
}
#if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
int nonBlocking = 1;
if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1)
{
printf("failed to set non-blocking\n");
return false;
}
#elif PLATFORM == PLATFORM_WINDOWS
DWORD nonBlocking = 1;
if (ioctlsocket(handle, FIONBIO, &nonBlocking) != 0)
{
printf("failed to set non-blocking\n");
return false;
}
#endif
void HelloWorld(void);
void HelloWorld()
{
TraceLog(LOG_DEBUG, "Hello, World!");
return true;
}
bool SendData(int handle, const Address* destination, const void* data, int size)
{
int sent_bytes = sendto(handle, (const char*) data, size, 0, ( struct sockaddr*) &destination, sizeof(struct sockaddr_in));
if (sent_bytes != size) {
printf("failed to send packet\n");
return false;
}
}
int ReceiveData(int handle, Address* sender, void* data, int size)
{
#if PLATFORM == PLATFORM_WINDOWS
typedef int socklen_t;
#endif
while (true) {
socklen_t fromLength = sizeof(AddressToInt(*sender));
int bytes = recvfrom(handle, (char*) data, size, 0, (struct sockaddr*) &sender, &fromLength);
if (bytes <= 0) break;
return bytes;
}
}
int AddressToInt(Address address)
{
return (address.a << 24) | (address.b << 16) | (address.c << 8) | address.d;
}

View file

@ -36,13 +36,17 @@
#ifndef NET_H
#define NET_H
#define NET_H
#ifdef __cplusplus
extern "C" { // Prevents name mangling of functions
#endif
void HelloWorld(void);
void HelloWorld(void);
bool InitializeSockets(void);
void ShutdownSockets(void);
void OpenSocket(int handle, unsigned short port);
#ifdef __cplusplus
}

80
src/sysnet.h Normal file
View file

@ -0,0 +1,80 @@
//----------------------------------------------------------------------------------
// Undefine any conflicting windows.h symbols
//----------------------------------------------------------------------------------
// If defined, the following flags inhibit definition of the indicated items.
#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_
#define NOVIRTUALKEYCODES // VK_*
#define NOWINMESSAGES // WM_*, EM_*, LB_*, CB_*
#define NOWINSTYLES // WS_*, CS_*, ES_*, LBS_*, SBS_*, CBS_*
#define NOSYSMETRICS // SM_*
#define NOMENUS // MF_*
#define NOICONS // IDI_*
#define NOKEYSTATES // MK_*
#define NOSYSCOMMANDS // SC_*
#define NORASTEROPS // Binary and Tertiary raster ops
#define NOSHOWWINDOW // SW_*
#define OEMRESOURCE // OEM Resource values
#define NOATOM // Atom Manager routines
#define NOCLIPBOARD // Clipboard routines
#define NOCOLOR // Screen colors
#define NOCTLMGR // Control and Dialog routines
#define NODRAWTEXT // DrawText() and DT_*
#define NOGDI // All GDI defines and routines
#define NOKERNEL // All KERNEL defines and routines
#define NOUSER // All USER defines and routines
#define NONLS // All NLS defines and routines
#define NOMB // MB_* and MessageBox()
#define NOMEMMGR // GMEM_*, LMEM_*, GHND, LHND, associated routines
#define NOMETAFILE // typedef METAFILEPICT
#define NOMINMAX // Macros min(a,b) and max(a,b)
#define NOMSG // typedef MSG and associated routines
#define NOOPENFILE // OpenFile(), OemToAnsi, AnsiToOem, and OF_*
#define NOSCROLL // SB_* and scrolling routines
#define NOSERVICE // All Service Controller routines, SERVICE_ equates, etc.
#define NOSOUND // Sound driver routines
#define NOTEXTMETRIC // typedef TEXTMETRIC and associated routines
#define NOWH // SetWindowsHook and WH_*
#define NOWINOFFSETS // GWL_*, GCL_*, associated routines
#define NOCOMM // COMM driver routines
#define NOKANJI // Kanji support stuff.
#define NOHELP // Help engine interface.
#define NOPROFILER // Profiler interface.
#define NODEFERWINDOWPOS // DeferWindowPos routines
#define NOMCX // Modem Configuration Extensions
#define MMNOSOUND
//----------------------------------------------------------------------------------
// Platform specific network includes
//----------------------------------------------------------------------------------
#define PLATFORM_WINDOWS 1
#define PLATFORM_MAC 2
#define PLATFORM_UNIX 3
#if defined(_WIN32)
#define PLATFORM PLATFORM_WINDOWS
#elif defined(__APPLE__)
#define PLATFORM PLATFORM_MAC
#else
#define PLATFORM PLATFORM_UNIX
#endif
#if PLATFORM == PLATFORM_WINDOWS
// #define _INC_WINDOWS
// #define WIN32_LEAN_AND_MEAN
// #include <minwindef.h>
#endif
#if PLATFORM == PLATFORM_WINDOWS
#include <winsock2.h>
#elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#if PLATFORM == PLATFORM_WINDOWS
#pragma comment(lib, "wsock32.lib")
#endif