Browse Source

improved networking

Kajetan Johannes Hammerle 2 years ago
parent
commit
ab398c30bc
7 changed files with 92 additions and 50 deletions
  1. 2 2
      network/Client.cpp
  2. 1 1
      network/Client.h
  3. 11 11
      network/Packet.cpp
  4. 38 11
      network/Packet.h
  5. 5 4
      network/Server.cpp
  6. 2 2
      network/Server.h
  7. 33 19
      tests/NetworkTests.cpp

+ 2 - 2
network/Client.cpp

@@ -76,11 +76,11 @@ void Client::disconnect(int timeoutTicks) {
     disconnectTimeoutTicks = timeoutTicks;
 }
 
-void Client::send(OutPacket& p, PacketType pt) {
+void Client::send(OutPacket& p, PacketSendMode mode) {
     if(client != nullptr && connection != nullptr && connectTicks < 0) {
         constexpr enet_uint32 flags[] = {ENET_PACKET_FLAG_RELIABLE, 0,
                                          ENET_PACKET_FLAG_UNSEQUENCED};
-        int index = static_cast<int>(pt);
+        int index = static_cast<int>(mode);
         enet_peer_send(
             connection, index,
             enet_packet_create(p.buffer, p.buffer.getLength(), flags[index]));

+ 1 - 1
network/Client.h

@@ -19,7 +19,7 @@ namespace Client {
 
     void tick();
 
-    void send(OutPacket& p, PacketType pt);
+    void send(OutPacket& p, PacketSendMode mode);
 
     void setConnectHandler(OnConnect oc);
     void setDisconnectHandler(OnDisconnect od);

+ 11 - 11
network/Packet.cpp

@@ -18,11 +18,11 @@ bool InPacket::read(void* buffer, int length) {
     return false;
 }
 
-bool InPacket::readU8(uint8& u) {
+bool InPacket::read(uint8& u) {
     return read(&u, sizeof(u));
 }
 
-bool InPacket::readU16(uint16& u) {
+bool InPacket::read(uint16& u) {
     if(read(&u, sizeof(u))) {
         return true;
     }
@@ -30,7 +30,7 @@ bool InPacket::readU16(uint16& u) {
     return false;
 }
 
-bool InPacket::readU32(uint32& u) {
+bool InPacket::read(uint32& u) {
     if(read(&u, sizeof(u))) {
         return true;
     }
@@ -38,9 +38,9 @@ bool InPacket::readU32(uint32& u) {
     return false;
 }
 
-bool InPacket::readS8(int8& s) {
+bool InPacket::read(int8& s) {
     uint8 u;
-    if(readU8(u)) {
+    if(read(u)) {
         return true;
     }
     if(u < 128) {
@@ -51,9 +51,9 @@ bool InPacket::readS8(int8& s) {
     return false;
 }
 
-bool InPacket::readS16(int16& s) {
+bool InPacket::read(int16& s) {
     uint16 u;
-    if(readU16(u)) {
+    if(read(u)) {
         return true;
     }
     if(u < 32768) {
@@ -64,9 +64,9 @@ bool InPacket::readS16(int16& s) {
     return false;
 }
 
-bool InPacket::readS32(int32& s) {
+bool InPacket::read(int32& s) {
     uint32 u;
-    if(readU32(u)) {
+    if(read(u)) {
         return true;
     }
     if(u < 2147483648) {
@@ -77,9 +77,9 @@ bool InPacket::readS32(int32& s) {
     return false;
 }
 
-bool InPacket::readFloat(float& f) {
+bool InPacket::read(float& f) {
     uint32 u;
-    if(readU32(u)) {
+    if(read(u)) {
         return true;
     }
     memcpy(&f, &u, sizeof(float));

+ 38 - 11
network/Packet.h

@@ -1,11 +1,12 @@
 #ifndef PACKET_H
 #define PACKET_H
 
+#include "math/Vector.h"
 #include "utils/Buffer.h"
 #include "utils/StringBuffer.h"
 #include "utils/Types.h"
 
-enum class PacketType { RELIABLE, SEQUENCED, UNSEQUENCED };
+enum class PacketSendMode { RELIABLE, SEQUENCED, UNSEQUENCED };
 
 class InPacket {
     const char* data;
@@ -15,24 +16,24 @@ class InPacket {
 public:
     InPacket(const void* data, int size);
 
-    bool readU8(uint8& u);
-    bool readU16(uint16& u);
-    bool readU32(uint32& u);
-    bool readS8(int8& s);
-    bool readS16(int16& s);
-    bool readS32(int32& s);
-    bool readFloat(float& f);
+    bool read(uint8& u);
+    bool read(uint16& u);
+    bool read(uint32& u);
+    bool read(int8& s);
+    bool read(int16& s);
+    bool read(int32& s);
+    bool read(float& f);
 
     template<int N>
-    bool readString(StringBuffer<N>& s) {
+    bool read(StringBuffer<N>& s) {
         uint16 end;
-        if(readU16(end)) {
+        if(read(end)) {
             return true;
         }
         s.clear();
         for(unsigned int i = 0; i < end; i++) {
             int8 c;
-            if(readS8(c)) {
+            if(read(c)) {
                 return true;
             }
             s.append(c);
@@ -40,6 +41,16 @@ public:
         return false;
     }
 
+    template<int N, typename T>
+    bool read(Vector<N, T>& v) {
+        for(int i = 0; i < N; i++) {
+            if(read(v[i])) {
+                return true;
+            }
+        }
+        return false;
+    }
+
 private:
     bool read(void* buffer, int length);
 };
@@ -66,6 +77,22 @@ struct OutPacket {
         }
         return *this;
     }
+
+    template<int N>
+    OutPacket& writeVector(const Vector<N, int>& v) {
+        for(int i = 0; i < N; i++) {
+            writeS32(v[i]);
+        }
+        return *this;
+    }
+
+    template<int N>
+    OutPacket& writeVector(const Vector<N, float>& v) {
+        for(int i = 0; i < N; i++) {
+            writeFloat(v[i]);
+        }
+        return *this;
+    }
 };
 
 #endif

+ 5 - 4
network/Server.cpp

@@ -116,20 +116,21 @@ static ENetPacket* fromBuffer(const Buffer& buffer, int index) {
     return enet_packet_create(buffer, buffer.getLength(), flags[index]);
 }
 
-void Server::send(const OutPacket& p, PacketType pt) {
+void Server::send(const OutPacket& p, PacketSendMode mode) {
     if(server != nullptr) {
-        int index = static_cast<int>(pt);
+        int index = static_cast<int>(mode);
         enet_host_broadcast(server, index, fromBuffer(p.buffer, index));
     }
 }
 
-void Server::send(Server::Client client, const OutPacket& p, PacketType pt) {
+void Server::send(Server::Client client, const OutPacket& p,
+                  PacketSendMode mode) {
     if(server == nullptr) {
         return;
     }
     ENetPeer** peer = clients.search(client);
     if(peer != nullptr) {
-        int index = static_cast<int>(pt);
+        int index = static_cast<int>(mode);
         enet_peer_send(*peer, index, fromBuffer(p.buffer, index));
     }
 }

+ 2 - 2
network/Server.h

@@ -17,8 +17,8 @@ namespace Server {
 
     void tick();
 
-    void send(const OutPacket& p, PacketType pt);
-    void send(Client client, const OutPacket& p, PacketType pt);
+    void send(const OutPacket& p, PacketSendMode mode);
+    void send(Client client, const OutPacket& p, PacketSendMode mode);
 
     void disconnect(Client client);
 

+ 33 - 19
tests/NetworkTests.cpp

@@ -27,7 +27,7 @@ static void tick(int ticks) {
     }
 }
 
-static void testConnect(Test& test, PacketType pt) {
+static void testConnect(Test& test, PacketSendMode mode) {
     static bool clientConnected = false;
     static bool clientDisconnected = false;
     static bool clientPackage = false;
@@ -49,6 +49,8 @@ static void testConnect(Test& test, PacketType pt) {
     static int32 data9 = 0;
     static StringBuffer<20> data10;
     static float data11 = 0.0f;
+    static IntVector3 data12;
+    static Vector3 data13;
 
     serverConnected = false;
     serverDisconnect = false;
@@ -63,33 +65,37 @@ static void testConnect(Test& test, PacketType pt) {
     data9 = 0;
     data10.clear();
     data11 = 0.0f;
+    data12 = IntVector3();
+    data13 = Vector3();
 
     Server::resetHandler();
     Server::setConnectHandler([](Server::Client) { serverConnected = true; });
     Server::setDisconnectHandler(
         [](Server::Client) { serverDisconnect = true; });
     Server::setPacketHandler([](Server::Client client, InPacket& in) {
-        in.readU8(data1);
-        in.readU16(data2);
-        in.readU32(data3);
-        in.readS8(data4);
-        in.readS16(data5);
-        in.readS32(data6);
-        in.readS8(data7);
-        in.readS16(data8);
-        in.readS32(data9);
-        in.readString(data10);
-        in.readFloat(data11);
+        in.read(data1);
+        in.read(data2);
+        in.read(data3);
+        in.read(data4);
+        in.read(data5);
+        in.read(data6);
+        in.read(data7);
+        in.read(data8);
+        in.read(data9);
+        in.read(data10);
+        in.read(data11);
+        in.read(data12);
+        in.read(data13);
 
         if(packageCounter == 0) {
             OutPacket out(0);
-            Server::send(client, out, PacketType::RELIABLE);
+            Server::send(client, out, PacketSendMode::RELIABLE);
         } else if(packageCounter == 1) {
             OutPacket out(0);
-            Server::send(client, out, PacketType::SEQUENCED);
+            Server::send(client, out, PacketSendMode::SEQUENCED);
         } else if(packageCounter == 2) {
             OutPacket out(0);
-            Server::send(client, out, PacketType::UNSEQUENCED);
+            Server::send(client, out, PacketSendMode::UNSEQUENCED);
         }
         packageCounter++;
     });
@@ -123,7 +129,9 @@ static void testConnect(Test& test, PacketType pt) {
     StringBuffer<20> s("Hi there");
     out.writeString(s);
     out.writeFloat(252345.983f);
-    Client::send(out, pt);
+    out.writeVector(IntVector3(1, 2, 3));
+    out.writeVector(Vector3(1.5f, 2.5f, 3.5f));
+    Client::send(out, mode);
 
     tick(100);
 
@@ -141,6 +149,12 @@ static void testConnect(Test& test, PacketType pt) {
     test.checkEqual(0x71234567, data9, "correct value is sent 9");
     test.checkEqual(s, data10, "correct value is sent 10");
     test.checkFloat(252345.983f, data11, 0.01f, "correct value is sent 11");
+    test.checkEqual(1, data12[0], "correct value is sent 12|1");
+    test.checkEqual(2, data12[1], "correct value is sent 12|2");
+    test.checkEqual(3, data12[2], "correct value is sent 12|3");
+    test.checkFloat(1.5f, data13[0], 0.01f, "correct value is sent 13|1");
+    test.checkFloat(2.5f, data13[1], 0.01f, "correct value is sent 13|2");
+    test.checkFloat(3.5f, data13[2], 0.01f, "correct value is sent 13|3");
 
     Client::disconnect(90);
     test.checkFalse(Client::isConnected(), "client was disconnected");
@@ -195,9 +209,9 @@ static void testStop(Test& test) {
 
 void NetworkTests::test() {
     Test test("Network");
-    testConnect(test, PacketType::UNSEQUENCED);
-    testConnect(test, PacketType::RELIABLE);
-    testConnect(test, PacketType::SEQUENCED);
+    testConnect(test, PacketSendMode::UNSEQUENCED);
+    testConnect(test, PacketSendMode::RELIABLE);
+    testConnect(test, PacketSendMode::SEQUENCED);
     testDisconnect(test);
     testStop(test);
     test.finalize();