NetworkTests.cpp 7.4 KB

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