/** * @return A null header if error. */ Common::MessageHeader UDPListener::readDatagramToBuffer(QUdpSocket& socket, QHostAddress& peerAddress) { quint16 port; const qint64 datagramSize = socket.readDatagram(this->buffer, BUFFER_SIZE, &peerAddress, &port); if (datagramSize == -1) { L_WARN(QString("UDPListener::readDatagramToBuffer(..): Unable to read multicast datagram from address:port: %1:%2").arg(peerAddress.toString()).arg(port)); return Common::MessageHeader(); } Common::MessageHeader header = Common::MessageHeader::readHeader(buffer); if (header.getSize() > datagramSize - Common::MessageHeader::HEADER_SIZE) { L_ERRO("The message size (header.size) exceeds the datagram size received"); header.setNull(); return header; } if (header.getSenderID() == this->peerManager->getSelf()->getID()) { // L_WARN("We receive a datagram from ourself, skip"); // Don't care . . . header.setNull(); return header; } if (header.getType() != Common::MessageHeader::CORE_IM_ALIVE) { PM::IPeer* peer = this->peerManager->getPeer(header.getSenderID()); if (!peer) { L_WARN(QString("We receive a datagram from an unknown peer (%1), skip").arg(peerAddress.toString())); header.setNull(); return header; } if (!peer->isAlive()) { L_WARN(QString("We receive a datagram from a dead peer (%1), skip").arg(peerAddress.toString())); header.setNull(); return header; } L_DEBU(QString("Receive a datagram UDP from %1, %2").arg(peer->toStringLog()).arg(header.toStr())); } else { L_DEBU(QString("Receive a datagram UDP from %1, %2").arg(header.getSenderID().toStr()).arg(header.toStr())); } return header; }
/** * @return A null header if error. */ Common::MessageHeader UDPListener::readDatagramToBuffer(QUdpSocket& socket, QHostAddress& peerAddress) { qint64 datagramSize = socket.readDatagram(this->buffer, BUFFER_SIZE, &peerAddress); Common::MessageHeader header = Common::MessageHeader::readHeader(buffer); if (header.getSize() > datagramSize - Common::MessageHeader::HEADER_SIZE) { L_ERRO("header.getSize() > datagramSize"); header.setNull(); return header; } if (header.getSenderID() == this->peerManager->getID()) { // L_WARN("We receive a datagram from ourself, skip"); // Don't care.. header.setNull(); return header; } if (header.getType() != Common::MessageHeader::CORE_IM_ALIVE) { PM::IPeer* peer = this->peerManager->getPeer(header.getSenderID()); if (!peer) { L_WARN("We receive a datagram from an unknown peer, skip"); header.setNull(); return header; } if (!peer->isAlive()) { L_WARN("We receive a datagram from a dead peer, skip"); header.setNull(); return header; } L_DEBU(QString("Receive a datagram UDP from %1, %2").arg(peer->toStringLog()).arg(header.toStr())); } else { L_DEBU(QString("Receive a datagram UDP from %1, %2").arg(header.getSenderID().toStr()).arg(header.toStr())); } return header; }
/** * Writes a given protobuff message to the buffer (this->buffer) prefixed by a header. * @return the total size (header size + message size). Return 0 if the total size is bigger than 'Protos.Core.Settings.max_udp_datagram_size'. */ int UDPListener::writeMessageToBuffer(Common::MessageHeader::MessageType type, const google::protobuf::Message& message) { const Common::MessageHeader header(type, message.ByteSize(), this->getOwnID()); const int nbBytesWritten = Common::Message::writeMessageToBuffer(this->buffer, this->MAX_UDP_DATAGRAM_PAYLOAD_SIZE, header, &message); if (!nbBytesWritten) L_ERRO(QString("Datagram size too big: %1, max allowed: %2").arg(Common::MessageHeader::HEADER_SIZE + header.getSize()).arg(this->MAX_UDP_DATAGRAM_PAYLOAD_SIZE)); return nbBytesWritten; }