diff --git a/client.cpp b/client.cpp index e644ec3..272c2aa 100644 --- a/client.cpp +++ b/client.cpp @@ -2,6 +2,7 @@ #include #include +#include Client::Client(int fd, ThreadData_p threadData) : fd(fd), @@ -65,6 +66,19 @@ void Client::writeMqttPacket(MqttPacket &packet) wwi += packet.getSize(); } +// Ping responses are always the same, so hardcoding it for optimization. +void Client::writePingResp() +{ + std::cout << "Sending ping response to " << repr() << std::endl; + + if (2 > getWriteBufMaxWriteSize()) + growWriteBuffer(CLIENT_BUFFER_SIZE); + + writebuf[wwi++] = 0b11010000; + writebuf[wwi++] = 0; + writeBufIntoFd(); +} + bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also get when a client disappears. { int n; @@ -92,6 +106,8 @@ bool Client::writeBufIntoFd() // TODO: ignore the signal BROKEN PIPE we now also return true; } + + std::string Client::repr() { std::ostringstream a; @@ -169,3 +185,5 @@ void Client::setClientProperties(const std::string &clientId, const std::string + + diff --git a/client.h b/client.h index c6381ea..02082d2 100644 --- a/client.h +++ b/client.h @@ -90,6 +90,7 @@ public: bool getAuthenticated() { return authenticated; } bool hasConnectPacketSeen() { return connectPacketSeen; } + void writePingResp(); void writeMqttPacket(MqttPacket &packet); bool writeBufIntoFd(); diff --git a/mqttpacket.cpp b/mqttpacket.cpp index cb73c57..5dbce12 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -51,7 +51,7 @@ void MqttPacket::handle() if (packetType == PacketType::CONNECT) handleConnect(); else if (packetType == PacketType::PINGREQ) - std::cout << "PING" << std::endl; + sender->writePingResp(); else if (packetType == PacketType::SUBSCRIBE) handleSubscribe(); } @@ -158,12 +158,6 @@ void MqttPacket::handleSubscribe() sender->writeBufIntoFd(); } -void MqttPacket::handlePing() -{ - -} - - char *MqttPacket::readBytes(size_t length) {