├── .gitignore ├── .gitmodules ├── LICENSE ├── NetworkLib.sln ├── NetworkLib ├── Client.cpp ├── Client.h ├── Constants.h ├── Factory.cpp ├── Factory.h ├── IClient.h ├── IServer.h ├── LockedQueue.h ├── Log.h ├── NetworkLib.vcxproj ├── NetworkLib.vcxproj.filters ├── Server.cpp ├── Server.h ├── Statistics.cpp ├── Statistics.h └── targetver.h ├── NetworkLib_UnitTests ├── NetworkLib_UnitTests.vcxproj ├── NetworkLib_UnitTests.vcxproj.filters └── NetworkTests.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | [Rr]eleases/ 14 | x64/ 15 | x86/ 16 | build/ 17 | bld/ 18 | [Bb]in/ 19 | [Oo]bj/ 20 | 21 | # Roslyn cache directories 22 | *.ide/ 23 | 24 | # MSTest test Results 25 | [Tt]est[Rr]esult*/ 26 | [Bb]uild[Ll]og.* 27 | 28 | #NUNIT 29 | *.VisualState.xml 30 | TestResult.xml 31 | 32 | # Build Results of an ATL Project 33 | [Dd]ebugPS/ 34 | [Rr]eleasePS/ 35 | dlldata.c 36 | 37 | *_i.c 38 | *_p.c 39 | *_i.h 40 | *.ilk 41 | *.meta 42 | *.obj 43 | *.pch 44 | *.pdb 45 | *.pgc 46 | *.pgd 47 | *.rsp 48 | *.sbr 49 | *.tlb 50 | *.tli 51 | *.tlh 52 | *.tmp 53 | *.tmp_proj 54 | *.log 55 | *.vspscc 56 | *.vssscc 57 | .builds 58 | *.pidb 59 | *.svclog 60 | *.scc 61 | 62 | # Chutzpah Test files 63 | _Chutzpah* 64 | 65 | # Visual C++ cache files 66 | ipch/ 67 | *.aps 68 | *.ncb 69 | *.opensdf 70 | *.sdf 71 | *.cachefile 72 | 73 | # Visual Studio profiler 74 | *.psess 75 | *.vsp 76 | *.vspx 77 | 78 | # TFS 2012 Local Workspace 79 | $tf/ 80 | 81 | # Guidance Automation Toolkit 82 | *.gpState 83 | 84 | # ReSharper is a .NET coding add-in 85 | _ReSharper*/ 86 | *.[Rr]e[Ss]harper 87 | *.DotSettings.user 88 | 89 | # JustCode is a .NET coding addin-in 90 | .JustCode 91 | 92 | # TeamCity is a build add-in 93 | _TeamCity* 94 | 95 | # DotCover is a Code Coverage Tool 96 | *.dotCover 97 | 98 | # NCrunch 99 | _NCrunch_* 100 | .*crunch*.local.xml 101 | 102 | # MightyMoose 103 | *.mm.* 104 | AutoTest.Net/ 105 | 106 | # Web workbench (sass) 107 | .sass-cache/ 108 | 109 | # Installshield output folder 110 | [Ee]xpress/ 111 | 112 | # DocProject is a documentation generator add-in 113 | DocProject/buildhelp/ 114 | DocProject/Help/*.HxT 115 | DocProject/Help/*.HxC 116 | DocProject/Help/*.hhc 117 | DocProject/Help/*.hhk 118 | DocProject/Help/*.hhp 119 | DocProject/Help/Html2 120 | DocProject/Help/html 121 | 122 | # Click-Once directory 123 | publish/ 124 | 125 | # Publish Web Output 126 | *.[Pp]ublish.xml 127 | *.azurePubxml 128 | # TODO: Comment the next line if you want to checkin your web deploy settings 129 | # but database connection strings (with potential passwords) will be unencrypted 130 | *.pubxml 131 | *.publishproj 132 | 133 | # NuGet Packages 134 | *.nupkg 135 | # The packages folder can be ignored because of Package Restore 136 | **/packages/* 137 | # except build/, which is used as an MSBuild target. 138 | !**/packages/build/ 139 | # If using the old MSBuild-Integrated Package Restore, uncomment this: 140 | #!**/packages/repositories.config 141 | 142 | # Windows Azure Build Output 143 | csx/ 144 | *.build.csdef 145 | 146 | # Windows Store app package directory 147 | AppPackages/ 148 | 149 | # Others 150 | sql/ 151 | *.Cache 152 | ClientBin/ 153 | [Ss]tyle[Cc]op.* 154 | ~$* 155 | *~ 156 | *.dbmdl 157 | *.dbproj.schemaview 158 | *.pfx 159 | *.publishsettings 160 | node_modules/ 161 | 162 | # RIA/Silverlight projects 163 | Generated_Code/ 164 | 165 | # Backup & report files from converting an old project file 166 | # to a newer Visual Studio version. Backup files are not needed, 167 | # because we have git ;-) 168 | _UpgradeReport_Files/ 169 | Backup*/ 170 | UpgradeLog*.XML 171 | UpgradeLog*.htm 172 | 173 | # SQL Server files 174 | *.mdf 175 | *.ldf 176 | 177 | # Business Intelligence projects 178 | *.rdl.data 179 | *.bim.layout 180 | *.bim_*.settings 181 | 182 | # Microsoft Fakes 183 | FakesAssemblies/ 184 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "asio"] 2 | path = asio 3 | url = https://github.com/chriskohlhoff/asio.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 DarkWanderer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /NetworkLib.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NetworkLib", "NetworkLib\NetworkLib.vcxproj", "{E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NetworkLib_UnitTests", "NetworkLib_UnitTests\NetworkLib_UnitTests.vcxproj", "{0F681205-52D6-4C96-9917-58EA39A16481}" 9 | ProjectSection(ProjectDependencies) = postProject 10 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF} = {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF} 11 | EndProjectSection 12 | EndProject 13 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{412C8F68-B5C4-4BAA-BFF1-74705143A4D3}" 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|x64 = Debug|x64 18 | Debug|x86 = Debug|x86 19 | Release|x64 = Release|x64 20 | Release|x86 = Release|x86 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}.Debug|x64.ActiveCfg = Debug|Win32 24 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}.Debug|x86.ActiveCfg = Debug|Win32 25 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}.Debug|x86.Build.0 = Debug|Win32 26 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}.Release|x64.ActiveCfg = Release|Win32 27 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}.Release|x86.ActiveCfg = Release|Win32 28 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF}.Release|x86.Build.0 = Release|Win32 29 | {0F681205-52D6-4C96-9917-58EA39A16481}.Debug|x64.ActiveCfg = Debug|x64 30 | {0F681205-52D6-4C96-9917-58EA39A16481}.Debug|x64.Build.0 = Debug|x64 31 | {0F681205-52D6-4C96-9917-58EA39A16481}.Debug|x86.ActiveCfg = Debug|Win32 32 | {0F681205-52D6-4C96-9917-58EA39A16481}.Debug|x86.Build.0 = Debug|Win32 33 | {0F681205-52D6-4C96-9917-58EA39A16481}.Release|x64.ActiveCfg = Release|x64 34 | {0F681205-52D6-4C96-9917-58EA39A16481}.Release|x64.Build.0 = Release|x64 35 | {0F681205-52D6-4C96-9917-58EA39A16481}.Release|x86.ActiveCfg = Release|Win32 36 | {0F681205-52D6-4C96-9917-58EA39A16481}.Release|x86.Build.0 = Release|Win32 37 | EndGlobalSection 38 | GlobalSection(SolutionProperties) = preSolution 39 | HideSolutionNode = FALSE 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /NetworkLib/Client.cpp: -------------------------------------------------------------------------------- 1 | #include "Client.h" 2 | #include "Log.h" 3 | 4 | namespace NetworkLib { 5 | Client::Client(std::string host, unsigned short server_port, unsigned short local_port) : 6 | socket(io_service, udp::endpoint(udp::v4(), local_port)), 7 | service_thread(&Client::run_service, this) 8 | { 9 | udp::resolver resolver(io_service); 10 | udp::resolver::query query(udp::v4(), host, std::to_string(server_port)); 11 | server_endpoint = *resolver.resolve(query); 12 | Client::Send(""); 13 | } 14 | 15 | Client::~Client() 16 | { 17 | io_service.stop(); 18 | service_thread.join(); 19 | } 20 | 21 | void Client::start_receive() 22 | { 23 | socket.async_receive_from(asio::buffer(recv_buffer), remote_endpoint, 24 | [this](std::error_code ec, std::size_t bytes_recvd){ this->handle_receive(ec, bytes_recvd); }); 25 | } 26 | 27 | void Client::handle_receive(const std::error_code& error, std::size_t bytes_transferred) 28 | { 29 | if (!error) 30 | { 31 | std::string message(recv_buffer.data(), recv_buffer.data() + bytes_transferred); 32 | incomingMessages.push(message); 33 | statistics.RegisterReceivedMessage(bytes_transferred); 34 | } 35 | else 36 | { 37 | Log::Error("Client::handle_receive:", error); 38 | } 39 | 40 | start_receive(); 41 | } 42 | 43 | void Client::Send(const std::string& message) 44 | { 45 | socket.send_to(asio::buffer(message), server_endpoint); 46 | statistics.RegisterSentMessage(message.size()); 47 | } 48 | 49 | bool Client::HasMessages() 50 | { 51 | return !incomingMessages.empty(); 52 | } 53 | 54 | std::string Client::PopMessage() 55 | { 56 | if (incomingMessages.empty()) 57 | throw std::logic_error("No messages to pop"); 58 | return incomingMessages.pop(); 59 | } 60 | 61 | void Client::run_service() 62 | { 63 | start_receive(); 64 | while (!io_service.stopped()) { 65 | try { 66 | io_service.run(); 67 | } 68 | catch (const std::exception& e) { 69 | Log::Warning("Client: network exception: ", e.what()); 70 | } 71 | catch (...) { 72 | Log::Error("Unknown exception in client network thread"); 73 | } 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /NetworkLib/Client.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Constants.h" 3 | #include "Statistics.h" 4 | 5 | #include "LockedQueue.h" 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include "IClient.h" 12 | 13 | 14 | using asio::ip::udp; 15 | 16 | namespace NetworkLib { 17 | class Client : public IClient { 18 | public: 19 | Client(std::string host, unsigned short server_port, unsigned short local_port = 0); 20 | virtual ~Client(); 21 | 22 | void Send(const std::string& message) override; 23 | 24 | bool HasMessages() override;; 25 | 26 | std::string PopMessage() override;; 27 | 28 | private: 29 | // Network send/receive stuff 30 | asio::io_service io_service; 31 | udp::socket socket; 32 | udp::endpoint server_endpoint; 33 | udp::endpoint remote_endpoint; 34 | std::array recv_buffer; 35 | std::thread service_thread; 36 | 37 | // Queues for messages 38 | LockedQueue incomingMessages; 39 | 40 | void start_receive(); 41 | void handle_receive(const std::error_code& error, std::size_t bytes_transferred); 42 | void run_service(); 43 | 44 | Client(Client&); // block default copy constructor 45 | 46 | // Statistics 47 | Statistics statistics; 48 | }; 49 | } 50 | -------------------------------------------------------------------------------- /NetworkLib/Constants.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | static const int NetworkBufferSize = 4096; 3 | -------------------------------------------------------------------------------- /NetworkLib/Factory.cpp: -------------------------------------------------------------------------------- 1 | #include "targetver.h" 2 | #include "Factory.h" 3 | #include "Client.h" 4 | #include "Server.h" 5 | 6 | namespace NetworkLib { 7 | std::unique_ptr Factory::CreateClient(std::string host, unsigned int server_port, unsigned int client_port) 8 | { 9 | auto client = new Client(host, server_port, client_port); 10 | return std::unique_ptr(client); 11 | } 12 | 13 | std::unique_ptr Factory::CreateServer(unsigned int port) 14 | { 15 | auto server = new Server(port); 16 | return std::unique_ptr(server); 17 | } 18 | } -------------------------------------------------------------------------------- /NetworkLib/Factory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "IClient.h" 3 | #include "IServer.h" 4 | #include 5 | #include 6 | 7 | namespace NetworkLib 8 | { 9 | class Factory 10 | { 11 | public: 12 | static std::unique_ptr CreateClient(std::string host, unsigned int server_port, unsigned int client_port); 13 | static std::unique_ptr CreateServer(unsigned int port); 14 | }; 15 | } -------------------------------------------------------------------------------- /NetworkLib/IClient.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace NetworkLib 5 | { 6 | class IClient abstract 7 | { 8 | public: 9 | virtual ~IClient() {}; 10 | virtual bool HasMessages() abstract = 0; 11 | virtual void Send(const std::string& message) abstract = 0; 12 | virtual std::string PopMessage() abstract = 0; 13 | }; 14 | } -------------------------------------------------------------------------------- /NetworkLib/IServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace NetworkLib 6 | { 7 | typedef std::pair ClientMessage; 8 | 9 | class IServer abstract 10 | { 11 | public: 12 | virtual ~IServer() {}; 13 | virtual bool HasMessages() abstract = 0; 14 | virtual void SendToClient(const std::string& message, uint32_t clientID) abstract = 0; 15 | virtual ClientMessage PopMessage() abstract = 0; 16 | virtual size_t GetClientCount() abstract = 0; 17 | virtual uint32_t GetClientIdByIndex(size_t) abstract = 0; 18 | }; 19 | } -------------------------------------------------------------------------------- /NetworkLib/LockedQueue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef NETWORKLIB_LOCKEDQUEUE 3 | #define NETWORKLIB_LOCKEDQUEUE 4 | #include 5 | #include 6 | #include 7 | 8 | namespace NetworkLib { 9 | // Simple mutex-guarded queue 10 | template class LockedQueue 11 | { 12 | private: 13 | std::mutex mutex; 14 | std::queue<_T> queue; 15 | public: 16 | void push(_T value) 17 | { 18 | std::unique_lock lock(mutex); 19 | queue.push(value); 20 | }; 21 | 22 | // Get top message in the queue 23 | // Note: not exception-safe (will lose the message) 24 | _T pop() 25 | { 26 | std::unique_lock lock(mutex); 27 | _T value; 28 | std::swap(value, queue.front()); 29 | queue.pop(); 30 | return value; 31 | }; 32 | 33 | bool empty() { 34 | std::unique_lock lock(mutex); 35 | return queue.empty(); 36 | } 37 | }; 38 | } 39 | #endif -------------------------------------------------------------------------------- /NetworkLib/Log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace Log { 3 | // Empty function prototypes to be replaced with your implementations 4 | template inline void Debug(T... args) { 5 | }; 6 | template inline void Info(T... args) { 7 | }; 8 | template inline void Warning(T... args) { 9 | }; 10 | template inline void Error(T... args) { 11 | }; 12 | } -------------------------------------------------------------------------------- /NetworkLib/NetworkLib.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {E8AAD2D1-6608-4F60-8B8B-5C02A829BCFF} 15 | NetworkLib 16 | 17 | 18 | 19 | StaticLibrary 20 | true 21 | v140 22 | MultiByte 23 | 24 | 25 | StaticLibrary 26 | false 27 | v140 28 | true 29 | MultiByte 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Level3 45 | Disabled 46 | true 47 | $(SolutionDir)asio\asio\include;%(AdditionalIncludeDirectories) 48 | ASIO_STANDALONE;_WIN32_WINNT=0x0601;%(PreprocessorDefinitions) 49 | 50 | 51 | true 52 | $(BOOSTDIR)\stage\lib;%(AdditionalLibraryDirectories) 53 | 54 | 55 | 56 | 57 | Level3 58 | MaxSpeed 59 | true 60 | true 61 | true 62 | $(SolutionDir)asio\asio\include;%(AdditionalIncludeDirectories) 63 | ASIO_STANDALONE;_WIN32_WINNT=0x0601;%(PreprocessorDefinitions) 64 | 65 | 66 | true 67 | true 68 | true 69 | $(BOOSTDIR)\stage\lib;%(AdditionalLibraryDirectories) 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /NetworkLib/NetworkLib.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /NetworkLib/Server.cpp: -------------------------------------------------------------------------------- 1 | #include "Server.h" 2 | #include "Log.h" 3 | 4 | namespace NetworkLib { 5 | Server::Server(unsigned short local_port) : 6 | socket(io_service, udp::endpoint(udp::v4(), local_port)), 7 | service_thread(&Server::run_service, this), 8 | nextClientID(0L) 9 | { 10 | Log::Info("Starting server on port ", local_port); 11 | }; 12 | 13 | Server::~Server() 14 | { 15 | io_service.stop(); 16 | service_thread.join(); 17 | } 18 | 19 | void Server::start_receive() 20 | { 21 | socket.async_receive_from(asio::buffer(recv_buffer), remote_endpoint, 22 | [this](std::error_code ec, std::size_t bytes_recvd){ this->handle_receive(ec, bytes_recvd); }); 23 | } 24 | 25 | void Server::on_client_disconnected(int32_t id) 26 | { 27 | for (auto& handler : clientDisconnectedHandlers) 28 | if (handler) 29 | handler(id); 30 | } 31 | 32 | void Server::handle_remote_error(const std::error_code error_code, const udp::endpoint remote_endpoint) 33 | { 34 | bool found = false; 35 | int32_t id; 36 | for (const auto& client : clients) 37 | if (client.second == remote_endpoint) { 38 | found = true; 39 | id = client.first; 40 | break; 41 | } 42 | if (found == false) 43 | return; 44 | 45 | clients.erase(id); 46 | on_client_disconnected(id); 47 | } 48 | 49 | void Server::handle_receive(const std::error_code& error, std::size_t bytes_transferred) 50 | { 51 | if (!error) 52 | { 53 | try { 54 | auto message = ClientMessage(std::string(recv_buffer.data(), recv_buffer.data() + bytes_transferred), get_or_create_client_id(remote_endpoint)); 55 | if (!message.first.empty()) 56 | incomingMessages.push(message); 57 | statistics.RegisterReceivedMessage(bytes_transferred); 58 | } 59 | catch (std::exception ex) { 60 | Log::Error("handle_receive: Error parsing incoming message:", ex.what()); 61 | } 62 | catch (...) { 63 | Log::Error("handle_receive: Unknown error while parsing incoming message"); 64 | } 65 | } 66 | else 67 | { 68 | Log::Error("handle_receive: error: ", error.message(), " while receiving from address ", remote_endpoint); 69 | handle_remote_error(error, remote_endpoint); 70 | } 71 | 72 | start_receive(); 73 | } 74 | 75 | void Server::send(const std::string& message, udp::endpoint target_endpoint) 76 | { 77 | socket.send_to(asio::buffer(message), target_endpoint); 78 | statistics.RegisterSentMessage(message.size()); 79 | } 80 | 81 | void Server::run_service() 82 | { 83 | start_receive(); 84 | while (!io_service.stopped()){ 85 | try { 86 | io_service.run(); 87 | } 88 | catch (const std::exception& e) { 89 | Log::Error("Server: Network exception: ", e.what()); 90 | } 91 | catch (...) { 92 | Log::Error("Server: Network exception: unknown"); 93 | } 94 | } 95 | Log::Debug("Server network thread stopped"); 96 | }; 97 | 98 | int32_t Server::get_or_create_client_id(udp::endpoint endpoint) 99 | { 100 | for (const auto& client : clients) 101 | if (client.second == endpoint) 102 | return client.first; 103 | 104 | auto id = ++nextClientID; 105 | clients.insert(Client(id, endpoint)); 106 | return id; 107 | }; 108 | 109 | void Server::SendToClient(const std::string& message, uint32_t clientID) 110 | { 111 | try { 112 | send(message, clients.at(clientID)); 113 | } 114 | catch (std::out_of_range) { 115 | Log::Error(__FUNCTION__": Unknown client ID ", clientID); 116 | } 117 | }; 118 | 119 | void Server::SendToAllExcept(const std::string& message, uint32_t clientID) 120 | { 121 | for (auto client : clients) 122 | if (client.first != clientID) 123 | send(message, client.second); 124 | }; 125 | 126 | void Server::SendToAll(const std::string& message) 127 | { 128 | for (auto client : clients) 129 | send(message, client.second); 130 | } 131 | 132 | size_t Server::GetClientCount() 133 | { 134 | return clients.size(); 135 | } 136 | 137 | uint32_t Server::GetClientIdByIndex(size_t index) 138 | { 139 | auto it = clients.begin(); 140 | for (int i = 0; i < index; i++) 141 | ++it; 142 | return it->first; 143 | }; 144 | 145 | ClientMessage Server::PopMessage() { 146 | return incomingMessages.pop(); 147 | } 148 | 149 | bool Server::HasMessages() 150 | { 151 | return !incomingMessages.empty(); 152 | }; 153 | } -------------------------------------------------------------------------------- /NetworkLib/Server.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Constants.h" 3 | #include "Statistics.h" 4 | 5 | #include "LockedQueue.h" 6 | 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "IServer.h" 14 | 15 | using asio::ip::udp; 16 | 17 | typedef std::map ClientList; 18 | typedef ClientList::value_type Client; 19 | 20 | namespace NetworkLib { 21 | class Server : public IServer { 22 | public: 23 | explicit Server(unsigned short local_port); 24 | virtual ~Server(); 25 | 26 | bool HasMessages() override; 27 | ClientMessage PopMessage() override; 28 | 29 | void SendToClient(const std::string& message, uint32_t clientID) override; 30 | void SendToAllExcept(const std::string& message, uint32_t clientID); 31 | void SendToAll(const std::string& message); 32 | 33 | size_t GetClientCount() override; 34 | uint32_t GetClientIdByIndex(size_t index) override; 35 | 36 | const Statistics& GetStatistics() const { return statistics; }; 37 | std::vector> clientDisconnectedHandlers; 38 | private: 39 | // Network send/receive stuff 40 | asio::io_service io_service; 41 | udp::socket socket; 42 | udp::endpoint server_endpoint; 43 | udp::endpoint remote_endpoint; 44 | std::array recv_buffer; 45 | std::thread service_thread; 46 | 47 | // Low-level network functions 48 | void start_receive(); 49 | void handle_remote_error(const std::error_code error_code, const udp::endpoint remote_endpoint); 50 | void handle_receive(const std::error_code& error, std::size_t bytes_transferred); 51 | void handle_send(std::string /*message*/, const std::error_code& /*error*/, std::size_t /*bytes_transferred*/) {} 52 | void run_service(); 53 | 54 | // Client management 55 | int32_t get_or_create_client_id(udp::endpoint endpoint); 56 | void on_client_disconnected(int32_t id); 57 | 58 | void send(const std::string& message, udp::endpoint target); 59 | 60 | // Incoming messages queue 61 | LockedQueue incomingMessages; 62 | 63 | // Clients of the server 64 | ClientList clients; 65 | std::atomic_int32_t nextClientID; 66 | 67 | Server(Server&); // block default copy constructor 68 | 69 | // Statistics 70 | Statistics statistics; 71 | }; 72 | } -------------------------------------------------------------------------------- /NetworkLib/Statistics.cpp: -------------------------------------------------------------------------------- 1 | #include "targetver.h" 2 | #include "Statistics.h" 3 | #include 4 | #include 5 | #include 6 | 7 | namespace NetworkLib { 8 | Statistics::Statistics() : 9 | receivedMessages(0), sentMessages(0), receivedBytes(0), sentBytes(0) 10 | { 11 | } 12 | 13 | Statistics::Statistics(const Statistics& other) : 14 | receivedMessages(other.GetReceivedMessages()), 15 | sentMessages(other.GetSentMessages()), 16 | receivedBytes(other.GetReceivedBytes()), 17 | sentBytes(other.GetSentBytes()) 18 | { 19 | } 20 | 21 | unsigned Statistics::GetReceivedMessages() const 22 | { 23 | return receivedMessages; 24 | } 25 | 26 | uint64_t Statistics::GetReceivedBytes() const 27 | { 28 | return receivedBytes; 29 | } 30 | 31 | unsigned Statistics::GetSentMessages() const 32 | { 33 | return sentMessages; 34 | } 35 | 36 | uint64_t Statistics::GetSentBytes() const 37 | { 38 | return sentBytes; 39 | } 40 | 41 | void Statistics::RegisterSentMessage(int32_t num_bytes) 42 | { 43 | ++sentMessages; 44 | sentBytes.fetch_add(num_bytes); 45 | } 46 | 47 | void Statistics::RegisterReceivedMessage(int32_t messageSize) 48 | { 49 | ++receivedMessages; 50 | receivedBytes.fetch_add(messageSize); 51 | } 52 | 53 | std::string data_size_to_string(uint64_t size) 54 | { 55 | std::array sizeStrings = { "B", "KB", "MB", "GB" }; 56 | for (int i = sizeStrings.size() - 1; i >= 0; i--) 57 | { 58 | auto referenceSize = size_t(1 << i * 10); 59 | if (size < referenceSize) 60 | continue; 61 | auto scaledSize = static_cast(size) / static_cast(referenceSize); 62 | std::ostringstream oss; 63 | oss << std::setprecision(2) << static_cast(scaledSize) << sizeStrings[i]; 64 | return oss.str(); 65 | } 66 | return std::to_string(size) + " bytes"; 67 | } 68 | 69 | std::ostream& operator<<(std::ostream& os, const Statistics& stat) 70 | { 71 | os << "Sent " << stat.GetSentMessages() << " msgs (" 72 | << data_size_to_string(stat.GetSentBytes()) << ") "; 73 | os << "Rcvd " << stat.GetReceivedMessages() << " msgs (" 74 | << data_size_to_string(stat.GetReceivedBytes()) << ")"; 75 | return os; 76 | } 77 | } -------------------------------------------------------------------------------- /NetworkLib/Statistics.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace NetworkLib { 5 | struct Statistics 6 | { 7 | private: 8 | std::atomic_uint32_t receivedMessages; 9 | std::atomic_uint32_t sentMessages; 10 | 11 | std::atomic_uint64_t receivedBytes; 12 | std::atomic_uint64_t sentBytes; 13 | public: 14 | Statistics(); 15 | Statistics(const Statistics& other); 16 | 17 | uint32_t GetReceivedMessages() const; 18 | 19 | uint64_t GetReceivedBytes() const; 20 | 21 | uint32_t GetSentMessages() const; 22 | 23 | uint64_t GetSentBytes() const; 24 | 25 | void RegisterSentMessage(int32_t size); 26 | 27 | void RegisterReceivedMessage(int32_t size); 28 | 29 | }; 30 | 31 | std::ostream& operator<<(std::ostream& os, const Statistics& obj); 32 | } 33 | -------------------------------------------------------------------------------- /NetworkLib/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /NetworkLib_UnitTests/NetworkLib_UnitTests.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {0F681205-52D6-4C96-9917-58EA39A16481} 23 | Win32Proj 24 | NetworkLib_UnitTests 25 | 8.1 26 | 27 | 28 | 29 | DynamicLibrary 30 | true 31 | v140 32 | Unicode 33 | false 34 | 35 | 36 | DynamicLibrary 37 | false 38 | v140 39 | true 40 | Unicode 41 | false 42 | 43 | 44 | DynamicLibrary 45 | true 46 | v140 47 | Unicode 48 | false 49 | 50 | 51 | DynamicLibrary 52 | false 53 | v140 54 | true 55 | Unicode 56 | false 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | true 78 | 79 | 80 | true 81 | 82 | 83 | true 84 | 85 | 86 | true 87 | 88 | 89 | 90 | Level3 91 | Disabled 92 | $(SolutionDir)asio\asio\include;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) 93 | ASIO_STANDALONE;_WIN32_WINNT=0x0601;%(PreprocessorDefinitions) 94 | true 95 | 96 | 97 | Windows 98 | true 99 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 100 | 101 | 102 | 103 | 104 | Use 105 | Level3 106 | Disabled 107 | $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) 108 | _DEBUG;%(PreprocessorDefinitions) 109 | true 110 | 111 | 112 | Windows 113 | true 114 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 115 | 116 | 117 | 118 | 119 | Level3 120 | MaxSpeed 121 | true 122 | true 123 | $(SolutionDir)asio\asio\include;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) 124 | ASIO_STANDALONE;_WIN32_WINNT=0x0601;%(PreprocessorDefinitions) 125 | true 126 | 127 | 128 | Windows 129 | true 130 | true 131 | true 132 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 133 | 134 | 135 | 136 | 137 | Level3 138 | Use 139 | MaxSpeed 140 | true 141 | true 142 | $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) 143 | NDEBUG;%(PreprocessorDefinitions) 144 | true 145 | 146 | 147 | Windows 148 | true 149 | true 150 | true 151 | $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | {e8aad2d1-6608-4f60-8b8b-5c02a829bcff} 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /NetworkLib_UnitTests/NetworkLib_UnitTests.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /NetworkLib_UnitTests/NetworkTests.cpp: -------------------------------------------------------------------------------- 1 | #include "../NetworkLib/Factory.h" 2 | #include "CppUnitTest.h" 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 8 | namespace chrono = std::chrono; 9 | 10 | namespace Multiorb_UnitTests 11 | { 12 | TEST_CLASS(NetworkTests) 13 | { 14 | static std::unique_ptr CreateServer() 15 | { 16 | return NetworkLib::Factory::CreateServer(12345); 17 | }; 18 | 19 | static std::unique_ptr CreateClient() 20 | { 21 | return NetworkLib::Factory::CreateClient("localhost", 12345, 0); 22 | }; 23 | 24 | static void Sleep() 25 | { 26 | std::this_thread::sleep_for(chrono::milliseconds(5)); 27 | } 28 | 29 | public: 30 | TEST_METHOD(ServerConstructorShouldWork) 31 | { 32 | auto server = CreateServer(); 33 | }; 34 | 35 | TEST_METHOD(ServerShouldHaveNoMessagesWhenCreated) 36 | { 37 | auto server = CreateServer(); 38 | Assert::IsFalse(server->HasMessages()); 39 | }; 40 | 41 | TEST_METHOD(ServerShouldHaveZeroClientsWhenCreated) 42 | { 43 | auto server = CreateServer(); 44 | Assert::IsFalse(server->GetClientCount() > 0); 45 | }; 46 | 47 | 48 | TEST_METHOD(ClientConstructorShouldWork) 49 | { 50 | auto client = CreateClient(); 51 | }; 52 | 53 | TEST_METHOD(ClientShouldHaveNoMessagesWhenCreated) 54 | { 55 | auto client = CreateClient(); 56 | Assert::IsFalse(client->HasMessages()); 57 | }; 58 | 59 | TEST_METHOD(ServerShouldCountMultipleClients) 60 | { 61 | auto server = CreateServer(); 62 | std::vector> clients; 63 | for (int i = 0; i < 5; i++) 64 | clients.emplace_back(CreateClient()); 65 | Sleep(); 66 | Assert::IsTrue(server->GetClientCount() == clients.size()); 67 | } 68 | 69 | //TEST_METHOD(ClientShouldDisconnectFromServerWhenDestroyed) 70 | //{ 71 | // auto server = CreateServer(); 72 | // { 73 | // std::vector> clients; 74 | // for (int i = 0; i < 5; i++) 75 | // clients.emplace_back(CreateClient()); 76 | // Sleep(); 77 | // Assert::IsTrue(server->GetClientCount() == clients.size()); 78 | // } 79 | // Sleep(); 80 | // Assert::IsTrue(server->GetClientCount() == 0); 81 | //} 82 | 83 | TEST_METHOD(SendMessageFromClientToServerShouldProduceSameMessage) 84 | { 85 | auto server = CreateServer(); 86 | auto client = CreateClient(); 87 | std::string message("Test message"); 88 | 89 | // Send client->server 90 | client->Send(message); 91 | 92 | Sleep(); 93 | 94 | Assert::IsTrue(server->HasMessages()); 95 | Assert::IsFalse(client->HasMessages()); 96 | 97 | auto receivedMessage = server->PopMessage().first; 98 | Assert::IsTrue(message == receivedMessage); 99 | 100 | Assert::IsFalse(server->HasMessages()); 101 | Assert::IsFalse(client->HasMessages()); 102 | } 103 | 104 | TEST_METHOD(SendMessageFromServerToClientShouldProduceSameMessage) 105 | { 106 | auto server = CreateServer(); 107 | auto client = CreateClient(); 108 | std::string message("Test message"); 109 | 110 | // Sleep for a bit so that server has time to 111 | // receive client announcement message 112 | Sleep(); 113 | 114 | // Send from server to client 115 | // TODO: get client ID from server itself 116 | Assert::IsTrue(server->GetClientCount() == 1); 117 | server->SendToClient(message, server->GetClientIdByIndex(0)); 118 | Sleep(); 119 | 120 | Assert::IsFalse(server->HasMessages()); 121 | 122 | auto receivedMessage = client->PopMessage(); 123 | Assert::IsTrue(message == receivedMessage); 124 | 125 | Assert::IsFalse(server->HasMessages()); 126 | Assert::IsFalse(client->HasMessages()); 127 | } 128 | }; 129 | } 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### NetworkLib ### 2 | Simple thread-safe network client/server built on top of asio library 3 | 4 | Uses std::string for input/output --------------------------------------------------------------------------------