123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #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, PacketType pt) {
- 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;
- 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;
- Server::resetHandler();
- Server::setConnectHandler([](Server::Client) { serverConnected = true; });
- Server::setDisconnectHandler(
- [](Server::Client) { serverDisconnect = true; });
- Server::setPacketHandler([](Server::Client client, InPacket& in) {
- in.readU8(data1);
- in.readU16(data2);
- in.readU32(data3);
- in.readS8(data4);
- in.readS16(data5);
- in.readS32(data6);
- in.readS8(data7);
- in.readS16(data8);
- in.readS32(data9);
- in.readString(data10);
- in.readFloat(data11);
- if(packageCounter == 0) {
- OutPacket out(0);
- Server::send(client, out, PacketType::RELIABLE);
- } else if(packageCounter == 1) {
- OutPacket out(0);
- Server::send(client, out, PacketType::SEQUENCED);
- } else if(packageCounter == 2) {
- OutPacket out(0);
- Server::send(client, out, PacketType::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);
- Client::send(out, pt);
- 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");
- 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, PacketType::UNSEQUENCED);
- testConnect(test, PacketType::RELIABLE);
- testConnect(test, PacketType::SEQUENCED);
- testDisconnect(test);
- testStop(test);
- test.finalize();
- }
|