client.h
2.39 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
#ifndef CLIENT_H
#define CLIENT_H
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <mutex>
#include <iostream>
#include "forward_declarations.h"
#include "threaddata.h"
#include "mqttpacket.h"
#include "exceptions.h"
#include "cirbuf.h"
#define CLIENT_BUFFER_SIZE 1024 // Must be power of 2
#define MAX_PACKET_SIZE 268435461 // 256 MB + 5
#define MQTT_HEADER_LENGH 2
class Client
{
int fd;
CirBuf readbuf;
uint8_t readBufIsZeroCount = 0;
CirBuf writebuf;
uint8_t writeBufIsZeroCount = 0;
bool authenticated = false;
bool connectPacketSeen = false;
bool readyForWriting = false;
bool readyForReading = true;
bool disconnectWhenBytesWritten = false;
bool disconnecting = false;
std::string clientid;
std::string username;
uint16_t keepalive = 0;
std::string will_topic;
std::string will_payload;
bool will_retain = false;
char will_qos = 0;
ThreadData_p threadData;
std::mutex writeBufMutex;
void setReadyForWriting(bool val);
void setReadyForReading(bool val);
public:
Client(int fd, ThreadData_p threadData);
Client(const Client &other) = delete;
Client(Client &&other) = delete;
~Client();
int getFd() { return fd;}
void markAsDisconnecting();
bool readFdIntoBuffer();
bool bufferToMqttPackets(std::vector<MqttPacket> &packetQueueIn, Client_p &sender);
void setClientProperties(const std::string &clientId, const std::string username, bool connectPacketSeen, uint16_t keepalive);
void setWill(const std::string &topic, const std::string &payload, bool retain, char qos);
void setAuthenticated(bool value) { authenticated = value;}
bool getAuthenticated() { return authenticated; }
bool hasConnectPacketSeen() { return connectPacketSeen; }
ThreadData_p getThreadData() { return threadData; }
std::string &getClientId() { return this->clientid; }
void writePingResp();
void writeMqttPacket(const MqttPacket &packet);
void writeMqttPacketAndBlameThisClient(const MqttPacket &packet);
bool writeBufIntoFd();
bool readyForDisconnecting() const { return disconnectWhenBytesWritten && writebuf.usedBytes() == 0; }
// Do this before calling an action that makes this client ready for writing, so that the EPOLLOUT will handle it.
void setReadyForDisconnect() { disconnectWhenBytesWritten = true; }
std::string repr();
};
#endif // CLIENT_H