Prechádzať zdrojové kódy

network read and write for strings

Kajetan Johannes Hammerle 3 rokov pred
rodič
commit
bb4b4f1a41

+ 1 - 1
network/Client.cpp

@@ -3,7 +3,7 @@
 Client::Client()
     : client(enet_host_create(nullptr, 1, 2, 0, 0)), connection(nullptr) {
     if(client == nullptr) {
-        error.clear().append("cannot crate ENet client host");
+        error.clear().append("cannot create ENet client host");
     }
 }
 

+ 30 - 3
network/Packet.h

@@ -2,6 +2,7 @@
 #define PACKET_H
 
 #include "network/ENet.h"
+#include "utils/StringBuffer.h"
 #include "utils/Types.h"
 
 class InPacket {
@@ -18,9 +19,26 @@ public:
     bool readU8(uint8& u);
     bool readU16(uint16& u);
     bool readU32(uint32& u);
-    bool readS8(int8& u);
-    bool readS16(int16& u);
-    bool readS32(int32& u);
+    bool readS8(int8& s);
+    bool readS16(int16& s);
+    bool readS32(int32& s);
+
+    template<int N>
+    bool readString(StringBuffer<N>& s) {
+        uint16 end;
+        if(readU16(end)) {
+            return true;
+        }
+        s.clear();
+        for(unsigned int i = 0; i < end; i++) {
+            int8 c;
+            if(readS8(c)) {
+                return true;
+            }
+            s.append(c);
+        }
+        return false;
+    }
 };
 
 class OutPacket {
@@ -43,6 +61,15 @@ public:
     void writeS16(int16 s);
     void writeS32(int32 s);
 
+    template<int N>
+    void writeString(const StringBuffer<N>& s) {
+        uint16 end = s.getLength() > 65535 ? 65535 : s.getLength();
+        writeU16(end);
+        for(unsigned int i = 0; i < end; i++) {
+            writeS8(s[i]);
+        }
+    }
+
 private:
     void write(const void* buffer, unsigned int length);
 };

+ 1 - 1
network/Server.cpp

@@ -33,7 +33,7 @@ Server::Server(Port port, int maxClients) : server(nullptr), idCounter(1) {
 
     server = enet_host_create(&address, maxClients, 2, 0, 0);
     if(server == nullptr) {
-        error.clear().append("cannot crate ENet server host");
+        error.clear().append("cannot create ENet server host");
     }
 }
 

+ 6 - 1
tests/NetworkTests.cpp

@@ -23,6 +23,7 @@ struct ServerConsumer {
     int8 data7 = 0;
     int16 data8 = 0;
     int32 data9 = 0;
+    StringBuffer<20> data10;
 
     void onConnection(Server::Client& client) {
         (void)client;
@@ -45,6 +46,7 @@ struct ServerConsumer {
         in.readS8(data7);
         in.readS16(data8);
         in.readS32(data9);
+        in.readString(data10);
     }
 };
 
@@ -78,7 +80,7 @@ static void testConnect(Test& test) {
         client.consumeEvents(clientConsumer);
     }
 
-    OutPacket out(21, 0);
+    OutPacket out(50, 0);
     out.writeU8(0xF1);
     out.writeU16(0xF123);
     out.writeU32(0xF1234567);
@@ -88,6 +90,8 @@ static void testConnect(Test& test) {
     out.writeS8(0x71);
     out.writeS16(0x7123);
     out.writeS32(0x71234567);
+    StringBuffer<20> s("Hi there");
+    out.writeString(s);
     client.send(out);
 
     for(int i = 0; i < 100; i++) {
@@ -114,6 +118,7 @@ static void testConnect(Test& test) {
                     "correct value is sent 8");
     test.checkEqual(0x71234567, serverConsumer.data9,
                     "correct value is sent 9");
+    test.checkEqual(s, serverConsumer.data10, "correct value is sent 10");
 
     client.disconnect();
     sleep(100);

+ 4 - 0
utils/StringBuffer.h

@@ -69,6 +69,10 @@ public:
         return *this;
     }
 
+    StringBuffer& append(signed char c) {
+        return append(static_cast<char>(c));
+    }
+
     StringBuffer& append(const char* str) {
         for(int i = 0; length < N - 1 && str[i] != '\0'; length++, i++) {
             data[length] = str[i];