NetworkTests.cpp 7.4 KB

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