diff --git a/examples/network/network_ping_pong.c b/examples/network/network_ping_pong.c index 3763d451f..166674e9a 100644 --- a/examples/network/network_ping_pong.c +++ b/examples/network/network_ping_pong.c @@ -23,7 +23,8 @@ #include "raylib.h" int main() -{ +{ + // Setup int screenWidth = 800; int screenHeight = 450; @@ -40,7 +41,8 @@ int main() AddressInformation serveraddr; Socket server; server.blocking = false; - GetAddressInformation(&serveraddr, "localhost", "3490", SOCKET_TCP, PROTOCOL_TCP); + ResolveHost(&serveraddr, "localhost", "3490", SOCKET_TCP); + CreateSocket(&server, serveraddr); BindSocket(server, serveraddr); ListenSocket(server); @@ -49,7 +51,7 @@ int main() AddressInformation clientaddr; Socket client; client.blocking = false; - GetAddressInformation(&clientaddr, "localhost", "3490", SOCKET_TCP, PROTOCOL_TCP); + ResolveHost(&clientaddr, "localhost", "3490", SOCKET_TCP); CreateSocket(&client, clientaddr); ConnectSocket(client, clientaddr); diff --git a/examples/network/network_resolve_host.c b/examples/network/network_resolve_host.c index 0020675cb..e580745b0 100644 --- a/examples/network/network_resolve_host.c +++ b/examples/network/network_resolve_host.c @@ -20,7 +20,7 @@ * ********************************************************************************************/ -#include "raylib.h" +#include "raylib.h" int main() { @@ -31,11 +31,15 @@ int main() screenWidth, screenHeight, "raylib [network] example - ping pong"); SetTargetFPS(60); + SetTraceLogLevel(LOG_DEBUG); + // Networking - InitNetwork(); - ResolveHost("www.google.com"); - ResolveIP("2001:4860:4860::8888"); - ResolveIP("8.8.8.8"); + InitNetwork(); + + AddressInformation addr; + ResolveHost(&addr, "www.google.com", NULL, SOCKET_TCP); + ResolveIP("8.8.8.8", NULL); + ResolveIP("2001:4860:4860::8888", "80"); // Main game loop while (!WindowShouldClose()) diff --git a/examples/network/network_serialisation.c b/examples/network/network_serialisation.c new file mode 100644 index 000000000..a3be400fd --- /dev/null +++ b/examples/network/network_serialisation.c @@ -0,0 +1,74 @@ +/******************************************************************************************* + * + * raylib [network] example - Resolve host + * + * 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" +#include + +int main() +{ + // Setup + int screenWidth = 800; + int screenHeight = 450; + InitWindow( + screenWidth, screenHeight, "raylib [network] example - ping pong"); + SetTargetFPS(60); + + SetTraceLogLevel(LOG_DEBUG); + + // Networking + InitNetwork(); + + unsigned char buf[1024]; + unsigned char magic; + int monkeycount; + long altitude; + double absurdityfactor; + char* s = "Great unmitigated Zot! You've found the Runestaff!"; + char s2[96]; + unsigned int packetsize, ps2; + + packetsize = PackData(buf, "CHhlsd", 'B', 0, 37, -5, s, -3490.5); + packi16(buf + 1, packetsize); // store packet size in packet for kicks + + printf("packet is %u bytes\n", packetsize); + UnpackData(buf, "CHhl96sd", &magic, &ps2, &monkeycount, &altitude, s2, &absurdityfactor); + printf("'%c' %hhu %u %ld \"%s\" %f\n", magic, ps2, monkeycount, altitude, s2, absurdityfactor); + + + // 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/projects/VS2017/examples/network_chat_client.vcxproj b/projects/VS2017/examples/network_chat_client.vcxproj index 87309e2ee..2c111213f 100644 --- a/projects/VS2017/examples/network_chat_client.vcxproj +++ b/projects/VS2017/examples/network_chat_client.vcxproj @@ -30,7 +30,7 @@ 15.0 {80AE5E50-FAC7-434F-A898-2878568E7D79} Win32Proj - networkpingpong + network_chat_client 10.0.17763.0 network_chat_client diff --git a/projects/VS2017/examples/network_chat_server.vcxproj b/projects/VS2017/examples/network_chat_server.vcxproj index f4593b743..8269e2086 100644 --- a/projects/VS2017/examples/network_chat_server.vcxproj +++ b/projects/VS2017/examples/network_chat_server.vcxproj @@ -18,21 +18,21 @@ x64 - - - {e89d61ac-55de-4482-afd4-df7242ebc859} + + + 15.0 {BC49AAF0-E8C2-4F94-A60A-C56993023C6D} Win32Proj - networkpingpong + network_chat_server 10.0.17763.0 - network_stream_client + network_chat_server diff --git a/projects/VS2017/examples/network_ping_pong.vcxproj b/projects/VS2017/examples/network_ping_pong.vcxproj index 5ddea9045..6d62e01d2 100644 --- a/projects/VS2017/examples/network_ping_pong.vcxproj +++ b/projects/VS2017/examples/network_ping_pong.vcxproj @@ -30,7 +30,7 @@ 15.0 {56EB485C-00A9-459E-B758-2E86316EB7FD} Win32Proj - networkpingpong + network_ping_pong 10.0.17763.0 diff --git a/projects/VS2017/examples/network_interfaces.vcxproj b/projects/VS2017/examples/network_resolve_host.vcxproj similarity index 97% rename from projects/VS2017/examples/network_interfaces.vcxproj rename to projects/VS2017/examples/network_resolve_host.vcxproj index f266f0fa5..f184462cb 100644 --- a/projects/VS2017/examples/network_interfaces.vcxproj +++ b/projects/VS2017/examples/network_resolve_host.vcxproj @@ -18,19 +18,19 @@ x64 - - - {e89d61ac-55de-4482-afd4-df7242ebc859} + + + 15.0 {A16D19CB-6AF4-4D17-8318-EABD8805247C} Win32Proj - networkpingpong + network_resolve_host 10.0.17763.0 network_resolve_host diff --git a/projects/VS2017/examples/network_serialisation.vcxproj b/projects/VS2017/examples/network_serialisation.vcxproj new file mode 100644 index 000000000..57048bd4f --- /dev/null +++ b/projects/VS2017/examples/network_serialisation.vcxproj @@ -0,0 +1,173 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + {e89d61ac-55de-4482-afd4-df7242ebc859} + + + + 15.0 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD} + Win32Proj + network_serialisation + 10.0.17763.0 + network_serialisation + + + + 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 7cc041f3b..7a104cad6 100644 --- a/projects/VS2017/raylib.sln +++ b/projects/VS2017/raylib.sln @@ -9,15 +9,17 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_basic_window_cpp", "ex 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 -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_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}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_chat_server", "examples\network_chat_server.vcxproj", "{BC49AAF0-E8C2-4F94-A60A-C56993023C6D}" +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}") = "network_serialisation", "examples\network_resolve_host.vcxproj", "{A16D19CB-6AF4-4D17-8318-EABD8805247C}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "network_resolve_host", "examples\network_serialisation.vcxproj", "{46B18968-56BC-4FB1-A7C0-FA418E095AFD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -55,22 +57,6 @@ Global {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 @@ -83,22 +69,6 @@ Global {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 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x64.ActiveCfg = Debug|x64 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x64.Build.0 = Debug|x64 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x86.ActiveCfg = Debug|Win32 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x86.Build.0 = Debug|Win32 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x64.ActiveCfg = Debug|x64 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x64.Build.0 = Debug|x64 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x86.ActiveCfg = Debug|Win32 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x86.Build.0 = Debug|Win32 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x64.ActiveCfg = Release|x64 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x64.Build.0 = Release|x64 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x86.ActiveCfg = Release|Win32 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x86.Build.0 = Release|Win32 - {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release|x64.ActiveCfg = Release|x64 - {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 @@ -131,17 +101,66 @@ Global {BC49AAF0-E8C2-4F94-A60A-C56993023C6D}.Release|x64.Build.0 = Release|x64 {BC49AAF0-E8C2-4F94-A60A-C56993023C6D}.Release|x86.ActiveCfg = Release|Win32 {BC49AAF0-E8C2-4F94-A60A-C56993023C6D}.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 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x64.ActiveCfg = Debug|x64 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x64.Build.0 = Debug|x64 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x86.ActiveCfg = Debug|Win32 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug.DLL|x86.Build.0 = Debug|Win32 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x64.ActiveCfg = Debug|x64 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x64.Build.0 = Debug|x64 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x86.ActiveCfg = Debug|Win32 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Debug|x86.Build.0 = Debug|Win32 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x64.ActiveCfg = Release|x64 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x64.Build.0 = Release|x64 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x86.ActiveCfg = Release|Win32 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release.DLL|x86.Build.0 = Release|Win32 + {A16D19CB-6AF4-4D17-8318-EABD8805247C}.Release|x64.ActiveCfg = Release|x64 + {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 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug.DLL|x64.ActiveCfg = Debug|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug.DLL|x64.Build.0 = Debug|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug.DLL|x86.ActiveCfg = Debug|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug.DLL|x86.Build.0 = Debug|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug|x64.ActiveCfg = Debug|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug|x64.Build.0 = Debug|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug|x86.ActiveCfg = Debug|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Debug|x86.Build.0 = Debug|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release.DLL|x64.ActiveCfg = Release|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release.DLL|x64.Build.0 = Release|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release.DLL|x86.ActiveCfg = Release|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release.DLL|x86.Build.0 = Release|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release|x64.ActiveCfg = Release|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release|x64.Build.0 = Release|x64 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.Release|x86.ActiveCfg = Release|Win32 + {46B18968-56BC-4FB1-A7C0-FA418E095AFD}.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} - {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} + {56EB485C-00A9-459E-B758-2E86316EB7FD} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} + {A16D19CB-6AF4-4D17-8318-EABD8805247C} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} + {46B18968-56BC-4FB1-A7C0-FA418E095AFD} = {8716DC0F-4FDE-4F57-8E25-5F78DFB80FE1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E926C768-6307-4423-A1EC-57E95B1FAB29} diff --git a/projects/VS2017/raylib/raylib.vcxproj b/projects/VS2017/raylib/raylib.vcxproj index eda027cfe..eef1c0144 100644 --- a/projects/VS2017/raylib/raylib.vcxproj +++ b/projects/VS2017/raylib/raylib.vcxproj @@ -185,6 +185,7 @@ + diff --git a/src/raylib.h b/src/raylib.h index b53926702..46c4a356f 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -100,6 +100,10 @@ // Network limits #define MAX_SOCKET_SET_SIZE 32 #define MAX_SOCKET_QUEUE_SIZE 16 +#define MAX_HOST_NAME_SIZE NI_MAXHOST +#define MAX_SERV_NAME_SIZE NI_MAXSERV +#define MAX_IPV4_NAME_SIZE INET6_ADDRSTRLEN +#define MAX_IPV6_NAME_SIZE INET_ADDRSTRLEN // NOTE: MSC C++ compiler does not support compound literals (C99 feature) // Plain structures in C++ (without constructors) can be initialized from { } initializers. @@ -155,8 +159,8 @@ typedef enum { false, true } bool; #endif -// Network typedefs -typedef int SocketHandle; +// Network typedefs +typedef int SocketHandle; // Vector2 type typedef struct Vector2 { @@ -424,6 +428,33 @@ typedef struct VrStereoConfig { int eyeViewportRight[4]; // VR stereo rendering right eye viewport [x, y, w, h] int eyeViewportLeft[4]; // VR stereo rendering left eye viewport [x, y, w, h] } VrStereoConfig; + +typedef struct IPAddress +{ + int family; + union { + struct + { + unsigned int host; /* 32-bit IPv4 host address */ + unsigned short port; /* 16-bit protocol port */ + } ip4; + struct + { + unsigned char host[16]; /* 128-bit IPv6 host address */ + } ip6; + } data; +} IPAddress; + +typedef struct IPv4address +{ + unsigned int host; /* 32-bit IPv4 host address */ + unsigned short port; /* 16-bit protocol port */ +} IPv4address; + +typedef struct IPv6address +{ + unsigned char bytes[16]; /* 128-bit IPv6 host address */ +} IPv6address; typedef struct AddressInformation { @@ -431,7 +462,7 @@ typedef struct AddressInformation int family; // PF_xxx int socktype; // SOCK_xxx int protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6 - size_t addrlen; // Length of ai_addr + unsigned int addrlen; // Length of ai_addr char * canonname; // Canonical name for nodename struct SocketAddress *sockaddr; // Binary address struct AddressInformation *next; // Next structure in linked list @@ -446,15 +477,56 @@ typedef struct SocketAddress // Socket typedef struct Socket { + int ready; SocketHandle handle; - bool blocking; -} Socket; + IPv4address address; + bool blocking; + // int ready; + // SocketHandle handle; + // IPAddress address; + // bool blocking; +} Socket; + + +//typedef struct UDPSocket +//{ +// int ready; +// SocketHandle handle; +// IPAddress address; +// bool blocking; +//} UDPSocket; +// +//typedef struct UDPPacket +//{ +// int channel; /* The src/dst channel of the packet */ +// char * data; /* The packet data */ +// int len; /* The length of the packet data */ +// int maxlen; /* The size of the data buffer */ +// int status; /* packet status after sending */ +// IPAddress address; /* The source/dest address of an incoming/outgoing packet */ +//} UDPPacket; +// +//typedef struct TCPSocket +//{ +// int ready; +// SocketHandle channel; +// IPAddress remoteAddress; +// IPAddress localAddress; +// bool isServer; +// bool blocking; +//} TCPSocket; +// +//typedef struct TCPPacket +//{ +// const void *data; /* The packet data */ +// int len; /* The length of the packet data */ +// int maxlen; /* The size of the data buffer */ +//} TCPPacket; typedef struct SocketSet { - Socket sockets[MAX_SOCKET_SET_SIZE]; -} SocketSet; - + Socket *sockets[MAX_SOCKET_SET_SIZE]; +} SocketSet; //---------------------------------------------------------------------------------- @@ -462,18 +534,16 @@ typedef struct SocketSet //---------------------------------------------------------------------------------- typedef enum -{ - SOCKET_ANY = 0, // ACCEPT_ALL +{ SOCKET_TCP = 1, // SOCK_STREAM SOCKET_UDP = 2 // SOCK_DGRAM -} SocketType; +} SocketType; -typedef enum +typedef enum { - PROTOCOL_ANY = 0, // ACCEPT_ALL - PROTOCOL_TCP = 6, // IPPROTO_TCP - PROTOCOL_UDP = 17 // IPPROTO_UDP -} ProtocolType; + FAMILY_IPv4 = 1, + FAMILY_IPv6 = 2 +} AddressFamily; // System config flags // NOTE: Used for bit masks @@ -1428,30 +1498,41 @@ 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 InitNetwork(void); -RLAPI void CloseNetwork(void); - -RLAPI void GetAddressInformation(AddressInformation* outaddr, const char* address, const char* port, int socketType, int protocolType); - -RLAPI bool CreateSocket(Socket *socket, AddressInformation outaddr); -RLAPI bool BindSocket(Socket socket, const AddressInformation addr); -RLAPI bool ConnectSocket(Socket socket, const AddressInformation addr); -RLAPI bool ListenSocket(Socket socket); -RLAPI void CloseSocket(Socket* socket); -RLAPI void AcceptSocket(Socket listenSock, Socket *newSock); - -RLAPI char* SocketAddressToString(SocketAddress* sockaddr, char buffer[]); -RLAPI void PrintSocket(SocketAddress* addr, const int family, const int socktype, const int protocol); -RLAPI void ResolveHost(const char* hostname); -RLAPI void ResolveIP(const char* ip); - -RLAPI void CreateListenServer(Socket* socket, const char* address, const int port, SocketType socketType); -RLAPI void CreateClient(Socket* socket, const char* address, const char* port, SocketType socketType); -RLAPI int Send(SocketHandle sockfd, const char* data, int len); -RLAPI int Receive(SocketHandle sockfd, const char* data, int len); -RLAPI void ResetSocket(Socket* socket); +// Network functions +RLAPI bool InitNetwork(void); +RLAPI void CloseNetwork(void); +RLAPI void ResolveHost(AddressInformation *outaddr, const char *address, const char *port, SocketType socketType); +RLAPI char *ResolveIP(const char *host, const char *port); +RLAPI bool IsIPv4Address(const char *host); +RLAPI bool IsIPv6Address(const char *host); +RLAPI int GetIPFamily(const char *host); +RLAPI void GetLocalAddresses(); +RLAPI bool CreateSocket(Socket *socket, AddressInformation outaddr); +RLAPI bool BindSocket(Socket socket, const AddressInformation addr); +RLAPI bool ConnectSocket(Socket socket, const AddressInformation addr); +RLAPI bool ListenSocket(Socket socket); +RLAPI void CloseSocket(Socket *socket); +RLAPI void AcceptSocket(Socket listenSock, Socket *newSock); +RLAPI char *SocketAddressToString(SocketAddress *sockaddr, char buffer[]); +RLAPI void PrintSocket(SocketAddress *addr, const int family, const int socktype, const int protocol); +RLAPI unsigned int PackData(unsigned char *buf, char *format, ...); +RLAPI void UnpackData(unsigned char *buf, char *format, ...); +RLAPI unsigned short HostToNetworkShort(unsigned short value); // 2 bytes - 0 to 65,535 +RLAPI unsigned long HostToNetworkLong(unsigned long value); // 4 bytes - 0 to 4,294,967,295 +RLAPI unsigned int HostToNetworkFloat(float value); // 4 bytes - 1.2E-38 to 3.4E+38 +RLAPI unsigned long long HostToNetworkDouble(double value); // 8 bytes - 2.3E-308 to 1.7E+308 +RLAPI unsigned long long HostToNetworkLongLong(unsigned long long value); // 8 bytes - 0 to 1.8446744073709551615 × 10^19 +RLAPI unsigned short NetworkToHostShort(unsigned short value); // 2 byte - 0 to 65,535 +RLAPI unsigned long NetworkToHostLong(unsigned long value); // 4 byte - 0 to 4,294,967,295 +RLAPI float NetworkToHostFloat(unsigned int value); // 4 byte - 1.2E-38 to 3.4E+38 +RLAPI double NetworkToHostDouble(unsigned long long value); // 8 byte - 2.3E-308 to 1.7E+308 +RLAPI unsigned long long NetworkToHostLongLong(unsigned long long value); // 8 byte - 0 to 1.8446744073709551615 × 10^19 +RLAPI void CreateListenServer(Socket *socket, const char *address, const int port, SocketType socketType); +RLAPI void CreateClient(Socket *socket, const char *address, const char *port, SocketType socketType); +RLAPI int Send(SocketHandle sockfd, const char *data, int len); +RLAPI int Receive(SocketHandle sockfd, const char *data, int len); +RLAPI void ResetSocket(Socket *socket); #if defined(__cplusplus) } diff --git a/src/rnet.c b/src/rnet.c index 8b8dc06b0..a9d96953a 100644 --- a/src/rnet.c +++ b/src/rnet.c @@ -47,10 +47,8 @@ //---------------------------------------------------------------------------------- #include "raylib.h" -#include "sysnet.h" - -#include -#include +#include "sysnet.h" +#include "rpack.h" //---------------------------------------------------------------------------------- // Module defines @@ -58,7 +56,7 @@ #if PLATFORM_WINDOWS # define errno WSAGetLastError() // Support UNIX socket error codes -#endif +#endif //---------------------------------------------------------------------------------- // Module implementation @@ -92,8 +90,87 @@ void CloseNetwork() #endif } +// Resolve the hostname +char* ResolveIP(const char* ip, const char* port) +{ + // Variables + char host[MAX_HOST_NAME_SIZE]; + char service[NI_MAXSERV]; + int status; // Status value to return (0) is success + struct addrinfo hints; // Address flags (IPV4, IPV6, UDP?) + struct addrinfo* results; // A pointer to the resulting address list + + // Zero out the host buffer + memset(&host, '\0', sizeof(host)); + memset(&service, '\0', sizeof(service)); + + // Set the hints + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; // Either IPv4 or IPv6 (AF_INET, AF_INET6) + hints.ai_socktype = SOCKET_TCP; // TCP (SOCK_STREAM), UDP (SOCK_DGRAM) + hints.ai_protocol = 0; // Automatically select correct protocol (IPPROTO_TCP), (IPPROTO_UDP) + + // Populate address information + status = getaddrinfo(ip, // e.g. "www.example.com" or IP + port, // e.g. "http" or port number + &hints, // e.g. SOCK_STREAM/SOCK_DGRAM + &results // The struct to populate + ); + + // Did we succeed? + if (status != 0) + { + TraceLog(LOG_WARNING, "Failed to get resolve host %s:%s: %ls", ip, port, gai_strerror(errno)); + } + else + { + TraceLog(LOG_DEBUG, "Resolving... %s::%s", ip, port); + } + + // Attempt to resolve network byte order ip to hostname + switch (results->ai_family) + { + case AF_INET: + status = getnameinfo(&*((struct sockaddr_in*) results->ai_addr), + sizeof(*((struct sockaddr_in*) results->ai_addr)), + host, + sizeof(host), + NULL, + NULL, + 0); + break; + case AF_INET6: + status = getnameinfo(&*((struct sockaddr_in6*) results->ai_addr), + sizeof(*((struct sockaddr_in6*) results->ai_addr)), + host, + sizeof(host), + NULL, + NULL, + 0); + break; + default: + break; + } + + // Did we succeed? + if (status != 0) + { + TraceLog(LOG_WARNING, "Failed to resolve ip %s: %ls", ip, gai_strerror(errno)); + } + else + { + TraceLog(LOG_INFO, "Successfully resolved %s::%s to %s", ip, port, host); + } + + // Free the pointer to the data returned by addrinfo + freeaddrinfo(results); + + // Return the resulting hostname + return host; +} + // Get address information -void GetAddressInformation(AddressInformation* outaddr, const char* address, const char* port, int socketType, int protocolType) +void ResolveHost(AddressInformation* outaddr, const char* address, const char* port, SocketType socketType) { // Variables int status; // Status value to return (0) is success @@ -103,8 +180,14 @@ void GetAddressInformation(AddressInformation* outaddr, const char* address, con // Set the hints memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // Either IPv4 or IPv6 (AF_INET, AF_INET6) - hints.ai_socktype = socketType; // TCP (SOCK_STREAM), UDP (SOCK_DGRAM) - hints.ai_protocol = protocolType; // TCP (IPPROTO_TCP), UDP (IPPROTO_UDP) + hints.ai_socktype = socketType == SOCKET_TCP ? SOCK_STREAM : SOCK_DGRAM; // TCP (SOCK_STREAM), UDP (SOCK_DGRAM) + hints.ai_protocol = 0; // Automatically select correct protocol (IPPROTO_TCP), (IPPROTO_UDP) + + // When the address is NULL, populate the IP for me + if (address == NULL) + { + hints.ai_flags = AI_PASSIVE; + } // Populate address information status = getaddrinfo(address, // e.g. "www.example.com" or IP @@ -118,7 +201,7 @@ void GetAddressInformation(AddressInformation* outaddr, const char* address, con // Did we succeed? if (status != 0) { - TraceLog(LOG_WARNING, "Failed to get resolve host %s:%s: %ls", address, port, gai_strerror(status)); + TraceLog(LOG_WARNING, "Failed to get resolve host %s:%s: %ls", address, port, gai_strerror(errno)); } else { @@ -141,6 +224,20 @@ void GetAddressInformation(AddressInformation* outaddr, const char* address, con freeaddrinfo(results); } +// IP helper method, checks if an address is a valid IPv4 address +bool IsIPv4Address(const char* address) +{ + struct sockaddr_in sa; + return inet_pton(AF_INET, address, &(sa.sin_addr)) != 0; +} + +// IP helper method, checks if an address is a valid IPv6 address +bool IsIPv6Address(const char* address) +{ + struct sockaddr_in6 sa; + return inet_pton(AF_INET6, address, &(sa.sin6_addr)) != 0; +} + // Create a socket from information provided by the filled Address information struct bool CreateSocket(Socket* sock, const AddressInformation addr) { @@ -150,7 +247,7 @@ bool CreateSocket(Socket* sock, const AddressInformation addr) // Did we succeed? if (sock->handle == INVALID_SOCKET) { - TraceLog(LOG_WARNING, "Failed to get create socket: %ls", gai_strerror(sock->handle)); + TraceLog(LOG_WARNING, "Failed to get create socket: %ls", gai_strerror(errno)); CloseSocket(sock->handle); return false; } @@ -173,7 +270,7 @@ bool BindSocket(Socket sock, const AddressInformation addr) // Did we succeed? if (status == SOCKET_ERROR) { - TraceLog(LOG_WARNING, "Failed to get bind socket: %ls", gai_strerror(status)); + TraceLog(LOG_WARNING, "Failed to get bind socket: %ls", gai_strerror(errno)); return false; } else @@ -194,7 +291,7 @@ bool ConnectSocket(Socket socket, AddressInformation addr) // Did we succeed? if (status == SOCKET_ERROR) { - TraceLog(LOG_WARNING, "Failed to connect socket: %ls", gai_strerror(status)); + TraceLog(LOG_WARNING, "Failed to connect socket: %ls", gai_strerror(errno)); return false; } else @@ -273,7 +370,7 @@ void CreateListenServer(Socket* tcpsock, const char* address, const char* port, // Did we succeed? if (status == -1) { - TraceLog(LOG_WARNING, "Failed to get resolve host %s:%s: %ls", address, port, gai_strerror(status)); + TraceLog(LOG_WARNING, "Failed to get resolve host %s:%s: %ls", address, port, gai_strerror(errno)); } else { @@ -444,80 +541,11 @@ void ResetSocket(Socket* socket) socket->blocking = false; }; -// Resolve the hostname -void ResolveHost(const char* hostname) -{ - struct addrinfo hints, *res, *p; - int status; - char ipstr[INET6_ADDRSTRLEN]; - - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version - hints.ai_socktype = SOCK_STREAM; - - if ((status = getaddrinfo(hostname, NULL, &hints, &res)) != 0) - { - TraceLog(LOG_WARNING, "getaddrinfo: %ls", gai_strerror(status)); - return 2; - } - - TraceLog(LOG_INFO, "IP addresses for %s:", hostname); - - for (p = res; p != NULL; p = p->ai_next) - { - void* addr; - char* ipver; - - // 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; - addr = &(ipv4->sin_addr); - ipver = "IPv4"; - } - else - { // IPv6 - struct sockaddr_in6* ipv6 = (struct sockaddr_in6*) p->ai_addr; - addr = &(ipv6->sin6_addr); - ipver = "IPv6"; - } - - // convert the IP to a string and print it: - inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); - TraceLog(LOG_INFO, "%s: %s", ipver, ipstr); - } - - freeaddrinfo(res); // free the linked list -} - -// Resolve the hostname -void ResolveIP(const char* ip) -{ - struct hostent* he; - struct in_addr ipv4addr; - struct in6_addr ipv6addr; - inet_pton(AF_INET, ip, &ipv4addr); - he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET); - if (he != NULL) - { - printf("Host name: %s\n", he->h_name); - } - inet_pton(AF_INET6, ip, &ipv6addr); - he = gethostbyaddr(&ipv6addr, sizeof ipv6addr, AF_INET6); - if (he != NULL) - { - printf("Host name: %s\n", he->h_name); - } -} - // Print socket information void PrintSocket(struct SocketAddress* addr, const int family, const int socktype, const int protocol) { - struct sockaddr_in* sockaddr_ipv4; - struct sockaddr_in6* sockaddr_ipv6; - struct sockaddr* sockaddr_ip; - char ip4[INET_ADDRSTRLEN]; // space to hold the IPv4 string - char ip6[INET6_ADDRSTRLEN]; // space to hold the IPv6 string + struct sockaddr* sockaddr_ip; + char ip[INET6_ADDRSTRLEN]; // Enough pace to hold a IPv6 string switch (family) { case AF_UNSPEC: @@ -528,13 +556,13 @@ void PrintSocket(struct SocketAddress* addr, const int family, const int socktyp case AF_INET: { TraceLog(LOG_DEBUG, "Family: AF_INET (IPv4)"); - TraceLog(LOG_INFO, "- IPv4 address %s", SocketAddressToString(addr, ip4)); + TraceLog(LOG_INFO, "- IPv4 address %s", SocketAddressToString(addr, ip)); } break; case AF_INET6: { TraceLog(LOG_DEBUG, "Family: AF_INET6 (IPv6)"); - TraceLog(LOG_INFO, "- IPv6 address %s", SocketAddressToString(addr, ip6)); + TraceLog(LOG_INFO, "- IPv6 address %s", SocketAddressToString(addr, ip)); } break; case AF_NETBIOS: @@ -613,3 +641,344 @@ char* SocketAddressToString(struct SocketAddress* sockaddr, char buffer[]) break; } } + +unsigned short HostToNetworkShort(unsigned short value) +{ + return htons(value); +} + +unsigned long HostToNetworkLong(unsigned long value) +{ + return htonl(value); +} + +unsigned int HostToNetworkFloat(float value) +{ + return htonf(value); +} + +unsigned long long HostToNetworkDouble(double value) +{ + return htond(value); +} + +unsigned long long HostToNetworkLongLong(unsigned long long value) +{ + return htonll(value); +} + +unsigned short NetworkToHostShort(unsigned short value) +{ + return ntohs(value); +} + +unsigned long NetworkToHostLong(unsigned long value) +{ + return ntohl(value); +} + +float NetworkToHostFloat(unsigned int value) +{ + return ntohf(value); +} + +double NetworkToHostDouble(unsigned long long value) +{ + return ntohd(value); +} + +double NetworkToHostLongDouble(unsigned long long value) +{ + return ntohd(value); +} + +unsigned long long NetworkToHostLongLong(unsigned long long value) +{ + return ntohll(value); +} + +/* +** PackData() -- store data dictated by the format string in the buffer +** +** bits |signed unsigned float string +** -----+---------------------------------- +** 8 | c C +** 16 | h H f +** 32 | l L d +** 64 | q Q g +** - | s +** +** (16-bit unsigned length is automatically prepended to strings) +*/ +unsigned int PackData(unsigned char* buf, char* format, ...) +{ + va_list ap; + + signed char c; // 8-bit + unsigned char C; + + int h; // 16-bit + unsigned int H; + + long int l; // 32-bit + unsigned long int L; + + long long int q; // 64-bit + unsigned long long int Q; + + float f; // floats + double d; + long double g; + unsigned long long int fhold; + + char* s; // strings + unsigned int len; + + unsigned int size = 0; + + va_start(ap, format); + + for (; *format != '\0'; format++) + { + switch (*format) + { + case 'c': // 8-bit + size += 1; + c = (signed char) va_arg(ap, int); // promoted + *buf++ = c; + break; + + case 'C': // 8-bit unsigned + size += 1; + C = (unsigned char) va_arg(ap, unsigned int); // promoted + *buf++ = C; + break; + + case 'h': // 16-bit + size += 2; + h = va_arg(ap, int); + packi16(buf, h); + buf += 2; + break; + + case 'H': // 16-bit unsigned + size += 2; + H = va_arg(ap, unsigned int); + packi16(buf, H); + buf += 2; + break; + + case 'l': // 32-bit + size += 4; + l = va_arg(ap, long int); + packi32(buf, l); + buf += 4; + break; + + case 'L': // 32-bit unsigned + size += 4; + L = va_arg(ap, unsigned long int); + packi32(buf, L); + buf += 4; + break; + + case 'q': // 64-bit + size += 8; + q = va_arg(ap, long long int); + packi64(buf, q); + buf += 8; + break; + + case 'Q': // 64-bit unsigned + size += 8; + Q = va_arg(ap, unsigned long long int); + packi64(buf, Q); + buf += 8; + break; + + case 'f': // float-16 + size += 2; + f = (float) va_arg(ap, double); // promoted + fhold = pack754_16(f); // convert to IEEE 754 + packi16(buf, fhold); + buf += 2; + break; + + case 'd': // float-32 + size += 4; + d = va_arg(ap, double); + fhold = pack754_32(d); // convert to IEEE 754 + packi32(buf, fhold); + buf += 4; + break; + + case 'g': // float-64 + size += 8; + g = va_arg(ap, long double); + fhold = pack754_64(g); // convert to IEEE 754 + packi64(buf, fhold); + buf += 8; + break; + + case 's': // string + s = va_arg(ap, char*); + len = strlen(s); + size += len + 2; + packi16(buf, len); + buf += 2; + memcpy(buf, s, len); + buf += len; + break; + } + } + + va_end(ap); + + return size; +} + +/* +** UnpackData() -- unpack data dictated by the format string into the buffer +** +** bits |signed unsigned float string +** -----+---------------------------------- +** 8 | c C +** 16 | h H f +** 32 | l L d +** 64 | q Q g +** - | s +** +** (string is extracted based on its stored length, but 's' can be +** prepended with a max length) +*/ +void UnpackData(unsigned char* buf, char* format, ...) +{ + va_list ap; + + signed char* c; // 8-bit + unsigned char* C; + + int* h; // 16-bit + unsigned int* H; + + long int* l; // 32-bit + unsigned long int* L; + + long long int* q; // 64-bit + unsigned long long int* Q; + + float* f; // floats + double* d; + long double* g; + unsigned long long int fhold; + + char* s; + unsigned int len, maxstrlen = 0, count; + + va_start(ap, format); + + for (; *format != '\0'; format++) + { + switch (*format) + { + case 'c': // 8-bit + c = va_arg(ap, signed char*); + if (*buf <= 0x7f) + { + *c = *buf; + } // re-sign + else + { + *c = -1 - (unsigned char) (0xffu - *buf); + } + buf++; + break; + + case 'C': // 8-bit unsigned + C = va_arg(ap, unsigned char*); + *C = *buf++; + break; + + case 'h': // 16-bit + h = va_arg(ap, int*); + *h = unpacki16(buf); + buf += 2; + break; + + case 'H': // 16-bit unsigned + H = va_arg(ap, unsigned int*); + *H = unpacku16(buf); + buf += 2; + break; + + case 'l': // 32-bit + l = va_arg(ap, long int*); + *l = unpacki32(buf); + buf += 4; + break; + + case 'L': // 32-bit unsigned + L = va_arg(ap, unsigned long int*); + *L = unpacku32(buf); + buf += 4; + break; + + case 'q': // 64-bit + q = va_arg(ap, long long int*); + *q = unpacki64(buf); + buf += 8; + break; + + case 'Q': // 64-bit unsigned + Q = va_arg(ap, unsigned long long int*); + *Q = unpacku64(buf); + buf += 8; + break; + + case 'f': // float + f = va_arg(ap, float*); + fhold = unpacku16(buf); + *f = unpack754_16(fhold); + buf += 2; + break; + + case 'd': // float-32 + d = va_arg(ap, double*); + fhold = unpacku32(buf); + *d = unpack754_32(fhold); + buf += 4; + break; + + case 'g': // float-64 + g = va_arg(ap, long double*); + fhold = unpacku64(buf); + *g = unpack754_64(fhold); + buf += 8; + break; + + case 's': // string + s = va_arg(ap, char*); + len = unpacku16(buf); + buf += 2; + if (maxstrlen > 0 && len > maxstrlen) + count = maxstrlen - 1; + else + count = len; + memcpy(s, buf, count); + s[count] = '\0'; + buf += len; + break; + + default: + if (isdigit(*format)) + { // track max str len + maxstrlen = maxstrlen * 10 + (*format - '0'); + } + } + + if (!isdigit(*format)) + maxstrlen = 0; + } + + va_end(ap); +} \ No newline at end of file diff --git a/src/rpack.h b/src/rpack.h new file mode 100644 index 000000000..f38e53e66 --- /dev/null +++ b/src/rpack.h @@ -0,0 +1,221 @@ +#include +#include +#include +#include + +// macros for packing floats and doubles: +#define pack754_16(f) (pack754((f), 16, 5)) +#define pack754_32(f) (pack754((f), 32, 8)) +#define pack754_64(f) (pack754((f), 64, 11)) +#define unpack754_16(i) (unpack754((i), 16, 5)) +#define unpack754_32(i) (unpack754((i), 32, 8)) +#define unpack754_64(i) (unpack754((i), 64, 11)) + +/* +** pack754() -- pack a floating point number into IEEE-754 format +*/ +unsigned long long int pack754(long double f, unsigned bits, unsigned expbits) +{ + long double fnorm; + int shift; + long long sign, exp, significand; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit + + if (f == 0.0) + return 0; // get this special case out of the way + + // check sign and begin normalization + if (f < 0) + { + sign = 1; + fnorm = -f; + } + else + { + sign = 0; + fnorm = f; + } + + // get the normalized form of f and track the exponent + shift = 0; + while (fnorm >= 2.0) + { + fnorm /= 2.0; + shift++; + } + while (fnorm < 1.0) + { + fnorm *= 2.0; + shift--; + } + fnorm = fnorm - 1.0; + + // calculate the binary form (non-float) of the significand data + significand = fnorm * ((1LL << significandbits) + 0.5f); + + // get the biased exponent + exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias + + // return the final answer + return (sign << (bits - 1)) | (exp << (bits - expbits - 1)) | significand; +} + +/* +** unpack754() -- unpack a floating point number from IEEE-754 format +*/ +long double unpack754(unsigned long long int i, unsigned bits, unsigned expbits) +{ + long double result; + long long shift; + unsigned bias; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit + + if (i == 0) + return 0.0; + + // pull the significand + result = (i & ((1LL << significandbits) - 1)); // mask + result /= (1LL << significandbits); // convert back to float + result += 1.0f; // add the one back on + + // deal with the exponent + bias = (1 << (expbits - 1)) - 1; + shift = ((i >> significandbits) & ((1LL << expbits) - 1)) - bias; + while (shift > 0) + { + result *= 2.0; + shift--; + } + while (shift < 0) + { + result /= 2.0; + shift++; + } + + // sign it + result *= (i >> (bits - 1)) & 1 ? -1.0 : 1.0; + + return result; +} + +/* +** packi16() -- store a 16-bit int into a char buffer (like htons()) +*/ +void packi16(unsigned char* buf, unsigned int i) +{ + *buf++ = i >> 8; + *buf++ = i; +} + +/* +** packi32() -- store a 32-bit int into a char buffer (like htonl()) +*/ +void packi32(unsigned char* buf, unsigned long int i) +{ + *buf++ = i >> 24; + *buf++ = i >> 16; + *buf++ = i >> 8; + *buf++ = i; +} + +/* +** packi64() -- store a 64-bit int into a char buffer (like htonl()) +*/ +void packi64(unsigned char* buf, unsigned long long int i) +{ + *buf++ = i >> 56; + *buf++ = i >> 48; + *buf++ = i >> 40; + *buf++ = i >> 32; + *buf++ = i >> 24; + *buf++ = i >> 16; + *buf++ = i >> 8; + *buf++ = i; +} + +/* +** unpacki16() -- unpack a 16-bit int from a char buffer (like ntohs()) +*/ +int unpacki16(unsigned char* buf) +{ + unsigned int i2 = ((unsigned int) buf[0] << 8) | buf[1]; + int i; + + // change unsigned numbers to signed + if (i2 <= 0x7fffu) + { + i = i2; + } + else + { + i = -1 - (unsigned int) (0xffffu - i2); + } + + return i; +} + +/* +** unpacku16() -- unpack a 16-bit unsigned from a char buffer (like ntohs()) +*/ +unsigned int unpacku16(unsigned char* buf) +{ + return ((unsigned int) buf[0] << 8) | buf[1]; +} + +/* +** unpacki32() -- unpack a 32-bit int from a char buffer (like ntohl()) +*/ +long int unpacki32(unsigned char* buf) +{ + unsigned long int i2 = ((unsigned long int) buf[0] << 24) | ((unsigned long int) buf[1] << 16) | ((unsigned long int) buf[2] << 8) | buf[3]; + long int i; + + // change unsigned numbers to signed + if (i2 <= 0x7fffffffu) + { + i = i2; + } + else + { + i = -1 - (long int) (0xffffffffu - i2); + } + + return i; +} + +/* +** unpacku32() -- unpack a 32-bit unsigned from a char buffer (like ntohl()) +*/ +unsigned long int unpacku32(unsigned char* buf) +{ + return ((unsigned long int) buf[0] << 24) | ((unsigned long int) buf[1] << 16) | ((unsigned long int) buf[2] << 8) | buf[3]; +} + +/* +** unpacki64() -- unpack a 64-bit int from a char buffer (like ntohl()) +*/ +long long int unpacki64(unsigned char* buf) +{ + unsigned long long int i2 = ((unsigned long long int) buf[0] << 56) | ((unsigned long long int) buf[1] << 48) | ((unsigned long long int) buf[2] << 40) | ((unsigned long long int) buf[3] << 32) | ((unsigned long long int) buf[4] << 24) | ((unsigned long long int) buf[5] << 16) | ((unsigned long long int) buf[6] << 8) | buf[7]; + long long int i; + + // change unsigned numbers to signed + if (i2 <= 0x7fffffffffffffffu) + { + i = i2; + } + else + { + i = -1 - (long long int) (0xffffffffffffffffu - i2); + } + + return i; +} + +/* +** unpacku64() -- unpack a 64-bit unsigned from a char buffer (like ntohl()) +*/ +unsigned long long int unpacku64(unsigned char* buf) +{ + return ((unsigned long long int) buf[0] << 56) | ((unsigned long long int) buf[1] << 48) | ((unsigned long long int) buf[2] << 40) | ((unsigned long long int) buf[3] << 32) | ((unsigned long long int) buf[4] << 24) | ((unsigned long long int) buf[5] << 16) | ((unsigned long long int) buf[6] << 8) | buf[7]; +} \ No newline at end of file diff --git a/src/sysnet.h b/src/sysnet.h index 3dcf18f55..aa73673a1 100644 --- a/src/sysnet.h +++ b/src/sysnet.h @@ -3,100 +3,99 @@ //---------------------------------------------------------------------------------- // If defined, the following flags inhibit definition of the indicated items. -#define NOGDICAPMASKS // CC_*, LC_*, PC_*, CP_*, TC_*, RC_ +#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 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 defines //---------------------------------------------------------------------------------- - -#define PLATFORM_WINDOWS 1 + +#define PLATFORM_WINDOWS 1 #define PLATFORM_UNIX 2 - + #if defined(__WIN32__) || defined(WIN32) -#define PLATFORM PLATFORM_WINDOWS +# define PLATFORM PLATFORM_WINDOWS #else -#define PLATFORM PLATFORM_UNIX +# define PLATFORM PLATFORM_UNIX #endif //---------------------------------------------------------------------------------- // Platform specific network includes //---------------------------------------------------------------------------------- - -#if PLATFORM_WINDOWS -#define __USE_W32_SOCKETS -#pragma comment(lib, "ws2_32.lib") -#include -#include -#include -#else /* UNIX */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif /* WIN32 */ - -/* System-dependent definitions */ -#ifndef __USE_W32_SOCKETS -#define closesocket close -#define SOCKET int -#define INVALID_SOCKET -1 -#define SOCKET_ERROR -1 + +#if PLATFORM_WINDOWS + #define __USE_W32_SOCKETS + #pragma comment(lib, "ws2_32.lib") + #include + #include + #include +#else /* UNIX */ + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include +#endif /* WIN32 */ + +/* System-dependent definitions */ +#ifndef __USE_W32_SOCKETS +# define closesocket close +# define SOCKET int +# define INVALID_SOCKET -1 +# define SOCKET_ERROR -1 #endif /* __USE_W32_SOCKETS */ - -#ifdef __USE_W32_SOCKETS -#define RNet_GetLastError WSAGetLastError -#define RNet_SetLastError WSASetLastError -#ifndef EINTR -#define EINTR WSAEINTR -#endif -#else -int RNet_GetLastError(void); -void RNet_SetLastError(int err); -#endif \ No newline at end of file + +#ifdef __USE_W32_SOCKETS +# define RNet_GetLastError WSAGetLastError +# define RNet_SetLastError WSASetLastError +# ifndef EINTR +# define EINTR WSAEINTR +# endif +#else +int RNet_GetLastError(void); +void RNet_SetLastError(int err); +#endif \ No newline at end of file