From ca90ec459ca0bd48c1483a45f30496aed61e9c21 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 20 Oct 2015 17:20:58 +0200 Subject: [PATCH] added message packet --- doc/protocol | 14 ++++++++++++ src/net/ConnectionHandler.hpp | 1 + src/net/Packet.hpp | 17 +++++++++++++++ src/net/net.cpp | 31 +++++++++++++++++++++++++++ tst/net/PacketTest.cpp | 40 +++++++++++++++++++++++++++++++++++ tst/net/PacketTest.hpp | 2 ++ 6 files changed, 105 insertions(+) diff --git a/doc/protocol b/doc/protocol index 3347158..102bf50 100644 --- a/doc/protocol +++ b/doc/protocol @@ -196,3 +196,17 @@ Payload: 18 first block data, 32bit 22 second block index... Length: 16 + multiple of 6, max 484 + + +Message +------- + +Sent by the client when the user submits a line on the chat input. +Sent by the server on various events like player chat, server status, command output, etc. + +Code: 12 +Payload: + 0 message type, 8bit unsigned int: 0 = notification, 1 = chat + 1 referral, 32bit unsigned int, entity ID if type = 1 + 5 message, max 450 byte UTF-8 string, should be zero terminated if shorter +Length: 6-455 diff --git a/src/net/ConnectionHandler.hpp b/src/net/ConnectionHandler.hpp index 0f0afda..77bcd7b 100644 --- a/src/net/ConnectionHandler.hpp +++ b/src/net/ConnectionHandler.hpp @@ -42,6 +42,7 @@ private: virtual void On(const Packet::ChunkBegin &) { } virtual void On(const Packet::ChunkData &) { } virtual void On(const Packet::BlockUpdate &) { } + virtual void On(const Packet::Message &) { } private: unsigned int packets_lost; diff --git a/src/net/Packet.hpp b/src/net/Packet.hpp index 7b9b69e..c9184d4 100644 --- a/src/net/Packet.hpp +++ b/src/net/Packet.hpp @@ -218,6 +218,23 @@ struct Packet { void ReadBlock(Block &, std::uint32_t) const noexcept; }; + struct Message : public Payload { + static constexpr std::uint8_t TYPE = 12; + static constexpr std::size_t MAX_LEN = 455; + + static constexpr std::size_t MAX_MESSAGE_LEN = 450; + static std::size_t GetSize(const std::string &msg) noexcept { + return 5 + std::min(msg.size() + 1, MAX_MESSAGE_LEN); + } + + void WriteType(std::uint8_t) noexcept; + void ReadType(std::uint8_t &) const noexcept; + void WriteReferral(std::uint32_t) noexcept; + void ReadReferral(std::uint32_t &) const noexcept; + void WriteMessage(const std::string &) noexcept; + void ReadMessage(std::string &) const noexcept; + }; + template PayloadType As() { diff --git a/src/net/net.cpp b/src/net/net.cpp index 115cb4e..e267855 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -27,6 +27,8 @@ constexpr size_t Packet::PlayerCorrection::MAX_LEN; constexpr size_t Packet::ChunkBegin::MAX_LEN; constexpr size_t Packet::ChunkData::MAX_LEN; constexpr size_t Packet::BlockUpdate::MAX_LEN; +constexpr size_t Packet::Message::MAX_LEN; +constexpr size_t Packet::Message::MAX_MESSAGE_LEN; Connection::Connection(const IPaddress &addr) : handler(nullptr) @@ -214,6 +216,8 @@ const char *Packet::Type2String(uint8_t t) noexcept { return "ChunkData"; case BlockUpdate::TYPE: return "BlockUpdate"; + case Message::TYPE: + return "Message"; default: return "Unknown"; } @@ -541,6 +545,30 @@ void Packet::BlockUpdate::ReadBlock(Block &block, uint32_t num) const noexcept { Read(block, off); } +void Packet::Message::WriteType(uint8_t type) noexcept { + Write(type, 0); +} + +void Packet::Message::ReadType(uint8_t &type) const noexcept { + Read(type, 0); +} + +void Packet::Message::WriteReferral(uint32_t ref) noexcept { + Write(ref, 1); +} + +void Packet::Message::ReadReferral(uint32_t &ref) const noexcept { + Read(ref, 1); +} + +void Packet::Message::WriteMessage(const string &msg) noexcept { + WriteString(msg, 5, MAX_MESSAGE_LEN); +} + +void Packet::Message::ReadMessage(string &msg) const noexcept { + ReadString(msg, 5, MAX_MESSAGE_LEN); +} + void ConnectionHandler::Handle(const UDPpacket &udp_pack) { const Packet &pack = *reinterpret_cast(udp_pack.data); @@ -581,6 +609,9 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) { case Packet::BlockUpdate::TYPE: On(Packet::As(udp_pack)); break; + case Packet::Message::TYPE: + On(Packet::As(udp_pack)); + break; default: // drop unknown or unhandled packets break; diff --git a/tst/net/PacketTest.cpp b/tst/net/PacketTest.cpp index 464c9d6..5b6deed 100644 --- a/tst/net/PacketTest.cpp +++ b/tst/net/PacketTest.cpp @@ -510,6 +510,46 @@ void PacketTest::testBlockUpdate() { ); } +void PacketTest::testMessage() { + auto pack = Packet::Make(udp_pack); + AssertPacket("Message", 12, 6, 455, pack); + + const uint8_t write_type = 1; + const uint32_t write_ref = 6433235; + const string write_msg("hello, world"); + + pack.length = Packet::Message::GetSize(write_msg); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "length not correctly set in BlockUpdate packet", + size_t(5 + write_msg.size() + 1), pack.length + ); + + pack.WriteType(write_type); + pack.WriteReferral(write_ref); + pack.WriteMessage(write_msg); + + uint8_t read_type = 5; + uint32_t read_ref = 884373; + string read_msg; + + pack.ReadType(read_type); + pack.ReadReferral(read_ref); + pack.ReadMessage(read_msg); + + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "type not correctly transported in Message packet", + write_type, read_type + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "referral not correctly transported in Message packet", + write_ref, read_ref + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "message not correctly transported in Message packet", + write_msg, read_msg + ); +} + void PacketTest::AssertPacket( const string &name, diff --git a/tst/net/PacketTest.hpp b/tst/net/PacketTest.hpp index 32976c8..54839ac 100644 --- a/tst/net/PacketTest.hpp +++ b/tst/net/PacketTest.hpp @@ -35,6 +35,7 @@ CPPUNIT_TEST(testPlayerCorrection); CPPUNIT_TEST(testChunkBegin); CPPUNIT_TEST(testChunkData); CPPUNIT_TEST(testBlockUpdate); +CPPUNIT_TEST(testMessage); CPPUNIT_TEST_SUITE_END(); @@ -56,6 +57,7 @@ public: void testChunkBegin(); void testChunkData(); void testBlockUpdate(); + void testMessage(); private: static void AssertPacket( -- 2.39.2