-
Notifications
You must be signed in to change notification settings - Fork 293
/
Listener.hpp
116 lines (94 loc) · 3.64 KB
/
Listener.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __LISTENER_HPP_
#define __LISTENER_HPP_
#include <memory>
#include <thread>
#include <vector>
#include <boost/asio.hpp>
#include "NetworkThread.hpp"
namespace MaNGOS
{
template <typename SocketType>
class Listener
{
private:
boost::asio::io_service m_service;
boost::asio::ip::tcp::acceptor m_acceptor;
std::thread m_acceptorThread;
std::vector<std::unique_ptr<NetworkThread<SocketType>>> m_workerThreads;
// the time in milliseconds to sleep a worker thread at the end of each tick
const int SleepInterval = 100;
NetworkThread<SocketType> *SelectWorker() const
{
int minIndex = 0;
size_t minSize = m_workerThreads[minIndex]->Size();
for (size_t i = 1; i < m_workerThreads.size(); ++i)
{
const size_t size = m_workerThreads[i]->Size();
if (size < minSize)
{
minSize = size;
minIndex = i;
}
}
return m_workerThreads[minIndex].get();
}
void BeginAccept();
void OnAccept(NetworkThread<SocketType> *worker, SocketType *socket, const boost::system::error_code &ec);
public:
Listener(int port, int workerThreads);
~Listener();
};
template <typename SocketType>
Listener<SocketType>::Listener(int port, int workerThreads)
: m_acceptor(m_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{
m_workerThreads.reserve(workerThreads);
for (int i = 0; i < workerThreads; ++i)
m_workerThreads.push_back(std::unique_ptr<NetworkThread<SocketType>>(new NetworkThread<SocketType>));
BeginAccept();
m_acceptorThread = std::thread([this]() { this->m_service.run(); });
}
// FIXME - is this needed?
template <typename SocketType>
Listener<SocketType>::~Listener()
{
m_service.stop();
m_acceptor.close();
m_acceptorThread.join();
}
template <typename SocketType>
void Listener<SocketType>::BeginAccept()
{
NetworkThread<SocketType> *worker = SelectWorker();
SocketType *socket = worker->CreateSocket();
m_acceptor.async_accept(socket->GetAsioSocket(), [this,worker,socket](const boost::system::error_code &ec) { this->OnAccept(worker, socket, ec); });
}
template <typename SocketType>
void Listener<SocketType>::OnAccept(NetworkThread<SocketType> *worker, SocketType *socket, const boost::system::error_code &ec)
{
// an error has occurred
if (ec)
worker->RemoveSocket(socket);
else
socket->Open();
BeginAccept();
}
}
#endif /* !__LISTENER_HPP_ */