Debian Codegraph @

plugins/dm.gameconnection/MessageTcp.cpp

raw
1 #include "MessageTcp.h"
2 #include <assert.h>
3 #include "itextstream.h"
4 #include "clsocket/ActiveSocket.h"
5
6 namespace gameconn
7 {
8
9 MessageTcp::~MessageTcp() {}
10 MessageTcp::MessageTcp() {}
11
12 void MessageTcp::init(std::unique_ptr<CActiveSocket> &&connection) {
13 tcp = std::move(connection);
14
15 inputBuffer.clear();
16 outputBuffer.clear();
17 inputPos = 0;
18 outputPos = 0;
19 }
20
21 bool MessageTcp::isAlive() const {
22 return tcp.get() && tcp->IsSocketValid(); //TODO: IsAlive?
23 }
24
25 bool MessageTcp::readMessage(std::vector<char> &message) {
26 message.clear();
27 think();
28
29 const char *buffer = inputBuffer.data() + inputPos;
30 auto remains = inputBuffer.size() - inputPos;
31 auto pull = [&](void* ptr, std::size_t size) -> void {
32 assert(size <= remains);
33 memcpy(ptr, buffer, size);
34 buffer += size;
35 remains -= size;
36 };
37
38 if (remains < 12)
39 return false;
40 int len = -1;
41 char magic[5] = {0};
42
43 //note: little-endianness is assumed
44 pull(magic, 4);
45 if (strcmp(magic, "TDM[") != 0)
46 goto zomg;
47 pull(&len, 4);
48 if (len < 0)
49 goto zomg;
50 pull(magic, 4);
51 if (strcmp(magic, "] ") != 0)
52 goto zomg;
53
54 if (remains < static_cast<std::size_t>(len) + 12)
55 return false;
56
57 message.reserve(len + 1);
58 message.resize(len);
59 pull(message.data(), len);
60 message.data()[len] = '\0';
61
62 pull(magic, 4);
63 if (strcmp(magic, " (") != 0)
64 goto zomg;
65 int len2;
66 pull(&len2, 4);
67 if (len2 != len)
68 goto zomg;
69 pull(magic, 4);
70 if (strcmp(magic, ")TDM") != 0)
71 goto zomg;
72
73 inputPos = buffer - inputBuffer.data();
74 return true;
75
76 zomg:
77 rError() << "ERROR: MessageTCP: wrong message format\n";
78 message.clear();
79 init({});
80 return false;
81 }
82
83 void MessageTcp::writeMessage(const char *message, int len) {
84 auto where = outputBuffer.size();
85 outputBuffer.resize(where + len + 24);
86 auto push = [&](const void *ptr, std::size_t size) -> void {
87 memcpy(&outputBuffer[where], ptr, size);
88 where += size;
89 };
90
91 //note: little-endianness is assumed
92 push("TDM[", 4);
93 push(&len, 4);
94 push("] ", 4);
95 push(message, len);
96 push(" (", 4);
97 push(&len, 4);
98 push(")TDM", 4);
99
100 assert(where == outputBuffer.size());
101
102 think();
103 }
104
105 void MessageTcp::think() {
106 if (!tcp)
107 return;
108 static const int BUFFER_SIZE = 1024;
109
110 //if data in buffer is too far from start, then it is moved to the beginning
111 auto compactBuffer = [](std::vector<char>& vec, std::size_t& pos) -> void {
112 auto remains = vec.size() - pos;
113 if (pos > remains + BUFFER_SIZE) {
114 memcpy(vec.data(), vec.data() + pos, remains);
115 vec.resize(remains);
116 pos = 0;
117 }
118 };
119
120 compactBuffer(inputBuffer, inputPos);
121
122 //fetch incoming data from socket
123 static_assert(BUFFER_SIZE >= 256); //ZeroMQ identity fits
124 char buffer[BUFFER_SIZE];
125 for (int iter = 0; ; iter++) {
126 int read = tcp->Receive(BUFFER_SIZE, (uint8*)buffer);
127 if (read == -1 && tcp->GetSocketError() == CSimpleSocket::SocketEwouldblock)
128 break; //no more data
129 if (read == -1)
130 goto onerror; //socket error
131 if (read == 0)
132 goto onerror; //connection closed on other side
133 inputBuffer.resize(inputBuffer.size() + read);
134 memcpy(inputBuffer.data() + inputBuffer.size() - read, buffer, read);
135 }
136
137 //push outcoming data to socket
138 while (outputPos < outputBuffer.size()) {
139 auto remains = outputBuffer.size() - outputPos;
140 int written = tcp->Send((uint8*)&outputBuffer[outputPos], remains);
141 if (written == -1 && tcp->GetSocketError() == CSimpleSocket::SocketEwouldblock)
142 break; //no more data
143 if (written == -1)
144 goto onerror; //socket error
145 if (written == 0)
146 goto onerror; //connection closed on other side
147 outputPos += written;
148 }
149
150 compactBuffer(outputBuffer, outputPos);
151
152 return;
153
154 onerror:
155 rError() << "Automation lost connection\n";
156 tcp.reset();
157 }
158
159 }