Commit 2e26e00c4aeadb325355964d6cead406c06b755e
1 parent
76c18f38
Ping response
Showing
3 changed files
with
20 additions
and
7 deletions
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 &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 &clientId, const std::string |
| 169 | 185 | |
| 170 | 186 | |
| 171 | 187 | |
| 188 | + | |
| 189 | + | ... | ... |
client.h
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 | { | ... | ... |