client.cpp
8.16 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
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
#include "client.h"
#include <cstring>
#include <sstream>
#include <iostream>
#include <cassert>
Client::Client(int fd, ThreadData_p threadData) :
fd(fd),
readbuf(CLIENT_BUFFER_SIZE),
threadData(threadData)
{
int flags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
char *writebuf = (char*)malloc(CLIENT_BUFFER_SIZE);
if (writebuf == NULL)
{
throw std::runtime_error("Malloc error constructing client.");
}
this->writebuf = writebuf;
}
Client::~Client()
{
close(fd);
free(writebuf);
}
// Do this from a place you'll know ownwership of the shared_ptr is being given up everywhere, so the close happens when the last owner gives it up.
void Client::markAsDisconnecting()
{
if (disconnecting)
return;
disconnecting = true;
check<std::runtime_error>(epoll_ctl(threadData->epollfd, EPOLL_CTL_DEL, fd, NULL));
}
// false means any kind of error we want to get rid of the client for.
bool Client::readFdIntoBuffer()
{
if (disconnecting)
return false;
int n = 0;
while (readbuf.freeSpace() > 0 && (n = read(fd, readbuf.headPtr(), readbuf.maxWriteSize())) != 0)
{
if (n > 0)
{
readbuf.advanceHead(n);
}
if (n < 0)
{
if (errno == EINTR)
continue;
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
else
check<std::runtime_error>(n);
}
// Make sure we either always have enough space for a next call of this method, or stop reading the fd.
if (readbuf.freeSpace() == 0)
{
if (readbuf.getSize() * 2 < CLIENT_MAX_BUFFER_SIZE)
{
readbuf.doubleSize();
}
else
{
setReadyForReading(false);
break;
}
}
}
if (n == 0) // client disconnected.
{
return false;
}
return true;
}
void Client::writeMqttPacket(const MqttPacket &packet)
{
std::lock_guard<std::mutex> locker(writeBufMutex);
if (packet.packetType == PacketType::PUBLISH && wwi > CLIENT_MAX_BUFFER_SIZE)
return;
if (packet.getSizeIncludingNonPresentHeader() > getWriteBufMaxWriteSize())
growWriteBuffer(packet.getSizeIncludingNonPresentHeader());
if (!packet.containsFixedHeader())
{
writebuf[wwi++] = packet.getFirstByte();
RemainingLength r = packet.getRemainingLength();
std::memcpy(&writebuf[wwi], r.bytes, r.len);
wwi += r.len;
}
std::memcpy(&writebuf[wwi], &packet.getBites()[0], packet.getBites().size());
wwi += packet.getBites().size();
assert(wwi >= static_cast<int>(packet.getSizeIncludingNonPresentHeader()));
assert(wwi <= static_cast<int>(writeBufsize));
if (packet.packetType == PacketType::DISCONNECT)
setReadyForDisconnect();
setReadyForWriting(true);
}
// Helper method to avoid the exception ending up at the sender of messages, which would then get disconnected.
void Client::writeMqttPacketAndBlameThisClient(const MqttPacket &packet)
{
try
{
this->writeMqttPacket(packet);
}
catch (std::exception &ex)
{
threadData->removeClient(fd);
}
}
// Ping responses are always the same, so hardcoding it for optimization.
void Client::writePingResp()
{
std::lock_guard<std::mutex> locker(writeBufMutex);
std::cout << "Sending ping response to " << repr() << std::endl;
if (2 > getWriteBufMaxWriteSize())
growWriteBuffer(CLIENT_BUFFER_SIZE);
writebuf[wwi++] = 0b11010000;
writebuf[wwi++] = 0;
setReadyForWriting(true);
}
bool Client::writeBufIntoFd()
{
std::unique_lock<std::mutex> lock(writeBufMutex, std::try_to_lock);
if (!lock.owns_lock())
return true;
// We can abort the write; the client is about to be removed anyway.
if (disconnecting)
return false;
int n;
while ((n = write(fd, &writebuf[wri], getWriteBufBytesUsed())) != 0)
{
if (n > 0)
wri += n;
if (n < 0)
{
if (errno == EINTR)
continue;
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
else
check<std::runtime_error>(n);
}
}
if (wri == wwi)
{
wri = 0;
wwi = 0;
setReadyForWriting(false);
}
return true;
}
std::string Client::repr()
{
std::ostringstream a;
a << "[Client=" << clientid << ", user=" << username << ", fd=" << fd << "]";
a.flush();
return a.str();
}
void Client::growWriteBuffer(size_t add_size)
{
if (add_size == 0)
return;
const size_t grow_by = std::max<size_t>(add_size, writeBufsize*2);
const size_t newBufSize = writeBufsize + grow_by;
char *writebuf = (char*)realloc(this->writebuf, newBufSize);
if (writebuf == NULL)
throw std::runtime_error("Memory allocation failure in growWriteBuffer()");
this->writebuf = writebuf;
writeBufsize = newBufSize;
std::cout << "New write buf size: " << writeBufsize << std::endl;
}
void Client::setReadyForWriting(bool val)
{
if (disconnecting)
return;
if (val == this->readyForWriting)
return;
readyForWriting = val;
struct epoll_event ev;
memset(&ev, 0, sizeof (struct epoll_event));
ev.data.fd = fd;
if (readyForReading)
ev.events |= EPOLLIN;
if (readyForWriting)
ev.events |= EPOLLOUT;
check<std::runtime_error>(epoll_ctl(threadData->epollfd, EPOLL_CTL_MOD, fd, &ev));
}
void Client::setReadyForReading(bool val)
{
if (disconnecting)
return;
if (val == this->readyForReading)
return;
readyForReading = val;
struct epoll_event ev;
memset(&ev, 0, sizeof (struct epoll_event));
ev.data.fd = fd;
if (readyForReading)
ev.events |= EPOLLIN;
if (readyForWriting)
ev.events |= EPOLLOUT;
check<std::runtime_error>(epoll_ctl(threadData->epollfd, EPOLL_CTL_MOD, fd, &ev));
}
bool Client::bufferToMqttPackets(std::vector<MqttPacket> &packetQueueIn, Client_p &sender)
{
while (readbuf.usedBytes() >= MQTT_HEADER_LENGH)
{
// Determine the packet length by decoding the variable length
int remaining_length_i = 1; // index of 'remaining length' field is one after start.
int fixed_header_length = 1;
int multiplier = 1;
int packet_length = 0;
unsigned char encodedByte = 0;
do
{
fixed_header_length++;
// This happens when you only don't have all the bytes that specify the remaining length.
if (fixed_header_length > readbuf.usedBytes())
return false;
encodedByte = readbuf.peakAhead(remaining_length_i++);
packet_length += (encodedByte & 127) * multiplier;
multiplier *= 128;
if (multiplier > 128*128*128)
return false;
}
while ((encodedByte & 128) != 0);
packet_length += fixed_header_length;
if (!authenticated && packet_length >= 1024*1024)
{
throw ProtocolError("An unauthenticated client sends a packet of 1 MB or bigger? Probably it's just random bytes.");
}
if (packet_length <= readbuf.usedBytes())
{
MqttPacket packet(readbuf, packet_length, fixed_header_length, sender);
packetQueueIn.push_back(std::move(packet));
}
else
break;
}
setReadyForReading(readbuf.freeSpace() > 0);
if (readbuf.usedBytes() == 0)
{
// TODO: reset buffer to normal size after a while of not needing it, or not needing the extra space.
}
return true;
}
void Client::setClientProperties(const std::string &clientId, const std::string username, bool connectPacketSeen, uint16_t keepalive)
{
this->clientid = clientId;
this->username = username;
this->connectPacketSeen = connectPacketSeen;
this->keepalive = keepalive;
}
void Client::setWill(const std::string &topic, const std::string &payload, bool retain, char qos)
{
this->will_topic = topic;
this->will_payload = payload;
this->will_retain = retain;
this->will_qos = qos;
}