Commit 2e26e00c4aeadb325355964d6cead406c06b755e

Authored by Wiebe Cazemier
1 parent 76c18f38

Ping response

client.cpp
... ... @@ -2,6 +2,7 @@
2 2  
3 3 #include <cstring>
4 4 #include <sstream>
  5 +#include <iostream>
5 6  
6 7 Client::Client(int fd, ThreadData_p threadData) :
7 8 fd(fd),
... ... @@ -65,6 +66,19 @@ void Client::writeMqttPacket(MqttPacket &amp;packet)
65 66 wwi += packet.getSize();
66 67 }
67 68  
  69 +// Ping responses are always the same, so hardcoding it for optimization.
  70 +void Client::writePingResp()
  71 +{
  72 + std::cout << "Sending ping response to " << repr() << std::endl;
  73 +
  74 + if (2 > getWriteBufMaxWriteSize())
  75 + growWriteBuffer(CLIENT_BUFFER_SIZE);
  76 +
  77 + writebuf[wwi++] = 0b11010000;
  78 + writebuf[wwi++] = 0;
  79 + writeBufIntoFd();
  80 +}
  81 +
68 82 bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also get when a client disappears.
69 83 {
70 84 int n;
... ... @@ -92,6 +106,8 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also
92 106 return true;
93 107 }
94 108  
  109 +
  110 +
95 111 std::string Client::repr()
96 112 {
97 113 std::ostringstream a;
... ... @@ -169,3 +185,5 @@ void Client::setClientProperties(const std::string &amp;clientId, const std::string
169 185  
170 186  
171 187  
  188 +
  189 +
... ...
client.h
... ... @@ -90,6 +90,7 @@ public:
90 90 bool getAuthenticated() { return authenticated; }
91 91 bool hasConnectPacketSeen() { return connectPacketSeen; }
92 92  
  93 + void writePingResp();
93 94 void writeMqttPacket(MqttPacket &packet);
94 95 bool writeBufIntoFd();
95 96  
... ...
mqttpacket.cpp
... ... @@ -51,7 +51,7 @@ void MqttPacket::handle()
51 51 if (packetType == PacketType::CONNECT)
52 52 handleConnect();
53 53 else if (packetType == PacketType::PINGREQ)
54   - std::cout << "PING" << std::endl;
  54 + sender->writePingResp();
55 55 else if (packetType == PacketType::SUBSCRIBE)
56 56 handleSubscribe();
57 57 }
... ... @@ -158,12 +158,6 @@ void MqttPacket::handleSubscribe()
158 158 sender->writeBufIntoFd();
159 159 }
160 160  
161   -void MqttPacket::handlePing()
162   -{
163   -
164   -}
165   -
166   -
167 161  
168 162 char *MqttPacket::readBytes(size_t length)
169 163 {
... ...