NetworkTests.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <atomic>
  2. #include <thread>
  3. #include "network/Client.h"
  4. #include "network/Server.h"
  5. #include "tests/NetworkTests.h"
  6. #include "tests/Test.h"
  7. static void sleep(int millis) {
  8. std::this_thread::sleep_for(std::chrono::milliseconds(millis));
  9. }
  10. static int packageCounter = 0;
  11. struct ServerConsumer {
  12. bool connected = false;
  13. bool disconnect = false;
  14. uint8 data1 = 0;
  15. uint16 data2 = 0;
  16. uint32 data3 = 0;
  17. int8 data4 = 0;
  18. int16 data5 = 0;
  19. int32 data6 = 0;
  20. int8 data7 = 0;
  21. int16 data8 = 0;
  22. int32 data9 = 0;
  23. StringBuffer<20> data10;
  24. void onConnect(Server::Client& client) {
  25. (void)client;
  26. connected = true;
  27. }
  28. void onDisconnect(Server::Client& client) {
  29. (void)client;
  30. disconnect = true;
  31. }
  32. void onPacket(Server::Client& client, InPacket& in) {
  33. (void)client;
  34. in.readU8(data1);
  35. in.readU16(data2);
  36. in.readU32(data3);
  37. in.readS8(data4);
  38. in.readS16(data5);
  39. in.readS32(data6);
  40. in.readS8(data7);
  41. in.readS16(data8);
  42. in.readS32(data9);
  43. in.readString(data10);
  44. if(packageCounter == 0) {
  45. OutPacket out = OutPacket::reliable(0);
  46. client.send(out);
  47. } else if(packageCounter == 1) {
  48. OutPacket out = OutPacket::sequenced(0);
  49. client.send(out);
  50. } else if(packageCounter == 2) {
  51. OutPacket out = OutPacket::unsequenced(0);
  52. client.send(out);
  53. }
  54. packageCounter++;
  55. }
  56. };
  57. struct ClientConsumer {
  58. bool package = false;
  59. void onDisconnect() {
  60. }
  61. void onPacket(InPacket& in) {
  62. (void)in;
  63. package = true;
  64. }
  65. };
  66. static void testConnect(Test& test, OutPacket out) {
  67. Server server(54321, 5);
  68. if(server.hasError()) {
  69. test.checkEqual(false, true, "server can initialize");
  70. return;
  71. }
  72. Client client;
  73. if(client.hasError()) {
  74. test.checkEqual(false, true, "client can initialize");
  75. return;
  76. }
  77. std::atomic_bool running(true);
  78. ServerConsumer serverConsumer;
  79. std::thread listen([&running, &server, &serverConsumer]() {
  80. while(running) {
  81. server.consumeEvents(serverConsumer);
  82. }
  83. });
  84. test.checkEqual(false, client.connect("127.0.0.1", 54321, 5),
  85. "connection failed");
  86. ClientConsumer clientConsumer;
  87. for(int i = 0; i < 100; i++) {
  88. client.consumeEvents(clientConsumer);
  89. }
  90. out.writeU8(0xF1);
  91. out.writeU16(0xF123);
  92. out.writeU32(0xF1234567);
  93. out.writeS8(-0x71);
  94. out.writeS16(-0x7123);
  95. out.writeS32(-0x71234567);
  96. out.writeS8(0x71);
  97. out.writeS16(0x7123);
  98. out.writeS32(0x71234567);
  99. StringBuffer<20> s("Hi there");
  100. out.writeString(s);
  101. client.send(out);
  102. for(int i = 0; i < 100; i++) {
  103. client.consumeEvents(clientConsumer);
  104. }
  105. test.checkEqual(true, clientConsumer.package, "client has received data");
  106. test.checkEqual(true, serverConsumer.connected, "server has connection");
  107. test.checkEqual(static_cast<uint8>(0xF1), serverConsumer.data1,
  108. "correct value is sent 1");
  109. test.checkEqual(static_cast<uint16>(0xF123), serverConsumer.data2,
  110. "correct value is sent 2");
  111. test.checkEqual(0xF1234567u, serverConsumer.data3,
  112. "correct value is sent 3");
  113. test.checkEqual(static_cast<int8>(-0x71), serverConsumer.data4,
  114. "correct value is sent 4");
  115. test.checkEqual(static_cast<int16>(-0x7123), serverConsumer.data5,
  116. "correct value is sent 5");
  117. test.checkEqual(-0x71234567, serverConsumer.data6,
  118. "correct value is sent 6");
  119. test.checkEqual(static_cast<int8>(0x71), serverConsumer.data7,
  120. "correct value is sent 7");
  121. test.checkEqual(static_cast<int16>(0x7123), serverConsumer.data8,
  122. "correct value is sent 8");
  123. test.checkEqual(0x71234567, serverConsumer.data9,
  124. "correct value is sent 9");
  125. test.checkEqual(s, serverConsumer.data10, "correct value is sent 10");
  126. client.disconnect();
  127. sleep(100);
  128. test.checkEqual(true, serverConsumer.disconnect, "client has disconnected");
  129. running = false;
  130. listen.join();
  131. }
  132. void NetworkTests::test() {
  133. Test test("Network");
  134. ENet enet;
  135. if(enet.init()) {
  136. test.checkEqual(false, true, "enet init failed");
  137. } else {
  138. testConnect(test, OutPacket::unsequenced(50));
  139. testConnect(test, OutPacket::reliable(50));
  140. testConnect(test, OutPacket::sequenced(50));
  141. }
  142. test.finalize();
  143. }