-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendPoint.cpp
More file actions
270 lines (197 loc) · 7.57 KB
/
endPoint.cpp
File metadata and controls
270 lines (197 loc) · 7.57 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
/*
* File: endPoint.cpp
* Author: georgez
*
* Created on 22 October 2014, 16:15
*/
#include "endPoint.h"
#include "iasErrors.h"
#include "iasResponse.h"
#include "task_impl.h"
#include "task.h"
#include "syncController.h"
#include "transAct.h"
auto defaultIACallback=std::make_shared<defaultCB>();
endPoint::endPoint() :
_socket(_io_service),
_sync(this)
{
buffer_size=1028;
_is_async=false;
_is_connected=false;
_pResponse=new iasResponse(this);
_pCallback=(iasCallback*)defaultIACallback.get();
_pHead=(PACKETHEAD_PTR)&_data;
_transToken=1;
_serviceid=0;
}
endPoint::~endPoint() {
delete _pResponse;
}
bool endPoint::connect(std::string host,
std::string port,
iasCallback* cb){
bool result=true;
_host=host;
_port=port;
boost::system::error_code error = boost::asio::error::host_not_found;
try{
tcp::resolver resolver(_io_service);
tcp::resolver::query query(host,port);
tcp::resolver::iterator endpoint_iter = resolver.resolve(query);
tcp::resolver::iterator end;
while(error && endpoint_iter != end){
_socket.close();
_socket.connect(*endpoint_iter++,error);
}
}catch(std::exception& e){
result=false;
_error=e.what();
}catch(...){
// catch all
result=false;
_error="Unknown";
}
if(error){
_error=error.message();
}
if(cb && !error){
_pCallback=cb;
std::thread t([this](){
// Listen for responses & request from remote endpoint
// On separate thread
do_read();
if(!_is_async){
_is_async=true;
// Thread will block here in this separate thread
_io_service.run();
_is_async=false;
}
});
t.detach();
}
return result;
}
bool endPoint::connect_service(uint servType,std::string service,std::string auth){
task data;
std::strcpy(data.getBuffer(),service.c_str());
data.getImpl()->setHeader(IAS_PACKETTYPE_REQUEST,servType,service.length());
auto pHead=data.getImpl()->getHeader();
// move data into task buffer
std::memcpy(data.getBuffer(),service.c_str(),service.length());
transAct trans(&_sync,&data);
// write task
boost::asio::write(_socket,boost::asio::buffer(pHead,pHead->datasize+sizeof(PACKETHEAD)));
// wait for response or timeout
_sync.awaitCompletion();
_serviceid=(uint32_t)pHead->sid;
// success / erro in result flag
return pHead->result == 0;
}
uint endPoint::write(std::string s){
return write(s.c_str(),s.length());
}
uint endPoint::write(const char* data,size_t length){
task t;
// move data into task buffer
std::memcpy(t.getBuffer(),data,length);
// get task header
auto pHead=t.getImpl()->getHeader();
// setup header
pHead->length=(uint32_t)length+sizeof(PACKETHEAD);
pHead->datasize=(uint32_t)length;
pHead->sid=_serviceid;
pHead->resp=0;
boost::asio::write(_socket,boost::asio::buffer(pHead,pHead->length));
return length;
}
uint endPoint::writeAwaitResponse(char* data,size_t* plength){
task t;
size_t length=*plength;
// move data into task buffer
std::memcpy(t.getBuffer(),data,length);
// get task header
auto pHead=t.getImpl()->getHeader();
// setup header
pHead->length=(uint32_t)length+sizeof(PACKETHEAD);
pHead->datasize=(uint32_t)length;
pHead->sid=_serviceid;
// indicate response required
pHead->resp=1;
//Create the transAct object, this manages timeouts
transAct trans(&_sync,&t);
// write task
boost::asio::write(_socket,boost::asio::buffer(pHead,pHead->datasize+sizeof(PACKETHEAD)));
// wait for response or timeout
_sync.awaitCompletion();
// BUGBUG: No Check on Source buffersize
std::memcpy(data,t.getBuffer(),pHead->datasize);
data[pHead->datasize]=0;
*plength=pHead->datasize;
if(!_serviceid && pHead->sid){
_serviceid=pHead->sid;
}
return true;
}
void endPoint::do_read(){
// Clean buffer before next read
std::memset((void*)&_data,0,1028 + sizeof(PACKETHEAD));
// Wait for input
_socket.async_read_some(boost::asio::buffer(_pHead, 1028),
[this](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
std::cout << "DO Read: Data Read " << length << " bytes" << std::endl;
if(_pCallback)
{
std::cout << "DO Read:Callback is good" << std::endl;
// check if a response
if(_pHead->resp){
std::cout << "DO Read:Response Signalled" << std::endl;
{
std::lock_guard<std::mutex> lock(_sync._mx);
auto t=syncController::transMap.find(_pHead->id);
if(t!= syncController::transMap.end()){
auto pTrans=t->second;
task* pTask=pTrans->getTask();
auto pImpl=pTask->getImpl();
std::memcpy(pTask->getImpl()->getHeader(),_pHead,length);
_sync.notifyCompletion(IAS_RESPONSE_SUCCESS,pTrans);
// remove the transaction
_sync.transMap.erase(t);
}else{
//std::cout << "DO Read:Transaction Not Found - timed Out" << std::endl;
}
}
}else{
std::cout << "DO Read:No Response Signalled - Post Data" << std::endl;
// onResponse signals data to handle
//auto cb=_callback.lock();
try{
_pCallback->onData(_pResponse,_pHead->buffer);
}catch(...){
// The callback must never fail
_pCallback->onError(MAKE_ERROR_STR("Exception in onData Handler"));
}
}
}else{
//std::cout << "DO Read:BAD CALLBACK" << std::endl;
// find a way to report this failure
assert(true);
}
// wait for the next data
do_read();
}else{
std::cout << ec.message() << std::endl;
}
});
}
uint32_t endPoint::getTransactionToken(){
std::lock_guard<std::mutex> lock(_tokenMutex);
return _currentToken=_transToken++;
}
void endPoint::resetTransactionToken(){
std::lock_guard<std::mutex> lock(_tokenMutex);
_currentToken=0;
}