]> git.localhorst.tv Git - blank.git/commitdiff
added message packet
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 20 Oct 2015 15:20:58 +0000 (17:20 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 20 Oct 2015 15:40:21 +0000 (17:40 +0200)
doc/protocol
src/net/ConnectionHandler.hpp
src/net/Packet.hpp
src/net/net.cpp
tst/net/PacketTest.cpp
tst/net/PacketTest.hpp

index 3347158e516ff7b567dbd101e65535d7213f3378..102bf50d745769f004f8c282efefb66d0daf95f9 100644 (file)
@@ -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
index 0f0afdaf1a6d24c3e2cb05396c9de7f31ea1ce4c..77bcd7b4da179d8ab52f2c697e7eae2f4da6820f 100644 (file)
@@ -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;
index 7b9b69e447289efbd2831503f5e3ea2689ce64e4..c9184d47000b40e5252d296619176f0c6c3185d4 100644 (file)
@@ -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<class PayloadType>
        PayloadType As() {
index 115cb4e0fb4b6dc9f253e1e35927eb644c2629a5..e267855c301ff24bdf9409640724939e635b22af 100644 (file)
@@ -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<const Packet *>(udp_pack.data);
@@ -581,6 +609,9 @@ void ConnectionHandler::Handle(const UDPpacket &udp_pack) {
                case Packet::BlockUpdate::TYPE:
                        On(Packet::As<Packet::BlockUpdate>(udp_pack));
                        break;
+               case Packet::Message::TYPE:
+                       On(Packet::As<Packet::Message>(udp_pack));
+                       break;
                default:
                        // drop unknown or unhandled packets
                        break;
index 464c9d6ba6de912de87b511e3b627a58d006d257..5b6deed47974c1285f679d824d227ab603a0bc3d 100644 (file)
@@ -510,6 +510,46 @@ void PacketTest::testBlockUpdate() {
        );
 }
 
+void PacketTest::testMessage() {
+       auto pack = Packet::Make<Packet::Message>(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,
index 32976c8429ba61e8f709c0cb8d76c62b4f0456fc..54839acba9ebbc8c50b917c96771d9a9dc09c4fb 100644 (file)
@@ -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(