Commit 2e26e00c4aeadb325355964d6cead406c06b755e
1 parent
76c18f38
Ping response
Showing
3 changed files
with
20 additions
and
7 deletions
client.cpp
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | #include <cstring> | 3 | #include <cstring> |
| 4 | #include <sstream> | 4 | #include <sstream> |
| 5 | +#include <iostream> | ||
| 5 | 6 | ||
| 6 | Client::Client(int fd, ThreadData_p threadData) : | 7 | Client::Client(int fd, ThreadData_p threadData) : |
| 7 | fd(fd), | 8 | fd(fd), |
| @@ -65,6 +66,19 @@ void Client::writeMqttPacket(MqttPacket &packet) | @@ -65,6 +66,19 @@ void Client::writeMqttPacket(MqttPacket &packet) | ||
| 65 | wwi += packet.getSize(); | 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 | bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also get when a client disappears. | 82 | bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also get when a client disappears. |
| 69 | { | 83 | { |
| 70 | int n; | 84 | int n; |
| @@ -92,6 +106,8 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also | @@ -92,6 +106,8 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also | ||
| 92 | return true; | 106 | return true; |
| 93 | } | 107 | } |
| 94 | 108 | ||
| 109 | + | ||
| 110 | + | ||
| 95 | std::string Client::repr() | 111 | std::string Client::repr() |
| 96 | { | 112 | { |
| 97 | std::ostringstream a; | 113 | std::ostringstream a; |
| @@ -169,3 +185,5 @@ void Client::setClientProperties(const std::string &clientId, const std::string | @@ -169,3 +185,5 @@ void Client::setClientProperties(const std::string &clientId, const std::string | ||
| 169 | 185 | ||
| 170 | 186 | ||
| 171 | 187 | ||
| 188 | + | ||
| 189 | + |
client.h
| @@ -90,6 +90,7 @@ public: | @@ -90,6 +90,7 @@ public: | ||
| 90 | bool getAuthenticated() { return authenticated; } | 90 | bool getAuthenticated() { return authenticated; } |
| 91 | bool hasConnectPacketSeen() { return connectPacketSeen; } | 91 | bool hasConnectPacketSeen() { return connectPacketSeen; } |
| 92 | 92 | ||
| 93 | + void writePingResp(); | ||
| 93 | void writeMqttPacket(MqttPacket &packet); | 94 | void writeMqttPacket(MqttPacket &packet); |
| 94 | bool writeBufIntoFd(); | 95 | bool writeBufIntoFd(); |
| 95 | 96 |
mqttpacket.cpp
| @@ -51,7 +51,7 @@ void MqttPacket::handle() | @@ -51,7 +51,7 @@ void MqttPacket::handle() | ||
| 51 | if (packetType == PacketType::CONNECT) | 51 | if (packetType == PacketType::CONNECT) |
| 52 | handleConnect(); | 52 | handleConnect(); |
| 53 | else if (packetType == PacketType::PINGREQ) | 53 | else if (packetType == PacketType::PINGREQ) |
| 54 | - std::cout << "PING" << std::endl; | 54 | + sender->writePingResp(); |
| 55 | else if (packetType == PacketType::SUBSCRIBE) | 55 | else if (packetType == PacketType::SUBSCRIBE) |
| 56 | handleSubscribe(); | 56 | handleSubscribe(); |
| 57 | } | 57 | } |
| @@ -158,12 +158,6 @@ void MqttPacket::handleSubscribe() | @@ -158,12 +158,6 @@ void MqttPacket::handleSubscribe() | ||
| 158 | sender->writeBufIntoFd(); | 158 | sender->writeBufIntoFd(); |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | -void MqttPacket::handlePing() | ||
| 162 | -{ | ||
| 163 | - | ||
| 164 | -} | ||
| 165 | - | ||
| 166 | - | ||
| 167 | 161 | ||
| 168 | char *MqttPacket::readBytes(size_t length) | 162 | char *MqttPacket::readBytes(size_t length) |
| 169 | { | 163 | { |