NetworkTests.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <iostream>
  2. #include "network/Client.h"
  3. #include "network/Server.h"
  4. #include "tests/NetworkTests.h"
  5. #include "tests/Test.h"
  6. bool checkError(Test& test, const Error e, const char* msg) {
  7. if(e.has()) {
  8. test.checkFalse(true, msg);
  9. std::cout << "Error: " << e.message << "\n";
  10. return true;
  11. }
  12. return false;
  13. }
  14. static void tickClient(int ticks) {
  15. for(int i = 0; i < ticks; i++) {
  16. Client::tick();
  17. }
  18. }
  19. static void tick(int ticks) {
  20. for(int i = 0; i < ticks; i++) {
  21. Client::tick();
  22. Server::tick();
  23. }
  24. }
  25. static void testConnect(Test& test, PacketType pt) {
  26. static bool clientConnected = false;
  27. static bool clientDisconnected = false;
  28. static bool clientPackage = false;
  29. clientConnected = false;
  30. clientDisconnected = false;
  31. clientPackage = false;
  32. static int packageCounter = 0;
  33. static bool serverConnected = false;
  34. static bool serverDisconnect = false;
  35. static uint8 data1 = 0;
  36. static uint16 data2 = 0;
  37. static uint32 data3 = 0;
  38. static int8 data4 = 0;
  39. static int16 data5 = 0;
  40. static int32 data6 = 0;
  41. static int8 data7 = 0;
  42. static int16 data8 = 0;
  43. static int32 data9 = 0;
  44. static StringBuffer<20> data10;
  45. static float data11 = 0.0f;
  46. serverConnected = false;
  47. serverDisconnect = false;
  48. data1 = 0;
  49. data2 = 0;
  50. data3 = 0;
  51. data4 = 0;
  52. data5 = 0;
  53. data6 = 0;
  54. data7 = 0;
  55. data8 = 0;
  56. data9 = 0;
  57. data10.clear();
  58. data11 = 0.0f;
  59. Server::resetHandler();
  60. Server::setConnectHandler([](Server::Client) { serverConnected = true; });
  61. Server::setDisconnectHandler(
  62. [](Server::Client) { serverDisconnect = true; });
  63. Server::setPacketHandler([](Server::Client client, InPacket& in) {
  64. in.readU8(data1);
  65. in.readU16(data2);
  66. in.readU32(data3);
  67. in.readS8(data4);
  68. in.readS16(data5);
  69. in.readS32(data6);
  70. in.readS8(data7);
  71. in.readS16(data8);
  72. in.readS32(data9);
  73. in.readString(data10);
  74. in.readFloat(data11);
  75. if(packageCounter == 0) {
  76. OutPacket out(0);
  77. Server::send(client, out, PacketType::RELIABLE);
  78. } else if(packageCounter == 1) {
  79. OutPacket out(0);
  80. Server::send(client, out, PacketType::SEQUENCED);
  81. } else if(packageCounter == 2) {
  82. OutPacket out(0);
  83. Server::send(client, out, PacketType::UNSEQUENCED);
  84. }
  85. packageCounter++;
  86. });
  87. Client::resetHandler();
  88. Client::setConnectHandler([]() { clientConnected = true; });
  89. Client::setDisconnectHandler([]() { clientDisconnected = true; });
  90. Client::setPacketHandler([](InPacket&) { clientPackage = true; });
  91. if(checkError(test, Server::start(54321, 5), "server can initialize")) {
  92. return;
  93. } else if(checkError(test, Client::start(), "client can initialize")) {
  94. return;
  95. } else if(checkError(test, Client::connect("127.0.0.1", 54321, 90),
  96. "start connection failed")) {
  97. return;
  98. }
  99. test.checkFalse(Client::isConnected(), "client not connected yet");
  100. test.checkTrue(Client::isConnecting(), "client is connecting");
  101. tick(100);
  102. test.checkTrue(clientConnected, "client called connect callback");
  103. test.checkTrue(Client::isConnected(), "client is connected");
  104. test.checkFalse(Client::isConnecting(), "client is no more connecting");
  105. OutPacket out(50);
  106. out.writeU8(0xF1).writeU16(0xF123).writeU32(0xF1234567);
  107. out.writeS8(-0x71).writeS16(-0x7123).writeS32(-0x71234567);
  108. out.writeS8(0x71).writeS16(0x7123).writeS32(0x71234567);
  109. StringBuffer<20> s("Hi there");
  110. out.writeString(s);
  111. out.writeFloat(252345.983f);
  112. Client::send(out, pt);
  113. tick(100);
  114. test.checkTrue(clientPackage, "client has received data");
  115. test.checkTrue(serverConnected, "server has connection");
  116. test.checkUnsigned8(0xF1, data1, "correct value is sent 1");
  117. test.checkUnsigned16(0xF123, data2, "correct value is sent 2");
  118. test.checkEqual(0xF1234567u, data3, "correct value is sent 3");
  119. test.checkSigned8(-0x71, data4, "correct value is sent 4");
  120. test.checkSigned16(-0x7123, data5, "correct value is sent 5");
  121. test.checkEqual(-0x71234567, data6, "correct value is sent 6");
  122. test.checkSigned8(0x71, data7, "correct value is sent 7");
  123. test.checkSigned16(0x7123, data8, "correct value is sent 8");
  124. test.checkEqual(0x71234567, data9, "correct value is sent 9");
  125. test.checkEqual(s, data10, "correct value is sent 10");
  126. test.checkFloat(252345.983f, data11, 0.01f, "correct value is sent 11");
  127. Client::disconnect(90);
  128. test.checkFalse(Client::isConnected(), "client was disconnected");
  129. test.checkFalse(Client::isConnecting(), "client is not connecting");
  130. tick(100);
  131. test.checkTrue(clientDisconnected, "client has disconnected");
  132. test.checkTrue(serverDisconnect, "client has disconnected on server");
  133. Client::stop();
  134. Server::stop();
  135. }
  136. static void testDisconnect(Test& test) {
  137. static bool disconnected = false;
  138. disconnected = false;
  139. Client::resetHandler();
  140. Client::setDisconnectHandler([]() { disconnected = true; });
  141. if(checkError(test, Client::start(), "client can initialize")) {
  142. return;
  143. } else if(checkError(test, Client::connect("127.0.0.1", 54321, 90),
  144. "start connection failed")) {
  145. return;
  146. }
  147. test.checkFalse(Client::isConnected(), "client not connected yet");
  148. test.checkTrue(Client::isConnecting(), "client is connecting");
  149. Client::disconnect(50);
  150. tickClient(100);
  151. test.checkFalse(Client::isConnected(), "client was disconnected");
  152. test.checkFalse(Client::isConnecting(), "client is not connecting");
  153. test.checkTrue(disconnected, "client has disconnected");
  154. Client::stop();
  155. }
  156. static void testStop(Test& test) {
  157. static bool disconnected = false;
  158. disconnected = false;
  159. Client::resetHandler();
  160. Client::setDisconnectHandler([]() { disconnected = true; });
  161. if(checkError(test, Client::start(), "client can initialize")) {
  162. return;
  163. } else if(checkError(test, Client::connect("127.0.0.1", 54321, 90),
  164. "start connection failed")) {
  165. return;
  166. }
  167. test.checkFalse(Client::isConnected(), "client not connected yet");
  168. test.checkTrue(Client::isConnecting(), "client is connecting");
  169. Client::stop();
  170. test.checkFalse(Client::isConnected(), "client was disconnected");
  171. test.checkFalse(Client::isConnecting(), "client is not connecting");
  172. test.checkTrue(disconnected, "client has disconnected");
  173. }
  174. void NetworkTests::test() {
  175. Test test("Network");
  176. testConnect(test, PacketType::UNSEQUENCED);
  177. testConnect(test, PacketType::RELIABLE);
  178. testConnect(test, PacketType::SEQUENCED);
  179. testDisconnect(test);
  180. testStop(test);
  181. test.finalize();
  182. }