#include "tests/NetworkTests.h" #include "network/Client.h" #include "network/Server.h" #include "tests/Test.h" static bool checkError(Test& test, const Error e, const char* msg) { if(e.has()) { test.checkFalse(true, msg); return true; } return false; } static void tickClient(int ticks) { for(int i = 0; i < ticks; i++) { Client::tick(); } } static void tick(int ticks) { for(int i = 0; i < ticks; i++) { Client::tick(); Server::tick(); } } static void testConnect(Test& test, PacketSendMode mode) { static bool clientConnected = false; static bool clientDisconnected = false; static bool clientPackage = false; clientConnected = false; clientDisconnected = false; clientPackage = false; static int packageCounter = 0; static bool serverConnected = false; static bool serverDisconnect = false; static uint8 data1 = 0; static uint16 data2 = 0; static uint32 data3 = 0; static int8 data4 = 0; static int16 data5 = 0; static int32 data6 = 0; static int8 data7 = 0; static int16 data8 = 0; static int32 data9 = 0; static StringBuffer<20> data10; static float data11 = 0.0f; static IntVector3 data12; static Vector3 data13; serverConnected = false; serverDisconnect = false; data1 = 0; data2 = 0; data3 = 0; data4 = 0; data5 = 0; data6 = 0; data7 = 0; data8 = 0; data9 = 0; data10.clear(); data11 = 0.0f; data12 = IntVector3(); data13 = Vector3(); Server::resetHandler(); Server::setConnectHandler([](Server::Client) { serverConnected = true; }); Server::setDisconnectHandler( [](Server::Client) { serverDisconnect = true; }); Server::setPacketHandler([](Server::Client client, InPacket& in) { in.read(data1); in.read(data2); in.read(data3); in.read(data4); in.read(data5); in.read(data6); in.read(data7); in.read(data8); in.read(data9); in.read(data10); in.read(data11); in.read(data12); in.read(data13); if(packageCounter == 0) { OutPacket out(0); Server::send(client, out, PacketSendMode::RELIABLE); } else if(packageCounter == 1) { OutPacket out(0); Server::send(client, out, PacketSendMode::SEQUENCED); } else if(packageCounter == 2) { OutPacket out(0); Server::send(client, out, PacketSendMode::UNSEQUENCED); } packageCounter++; }); Client::resetHandler(); Client::setConnectHandler([]() { clientConnected = true; }); Client::setDisconnectHandler([]() { clientDisconnected = true; }); Client::setPacketHandler([](InPacket&) { clientPackage = true; }); if(checkError(test, Server::start(54321, 5), "server can initialize")) { return; } else if(checkError(test, Client::start(), "client can initialize")) { return; } else if(checkError(test, Client::connect("127.0.0.1", 54321, 90), "start connection failed")) { return; } test.checkFalse(Client::isConnected(), "client not connected yet"); test.checkTrue(Client::isConnecting(), "client is connecting"); tick(100); test.checkTrue(clientConnected, "client called connect callback"); test.checkTrue(Client::isConnected(), "client is connected"); test.checkFalse(Client::isConnecting(), "client is no more connecting"); OutPacket out(50); out.writeU8(0xF1).writeU16(0xF123).writeU32(0xF1234567); out.writeS8(-0x71).writeS16(-0x7123).writeS32(-0x71234567); out.writeS8(0x71).writeS16(0x7123).writeS32(0x71234567); StringBuffer<20> s("Hi there"); out.writeString(s); out.writeFloat(252345.983f); out.writeVector(IntVector3(1, 2, 3)); out.writeVector(Vector3(1.5f, 2.5f, 3.5f)); Client::send(out, mode); tick(100); test.checkTrue(clientPackage, "client has received data"); test.checkTrue(serverConnected, "server has connection"); test.checkUnsigned8(0xF1, data1, "correct value is sent 1"); test.checkUnsigned16(0xF123, data2, "correct value is sent 2"); test.checkEqual(0xF1234567u, data3, "correct value is sent 3"); test.checkSigned8(-0x71, data4, "correct value is sent 4"); test.checkSigned16(-0x7123, data5, "correct value is sent 5"); test.checkEqual(-0x71234567, data6, "correct value is sent 6"); test.checkSigned8(0x71, data7, "correct value is sent 7"); test.checkSigned16(0x7123, data8, "correct value is sent 8"); test.checkEqual(0x71234567, data9, "correct value is sent 9"); test.checkEqual(s, data10, "correct value is sent 10"); test.checkFloat(252345.983f, data11, 0.01f, "correct value is sent 11"); test.checkEqual(1, data12[0], "correct value is sent 12|1"); test.checkEqual(2, data12[1], "correct value is sent 12|2"); test.checkEqual(3, data12[2], "correct value is sent 12|3"); test.checkFloat(1.5f, data13[0], 0.01f, "correct value is sent 13|1"); test.checkFloat(2.5f, data13[1], 0.01f, "correct value is sent 13|2"); test.checkFloat(3.5f, data13[2], 0.01f, "correct value is sent 13|3"); Client::disconnect(90); test.checkFalse(Client::isConnected(), "client was disconnected"); test.checkFalse(Client::isConnecting(), "client is not connecting"); tick(100); test.checkTrue(clientDisconnected, "client has disconnected"); test.checkTrue(serverDisconnect, "client has disconnected on server"); Client::stop(); Server::stop(); } static void testDisconnect(Test& test) { static bool disconnected = false; disconnected = false; Client::resetHandler(); Client::setDisconnectHandler([]() { disconnected = true; }); if(checkError(test, Client::start(), "client can initialize")) { return; } else if(checkError(test, Client::connect("127.0.0.1", 54321, 90), "start connection failed")) { return; } test.checkFalse(Client::isConnected(), "client not connected yet"); test.checkTrue(Client::isConnecting(), "client is connecting"); Client::disconnect(50); tickClient(100); test.checkFalse(Client::isConnected(), "client was disconnected"); test.checkFalse(Client::isConnecting(), "client is not connecting"); test.checkTrue(disconnected, "client has disconnected"); Client::stop(); } static void testStop(Test& test) { static bool disconnected = false; disconnected = false; Client::resetHandler(); Client::setDisconnectHandler([]() { disconnected = true; }); if(checkError(test, Client::start(), "client can initialize")) { return; } else if(checkError(test, Client::connect("127.0.0.1", 54321, 90), "start connection failed")) { return; } test.checkFalse(Client::isConnected(), "client not connected yet"); test.checkTrue(Client::isConnecting(), "client is connecting"); Client::stop(); test.checkFalse(Client::isConnected(), "client was disconnected"); test.checkFalse(Client::isConnecting(), "client is not connecting"); test.checkTrue(disconnected, "client has disconnected"); } void NetworkTests::test() { Test test("Network"); testConnect(test, PacketSendMode::UNSEQUENCED); testConnect(test, PacketSendMode::RELIABLE); testConnect(test, PacketSendMode::SEQUENCED); testDisconnect(test); testStop(test); test.finalize(); }