TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SocketMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SocketMgr_h__
19 #define SocketMgr_h__
20 
21 #include "AsyncAcceptor.h"
22 #include "Errors.h"
23 #include "NetworkThread.h"
24 #include <boost/asio/ip/tcp.hpp>
25 #include <memory>
26 
27 using boost::asio::ip::tcp;
28 
29 template<class SocketType>
30 class SocketMgr
31 {
32 public:
33  virtual ~SocketMgr()
34  {
35  ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction");
36  }
37 
38  virtual bool StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port, int threadCount)
39  {
40  ASSERT(threadCount > 0);
41 
42  try
43  {
44  _acceptor = new AsyncAcceptor(service, bindIp, port);
45  }
46  catch (boost::system::system_error const& err)
47  {
48  TC_LOG_ERROR("network", "Exception caught in SocketMgr.StartNetwork (%s:%u): %s", bindIp.c_str(), port, err.what());
49  return false;
50  }
51 
52  _threadCount = threadCount;
54 
56 
57  for (int32 i = 0; i < _threadCount; ++i)
58  _threads[i].Start();
59 
60  return true;
61  }
62 
63  virtual void StopNetwork()
64  {
65  _acceptor->Close();
66 
67  if (_threadCount != 0)
68  for (int32 i = 0; i < _threadCount; ++i)
69  _threads[i].Stop();
70 
71  Wait();
72 
73  delete _acceptor;
74  _acceptor = nullptr;
75  delete[] _threads;
76  _threads = nullptr;
77  _threadCount = 0;
78  }
79 
80  void Wait()
81  {
82  if (_threadCount != 0)
83  for (int32 i = 0; i < _threadCount; ++i)
84  _threads[i].Wait();
85  }
86 
87  virtual void OnSocketOpen(tcp::socket&& sock, uint32 threadIndex)
88  {
89  try
90  {
91  std::shared_ptr<SocketType> newSocket = std::make_shared<SocketType>(std::move(sock));
92  newSocket->Start();
93 
94  _threads[threadIndex].AddSocket(newSocket);
95  }
96  catch (boost::system::system_error const& err)
97  {
98  TC_LOG_WARN("network", "Failed to retrieve client's remote address %s", err.what());
99  }
100  }
101 
103 
105  {
106  uint32 min = 0;
107 
108  for (int32 i = 1; i < _threadCount; ++i)
109  if (_threads[i].GetConnectionCount() < _threads[min].GetConnectionCount())
110  min = i;
111 
112  return min;
113  }
114 
115  std::pair<tcp::socket*, uint32> GetSocketForAccept()
116  {
117  uint32 threadIndex = SelectThreadWithMinConnections();
118  return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex);
119  }
120 
121 protected:
122  SocketMgr() : _acceptor(nullptr), _threads(nullptr), _threadCount(1)
123  {
124  }
125 
126  virtual NetworkThread<SocketType>* CreateThreads() const = 0;
127 
131 };
132 
133 #endif // SocketMgr_h__
void Wait()
Definition: SocketMgr.h:80
int32 _threadCount
Definition: SocketMgr.h:130
virtual ~SocketMgr()
Definition: SocketMgr.h:33
Definition: AsyncAcceptor.h:28
void Close()
Definition: AsyncAcceptor.h:69
virtual bool StartNetwork(boost::asio::io_service &service, std::string const &bindIp, uint16 port, int threadCount)
Definition: SocketMgr.h:38
SocketMgr()
Definition: SocketMgr.h:122
AsyncAcceptor * _acceptor
Definition: SocketMgr.h:128
virtual NetworkThread< SocketType > * CreateThreads() const =0
virtual void OnSocketOpen(tcp::socket &&sock, uint32 threadIndex)
Definition: SocketMgr.h:87
T min(const T &x, const T &y)
Definition: g3dmath.h:305
Definition: NetworkThread.h:37
Definition: SocketMgr.h:30
uint32 SelectThreadWithMinConnections() const
Definition: SocketMgr.h:104
NetworkThread< SocketType > * _threads
Definition: SocketMgr.h:129
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
uint16_t uint16
Definition: Define.h:151
int32 GetNetworkThreadCount() const
Definition: SocketMgr.h:102
virtual void StopNetwork()
Definition: SocketMgr.h:63
std::pair< tcp::socket *, uint32 > GetSocketForAccept()
Definition: SocketMgr.h:115
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
#define ASSERT
Definition: Errors.h:55
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207