-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.cpp
386 lines (346 loc) · 9.37 KB
/
connection.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "connection.h"
#include "protocol.h"
#include "outputmessage.h"
#include "protocolgame.h"
#include "protocollogin.h"
#include "status.h"
#include "tasks.h"
#include "scheduler.h"
#include <boost/bind.hpp>
Connection* ConnectionManager::createConnection(boost::asio::io_service& io_service)
{
OTSYS_THREAD_LOCK_CLASS(m_connectionManagerLock, "");
Connection* connection = new Connection(io_service);
m_connections.push_back(connection);
return connection;
}
void ConnectionManager::releaseConnection(Connection* connection)
{
OTSYS_THREAD_LOCK_CLASS lockClass(m_connectionManagerLock);
std::list<Connection*>::iterator it =
std::find(m_connections.begin(), m_connections.end(), connection);
if(it != m_connections.end())
m_connections.erase(it);
else
std::clog << "Error: [ConnectionManager::releaseConnection] Connection not found" << std::endl;
}
void ConnectionManager::closeAll()
{
OTSYS_THREAD_LOCK_CLASS lockClass(m_connectionManagerLock);
std::list<Connection*>::iterator it = m_connections.begin();
while(it != m_connections.end())
{
boost::system::error_code error;
(*it)->m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
(*it)->m_socket.close(error);
++it;
}
m_connections.clear();
}
//*****************
void Connection::closeConnection()
{
//any thread
OTSYS_THREAD_LOCK_CLASS lockClass(m_connectionLock);
if(m_closeState != CLOSE_STATE_NONE)
return;
m_closeState = CLOSE_STATE_REQUESTED;
Dispatcher::getDispatcher().addTask(
createTask(boost::bind(&Connection::closeConnectionTask, this)));
}
void Connection::closeConnectionTask()
{
//dispatcher thread
OTSYS_THREAD_LOCK(m_connectionLock, "");
if(m_closeState != CLOSE_STATE_REQUESTED)
{
std::clog << "Error: [Connection::closeConnectionTask] m_closeState = " << m_closeState << std::endl;
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return;
}
m_closeState = CLOSE_STATE_CLOSING;
if(m_protocol)
{
Dispatcher::getDispatcher().addTask(
createTask(boost::bind(&Protocol::deleteProtocolTask, m_protocol)));
m_protocol->setConnection(NULL);
m_protocol = NULL;
}
if(!closingConnection())
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
}
void Connection::acceptConnection()
{
// Read size of te first packet
m_pendingRead++;
boost::asio::async_read(m_socket,
boost::asio::buffer(m_msg.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::parseHeader, this, boost::asio::placeholders::error));
}
void Connection::parseHeader(const boost::system::error_code& error)
{
OTSYS_THREAD_LOCK(m_connectionLock, "");
m_pendingRead--;
if(m_closeState == CLOSE_STATE_CLOSING)
{
if(!closingConnection())
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return;
}
int32_t size = m_msg.decodeHeader();
if(!error && size > 0 && size < NETWORKMESSAGE_MAXSIZE - 16)
{
// Read packet content
m_pendingRead++;
m_msg.setMessageLength(size + NetworkMessage::header_length);
boost::asio::async_read(m_socket, boost::asio::buffer(m_msg.getBodyBuffer(), size),
boost::bind(&Connection::parsePacket, this, boost::asio::placeholders::error));
}
else
handleReadError(error);
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
}
void Connection::parsePacket(const boost::system::error_code& error)
{
OTSYS_THREAD_LOCK(m_connectionLock, "");
m_pendingRead--;
if(m_closeState == CLOSE_STATE_CLOSING)
{
if(!closingConnection())
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return;
}
if(!error)
{
// Protocol selection
if(!m_protocol)
{
// Protocol depends on the first byte of the packet
uint8_t protocolId = m_msg.GetByte();
switch(protocolId)
{
case 0x01: // Login server protocol
m_protocol = new ProtocolLogin(this);
break;
case 0x0A: // World server protocol
m_protocol = new ProtocolGame(this);
break;
case 0xFF: // Status protocol
m_protocol = new ProtocolStatus(this);
break;
default:
// No valid protocol
closeConnection();
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return;
break;
}
m_protocol->onRecvFirstMessage(m_msg);
}
else
{
// Send the packet to the current protocol
m_protocol->onRecvMessage(m_msg);
}
// Wait to the next packet
m_pendingRead++;
boost::asio::async_read(m_socket,
boost::asio::buffer(m_msg.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::parseHeader, this, boost::asio::placeholders::error));
}
else
handleReadError(error);
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
}
void Connection::handleReadError(const boost::system::error_code& error)
{
if(error == boost::asio::error::operation_aborted)
{
//Operation aborted because connection will be closed
//Do NOT call closeConnection() from here
}
else if(error == boost::asio::error::eof)
{
//No more to read
closeConnection();
}
else if(error == boost::asio::error::connection_reset ||
error == boost::asio::error::connection_aborted)
{
//Connection closed remotely
closeConnection();
}
else
{
PRINT_ASIO_ERROR("Reading");
closeConnection();
}
m_readError = true;
}
bool Connection::send(OutputMessage* msg)
{
OTSYS_THREAD_LOCK(m_connectionLock, "");
if(m_closeState == CLOSE_STATE_CLOSING || m_writeError)
{
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return false;
}
msg->getProtocol()->onSendMessage(msg);
if(m_pendingWrite == 0)
{
internalSend(msg);
}
else
{
m_outputQueue.push_back(msg);
m_pendingWrite++;
}
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return true;
}
void Connection::internalSend(OutputMessage* msg)
{
m_pendingWrite++;
boost::asio::async_write(m_socket,
boost::asio::buffer(msg->getOutputBuffer(), msg->getMessageLength()),
boost::bind(&Connection::onWriteOperation, this, msg, boost::asio::placeholders::error));
}
uint32_t Connection::getIP() const
{
//Ip is expressed in network byte order
boost::system::error_code error;
const boost::asio::ip::tcp::endpoint endpoint = m_socket.remote_endpoint(error);
if(!error)
return htonl(endpoint.address().to_v4().to_ulong());
else
{
PRINT_ASIO_ERROR("Getting remote ip");
return 0;
}
}
void Connection::onWriteOperation(OutputMessage* msg, const boost::system::error_code& error)
{
OutputMessagePool::getInstance()->releaseMessage(msg, true);
OTSYS_THREAD_LOCK(m_connectionLock, "");
if(!error)
{
if(m_pendingWrite > 0)
{
if(!m_outputQueue.empty())
{
OutputMessage* msg = m_outputQueue.front();
m_outputQueue.pop_front();
m_pendingWrite--;
internalSend(msg);
}
m_pendingWrite--;
}
else
{
std::clog << "Error: [Connection::onWriteOperation] Getting unexpected notification!" << std::endl;
}
}
else
{
m_pendingWrite--;
handleWriteError(error);
}
if(m_closeState == CLOSE_STATE_CLOSING)
{
if(!closingConnection())
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
return;
}
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
}
void Connection::handleWriteError(const boost::system::error_code& error)
{
if(error == boost::asio::error::operation_aborted)
{
//Operation aborted because connection will be closed
//Do NOT call closeConnection() from here
}
else if(error == boost::asio::error::eof)
{
//No more to read
closeConnection();
}
else if(error == boost::asio::error::connection_reset ||
error == boost::asio::error::connection_aborted)
{
//Connection closed remotely
closeConnection();
}
else
{
PRINT_ASIO_ERROR("Writting");
closeConnection();
}
m_writeError = true;
}
bool Connection::closingConnection()
{
//any thread
if(m_pendingWrite == 0 || m_writeError == true)
{
if(!m_socketClosed)
{
boost::system::error_code error;
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if(error)
{
if(error == boost::asio::error::not_connected)
{
//Transport endpoint is not connected.
}
else
PRINT_ASIO_ERROR("Shutdown");
}
m_socket.close(error);
m_socketClosed = true;
if(error)
{
PRINT_ASIO_ERROR("Close");
}
}
if(m_pendingRead == 0)
{
OTSYS_THREAD_UNLOCK(m_connectionLock, "");
Dispatcher::getDispatcher().addTask(
createTask(boost::bind(&Connection::deleteConnectionTask, this)));
return true;
}
}
return false;
}
void Connection::deleteConnectionTask()
{
//dispather thread
while(!m_outputQueue.empty())
{
OutputMessagePool::getInstance()->releaseMessage(m_outputQueue.back(), true);
m_outputQueue.pop_back();
--m_pendingWrite;
}
delete this;
}