From d1206837537f83e95e112320c2fa1008362324f3 Mon Sep 17 00:00:00 2001 From: Jak Barnes Date: Thu, 14 Feb 2019 00:29:37 +0000 Subject: [PATCH] Added stub functions, fleshed out network includes --- examples/network/network_ping_pong.c | 74 ++++++++ .../VS2017/examples/network_ping_pong.vcxproj | 172 ++++++++++++++++++ projects/VS2017/raylib.sln | 149 +++++++++------ projects/VS2017/raylib/cpp.hint | 10 + projects/VS2017/raylib/raylib.vcxproj | 5 +- src/raylib.h | 18 ++ src/rnet.c | 117 ++++++++++-- src/rnet.h | 8 +- src/sysnet.h | 80 ++++++++ 9 files changed, 560 insertions(+), 73 deletions(-) create mode 100644 examples/network/network_ping_pong.c create mode 100644 projects/VS2017/examples/network_ping_pong.vcxproj create mode 100644 projects/VS2017/raylib/cpp.hint create mode 100644 src/sysnet.h diff --git a/examples/network/network_ping_pong.c b/examples/network/network_ping_pong.c new file mode 100644 index 000000000..3520ffc86 --- /dev/null +++ b/examples/network/network_ping_pong.c @@ -0,0 +1,74 @@ +/******************************************************************************************* +* +* 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() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + const int port = 5000; + Address* address = &(struct Address) { 127, 0, 0, 1, port }; + Address* sender = NULL; + int handle = -1; + const char data[] = "Ping!"; + + // Socket creation + InitializeSockets(); + handle = CreateUDPSocket(); + OpenSocket(handle, port); + SendData(handle, address, data, sizeof(data)); + + // Main game loop + 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); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/projects/VS2017/examples/network_ping_pong.vcxproj b/projects/VS2017/examples/network_ping_pong.vcxproj new file mode 100644 index 000000000..5ddea9045 --- /dev/null +++ b/projects/VS2017/examples/network_ping_pong.vcxproj @@ -0,0 +1,172 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + {e89d61ac-55de-4482-afd4-df7242ebc859} + + + + 15.0 + {56EB485C-00A9-459E-B758-2E86316EB7FD} + Win32Proj + networkpingpong + 10.0.17763.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(ProjectDir)$(ProjectName)\$(Configuration)\ + $(ProjectDir)$(ProjectName)\$(Configuration)\temp + + + true + + + false + + + false + + + + NotUsing + Level4 + Disabled + + + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + false + + + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + false + + + Console + true + %(AdditionalLibraryDirectories) + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/projects/VS2017/raylib.sln b/projects/VS2017/raylib.sln index f163b7b70..0e1d26553 100644 --- a/projects/VS2017/raylib.sln +++ b/projects/VS2017/raylib.sln @@ -1,57 +1,92 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2024 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_basic_window", "examples\core_basic_window.vcxproj", "{0981CA98-E4A5-4DF1-987F-A41D09131EFC}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "raylib", "raylib\raylib.vcxproj", "{E89D61AC-55DE-4482-AFD4-DF7242EBC859}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_basic_window_cpp", "examples\core_basic_window_cpp.vcxproj", "{B655E850-3322-42F7-941D-6AC18FD66CA1}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug.DLL|x86 = Debug.DLL|x86 - Debug|x86 = Debug|x86 - Release.DLL|x86 = Release.DLL|x86 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug|x86.ActiveCfg = Debug|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug|x86.Build.0 = Debug|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release.DLL|x86.Build.0 = Release.DLL|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release|x86.ActiveCfg = Release|Win32 - {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release|x86.Build.0 = Release|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug|x86.ActiveCfg = Debug|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug|x86.Build.0 = Debug|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release.DLL|x86.Build.0 = Release.DLL|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release|x86.ActiveCfg = Release|Win32 - {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release|x86.Build.0 = Release|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug|x86.ActiveCfg = Debug|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug|x86.Build.0 = Debug|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release.DLL|x86.Build.0 = Release.DLL|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release|x86.ActiveCfg = Release|Win32 - {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {0981CA98-E4A5-4DF1-987F-A41D09131EFC} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} - {B655E850-3322-42F7-941D-6AC18FD66CA1} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {E926C768-6307-4423-A1EC-57E95B1FAB29} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27130.2024 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "raylib", "raylib\raylib.vcxproj", "{E89D61AC-55DE-4482-AFD4-DF7242EBC859}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_basic_window_cpp", "examples\core_basic_window_cpp.vcxproj", "{B655E850-3322-42F7-941D-6AC18FD66CA1}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_ping_pong", "examples\network_ping_pong.vcxproj", "{56EB485C-00A9-459E-B758-2E86316EB7FD}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_basic_window", "examples\core_basic_window.vcxproj", "{0981CA98-E4A5-4DF1-987F-A41D09131EFC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug.DLL|x64 = Debug.DLL|x64 + Debug.DLL|x86 = Debug.DLL|x86 + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release.DLL|x64 = Release.DLL|x64 + Release.DLL|x86 = Release.DLL|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug.DLL|x64.ActiveCfg = Debug.DLL|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug|x64.ActiveCfg = Debug|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug|x86.ActiveCfg = Debug|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Debug|x86.Build.0 = Debug|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release.DLL|x64.ActiveCfg = Release.DLL|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release.DLL|x86.Build.0 = Release.DLL|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release|x64.ActiveCfg = Release|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release|x86.ActiveCfg = Release|Win32 + {E89D61AC-55DE-4482-AFD4-DF7242EBC859}.Release|x86.Build.0 = Release|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug.DLL|x64.ActiveCfg = Debug.DLL|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug|x64.ActiveCfg = Debug|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug|x86.ActiveCfg = Debug|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Debug|x86.Build.0 = Debug|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release.DLL|x64.ActiveCfg = Release.DLL|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release.DLL|x86.Build.0 = Release.DLL|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release|x64.ActiveCfg = Release|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release|x86.ActiveCfg = Release|Win32 + {B655E850-3322-42F7-941D-6AC18FD66CA1}.Release|x86.Build.0 = Release|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug.DLL|x64.ActiveCfg = Debug|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug.DLL|x64.Build.0 = Debug|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug.DLL|x86.ActiveCfg = Debug|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug.DLL|x86.Build.0 = Debug|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug|x64.ActiveCfg = Debug|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug|x64.Build.0 = Debug|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug|x86.ActiveCfg = Debug|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Debug|x86.Build.0 = Debug|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release.DLL|x64.ActiveCfg = Release|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release.DLL|x64.Build.0 = Release|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release.DLL|x86.ActiveCfg = Release|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release.DLL|x86.Build.0 = Release|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release|x64.ActiveCfg = Release|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release|x64.Build.0 = Release|x64 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release|x86.ActiveCfg = Release|Win32 + {56EB485C-00A9-459E-B758-2E86316EB7FD}.Release|x86.Build.0 = Release|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug.DLL|x64.ActiveCfg = Debug.DLL|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug|x64.ActiveCfg = Debug|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug|x86.ActiveCfg = Debug|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Debug|x86.Build.0 = Debug|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release.DLL|x64.ActiveCfg = Release.DLL|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release.DLL|x86.Build.0 = Release.DLL|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release|x64.ActiveCfg = Release|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release|x86.ActiveCfg = Release|Win32 + {0981CA98-E4A5-4DF1-987F-A41D09131EFC}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {B655E850-3322-42F7-941D-6AC18FD66CA1} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} + {56EB485C-00A9-459E-B758-2E86316EB7FD} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} + {0981CA98-E4A5-4DF1-987F-A41D09131EFC} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E926C768-6307-4423-A1EC-57E95B1FAB29} + EndGlobalSection +EndGlobal diff --git a/projects/VS2017/raylib/cpp.hint b/projects/VS2017/raylib/cpp.hint new file mode 100644 index 000000000..82a482dd8 --- /dev/null +++ b/projects/VS2017/raylib/cpp.hint @@ -0,0 +1,10 @@ +// Hint files help the Visual Studio IDE interpret Visual C++ identifiers +// such as names of functions and macros. +// For more information see https://go.microsoft.com/fwlink/?linkid=865984 +#define RLAPI __declspec(dllexport) +#define RLAPI __declspec(dllimport) +// Hint files help the Visual Studio IDE interpret Visual C++ identifiers +// such as names of functions and macros. +// For more information see https://go.microsoft.com/fwlink/?linkid=865984 +#define RLAPI __declspec(dllexport) +#define RLAPI __declspec(dllimport) diff --git a/projects/VS2017/raylib/raylib.vcxproj b/projects/VS2017/raylib/raylib.vcxproj index fd4ada09c..eda027cfe 100644 --- a/projects/VS2017/raylib/raylib.vcxproj +++ b/projects/VS2017/raylib/raylib.vcxproj @@ -185,9 +185,12 @@ - + + + + diff --git a/src/raylib.h b/src/raylib.h index 17a6efc6a..ef8fcdb9a 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -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 diff --git a/src/rnet.c b/src/rnet.c index daf72167e..238d622f9 100644 --- a/src/rnet.c +++ b/src/rnet.c @@ -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 // 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; +} \ No newline at end of file diff --git a/src/rnet.h b/src/rnet.h index 31b06c002..e9211975f 100644 --- a/src/rnet.h +++ b/src/rnet.h @@ -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 } diff --git a/src/sysnet.h b/src/sysnet.h new file mode 100644 index 000000000..048a79281 --- /dev/null +++ b/src/sysnet.h @@ -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 +#endif + +#if PLATFORM == PLATFORM_WINDOWS +#include +#elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX +#include +#include +#include +#endif + +#if PLATFORM == PLATFORM_WINDOWS +#pragma comment(lib, "wsock32.lib") +#endif \ No newline at end of file