#include #include #include "network/Client.h" #include "network/Server.h" #include "tests/NetworkTests.h" #include "tests/Test.h" static void sleep(int millis) { std::this_thread::sleep_for(std::chrono::milliseconds(millis)); } struct ServerConsumer { bool connected = false; bool disconnect = false; uint8 data1 = 0; uint16 data2 = 0; uint32 data3 = 0; int8 data4 = 0; int16 data5 = 0; int32 data6 = 0; int8 data7 = 0; int16 data8 = 0; int32 data9 = 0; StringBuffer<20> data10; void onConnection(Server::Client& client) { (void)client; connected = true; } void onDisconnect(Server::Client& client) { (void)client; disconnect = true; } void onPackage(Server::Client& client, InPacket& in) { (void)client; 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); } }; struct ClientConsumer {}; static void testConnect(Test& test) { Server server(54321, 5); if(server.hasError()) { test.checkEqual(false, true, "server can initialize"); return; } Client client; if(client.hasError()) { test.checkEqual(false, true, "client can initialize"); return; } std::atomic_bool running(true); ServerConsumer serverConsumer; std::thread listen([&running, &server, &serverConsumer]() { while(running) { server.consumeEvents(serverConsumer); } }); test.checkEqual(false, client.connect("127.0.0.1", 54321, 5), "connection failed"); ClientConsumer clientConsumer; for(int i = 0; i < 100; i++) { client.consumeEvents(clientConsumer); } OutPacket out(50, 0); out.writeU8(0xF1); out.writeU16(0xF123); out.writeU32(0xF1234567); out.writeS8(-0x71); out.writeS16(-0x7123); out.writeS32(-0x71234567); out.writeS8(0x71); out.writeS16(0x7123); out.writeS32(0x71234567); StringBuffer<20> s("Hi there"); out.writeString(s); client.send(out); for(int i = 0; i < 100; i++) { client.consumeEvents(clientConsumer); } test.checkEqual(true, serverConsumer.connected, "server has connection"); test.checkEqual(static_cast(0xF1), serverConsumer.data1, "correct value is sent 1"); test.checkEqual(static_cast(0xF123), serverConsumer.data2, "correct value is sent 2"); test.checkEqual(0xF1234567u, serverConsumer.data3, "correct value is sent 3"); test.checkEqual(static_cast(-0x71), serverConsumer.data4, "correct value is sent 4"); test.checkEqual(static_cast(-0x7123), serverConsumer.data5, "correct value is sent 5"); test.checkEqual(-0x71234567, serverConsumer.data6, "correct value is sent 6"); test.checkEqual(static_cast(0x71), serverConsumer.data7, "correct value is sent 7"); test.checkEqual(static_cast(0x7123), serverConsumer.data8, "correct value is sent 8"); test.checkEqual(0x71234567, serverConsumer.data9, "correct value is sent 9"); test.checkEqual(s, serverConsumer.data10, "correct value is sent 10"); client.disconnect(); sleep(100); test.checkEqual(true, serverConsumer.disconnect, "client has disconnected"); running = false; listen.join(); } void NetworkTests::test() { Test test("Network"); ENet enet; if(enet.init()) { test.checkEqual(false, true, "enet init failed"); } else { testConnect(test); } test.finalize(); }