diff --git a/examples/network/network_chat_client.c b/examples/network/network_chat_client.c new file mode 100644 index 000000000..a864891ce --- /dev/null +++ b/examples/network/network_chat_client.c @@ -0,0 +1,53 @@ +/******************************************************************************************* + * + * raylib [network] example - Chat client + * + * 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 2.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() +{ + // Setup + int screenWidth = 800; + int screenHeight = 450; + InitWindow( + screenWidth, screenHeight, "raylib [network] example - ping pong"); + SetTargetFPS(60); + + // Networking + InitNetwork(); + + // Main game loop + while (!WindowShouldClose()) + { + // Draw + BeginDrawing(); + + // Clear + ClearBackground(RAYWHITE); + + // End draw + EndDrawing(); + } + + // Cleanup + CloseWindow(); + return 0; +} \ No newline at end of file diff --git a/examples/network/network_chat_server.c b/examples/network/network_chat_server.c new file mode 100644 index 000000000..c9ded621a --- /dev/null +++ b/examples/network/network_chat_server.c @@ -0,0 +1,53 @@ +/******************************************************************************************* + * + * raylib [network] example - Chat server + * + * 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 2.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() +{ + // Setup + int screenWidth = 800; + int screenHeight = 450; + InitWindow( + screenWidth, screenHeight, "raylib [network] example - ping pong"); + SetTargetFPS(60); + + // Networking + InitNetwork(); + + // Main game loop + while (!WindowShouldClose()) + { + // Draw + BeginDrawing(); + + // Clear + ClearBackground(RAYWHITE); + + // End draw + EndDrawing(); + } + + // Cleanup + CloseWindow(); + return 0; +} \ No newline at end of file diff --git a/examples/network/network_ping_pong.c b/examples/network/network_ping_pong.c index 1bf8fb572..3763d451f 100644 --- a/examples/network/network_ping_pong.c +++ b/examples/network/network_ping_pong.c @@ -1,6 +1,6 @@ /******************************************************************************************* * - * raylib [core] example - Basic window + * raylib [network] example - Client/Server ping-pong * * Welcome to raylib! * @@ -12,7 +12,7 @@ * * Enjoy using raylib. :) * - * This example has been created using raylib 1.0 (www.raylib.com) + * This example has been created using raylib 2.0 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h *for details) * diff --git a/examples/network/network_resolve_host.c b/examples/network/network_resolve_host.c index a470bede4..0020675cb 100644 --- a/examples/network/network_resolve_host.c +++ b/examples/network/network_resolve_host.c @@ -1,6 +1,6 @@ /******************************************************************************************* * - * raylib [core] example - Basic window + * raylib [network] example - Resolve host * * Welcome to raylib! * @@ -12,7 +12,7 @@ * * Enjoy using raylib. :) * - * This example has been created using raylib 1.0 (www.raylib.com) + * This example has been created using raylib 2.0 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h *for details) * diff --git a/projects/VS2017/examples/network_chat_client.vcxproj b/projects/VS2017/examples/network_chat_client.vcxproj new file mode 100644 index 000000000..87309e2ee --- /dev/null +++ b/projects/VS2017/examples/network_chat_client.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + {e89d61ac-55de-4482-afd4-df7242ebc859} + + + + + + + 15.0 + {80AE5E50-FAC7-434F-A898-2878568E7D79} + Win32Proj + networkpingpong + 10.0.17763.0 + network_chat_client + + + + 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/examples/network_chat_server.vcxproj b/projects/VS2017/examples/network_chat_server.vcxproj new file mode 100644 index 000000000..f4593b743 --- /dev/null +++ b/projects/VS2017/examples/network_chat_server.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + {e89d61ac-55de-4482-afd4-df7242ebc859} + + + + 15.0 + {BC49AAF0-E8C2-4F94-A60A-C56993023C6D} + Win32Proj + networkpingpong + 10.0.17763.0 + network_stream_client + + + + 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 b104e1519..7cc041f3b 100644 --- a/projects/VS2017/raylib.sln +++ b/projects/VS2017/raylib.sln @@ -15,7 +15,9 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_basic_window", "exampl EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_resolve_host", "examples\network_interfaces.vcxproj", "{A16D19CB-6AF4-4D17-8318-EABD8805247C}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_stream_client", "examples\network_stream_client.vcxproj", "{BC49AAF0-E8C2-4F94-A60A-C56993023C6D}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_chat_client", "examples\network_chat_client.vcxproj", "{80AE5E50-FAC7-434F-A898-2878568E7D79}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_stream_client", "examples\network_chat_server.vcxproj", "{BC49AAF0-E8C2-4F94-A60A-C56993023C6D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -97,6 +99,22 @@ Global {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release|x64.Build.0 = Release|x64 {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release|x86.ActiveCfg = Release|Win32 {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release|x86.Build.0 = Release|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug.DLL|x64.ActiveCfg = Debug|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug.DLL|x64.Build.0 = Debug|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug.DLL|x86.ActiveCfg = Debug|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug.DLL|x86.Build.0 = Debug|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug|x64.ActiveCfg = Debug|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug|x64.Build.0 = Debug|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug|x86.ActiveCfg = Debug|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Debug|x86.Build.0 = Debug|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release.DLL|x64.ActiveCfg = Release|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release.DLL|x64.Build.0 = Release|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release.DLL|x86.ActiveCfg = Release|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release.DLL|x86.Build.0 = Release|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release|x64.ActiveCfg = Release|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release|x64.Build.0 = Release|x64 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release|x86.ActiveCfg = Release|Win32 + {80AE5E50-FAC7-434F-A898-2878568E7D79}.Release|x86.Build.0 = Release|Win32 {BC49AAF0-E8C2-4F94-A60A-C56993023C6D}.Debug.DLL|x64.ActiveCfg = Debug|x64 {BC49AAF0-E8C2-4F94-A60A-C56993023C6D}.Debug.DLL|x64.Build.0 = Debug|x64 {BC49AAF0-E8C2-4F94-A60A-C56993023C6D}.Debug.DLL|x86.ActiveCfg = Debug|Win32 @@ -122,6 +140,7 @@ Global {56EB485C-00A9-459E-B758-2E86316EB7FD} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} {0981CA98-E4A5-4DF1-987F-A41D09131EFC} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} {A16D19CB-6AF4-4D17-8318-EABD8805247C} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} + {80AE5E50-FAC7-434F-A898-2878568E7D79} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} {BC49AAF0-E8C2-4F94-A60A-C56993023C6D} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution diff --git a/src/rnet.c b/src/rnet.c index 6d1720c33..8b8dc06b0 100644 --- a/src/rnet.c +++ b/src/rnet.c @@ -6,7 +6,8 @@ * - Manage network stuff * * DEPENDENCIES: -* core.h - core stuff +* raylib.h - TraceLog +* sysnet.h - platform-specific network includes * * CONTRIBUTORS: * Jak Barnes (github: @syphonx) (Feb. 2019): @@ -41,13 +42,27 @@ # include "config.h" // Defines module configuration flags #endif +//---------------------------------------------------------------------------------- +// Module dependencies +//---------------------------------------------------------------------------------- + #include "raylib.h" #include "sysnet.h" #include #include -#define errno WSAGetLastError() +//---------------------------------------------------------------------------------- +// Module defines +//---------------------------------------------------------------------------------- + +#if PLATFORM_WINDOWS +# define errno WSAGetLastError() // Support UNIX socket error codes +#endif + +//---------------------------------------------------------------------------------- +// Module implementation +//---------------------------------------------------------------------------------- // Initialise the network (requires for windows platforms only) bool InitNetwork() @@ -192,7 +207,7 @@ bool ConnectSocket(Socket socket, AddressInformation addr) return true; } -// +// Listen on a socket bool ListenSocket(Socket socket) { int status = listen(socket.handle, MAX_SOCKET_QUEUE_SIZE); @@ -235,7 +250,7 @@ void CloseSocket(Socket* socket) #endif } -// +// Create a listen server, this combines Create/Bind/Listen into 1 function void CreateListenServer(Socket* tcpsock, const char* address, const char* port, SocketType socketType) { // Variables @@ -313,7 +328,7 @@ void CreateListenServer(Socket* tcpsock, const char* address, const char* port, return; } -// +// Create a simple client, this combines Create/Bind/Connect into 1 function void CreateClient(Socket* tcpsock, char* address, char* port, SocketType socketType) { int status; @@ -369,7 +384,7 @@ void CreateClient(Socket* tcpsock, char* address, char* port, SocketType socketT tcpsock->handle = handle; } -// +// Try to accept connections on ListenSock and store them in NewSock void AcceptSocket(Socket listenSock, Socket* newSock) { struct sockaddr_storage their_addr; @@ -389,7 +404,7 @@ void AcceptSocket(Socket listenSock, Socket* newSock) } } -// +// Send data over TCP int SendTCP(SocketHandle handle, const char* data, int len) { int sentBytes = send(handle, data, len, 0); @@ -403,7 +418,7 @@ int SendTCP(SocketHandle handle, const char* data, int len) } } -// +// Receiive data over TCP int ReceiveTCP(SocketHandle handle, const char* data, int len) { int status = recv(handle, data, len, 0); @@ -453,8 +468,7 @@ void ResolveHost(const char* hostname) void* addr; char* ipver; - // get the pointer to the address itself, - // different fields in IPv4 and IPv6: + // get the pointer to the address itself, different fields in IPv4 and IPv6: if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in* ipv4 = (struct sockaddr_in*) p->ai_addr;