Documentation for module: Network
Former-commit-id: d36042138d0883eb118cc9a70f94f3522214dd46
This commit is contained in:
47
tests/Engine/Network/IpAddress.cpp
Normal file
47
tests/Engine/Network/IpAddress.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <Nazara/Network/IpAddress.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
SCENARIO("IpAddress", "[NETWORK][IPADDRESS]")
|
||||
{
|
||||
GIVEN("Two default IpAddress")
|
||||
{
|
||||
Nz::IpAddress ipAddressV4;
|
||||
Nz::IpAddress ipAddressV6;
|
||||
|
||||
WHEN("We parse localhost")
|
||||
{
|
||||
Nz::String localhostIPv4 = "127.0.0.1";
|
||||
Nz::String localhostIPv6 = "::1";
|
||||
REQUIRE(ipAddressV4.BuildFromAddress(localhostIPv4.GetConstBuffer()));
|
||||
REQUIRE(ipAddressV6.BuildFromAddress(localhostIPv6.GetConstBuffer()));
|
||||
|
||||
THEN("It's the loop back")
|
||||
{
|
||||
REQUIRE(ipAddressV4.IsLoopback());
|
||||
REQUIRE(ipAddressV6.IsLoopback());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GIVEN("No IpAddress")
|
||||
{
|
||||
WHEN("We get the IP of Nazara")
|
||||
{
|
||||
std::vector<Nz::HostnameInfo> hostnameInfos = Nz::IpAddress::ResolveHostname(Nz::NetProtocol_IPv4, "nazara.digitalpulsesoftware.net");
|
||||
|
||||
THEN("Result is not null")
|
||||
{
|
||||
REQUIRE(!hostnameInfos.empty());
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("We convert IP to hostname")
|
||||
{
|
||||
Nz::IpAddress google(8, 8, 8, 8);
|
||||
THEN("Google (DNS) is 8.8.8.8")
|
||||
{
|
||||
REQUIRE(Nz::IpAddress::ResolveAddress(google) == "google-public-dns-a.google.com");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
tests/Engine/Network/RUdpConnection.cpp
Normal file
40
tests/Engine/Network/RUdpConnection.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <Nazara/Network/RUdpConnection.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
|
||||
SCENARIO("RUdpConnection", "[NETWORK][RUDPCONNECTION]")
|
||||
{
|
||||
GIVEN("Two RUdpConnection, one client, one server")
|
||||
{
|
||||
Nz::UInt16 port = 64266;
|
||||
Nz::RUdpConnection server;
|
||||
REQUIRE(server.Listen(Nz::NetProtocol_IPv4, port));
|
||||
Nz::IpAddress serverIP = server.GetBoundAddress();
|
||||
REQUIRE(serverIP.IsValid());
|
||||
Nz::RUdpConnection client;
|
||||
REQUIRE(client.Listen(Nz::NetProtocol_IPv4, port + 1));
|
||||
Nz::IpAddress clientIP = client.GetBoundAddress();
|
||||
REQUIRE(client.Connect(serverIP));
|
||||
REQUIRE(clientIP.IsValid());
|
||||
|
||||
WHEN("We send data from client")
|
||||
{
|
||||
Nz::NetPacket packet(1);
|
||||
Nz::Vector3f vector123(1.f, 2.f, 3.f);
|
||||
packet << vector123;
|
||||
REQUIRE(client.Send(serverIP, Nz::PacketPriority_Immediate, Nz::PacketReliability_Reliable, packet));
|
||||
client.Update();
|
||||
|
||||
THEN("We should get it on the server")
|
||||
{
|
||||
Nz::RUdpMessage rudpMessage;
|
||||
server.Update();
|
||||
REQUIRE(server.PollMessage(&rudpMessage));
|
||||
Nz::Vector3f result;
|
||||
rudpMessage.data >> result;
|
||||
REQUIRE(result == vector123);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
49
tests/Engine/Network/TCP.cpp
Normal file
49
tests/Engine/Network/TCP.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <Nazara/Network/TcpClient.hpp>
|
||||
#include <Nazara/Network/TcpServer.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Network/NetPacket.hpp>
|
||||
|
||||
#include <random>
|
||||
|
||||
SCENARIO("TCP", "[NETWORK][TCP]")
|
||||
{
|
||||
GIVEN("Two TCP, one client, one server")
|
||||
{
|
||||
// Avoid reusing the same socket
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(1025, 64245);
|
||||
|
||||
Nz::UInt16 port = dis(gen);
|
||||
Nz::TcpServer server;
|
||||
REQUIRE(server.Listen(Nz::NetProtocol_IPv4, port) == Nz::SocketState_Bound);
|
||||
Nz::IpAddress serverIP = server.GetBoundAddress();
|
||||
REQUIRE(serverIP.IsValid());
|
||||
Nz::TcpClient client;
|
||||
REQUIRE(client.Connect(serverIP) == Nz::SocketState_Connecting);
|
||||
Nz::IpAddress clientIP = client.GetRemoteAddress();
|
||||
REQUIRE(clientIP.IsValid());
|
||||
|
||||
Nz::TcpClient serverToClient;
|
||||
REQUIRE(server.AcceptClient(&serverToClient));
|
||||
|
||||
WHEN("We send data from client")
|
||||
{
|
||||
Nz::NetPacket packet(1);
|
||||
Nz::Vector3f vector123(1.f, 2.f, 3.f);
|
||||
packet << vector123;
|
||||
REQUIRE(client.SendPacket(packet));
|
||||
|
||||
THEN("We should get it on the server")
|
||||
{
|
||||
Nz::NetPacket resultPacket;
|
||||
REQUIRE(serverToClient.ReceivePacket(&resultPacket));
|
||||
Nz::Vector3f result;
|
||||
resultPacket >> result;
|
||||
REQUIRE(result == vector123);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
39
tests/Engine/Network/UdpSocket.cpp
Normal file
39
tests/Engine/Network/UdpSocket.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <Nazara/Network/UdpSocket.hpp>
|
||||
#include <Catch/catch.hpp>
|
||||
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Network/NetPacket.hpp>
|
||||
|
||||
SCENARIO("UdpSocket", "[NETWORK][UDPSOCKET]")
|
||||
{
|
||||
GIVEN("Two UdpSocket, one client, one server")
|
||||
{
|
||||
Nz::UInt16 port = 64256;
|
||||
Nz::UdpSocket server(Nz::NetProtocol_IPv4);
|
||||
REQUIRE(server.Bind(port) == Nz::SocketState_Bound);
|
||||
Nz::IpAddress serverIP = server.GetBoundAddress();
|
||||
REQUIRE(serverIP.IsValid());
|
||||
Nz::UdpSocket client(Nz::NetProtocol_IPv4);
|
||||
REQUIRE(client.Bind(port + 1) == Nz::SocketState_Bound);
|
||||
Nz::IpAddress clientIP = client.GetBoundAddress();
|
||||
REQUIRE(clientIP.IsValid());
|
||||
|
||||
WHEN("We send data from client")
|
||||
{
|
||||
Nz::NetPacket packet(1);
|
||||
Nz::Vector3f vector123(1.f, 2.f, 3.f);
|
||||
packet << vector123;
|
||||
REQUIRE(client.SendPacket(serverIP, packet));
|
||||
|
||||
THEN("We should get it on the server")
|
||||
{
|
||||
Nz::NetPacket resultPacket;
|
||||
Nz::IpAddress fromIp;
|
||||
REQUIRE(server.ReceivePacket(&resultPacket, &fromIp));
|
||||
Nz::Vector3f result;
|
||||
resultPacket >> result;
|
||||
REQUIRE(result == vector123);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user