123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #include <iostream>
- #include "network/Client.h"
- #include "network/Server.h"
- #include "tests/NetworkTests.h"
- #include "tests/Test.h"
- bool checkError(Test& test, const Error e, const char* msg) {
- if(e.has()) {
- test.checkFalse(true, msg);
- std::cout << "Error: " << e.message << "\n";
- 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();
- }
|