forked from LiXizhi/TMInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.cpp
More file actions
290 lines (270 loc) · 9.73 KB
/
Copy pathconnection.cpp
File metadata and controls
290 lines (270 loc) · 9.73 KB
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
//
// connection.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "TMInterface.h"
#include "connection.hpp"
#include <vector>
#include <boost/bind.hpp>
#include "queue.hpp"
#include "ConnectionManager.h"
using namespace boost::asio;
connection::connection(boost::asio::io_service& io_service)
: strand_(io_service),
socket_(io_service),
resolver_(io_service),
_timer(io_service)
{
_IsSuccess = false;
_IsStopConnection = false;
}
connection::~connection()
{
/*boost::mutex::scoped_lock lock_(queue::Instance().m_QueueCountMutex);
queue::Instance().iWorkingSocketCount--;*/
}
boost::asio::ip::tcp::socket& connection::socket()
{
return socket_;
}
void connection::set_callback(const string& callback)
{
_callback = callback;
}
void connection::set_forword(NPLInterface::NPLObjectProxy& forward)
{
_forward = forward;
}
void connection::start(const std::string& server,const std::string& port,const char * sendbuf,int iOutLength,int iProxyFlag,const char* sMsg, int iMsgLength)
{
if(iProxyFlag == PROXY_GETIDFROMEMAIL||iProxyFlag == PROXY_GETEMAILFROMID || iProxyFlag == PROXY_LOGIN)
{
memset(_sMsg,0,FORWARD_MSG_LENGTH);
_nMsgLength = iMsgLength;
if(iMsgLength >= (FORWARD_MSG_LENGTH-1))
{
fprintf(TMService::Instance().fp,"%s|msg too long:%s!\n",TMService::Instance().GetLogFormatTime(),sMsg);
fflush(TMService::Instance().fp);
}
memcpy(_sMsg,sMsg,iMsgLength);
//fprintf(TMService::Instance().fp,"star:len:%d,msg:%s\n",_nMsgLength,_sMsg);
}
tabMsg = NPLInterface::NPLHelper::MsgStringToNPLTable(sMsg);
NPLInterface::NPLObjectProxy forward = tabMsg["forward"];
_forward = forward;
_iProxyFlag = iProxyFlag;
_iOutLength = iOutLength;
_iInLength = 4096;
all_transferred_bytes=0;
memset(_recvbuf,0,4096);
memset(_sendbuf,0,4096);
memcpy(_sendbuf,sendbuf,iOutLength);
boost::asio::ip::tcp::resolver::query query(server, port);
resolver_.async_resolve(query,
boost::bind(&connection::handle_resolve, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
//fprintf(TMService::Instance().fp,"starting!\n");
_timer.expires_from_now(boost::posix_time::seconds(TIME_OUT_SECOND));
_timer.async_wait(boost::bind(&connection::handle_timeout, shared_from_this()));
}
void connection::handle_resolve(const boost::system::error_code& err,boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
if (!err)
{
// Attempt a connection to the first endpoint in the list. Each endpoint
// will be tried until we successfully establish a connection.
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&connection::handle_connect, shared_from_this(),
boost::asio::placeholders::error, ++endpoint_iterator));
//fprintf(TMService::Instance().fp,"connecting!\n");
}
else
{
fprintf(TMService::Instance().fp,"%s|query err!%s\n",TMService::Instance().GetLogFormatTime(),err.message().c_str());
stop_connection();
}
}
void connection::handle_connect(const boost::system::error_code& err,boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{
if (!err)
{
// The connection was successful. Send the request.
boost::asio::async_write(socket_, boost::asio::buffer(_sendbuf, _iOutLength),
boost::bind(&connection::handle_write_request, shared_from_this(),
boost::asio::placeholders::error));
//fprintf(TMService::Instance().fp,"sending datas!\n");
}
else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
{
// The connection failed. Try the next endpoint in the list.
socket_.close();
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&connection::handle_connect, shared_from_this(),
boost::asio::placeholders::error, ++endpoint_iterator));
//fprintf(TMService::Instance().fp,"reconnecting!\n");
}
else
{
fprintf(TMService::Instance().fp,"%s|connecting err!%s\n",TMService::Instance().GetLogFormatTime(),err.message().c_str());
stop_connection();
}
}
void connection::handle_write_request(const boost::system::error_code& err)
{
if (!err)
{
if(_iProxyFlag == PROXY_LOGOUT)
{
fprintf(TMService::Instance().fp,"%s|write end!\n",TMService::Instance().GetLogFormatTime());
fprintf(TMService::Instance().fp,"--------------------\n");
fflush(TMService::Instance().fp);
_IsSuccess = true;
stop_connection();
}
else
{
socket_.async_read_some(boost::asio::buffer(_recvbuf, _iInLength),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
//fprintf(TMService::Instance().fp,"recving datas!\n");
}
}
else
{
fprintf(TMService::Instance().fp,"%s|recving err!%s\n",TMService::Instance().GetLogFormatTime(),err.message().c_str());
stop_connection();
}
}
void connection::handle_read(const boost::system::error_code& e,std::size_t bytes_transferred)
{
if (!e)
{
all_transferred_bytes += bytes_transferred;
int iHeadLength = TMService::Instance().GetHeadLength(_iProxyFlag);
if(iHeadLength < 0)
{
fprintf(TMService::Instance().fp,"%s|Wrong protocal!\n",TMService::Instance().GetLogFormatTime());
}
int iPkgLength = 0;
if(all_transferred_bytes >= iHeadLength)
{
iPkgLength = TMService::Instance().GetMsgLength(_iProxyFlag,_recvbuf, iHeadLength);
}
fprintf(TMService::Instance().fp,"%s|head length:%d,pkg length:%d,recv length:%d,all_length:%d!\n",TMService::Instance().GetLogFormatTime(),iHeadLength,iPkgLength,bytes_transferred,all_transferred_bytes);
if (iPkgLength >= all_transferred_bytes)
{
//handle response
//fprintf(TMService::Instance().fp,"%s|recv length:%d\n",TMService::Instance().GetLogFormatTime(),bytes_transferred);
if(_iProxyFlag == PROXY_LOGIN)
{
TMService::Instance().DecodeLogin(_recvbuf,all_transferred_bytes, _sMsg,_nMsgLength,tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_REGISTER)
{
TMService::Instance().DecodeRegist(_recvbuf,all_transferred_bytes, tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_SETGAMEFLAG)
{
TMService::Instance().DecodeSetGameFlag(_recvbuf,all_transferred_bytes);
}
else if(_iProxyFlag == PROXY_POSTMSG)
{
TMService::Instance().DecodePostMsg(_recvbuf,all_transferred_bytes, tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_GETIDFROMEMAIL)
{
//fprintf(TMService::Instance().fp,"call decode:len:%d,msg:%s\n",_nMsgLength,_sMsg);
TMService::Instance().DecodeGetID(_recvbuf,all_transferred_bytes,_sMsg,_nMsgLength,tabMsg["params"]["passwd"],tabMsg["params"]["ip"],tabMsg["params"]["vfysession"],tabMsg["params"]["vfycode"],tabMsg["callback"],_forward,tabMsg["params"]["v"]);
}
else if(_iProxyFlag == PROXY_GETEMAILFROMID)
{
TMService::Instance().DecodeGetEmailByID(_recvbuf,all_transferred_bytes,_sMsg,_nMsgLength,tabMsg["params"]["loginflag"],tabMsg["params"]["sessionid"],tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_PAY)
{
TMService::Instance().DecodeBuyProduct(_recvbuf,all_transferred_bytes, tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_QUERY_MAGICWORD)
{
TMService::Instance().DecodeQueryMagicWord(_recvbuf,all_transferred_bytes, tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_CONSUME_MAGICWORD)
{
TMService::Instance().DecodeConsumeMagicWord(_recvbuf,all_transferred_bytes, tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_VFYIMG)
{
TMService::Instance().DecodeGetVfyImg(_recvbuf,all_transferred_bytes,tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_VFYIMG_SESSION)
{
TMService::Instance().DecodeGetVfyImg(_recvbuf,all_transferred_bytes,tabMsg["callback"],_forward);
}
else if(_iProxyFlag == PROXY_USERINFO)
{
TMService::Instance().DecodeGetUserInfo(_recvbuf,all_transferred_bytes,tabMsg["callback"],_forward);
}
_IsSuccess = true;
stop_connection();
}
else if (iPkgLength < 0)
{
//log err
stop_connection();
//boost::system::error_code ignored_ec;
//socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
//std::cout << "Error: " << "exit!" << "\n";
}
else
{
fprintf(TMService::Instance().fp,"%s|need read again!\n",TMService::Instance().GetLogFormatTime());
socket_.async_read_some(boost::asio::buffer(_recvbuf+all_transferred_bytes, _iInLength-all_transferred_bytes),
strand_.wrap(
boost::bind(&connection::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
}
// If an error occurs then no new asynchronous operations are started. This
// means that all shared_ptr references to the connection object will
// disappear and the object will be destroyed automatically after this
// handler returns. The connection class's destructor closes the socket.
}
void connection::stop_connection()
{
if(!_IsStopConnection)
{
_IsStopConnection = true;
CConnectionManager::GetSingleton().stop(shared_from_this());
}
}
void connection::stop()
{
_timer.cancel();
// Post a call to the stop function so that stop() is safe to call from any thread.
socket_.get_io_service().post(boost::bind(&connection::handle_stop, shared_from_this()));
}
void connection::handle_stop()
{
boost::system::error_code ignored_ec;
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
}
void connection::handle_timeout()
{
if(!_IsSuccess)
{
fprintf(TMService::Instance().fp,"%s|time out recv!\n",TMService::Instance().GetLogFormatTime());
fflush(TMService::Instance().fp);
}
stop_connection();
}