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