-
Notifications
You must be signed in to change notification settings - Fork 303
/
Copy pathxeTcpIpLink.cpp
571 lines (481 loc) · 15.9 KB
/
xeTcpIpLink.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*-------------------------------------------------------------------------
* drawElements Quality Program Test Executor
* ------------------------------------------
*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*//*!
* \file
* \brief Tcp/Ip communication link.
*//*--------------------------------------------------------------------*/
#include "xeTcpIpLink.hpp"
#include "xsProtocol.hpp"
#include "deClock.h"
#include "deInt32.h"
namespace xe
{
enum
{
SEND_BUFFER_BLOCK_SIZE = 1024,
SEND_BUFFER_NUM_BLOCKS = 64
};
// Utilities for writing messages out.
static void writeMessageHeader(de::BlockBuffer<uint8_t> &dst, xs::MessageType type, int messageSize)
{
uint8_t hdr[xs::MESSAGE_HEADER_SIZE];
xs::Message::writeHeader(type, messageSize, &hdr[0], xs::MESSAGE_HEADER_SIZE);
dst.write(xs::MESSAGE_HEADER_SIZE, &hdr[0]);
}
static void writeKeepalive(de::BlockBuffer<uint8_t> &dst)
{
writeMessageHeader(dst, xs::MESSAGETYPE_KEEPALIVE, xs::MESSAGE_HEADER_SIZE);
dst.flush();
}
static void writeExecuteBinary(de::BlockBuffer<uint8_t> &dst, const char *name, const char *params, const char *workDir,
const char *caseList)
{
int nameSize = (int)strlen(name) + 1;
int paramsSize = (int)strlen(params) + 1;
int workDirSize = (int)strlen(workDir) + 1;
int caseListSize = (int)strlen(caseList) + 1;
int totalSize = xs::MESSAGE_HEADER_SIZE + nameSize + paramsSize + workDirSize + caseListSize;
writeMessageHeader(dst, xs::MESSAGETYPE_EXECUTE_BINARY, totalSize);
dst.write(nameSize, (const uint8_t *)name);
dst.write(paramsSize, (const uint8_t *)params);
dst.write(workDirSize, (const uint8_t *)workDir);
dst.write(caseListSize, (const uint8_t *)caseList);
dst.flush();
}
static void writeStopExecution(de::BlockBuffer<uint8_t> &dst)
{
writeMessageHeader(dst, xs::MESSAGETYPE_STOP_EXECUTION, xs::MESSAGE_HEADER_SIZE);
dst.flush();
}
// TcpIpLinkState
TcpIpLinkState::TcpIpLinkState(CommLinkState initialState, const char *initialErr)
: m_state(initialState)
, m_error(initialErr)
, m_lastKeepaliveReceived(0)
, m_stateChangedCallback(nullptr)
, m_testLogDataCallback(nullptr)
, m_infoLogDataCallback(nullptr)
, m_userPtr(nullptr)
{
}
TcpIpLinkState::~TcpIpLinkState(void)
{
}
CommLinkState TcpIpLinkState::getState(void) const
{
de::ScopedLock lock(m_lock);
return m_state;
}
CommLinkState TcpIpLinkState::getState(std::string &error) const
{
de::ScopedLock lock(m_lock);
error = m_error;
return m_state;
}
void TcpIpLinkState::setCallbacks(CommLink::StateChangedFunc stateChangedCallback,
CommLink::LogDataFunc testLogDataCallback, CommLink::LogDataFunc infoLogDataCallback,
void *userPtr)
{
de::ScopedLock lock(m_lock);
m_stateChangedCallback = stateChangedCallback;
m_testLogDataCallback = testLogDataCallback;
m_infoLogDataCallback = infoLogDataCallback;
m_userPtr = userPtr;
}
void TcpIpLinkState::setState(CommLinkState state, const char *error)
{
CommLink::StateChangedFunc callback = nullptr;
void *userPtr = nullptr;
{
de::ScopedLock lock(m_lock);
m_state = state;
m_error = error;
callback = m_stateChangedCallback;
userPtr = m_userPtr;
}
if (callback)
callback(userPtr, state, error);
}
void TcpIpLinkState::onTestLogData(const uint8_t *bytes, size_t numBytes) const
{
CommLink::LogDataFunc callback = nullptr;
void *userPtr = nullptr;
m_lock.lock();
callback = m_testLogDataCallback;
userPtr = m_userPtr;
m_lock.unlock();
if (callback)
callback(userPtr, bytes, numBytes);
}
void TcpIpLinkState::onInfoLogData(const uint8_t *bytes, size_t numBytes) const
{
CommLink::LogDataFunc callback = nullptr;
void *userPtr = nullptr;
m_lock.lock();
callback = m_infoLogDataCallback;
userPtr = m_userPtr;
m_lock.unlock();
if (callback)
callback(userPtr, bytes, numBytes);
}
void TcpIpLinkState::onKeepaliveReceived(void)
{
de::ScopedLock lock(m_lock);
m_lastKeepaliveReceived = deGetMicroseconds();
}
uint64_t TcpIpLinkState::getLastKeepaliveRecevied(void) const
{
de::ScopedLock lock(m_lock);
return m_lastKeepaliveReceived;
}
// TcpIpSendThread
TcpIpSendThread::TcpIpSendThread(de::Socket &socket, TcpIpLinkState &state)
: m_socket(socket)
, m_state(state)
, m_buffer(SEND_BUFFER_BLOCK_SIZE, SEND_BUFFER_NUM_BLOCKS)
, m_isRunning(false)
{
}
TcpIpSendThread::~TcpIpSendThread(void)
{
}
void TcpIpSendThread::start(void)
{
DE_ASSERT(!m_isRunning);
// Reset state.
m_buffer.clear();
m_isRunning = true;
de::Thread::start();
}
void TcpIpSendThread::run(void)
{
try
{
uint8_t buf[SEND_BUFFER_BLOCK_SIZE];
while (!m_buffer.isCanceled())
{
size_t numToSend = 0;
size_t numSent = 0;
deSocketResult result = DE_SOCKETRESULT_LAST;
try
{
// Wait for single byte and then try to read more.
m_buffer.read(1, &buf[0]);
numToSend = 1 + m_buffer.tryRead(DE_LENGTH_OF_ARRAY(buf) - 1, &buf[1]);
}
catch (const de::BlockBuffer<uint8_t>::CanceledException &)
{
// Handled in loop condition.
}
while (numSent < numToSend)
{
result = m_socket.send(&buf[numSent], numToSend - numSent, &numSent);
if (result == DE_SOCKETRESULT_CONNECTION_CLOSED)
XE_FAIL("Connection closed");
else if (result == DE_SOCKETRESULT_CONNECTION_TERMINATED)
XE_FAIL("Connection terminated");
else if (result == DE_SOCKETRESULT_ERROR)
XE_FAIL("Socket error");
else if (result == DE_SOCKETRESULT_WOULD_BLOCK)
{
// \note Socket should not be in non-blocking mode.
DE_ASSERT(numSent == 0);
deYield();
}
else
DE_ASSERT(result == DE_SOCKETRESULT_SUCCESS);
}
}
}
catch (const std::exception &e)
{
m_state.setState(COMMLINKSTATE_ERROR, e.what());
}
}
void TcpIpSendThread::stop(void)
{
if (m_isRunning)
{
m_buffer.cancel();
join();
m_isRunning = false;
}
}
// TcpIpRecvThread
TcpIpRecvThread::TcpIpRecvThread(de::Socket &socket, TcpIpLinkState &state)
: m_socket(socket)
, m_state(state)
, m_curMsgPos(0)
, m_isRunning(false)
{
}
TcpIpRecvThread::~TcpIpRecvThread(void)
{
}
void TcpIpRecvThread::start(void)
{
DE_ASSERT(!m_isRunning);
// Reset state.
m_curMsgPos = 0;
m_isRunning = true;
de::Thread::start();
}
void TcpIpRecvThread::run(void)
{
try
{
for (;;)
{
bool hasHeader = m_curMsgPos >= xs::MESSAGE_HEADER_SIZE;
bool hasPayload = false;
size_t messageSize = 0;
xs::MessageType messageType = (xs::MessageType)0;
if (hasHeader)
{
xs::Message::parseHeader(&m_curMsgBuf[0], xs::MESSAGE_HEADER_SIZE, messageType, messageSize);
hasPayload = m_curMsgPos >= messageSize;
}
if (hasPayload)
{
// Process message.
handleMessage(messageType,
m_curMsgPos > xs::MESSAGE_HEADER_SIZE ? &m_curMsgBuf[xs::MESSAGE_HEADER_SIZE] : nullptr,
messageSize - xs::MESSAGE_HEADER_SIZE);
m_curMsgPos = 0;
}
else
{
// Try to receive missing bytes.
size_t curSize = hasHeader ? messageSize : (size_t)xs::MESSAGE_HEADER_SIZE;
size_t bytesToRecv = curSize - m_curMsgPos;
size_t numRecv = 0;
deSocketResult result = DE_SOCKETRESULT_LAST;
if (m_curMsgBuf.size() < curSize)
m_curMsgBuf.resize(curSize);
result = m_socket.receive(&m_curMsgBuf[m_curMsgPos], bytesToRecv, &numRecv);
if (result == DE_SOCKETRESULT_CONNECTION_CLOSED)
XE_FAIL("Connection closed");
else if (result == DE_SOCKETRESULT_CONNECTION_TERMINATED)
XE_FAIL("Connection terminated");
else if (result == DE_SOCKETRESULT_ERROR)
XE_FAIL("Socket error");
else if (result == DE_SOCKETRESULT_WOULD_BLOCK)
{
// \note Socket should not be in non-blocking mode.
DE_ASSERT(numRecv == 0);
deYield();
}
else
{
DE_ASSERT(result == DE_SOCKETRESULT_SUCCESS);
DE_ASSERT(numRecv <= bytesToRecv);
m_curMsgPos += numRecv;
// Continue receiving bytes / handle message in next iter.
}
}
}
}
catch (const std::exception &e)
{
m_state.setState(COMMLINKSTATE_ERROR, e.what());
}
}
void TcpIpRecvThread::stop(void)
{
if (m_isRunning)
{
// \note Socket must be closed before terminating receive thread.
XE_CHECK(!m_socket.isReceiveOpen());
join();
m_isRunning = false;
}
}
void TcpIpRecvThread::handleMessage(xs::MessageType messageType, const uint8_t *data, size_t dataSize)
{
switch (messageType)
{
case xs::MESSAGETYPE_KEEPALIVE:
m_state.onKeepaliveReceived();
break;
case xs::MESSAGETYPE_PROCESS_STARTED:
XE_CHECK_MSG(m_state.getState() == COMMLINKSTATE_TEST_PROCESS_LAUNCHING, "Unexpected PROCESS_STARTED message");
m_state.setState(COMMLINKSTATE_TEST_PROCESS_RUNNING);
break;
case xs::MESSAGETYPE_PROCESS_LAUNCH_FAILED:
{
xs::ProcessLaunchFailedMessage msg(data, dataSize);
XE_CHECK_MSG(m_state.getState() == COMMLINKSTATE_TEST_PROCESS_LAUNCHING,
"Unexpected PROCESS_LAUNCH_FAILED message");
m_state.setState(COMMLINKSTATE_TEST_PROCESS_LAUNCH_FAILED, msg.reason.c_str());
break;
}
case xs::MESSAGETYPE_PROCESS_FINISHED:
{
XE_CHECK_MSG(m_state.getState() == COMMLINKSTATE_TEST_PROCESS_RUNNING, "Unexpected PROCESS_FINISHED message");
xs::ProcessFinishedMessage msg(data, dataSize);
m_state.setState(COMMLINKSTATE_TEST_PROCESS_FINISHED);
DE_UNREF(msg); // \todo [2012-06-19 pyry] Report exit code.
break;
}
case xs::MESSAGETYPE_PROCESS_LOG_DATA:
case xs::MESSAGETYPE_INFO:
// Ignore leading \0 if such is present. \todo [2012-06-19 pyry] Improve protocol.
if (data[dataSize - 1] == 0)
dataSize -= 1;
if (messageType == xs::MESSAGETYPE_PROCESS_LOG_DATA)
{
XE_CHECK_MSG(m_state.getState() == COMMLINKSTATE_TEST_PROCESS_RUNNING,
"Unexpected PROCESS_LOG_DATA message");
m_state.onTestLogData(&data[0], dataSize);
}
else
m_state.onInfoLogData(&data[0], dataSize);
break;
default:
XE_FAIL("Unknown message");
}
}
// TcpIpLink
TcpIpLink::TcpIpLink(void)
: m_state(COMMLINKSTATE_ERROR, "Not connected")
, m_sendThread(m_socket, m_state)
, m_recvThread(m_socket, m_state)
, m_keepaliveTimer(nullptr)
{
m_keepaliveTimer = deTimer_create(keepaliveTimerCallback, this);
XE_CHECK(m_keepaliveTimer);
}
TcpIpLink::~TcpIpLink(void)
{
try
{
closeConnection();
}
catch (...)
{
// Can't do much except to ignore error.
}
deTimer_destroy(m_keepaliveTimer);
}
void TcpIpLink::closeConnection(void)
{
{
deSocketState state = m_socket.getState();
if (state != DE_SOCKETSTATE_DISCONNECTED && state != DE_SOCKETSTATE_CLOSED)
m_socket.shutdown();
}
if (deTimer_isActive(m_keepaliveTimer))
deTimer_disable(m_keepaliveTimer);
if (m_sendThread.isRunning())
m_sendThread.stop();
if (m_recvThread.isRunning())
m_recvThread.stop();
if (m_socket.getState() != DE_SOCKETSTATE_CLOSED)
m_socket.close();
}
void TcpIpLink::connect(const de::SocketAddress &address)
{
XE_CHECK(m_socket.getState() == DE_SOCKETSTATE_CLOSED);
XE_CHECK(m_state.getState() == COMMLINKSTATE_ERROR);
XE_CHECK(!m_sendThread.isRunning());
XE_CHECK(!m_recvThread.isRunning());
m_socket.connect(address);
try
{
// Clear error and set state to ready.
m_state.setState(COMMLINKSTATE_READY, "");
m_state.onKeepaliveReceived();
// Launch threads.
m_sendThread.start();
m_recvThread.start();
XE_CHECK(deTimer_scheduleInterval(m_keepaliveTimer, xs::KEEPALIVE_SEND_INTERVAL));
}
catch (const std::exception &e)
{
closeConnection();
m_state.setState(COMMLINKSTATE_ERROR, e.what());
throw;
}
}
void TcpIpLink::disconnect(void)
{
try
{
closeConnection();
m_state.setState(COMMLINKSTATE_ERROR, "Not connected");
}
catch (const std::exception &e)
{
m_state.setState(COMMLINKSTATE_ERROR, e.what());
}
}
void TcpIpLink::reset(void)
{
// \note Just clears error state if we are connected.
if (m_socket.getState() == DE_SOCKETSTATE_CONNECTED)
{
m_state.setState(COMMLINKSTATE_READY, "");
// \todo [2012-07-10 pyry] Do we need to reset send/receive buffers?
}
else
disconnect(); // Abnormal state/usage. Disconnect socket.
}
void TcpIpLink::keepaliveTimerCallback(void *ptr)
{
TcpIpLink *link = static_cast<TcpIpLink *>(ptr);
uint64_t lastKeepalive = link->m_state.getLastKeepaliveRecevied();
uint64_t curTime = deGetMicroseconds();
// Check for timeout.
if ((int64_t)curTime - (int64_t)lastKeepalive > xs::KEEPALIVE_TIMEOUT * 1000)
link->m_state.setState(COMMLINKSTATE_ERROR, "Keepalive timeout");
// Enqueue new keepalive.
try
{
writeKeepalive(link->m_sendThread.getBuffer());
}
catch (const de::BlockBuffer<uint8_t>::CanceledException &)
{
// Ignore. Can happen in connection teardown.
}
}
CommLinkState TcpIpLink::getState(void) const
{
return m_state.getState();
}
CommLinkState TcpIpLink::getState(std::string &message) const
{
return m_state.getState(message);
}
void TcpIpLink::setCallbacks(StateChangedFunc stateChangedCallback, LogDataFunc testLogDataCallback,
LogDataFunc infoLogDataCallback, void *userPtr)
{
m_state.setCallbacks(stateChangedCallback, testLogDataCallback, infoLogDataCallback, userPtr);
}
void TcpIpLink::startTestProcess(const char *name, const char *params, const char *workingDir, const char *caseList)
{
XE_CHECK(m_state.getState() == COMMLINKSTATE_READY);
m_state.setState(COMMLINKSTATE_TEST_PROCESS_LAUNCHING);
writeExecuteBinary(m_sendThread.getBuffer(), name, params, workingDir, caseList);
}
void TcpIpLink::stopTestProcess(void)
{
XE_CHECK(m_state.getState() != COMMLINKSTATE_ERROR);
writeStopExecution(m_sendThread.getBuffer());
}
} // namespace xe