Explorar el Código

reliable packages on channel 0, sequenced packages on channel 1 and
unsequenced packages on channel 2

Kajetan Johannes Hammerle hace 3 años
padre
commit
25e0951e37
Se han modificado 5 ficheros con 29 adiciones y 9 borrados
  1. 2 2
      network/Client.cpp
  2. 15 2
      network/Packet.cpp
  3. 7 1
      network/Packet.h
  4. 1 1
      network/Server.cpp
  5. 4 3
      tests/NetworkTests.cpp

+ 2 - 2
network/Client.cpp

@@ -26,7 +26,7 @@ bool Client::connect(const char* server, Port port, int timeout) {
     enet_address_set_host(&address, server);
     address.port = port;
 
-    connection = enet_host_connect(client, &address, 2, 0);
+    connection = enet_host_connect(client, &address, 3, 0);
     if(connection == nullptr) {
         error.clear().append("server is not available");
         return true;
@@ -60,7 +60,7 @@ void Client::disconnect() {
 
 void Client::send(OutPacket& p) {
     if(p.packet != nullptr) {
-        enet_peer_send(connection, 0, p.packet);
+        enet_peer_send(connection, p.channel, p.packet);
         p.packet = nullptr;
     }
 }

+ 15 - 2
network/Packet.cpp

@@ -73,8 +73,21 @@ bool InPacket::readS32(int32& s) {
     return false;
 }
 
-OutPacket::OutPacket(unsigned int size, int flags)
-    : packet(enet_packet_create(nullptr, size, flags)), index(0) {
+OutPacket::OutPacket(unsigned int size, int flags, int channel)
+    : packet(enet_packet_create(nullptr, size, flags)), index(0),
+      channel(channel) {
+}
+
+OutPacket OutPacket::reliable(unsigned int size) {
+    return OutPacket(size, ENET_PACKET_FLAG_RELIABLE, 0);
+}
+
+OutPacket OutPacket::sequenced(unsigned int size) {
+    return OutPacket(size, 0, 1);
+}
+
+OutPacket OutPacket::unsequenced(unsigned int size) {
+    return OutPacket(size, ENET_PACKET_FLAG_UNSEQUENCED, 2);
 }
 
 OutPacket::~OutPacket() {

+ 7 - 1
network/Packet.h

@@ -44,11 +44,17 @@ public:
 class OutPacket {
     ENetPacket* packet;
     unsigned int index;
+    int channel;
 
     friend class Client;
 
+    OutPacket(unsigned int size, int flags, int channel);
+
 public:
-    OutPacket(unsigned int size, int flags);
+    static OutPacket reliable(unsigned int size);
+    static OutPacket sequenced(unsigned int size);
+    static OutPacket unsequenced(unsigned int size);
+
     ~OutPacket();
     OutPacket(const OutPacket& other);
     OutPacket(OutPacket&& other);

+ 1 - 1
network/Server.cpp

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

+ 4 - 3
tests/NetworkTests.cpp

@@ -52,7 +52,7 @@ struct ServerConsumer {
 
 struct ClientConsumer {};
 
-static void testConnect(Test& test) {
+static void testConnect(Test& test, OutPacket out) {
     Server server(54321, 5);
     if(server.hasError()) {
         test.checkEqual(false, true, "server can initialize");
@@ -80,7 +80,6 @@ static void testConnect(Test& test) {
         client.consumeEvents(clientConsumer);
     }
 
-    OutPacket out(50, 0);
     out.writeU8(0xF1);
     out.writeU16(0xF123);
     out.writeU32(0xF1234567);
@@ -135,7 +134,9 @@ void NetworkTests::test() {
     if(enet.init()) {
         test.checkEqual(false, true, "enet init failed");
     } else {
-        testConnect(test);
+        testConnect(test, OutPacket::unsequenced(50));
+        testConnect(test, OutPacket::reliable(50));
+        testConnect(test, OutPacket::sequenced(50));
     }
     test.finalize();
 }