Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ target_sources(${FGE_SERVER_LIB_NAME} PRIVATE
sources/network/C_packet.cpp
sources/network/C_packetLZ4.cpp
sources/network/C_server.cpp
sources/network/C_netServer.cpp
sources/network/C_netClient.cpp
sources/network/C_socket.cpp
sources/network/C_netCommand.cpp
sources/network/C_protocol.cpp)
Expand Down Expand Up @@ -403,6 +405,8 @@ target_sources(${FGE_LIB_NAME} PRIVATE
sources/network/C_packet.cpp
sources/network/C_packetLZ4.cpp
sources/network/C_server.cpp
sources/network/C_netServer.cpp
sources/network/C_netClient.cpp
sources/network/C_socket.cpp
sources/network/C_netCommand.cpp
sources/network/C_protocol.cpp)
Expand Down
2 changes: 1 addition & 1 deletion examples/clientServerLifeSimulator_004/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
std::cout << "connection ok" << std::endl;

server.enableReturnPacket(true);
server._client.getPacketReorderer().setMaximumSize(FGE_NET_PACKET_REORDERER_CACHE_COMPUTE(
server.getClientContext()._reorderer.setMaximumSize(FGE_NET_PACKET_REORDERER_CACHE_COMPUTE(
FGE_NET_DEFAULT_RETURN_PACKET_RATE.count(), LIFESIM_SERVER_TICK));

auto transmissionPacket = fge::net::CreatePacket(ls::LS_PROTOCOL_C_PLEASE_CONNECT_ME);
Expand Down
9 changes: 4 additions & 5 deletions examples/clientServerLifeSimulator_004/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
//Handling clients connection
serverFlux->_onClientConnected.addLambda([](fge::net::ClientSharedPtr const& client, fge::net::Identity const& id) {
client->getStatus().setTimeout(LIFESIM_TIME_TIMEOUT);
std::cout << "New user connected and now trying to authenticate : " << id.toString() << std::endl;
});

//Handling clients return packet
Expand Down Expand Up @@ -212,7 +213,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
fge::net::ReceivedPacketPtr packet;
fge::net::FluxProcessResults processResult;
do {
processResult = serverFlux->process(client, packet, true);
processResult = serverFlux->process(client, packet);
if (processResult != fge::net::FluxProcessResults::USER_RETRIEVABLE)
{
continue;
Expand Down Expand Up @@ -274,12 +275,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
transmissionPacket->packet() << true;
transmissionPacket->doNotReorder();

std::cout << "new user : " << packet->getIdentity().toString() << " connected !"
std::cout << "new user : " << packet->getIdentity().toString() << " authenticated !"
<< std::endl;

//Create the new client with the packet identity
//client = std::make_shared<fge::net::Client>();
//clients.add(packet->getIdentity(), client);
//Authenticate the new client
client->getStatus().setNetworkStatus(fge::net::ClientStatus::NetworkStatus::AUTHENTICATED);

//Pack data required by the LatencyPlanner in order to compute latency
Expand Down
28 changes: 12 additions & 16 deletions includes/FastEngine/network/C_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <deque>
#include <memory>
#include <mutex>
#include <unordered_set>

#define FGE_NET_DEFAULT_LATENCY 20
#define FGE_NET_CLIENT_TIMESTAMP_MODULO 65536
Expand Down Expand Up @@ -212,20 +213,22 @@ class FGE_API ClientStatus
std::chrono::steady_clock::time_point g_currentTimeout{std::chrono::steady_clock::now()};
};

struct FGE_API CryptInfo
{
~CryptInfo();

void* _ssl{nullptr};
void* _rbio{nullptr};
void* _wbio{nullptr};
};

/**
* \class Client
* \brief Class that represent the identity of a client
*/
class FGE_API Client
{
public:
struct CryptInfo
{
void* _ssl{nullptr};
void* _rbio{nullptr};
void* _wbio{nullptr};
};

Client();
~Client();
/**
Expand Down Expand Up @@ -390,13 +393,8 @@ class FGE_API Client
void resetLastReorderedPacketCounter();
[[nodiscard]] ProtocolPacket::CounterType getLastReorderedPacketCounter() const;

[[nodiscard]] PacketReorderer& getPacketReorderer();
[[nodiscard]] PacketReorderer const& getPacketReorderer() const;

[[nodiscard]] DataLockPair<PacketCache*, std::recursive_mutex> getPacketCache();
[[nodiscard]] DataLockPair<PacketCache const*, std::recursive_mutex> getPacketCache() const;
void acknowledgeReception(ReceivedPacketPtr const& packet);
[[nodiscard]] std::vector<PacketCache::Label> const& getAcknowledgedList() const;
[[nodiscard]] std::unordered_set<PacketCache::Label, PacketCache::Label::Hash> const& getAcknowledgedList() const;
void clearAcknowledgedList();

void clearLostPacketCount();
Expand Down Expand Up @@ -439,9 +437,7 @@ class FGE_API Client
ProtocolPacket::CounterType g_lastReorderedPacketCounter{0};
ProtocolPacket::CounterType g_clientPacketCounter{0};

std::vector<PacketCache::Label> g_acknowledgedPackets;
PacketCache g_packetCache;
PacketReorderer g_packetReorderer;
std::unordered_set<PacketCache::Label, PacketCache::Label::Hash> g_acknowledgedPackets;
uint32_t g_lostPacketCount{0};
uint32_t g_lostPacketThreshold{FGE_NET_DEFAULT_lOST_PACKET_THRESHOLD};

Expand Down
52 changes: 32 additions & 20 deletions includes/FastEngine/network/C_clientList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,12 @@ class SocketUdp;

using ClientSharedPtr = std::shared_ptr<Client>;

/**
* \struct ClientListEvent
* \ingroup network
* \brief Represents an event on the client list (client added, client removed, ...)
*/
struct ClientListEvent
struct ClientContext
{
enum Events : uint8_t
{
CLEVT_DELCLIENT = 0,
CLEVT_NEWCLIENT
};

Events _event;
Identity _id;
PacketDefragmentation _defragmentation;
PacketCache _cache;
PacketReorderer _reorderer;
CommandQueue _commands;
};

/**
Expand All @@ -66,14 +57,34 @@ class FGE_API ClientList
{}

ClientSharedPtr _client;
PacketDefragmentation _defragmentation;
CommandQueue _commands;
ClientContext _context;
};

/**
* \struct Event
* \ingroup network
* \brief Represents an event on the client list (client added, client removed, ...)
*/
struct Event
{
enum class Types : uint8_t
{
EVT_DELCLIENT = 0,
EVT_NEWCLIENT
};
using Types_t = std::underlying_type_t<Types>;

inline Event(Types eventType, Identity const& clientId) :
_event(eventType),
_id(clientId)
{}

std::future<uint16_t> _mtuFuture;
Types _event;
Identity _id;
};

using DataList = std::unordered_map<Identity, Data, IdentityHash>;
using EventList = std::deque<ClientListEvent>;
using EventList = std::deque<Event>;

ClientList() = default;
~ClientList() = default;
Expand All @@ -100,6 +111,7 @@ class FGE_API ClientList
*/
void sendToAll(TransmitPacketPtr const& pck) const;

bool moveTo(ClientList& targetList, Identity const& id);
/**
* \brief Add a client to the list
*
Expand Down Expand Up @@ -190,13 +202,13 @@ class FGE_API ClientList
*
* \param evt A client event
*/
void pushClientEvent(ClientListEvent const& evt);
void pushClientEvent(Event const& evt);
/**
* \brief Get the client event with its index
*
* \return The client event
*/
ClientListEvent const& getClientEvent(std::size_t index) const;
Event const& getClientEvent(std::size_t index) const;
/**
* \brief Get the number of client events
*
Expand Down
99 changes: 75 additions & 24 deletions includes/FastEngine/network/C_netCommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ enum class NetCommandTypes
{
DISCOVER_MTU,
CONNECT,
DISCONNECT
DISCONNECT,
CONNECT_HANDLER
};
enum class NetCommandResults
{
Expand All @@ -91,35 +92,41 @@ class NetCommand
IpAddress::Types addressType,
Client& client,
std::chrono::milliseconds deltaTime);
[[nodiscard]] virtual NetCommandResults
onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) = 0;
virtual void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) = 0;

[[nodiscard]] virtual std::chrono::milliseconds getTimeoutTarget() const;

protected:
[[nodiscard]] virtual NetCommandResults
internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) = 0;
virtual void internalUpdate(TransmitPacketPtr& buffPacket,
IpAddress::Types addressType,
Client& client,
std::chrono::milliseconds deltaTime) = 0;
[[nodiscard]] virtual NetCommandResults timeout(Client& client);
void resetTimeout();

void markAsFailed();
void markAsSucceeded();

CommandQueue* _g_commandQueue{nullptr};

private:
std::chrono::milliseconds g_timeout{0};
NetCommandResults g_currentResultState{NetCommandResults::WORKING};
};

class FGE_API NetMTUCommand : public NetCommand
class FGE_API NetMTUCommand final : public NetCommand
{
public:
using NetCommand::NetCommand;
~NetMTUCommand() final = default;
~NetMTUCommand() override = default;

[[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::DISCOVER_MTU; }

[[nodiscard]] NetCommandResults
internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) override;
[[nodiscard]] NetCommandResults
onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
void internalUpdate(TransmitPacketPtr& buffPacket,
IpAddress::Types addressType,
Client& client,
std::chrono::milliseconds deltaTime) override;
void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;

[[nodiscard]] inline std::future<uint16_t> get_future() { return this->g_promise.get_future(); }

Expand All @@ -144,21 +151,22 @@ class FGE_API NetMTUCommand : public NetCommand
} g_state{States::ASKING};
};

class FGE_API NetConnectCommand : public NetCommand
class FGE_API NetConnectCommand final : public NetCommand
{
public:
using NetCommand::NetCommand;
~NetConnectCommand() final = default;
~NetConnectCommand() override = default;

void setVersioningString(std::string_view versioningString);
[[nodiscard]] std::string const& getVersioningString() const;

[[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT; }

[[nodiscard]] NetCommandResults
internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) override;
[[nodiscard]] NetCommandResults
onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
void internalUpdate(TransmitPacketPtr& buffPacket,
IpAddress::Types addressType,
Client& client,
std::chrono::milliseconds deltaTime) override;
void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;

[[nodiscard]] inline std::future<bool> get_future() { return this->g_promise.get_future(); }

Expand Down Expand Up @@ -189,18 +197,61 @@ class FGE_API NetConnectCommand : public NetCommand
std::string g_versioningString;
};

class FGE_API NetDisconnectCommand : public NetCommand
class FGE_API NetConnectHandlerCommand final : public NetCommand
{
public:
using NetCommand::NetCommand;
~NetDisconnectCommand() final = default;
~NetConnectHandlerCommand() override = default;

[[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT; }
[[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::CONNECT_HANDLER; }

void internalUpdate(TransmitPacketPtr& buffPacket,
IpAddress::Types addressType,
Client& client,
std::chrono::milliseconds deltaTime) override;
void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;

[[nodiscard]] inline std::future<bool> get_future() { return this->g_promise.get_future(); }

[[nodiscard]] inline std::chrono::milliseconds getTimeoutTarget() const override
{
return FGE_NET_CONNECT_TIMEOUT_MS;
}

private:
[[nodiscard]] NetCommandResults timeout(Client& client) override;

std::promise<bool> g_promise;
enum class States
{
LOOKUP_MTU,

DEALING_WITH_MTU,
WAITING_CLIENT_FINAL_MTU,

CRYPT_HANDSHAKE,
CRYPT_WAITING,

CONNECTED
} g_state{States::LOOKUP_MTU};

std::future<uint16_t> g_mtuFuture;
NetMTUCommand g_mtuCommand{this->_g_commandQueue};
};

class FGE_API NetDisconnectCommand final : public NetCommand
{
public:
using NetCommand::NetCommand;
~NetDisconnectCommand() override = default;

[[nodiscard]] NetCommandTypes getType() const override { return NetCommandTypes::DISCONNECT; }

[[nodiscard]] NetCommandResults
internalUpdate(TransmitPacketPtr& buffPacket, IpAddress::Types addressType, Client& client) override;
[[nodiscard]] NetCommandResults
onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;
void internalUpdate(TransmitPacketPtr& buffPacket,
IpAddress::Types addressType,
Client& client,
std::chrono::milliseconds deltaTime) override;
void onReceive(std::unique_ptr<ProtocolPacket>& packet, IpAddress::Types addressType, Client& client) override;

[[nodiscard]] inline std::future<void> get_future() { return this->g_promise.get_future(); }

Expand Down
Loading
Loading