diff --git a/Src/Network/TCPReceive.cpp b/Src/Network/TCPReceive.cpp new file mode 100644 index 0000000..920cdb4 --- /dev/null +++ b/Src/Network/TCPReceive.cpp @@ -0,0 +1,97 @@ +#include "TCPReceive.h" +#include + +using namespace std::chrono_literals; + +TCPReceive::TCPReceive(int port) : + m_listenSocket(nullptr), + m_receiveSocket(nullptr) +{ + SDLNet_Init(); + + IPaddress ip; + int result = SDLNet_ResolveHost(&ip, nullptr, port); + + if (result == 0) { + m_listenSocket = SDLNet_TCP_Open(&ip); + if (m_listenSocket) { + m_running = true; + m_listenThread = std::thread(&TCPReceive::ListenFunc, this); + } + } +} + +TCPReceive::~TCPReceive() +{ + m_running = false; + + if (m_listenThread.joinable()) { + m_listenThread.join(); + } + + if (m_listenSocket) { + SDLNet_TCP_Close(m_listenSocket); + m_listenSocket = nullptr; + } + + if (m_receiveSocket) { + SDLNet_TCP_Close(m_receiveSocket); + m_receiveSocket = nullptr; + } + + SDLNet_Quit(); +} + +std::vector& TCPReceive::Receive() +{ + if (!m_receiveSocket) { + printf("return here because no socket\n"); + m_recBuffer.clear(); + return m_recBuffer; + } + + int size = 0; + int result = 0; + + result = SDLNet_TCP_Recv(m_receiveSocket, &size, sizeof(int)); + printf("received %i\n", result); + if (result <= 0) { + SDLNet_TCP_Close(m_receiveSocket); + m_receiveSocket = nullptr; + } + + // reserve our space + m_recBuffer.resize(size); + + while (size) { + + result = SDLNet_TCP_Recv(m_receiveSocket, m_recBuffer.data() + (m_recBuffer.size() - size), size); + printf("received %i\n", result); + if (result <= 0) { + SDLNet_TCP_Close(m_receiveSocket); + m_receiveSocket = nullptr; + break; + } + + size -= result; + } + + return m_recBuffer; +} + +void TCPReceive::ListenFunc() +{ + while (m_running) { + + std::this_thread::sleep_for(16ms); + if (m_receiveSocket) continue; + + auto socket = SDLNet_TCP_Accept(m_listenSocket); + + if (socket) { + m_receiveSocket = socket; + printf("accepted a connectio\n"); + } + + } +} diff --git a/Src/Network/TCPReceive.h b/Src/Network/TCPReceive.h new file mode 100644 index 0000000..c8d06a4 --- /dev/null +++ b/Src/Network/TCPReceive.h @@ -0,0 +1,28 @@ +#ifndef _TCPRECEIVE_H_ +#define _TCPRECEIVE_H_ + +#include +#include +#include +#include "SDL_net.h" + +class TCPReceive +{ +public: + TCPReceive(int port); + ~TCPReceive(); + + std::vector& Receive(); + +private: + + void ListenFunc(); + + TCPsocket m_listenSocket; + std::atomic m_receiveSocket; + std::thread m_listenThread; + std::atomic_bool m_running; + std::vector m_recBuffer; +}; + +#endif \ No newline at end of file diff --git a/Src/Network/TCPSend.cpp b/Src/Network/TCPSend.cpp new file mode 100644 index 0000000..8a72b32 --- /dev/null +++ b/Src/Network/TCPSend.cpp @@ -0,0 +1,75 @@ +#include "TCPSend.h" + +static const int RETRY_COUNT = 10; // shrugs + +TCPSend::TCPSend(std::string& ip, int port) : + m_ip(ip), + m_port(port), + m_tryCount(0), + m_socket(nullptr) +{ + SDLNet_Init(); +} + +TCPSend::~TCPSend() +{ + if (m_socket) { + SDLNet_TCP_Close(m_socket); + m_socket = nullptr; + } + + SDLNet_Quit(); // unload lib (winsock dll for windows) +} + +bool TCPSend::Send(const void * data, int length) +{ + printf("trying to send data %i\n", length); + // If we aren't connected make a connection + if (!Connected()) { + Connect(); + printf("trying to connect\n"); + } + + // If we failed bail out + if (!Connected()) { + printf("not connected\n"); + return false; + } + + if (!length) { + return true; // 0 sized packet will blow our connex + } + + int sent = 0; + + sent = SDLNet_TCP_Send(m_socket, &length, sizeof(int)); // pack the length at the start of transmission. + sent = SDLNet_TCP_Send(m_socket, data, length); + + if (sent < length) { + SDLNet_TCP_Close(m_socket); + m_socket = nullptr; + } + + return false; +} + +bool TCPSend::Connected() +{ + return m_socket != 0; +} + +void TCPSend::Connect() +{ + if (m_tryCount >= RETRY_COUNT) { + return; // already tried and failed so bail out. We do this as instances might load diff times + } + + IPaddress ip; + int result = SDLNet_ResolveHost(&ip, m_ip.c_str(), m_port); + + if (result == 0) { + m_socket = SDLNet_TCP_Open(&ip); + } + + m_tryCount++; +} diff --git a/Src/Network/TCPSend.h b/Src/Network/TCPSend.h new file mode 100644 index 0000000..e7ac0da --- /dev/null +++ b/Src/Network/TCPSend.h @@ -0,0 +1,27 @@ +#ifndef _TCPSEND_H_ +#define _TCPSEND_H_ + +#include +#include "SDL_net.h" + +class TCPSend +{ +public: + TCPSend(std::string& ip, int port); + ~TCPSend(); + + bool Send(const void* data, int length); + bool Connected(); +private: + + void Connect(); + + std::string m_ip; + int m_port; + bool m_connected; + int m_tryCount; + TCPsocket m_socket; // sdl socket + +}; + +#endif diff --git a/VS2008/SDLnet/SDL_net.vcxproj b/VS2008/SDLnet/SDL_net.vcxproj new file mode 100644 index 0000000..6b1300d --- /dev/null +++ b/VS2008/SDLnet/SDL_net.vcxproj @@ -0,0 +1,240 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + SDL2_net + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4} + SDL_net + 8.1 + + + + DynamicLibrary + v141 + + + DynamicLibrary + v141 + + + DynamicLibrary + v141 + + + DynamicLibrary + v141 + + + + + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.40219.1 + $(SolutionDir)\$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration)\ + $(SolutionDir)\$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration)\ + $(SolutionDir)\$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration)\ + $(SolutionDir)\$(Platform)\$(Configuration)\ + $(Platform)\$(Configuration)\ + AllRules.ruleset + + + AllRules.ruleset + + + AllRules.ruleset + + + AllRules.ruleset + + + + + + _DEBUG;%(PreprocessorDefinitions) + true + true + Win32 + .\Debug/SDL2_net.tlb + + + + + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + OldStyle + StreamingSIMDExtensions + ..\..\Libraries\SDL2-2.0.12\include;%(AdditionalIncludeDirectories) + + + _DEBUG;%(PreprocessorDefinitions) + + + wsock32.lib;iphlpapi.lib;%(AdditionalDependencies) + true + Windows + + + + + _DEBUG;%(PreprocessorDefinitions) + true + true + X64 + .\Debug/SDL2_net.tlb + + + + + Disabled + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + OldStyle + StreamingSIMDExtensions + ..\..\Libraries\SDL2-2.0.12\include;%(AdditionalIncludeDirectories) + + + _DEBUG;%(PreprocessorDefinitions) + + + ws2_32.lib;iphlpapi.lib;%(AdditionalDependencies) + true + Windows + + + + + NDEBUG;%(PreprocessorDefinitions) + true + true + Win32 + .\Release/SDL2_net.tlb + + + + + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + StreamingSIMDExtensions + ..\..\Libraries\SDL2-2.0.12\include;%(AdditionalIncludeDirectories) + + + NDEBUG;%(PreprocessorDefinitions) + + + wsock32.lib;iphlpapi.lib;%(AdditionalDependencies) + Windows + + + + + + NDEBUG;%(PreprocessorDefinitions) + true + true + X64 + .\Release/SDL2_net.tlb + + + + + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + StreamingSIMDExtensions + ..\..\Libraries\SDL2-2.0.12\include;%(AdditionalIncludeDirectories) + + + NDEBUG;%(PreprocessorDefinitions) + + + ws2_32.lib;iphlpapi.lib;%(AdditionalDependencies) + Windows + + + + + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + + + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + + + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + + + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + + + + + + + + + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + %(PreprocessorDefinitions) + + + + + {81ce8daf-ebb2-4761-8e45-b71abcca8c68} + + + + + + \ No newline at end of file diff --git a/VS2008/Supermodel.sln b/VS2008/Supermodel.sln index 079d0ea..cf9b307 100755 --- a/VS2008/Supermodel.sln +++ b/VS2008/Supermodel.sln @@ -5,6 +5,7 @@ VisualStudioVersion = 15.0.28307.1082 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Supermodel", "Supermodel.vcxproj", "{B114BBD9-8AEA-4DAE-B367-A66A804CB3DD}" ProjectSection(ProjectDependencies) = postProject + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4} = {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4} {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68} = {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68} {DA956FD3-E142-46F2-9DD5-C78BEBB56B7A} = {DA956FD3-E142-46F2-9DD5-C78BEBB56B7A} EndProjectSection @@ -17,6 +18,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL2main", "SDLmain\SDLmain EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL2", "SDL\SDL.vcxproj", "{81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SDL2_net", "SDLnet\SDL_net.vcxproj", "{8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}" + ProjectSection(ProjectDependencies) = postProject + {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68} = {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -65,6 +71,14 @@ Global {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68}.Release|Win32.Build.0 = Release|Win32 {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68}.Release|x64.ActiveCfg = Release|x64 {81CE8DAF-EBB2-4761-8E45-B71ABCCA8C68}.Release|x64.Build.0 = Release|x64 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Debug|Win32.ActiveCfg = Debug|Win32 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Debug|Win32.Build.0 = Debug|Win32 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Debug|x64.ActiveCfg = Debug|x64 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Debug|x64.Build.0 = Debug|x64 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|Win32.ActiveCfg = Release|Win32 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|Win32.Build.0 = Release|Win32 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|x64.ActiveCfg = Release|x64 + {8AB3504F-5E58-4910-AFE8-7A1E595AC3F4}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VS2008/Supermodel.vcxproj b/VS2008/Supermodel.vcxproj index 133849a..c39be3e 100644 --- a/VS2008/Supermodel.vcxproj +++ b/VS2008/Supermodel.vcxproj @@ -93,8 +93,8 @@ Disabled - ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) - SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC + ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;..\Libraries\SDL2_net-2.0.1;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) + SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC;NET_BOARD true EnableFastChecks MultiThreadedDebug @@ -124,8 +124,8 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config" Disabled - ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) - SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC + ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;..\Libraries\SDL2_net-2.0.1;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) + SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC;NET_BOARD true EnableFastChecks MultiThreadedDebug @@ -153,8 +153,8 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config" Full false - ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) - SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC + ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;..\Libraries\SDL2_net-2.0.1;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) + SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC;NET_BOARD MultiThreaded true @@ -186,8 +186,8 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config" Full false - ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) - SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC + ..\Src;..\Src\OSD;..\Src\OSD\SDL;..\Src\OSD\Windows;..\Libraries\zlib-1.2.4;..\Libraries\SDL2-2.0.12\include;..\Libraries\SDL2_net-2.0.1;$(DXSDK_DIR)\Include;%(AdditionalIncludeDirectories) + SUPERMODEL_WIN32;INLINE=static __inline;_MBCS;_CRT_SECURE_NO_WARNINGS;GLEW_STATIC;NET_BOARD Sync MultiThreaded true @@ -221,6 +221,9 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config" {da956fd3-e142-46f2-9dd5-c78bebb56b7a} + + {8ab3504f-5e58-4910-afe8-7a1e595ac3f4} + {81ce8daf-ebb2-4761-8e45-b71abcca8c68} @@ -336,6 +339,8 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config" + + @@ -508,6 +513,8 @@ xcopy /D /Y "$(ProjectDir)..\Config\*" "$(TargetDir)Config" + + diff --git a/VS2008/Supermodel.vcxproj.filters b/VS2008/Supermodel.vcxproj.filters index f8cec54..3daa414 100644 --- a/VS2008/Supermodel.vcxproj.filters +++ b/VS2008/Supermodel.vcxproj.filters @@ -440,6 +440,12 @@ Source Files\Sound\MPEG + + Source Files\Network + + + Source Files\Network + @@ -808,6 +814,12 @@ Header Files\Pkgs + + Source Files\Network + + + Source Files\Network +