-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.cpp
289 lines (261 loc) · 6.77 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
/*
* Connection.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "Connection.h"
#include "../registerTypes/RegisterTypes.h"
#include "../mapping/CMap.h"
#include "../CGameState.h"
#if BOOST_VERSION >= 106600
#define BOOST_ASIO_ENABLE_OLD_SERVICES
#endif
#include <boost/asio.hpp>
using namespace boost;
using namespace boost::asio::ip;
#if defined(__hppa__) || \
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
(defined(__MIPS__) && defined(__MISPEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
defined(__sparc__)
#define BIG_ENDIAN
#else
#define LIL_ENDIAN
#endif
void CConnection::init()
{
boost::asio::ip::tcp::no_delay option(true);
socket->set_option(option);
enableSmartPointerSerialization();
disableStackSendingByID();
registerTypes(iser);
registerTypes(oser);
#ifdef LIL_ENDIAN
myEndianess = true;
#else
myEndianess = false;
#endif
connected = true;
std::string pom;
//we got connection
oser & std::string("Aiya!\n") & name & myEndianess; //identify ourselves
iser & pom & pom & contactEndianess;
logNetwork->info("Established connection with %s", pom);
wmx = new boost::mutex();
rmx = new boost::mutex();
handler = nullptr;
receivedStop = sendStop = false;
static int cid = 1;
connectionID = cid++;
iser.fileVersion = SERIALIZATION_VERSION;
}
CConnection::CConnection(std::string host, ui16 port, std::string Name)
:iser(this), oser(this), io_service(new asio::io_service), name(Name)
{
int i;
boost::system::error_code error = asio::error::host_not_found;
socket = new tcp::socket(*io_service);
tcp::resolver resolver(*io_service);
tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host, std::to_string(port)),error);
if(error)
{
logNetwork->error("Problem with resolving: \n%s", error.message());
goto connerror1;
}
pom = endpoint_iterator;
if(pom != end)
logNetwork->info("Found endpoints:");
else
{
logNetwork->error("Critical problem: No endpoints found!");
goto connerror1;
}
i=0;
while(pom != end)
{
logNetwork->info("\t%d:%s", i, (boost::asio::ip::tcp::endpoint&)*pom);
pom++;
}
i=0;
while(endpoint_iterator != end)
{
logNetwork->info("Trying connection to %s(%d)", (boost::asio::ip::tcp::endpoint&)*endpoint_iterator, i++);
socket->connect(*endpoint_iterator, error);
if(!error)
{
init();
return;
}
else
{
logNetwork->error("Problem with connecting: %s", error.message());
}
endpoint_iterator++;
}
//we shouldn't be here - error handling
connerror1:
logNetwork->error("Something went wrong... checking for error info");
if(error)
logNetwork->error(error.message());
else
logNetwork->error("No error info. ");
delete io_service;
//delete socket;
throw std::runtime_error("Can't establish connection :(");
}
CConnection::CConnection(TSocket * Socket, std::string Name )
:iser(this), oser(this), socket(Socket),io_service(&Socket->get_io_service()), name(Name)//, send(this), rec(this)
{
init();
}
CConnection::CConnection(TAcceptor * acceptor, boost::asio::io_service *Io_service, std::string Name)
: iser(this), oser(this), name(Name)//, send(this), rec(this)
{
boost::system::error_code error = asio::error::host_not_found;
socket = new tcp::socket(*io_service);
acceptor->accept(*socket,error);
if (error)
{
logNetwork->error("Error on accepting: %s", error.message());
delete socket;
throw std::runtime_error("Can't establish connection :(");
}
init();
}
int CConnection::write(const void * data, unsigned size)
{
try
{
int ret;
ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
return ret;
}
catch(...)
{
//connection has been lost
connected = false;
throw;
}
}
int CConnection::read(void * data, unsigned size)
{
try
{
int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
return ret;
}
catch(...)
{
//connection has been lost
connected = false;
throw;
}
}
CConnection::~CConnection()
{
if(handler)
handler->join();
delete handler;
close();
delete io_service;
delete wmx;
delete rmx;
}
template<class T>
CConnection & CConnection::operator&(const T &t) {
// throw std::exception();
//XXX this is temporaly ? solution to fix gcc (4.3.3, other?) compilation
// problem for more details contact t0@czlug.icis.pcz.pl or impono@gmail.com
// do not remove this exception it shoudnt be called
return *this;
}
void CConnection::close()
{
if(socket)
{
socket->close();
vstd::clear_pointer(socket);
}
}
bool CConnection::isOpen() const
{
return socket && connected;
}
bool CConnection::isHost() const
{
return connectionID == 1;
}
void CConnection::reportState(vstd::CLoggerBase * out)
{
out->debug("CConnection");
if(socket && socket->is_open())
{
out->debug("\tWe have an open and valid socket");
out->debug("\t %d bytes awaiting", socket->available());
}
}
CPack * CConnection::retrievePack()
{
CPack *ret = nullptr;
boost::unique_lock<boost::mutex> lock(*rmx);
logNetwork->trace("Listening... ");
iser & ret;
logNetwork->trace("\treceived server message of type %s", (ret? typeid(*ret).name() : "nullptr"));
return ret;
}
void CConnection::sendPackToServer(const CPack &pack, PlayerColor player, ui32 requestID)
{
boost::unique_lock<boost::mutex> lock(*wmx);
logNetwork->trace("Sending to server a pack of type %s", typeid(pack).name());
oser & player & requestID & &pack; //packs has to be sent as polymorphic pointers!
}
void CConnection::disableStackSendingByID()
{
CSerializer::sendStackInstanceByIds = false;
}
void CConnection::enableStackSendingByID()
{
CSerializer::sendStackInstanceByIds = true;
}
void CConnection::disableSmartPointerSerialization()
{
iser.smartPointerSerialization = oser.smartPointerSerialization = false;
}
void CConnection::enableSmartPointerSerialization()
{
iser.smartPointerSerialization = oser.smartPointerSerialization = true;
}
void CConnection::prepareForSendingHeroes()
{
iser.loadedPointers.clear();
oser.savedPointers.clear();
disableSmartVectorMemberSerialization();
enableSmartPointerSerialization();
disableStackSendingByID();
}
void CConnection::enterPregameConnectionMode()
{
iser.loadedPointers.clear();
oser.savedPointers.clear();
disableSmartVectorMemberSerialization();
disableSmartPointerSerialization();
}
void CConnection::disableSmartVectorMemberSerialization()
{
CSerializer::smartVectorMembersSerialization = false;
}
void CConnection::enableSmartVectorMemberSerializatoin()
{
CSerializer::smartVectorMembersSerialization = true;
}
std::string CConnection::toString() const
{
boost::format fmt("Connection with %s (ID: %d)");
fmt % name % connectionID;
return fmt.str();
}