Skip to content

Commit

Permalink
Redesign UDP part of Hanler
Browse files Browse the repository at this point in the history
  • Loading branch information
kachsheev committed Sep 23, 2024
1 parent d7b382c commit 40b4db8
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 114 deletions.
37 changes: 15 additions & 22 deletions src/Handler/Network/Udp/Client.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef HANDLERINTERNALUDPCLIENT_HPP
#define HANDLERINTERNALUDPCLIENT_HPP

#include <FlameIDE/../../src/Handler/Network/Udp/Config.hpp>
#include <FlameIDE/../../src/Handler/Network/Udp/Types.hpp>

#include <FlameIDE/Templates/Optional.hpp>
Expand All @@ -13,31 +12,25 @@ namespace flame_ide
{namespace udp
{

class Client
struct ClientMessage: public Message
{};
using ClientEndpoint = Endpoint<
::flame_ide::os::network::UdpClient, ClientMessage
, Constants::CLIENT_INPUT_QUEUE_SIZE, Constants::CLIENT_OUTPUT_QUEUE_SIZE
>;

class Client: public ClientEndpoint
{
public:
struct Message
inline ClientEndpoint::Optional &client() noexcept
{
::flame_ide::templates::StaticArray<
::flame_ide::byte_t, Constants::MESSAGE_SIZE
> bytes;
Types::ssize_t size = 0;
MessageState state = MessageState::EMPTY;
mutable os::threads::Spin spin;
};

public:
using Optional = ::flame_ide::templates::Optional<
::flame_ide::os::network::UdpClient
>;
return this->osEndpoint;
}

using ActualInput = ActualData<Message, Constants::CLIENT_INPUT_QUEUE_SIZE>;
using ActualOutput = ActualData<Message, Constants::CLIENT_OUTPUT_QUEUE_SIZE>;

public:
Optional client;
ActualInput input;
ActualOutput output;
inline const ClientEndpoint::Optional &client() const noexcept
{
return this->osEndpoint;
}
};

}}}} // namespace flame_ide::handler::network::udp
Expand Down
127 changes: 127 additions & 0 deletions src/Handler/Network/Udp/Endpoint.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#ifndef HANDLERINTERNALUDPENDPOINT_HPP
#define HANDLERINTERNALUDPENDPOINT_HPP

#include <FlameIDE/Common/Traits/Functional.hpp>
#include <FlameIDE/Os/Threads/Spin.hpp>

#include <FlameIDE/../../src/Handler/Network/Udp/Server.hpp>
#include <FlameIDE/../../src/Handler/Network/Udp/Client.hpp>

namespace flame_ide
{namespace handler
{namespace network
{namespace udp
{

// Container data

// WARNING: using UniquePointer because malloc doen't work with big sizes
using Servers = ::flame_ide::templates::StaticArray<
templates::UniquePointer<Server>, Constants::NUMBER_OF_SERVERS
>;

// WARNING: using UniquePointer because malloc doen't work with big sizes
using Clients = ::flame_ide::templates::StaticArray<
templates::UniquePointer<Client>, Constants::NUMBER_OF_CLIENTS
>;

using SocketDescriptors = ::flame_ide::templates::StaticArray<
::flame_ide::os::SocketDescriptor
, Constants::NUMBER_OF_SERVERS + Constants::NUMBER_OF_CLIENTS
>;

template<typename Container>
struct HandlerEndpointUdpData
{
templates::UniquePointer<Container> container = decltype(container)::makeEmpty();
os::threads::Spin spin;
};

// Matching traits

using ServerMatchingTrait = ::flame_ide::TypeMappingTrait<
os::network::UdpServer, Server
>;
using ClientMatchingTrait = ::flame_ide::TypeMappingTrait<
os::network::UdpClient, Client
>;

using ServerDataMatchingTrait = ::flame_ide::TypeMappingTrait<
Server, HandlerEndpointUdpData<Servers>
>;
using ClientDataMatchingTrait = ::flame_ide::TypeMappingTrait<
Client, HandlerEndpointUdpData<Clients>
>;

// Integral constants

template<typename EndpointType>
using IsServer = ::flame_ide::IntegralConstant<
bool
, ::flame_ide::ComparingTypes<
EndpointType, ::flame_ide::os::network::UdpServer
>::VALUE || ::flame_ide::ComparingTypes<EndpointType, Server>::VALUE
>;

template<typename EndpointType>
using IsClient = ::flame_ide::IntegralConstant<
bool
, ::flame_ide::ComparingTypes<
EndpointType, ::flame_ide::os::network::UdpClient
>::VALUE || ::flame_ide::ComparingTypes<EndpointType, Client>::VALUE
>;

template<typename T>
using IsCommonEndpoint = ::flame_ide::IntegralConstant<
bool, IsServer<T>::VALUE || IsClient<T>::VALUE
>;

template<typename T>
using IsHandlerEndpoint = ::flame_ide::IntegralConstant<
bool, ::flame_ide::ComparingTypes<T, Server>::VALUE
|| ::flame_ide::ComparingTypes<T, Client>::VALUE
>;
template<typename T>
using IsOsEndpoint = ::flame_ide::IntegralConstant<
bool
, ::flame_ide::ComparingTypes<T, ::flame_ide::os::network::UdpServer>::VALUE
|| ::flame_ide::ComparingTypes<T, ::flame_ide::os::network::UdpClient>::VALUE
>;

// Mappers

// Gets os::network::{ UdpServer, UdpCinet } <-> { Server, Client }
template<typename EndpointType>
using EndpointTypeMapper = ::flame_ide::TypeMapper<
EndpointType
, typename ::flame_ide::ChooseType<
IsServer<EndpointType>::VALUE, ServerMatchingTrait, ClientMatchingTrait
>::Type
>;

template<typename EndpointType>
using EndpointTypeMapper = ::flame_ide::TypeMapper<
EndpointType
, typename ::flame_ide::ChooseType<
IsServer<EndpointType>::VALUE, ServerMatchingTrait, ClientMatchingTrait
>::Type
>;

template<
typename HandlerEndpoint
, typename = typename ::flame_ide::EnableType<
IsHandlerEndpoint<HandlerEndpoint>::VALUE, HandlerEndpoint
>::Type
>
using HandlerEndpointDataMapper = ::flame_ide::TypeMapper<
HandlerEndpoint
, typename ::flame_ide::ChooseType<
IsServer<HandlerEndpoint>::VALUE
, ServerDataMatchingTrait
, ClientDataMatchingTrait
>::Type
>;

}}}} // namespace flame_ide::handler::network::udp

#endif // HANDLERINTERNALUDPENDPOINT_HPP
1 change: 1 addition & 0 deletions src/Handler/Network/Udp/Headers.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set (HEADER_LIST
${CMAKE_CURRENT_SOURCE_DIR}/Client.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Config.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Endpoint.hpp
${CMAKE_CURRENT_SOURCE_DIR}/InternalData.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Server.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Types.hpp
Expand Down
42 changes: 17 additions & 25 deletions src/Handler/Network/Udp/Server.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#ifndef HANDLERINTERNALUDPSERVER_HPP
#define HANDLERINTERNALUDPSERVER_HPP

#include <FlameIDE/../../src/Handler/Network/Udp/Config.hpp>
#include <FlameIDE/../../src/Handler/Network/Udp/Types.hpp>

#include <FlameIDE/Templates/Optional.hpp>
#include <FlameIDE/Os/Network/UdpServer.hpp>

namespace flame_ide
Expand All @@ -13,33 +11,27 @@ namespace flame_ide
{namespace udp
{

class Server
struct ServerMessage: public Message
{
public:
struct Message
{
mutable os::threads::Spin spin;

Types::ssize_t size = 0;
flame_ide::os::network::UdpServer::WithClient client;
flame_ide::templates::StaticArray<
flame_ide::byte_t, Constants::MESSAGE_SIZE
> bytes;
MessageState state = MessageState::EMPTY;
};
::flame_ide::os::network::UdpServer::WithClient client;
};
using ServerEndpoint = Endpoint<
::flame_ide::os::network::UdpServer, ServerMessage
, Constants::SERVER_INPUT_QUEUE_SIZE, Constants::SERVER_OUTPUT_QUEUE_SIZE
>;

class Server: public ServerEndpoint
{
public:
using Optional = flame_ide::templates::Optional<
flame_ide::os::network::UdpServer
>;

using ActualInput = ActualData<Message, Constants::SERVER_INPUT_QUEUE_SIZE>;
using ActualOutput = ActualData<Message, Constants::SERVER_OUTPUT_QUEUE_SIZE>;
inline ServerEndpoint::Optional &server() noexcept
{
return this->osEndpoint;
}

public:
Optional server;
ActualInput input;
ActualOutput output;
inline const ServerEndpoint::Optional &server() const noexcept
{
return this->osEndpoint;
}
};

}}}} // namespace flame_ide::handler::network::udp
Expand Down
121 changes: 121 additions & 0 deletions src/Handler/Network/Udp/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

#include <FlameIDE/Templates/Array.hpp>
#include <FlameIDE/Templates/Iterator/CircularIterator.hpp>
#include <FlameIDE/Templates/Optional.hpp>
#include <FlameIDE/Templates/Pointers.hpp>

#include <FlameIDE/Os/Threads/Spin.hpp>

#include <FlameIDE/../../src/Handler/Network/Udp/Config.hpp>

namespace flame_ide
{namespace handler
{namespace network
Expand All @@ -22,6 +25,17 @@ enum class MessageState
, READY
};

struct Message
{
::flame_ide::templates::StaticArray<
::flame_ide::byte_t, Constants::MESSAGE_SIZE
> bytes;
::flame_ide::Types::ssize_t size = 0;
MessageState state = MessageState::EMPTY;

mutable os::threads::Spin spin;
};

template<typename MessageType, Types::size_t SIZE>
struct ActualData
{
Expand All @@ -42,6 +56,113 @@ struct ActualData
mutable os::threads::Spin spin;
};

template<
typename EndpointData
, typename MessageType
, ::flame_ide::Types::size_t INPUT_SIZE
, ::flame_ide::Types::size_t OUTPUT_SIZE
>
class Endpoint
{
public:
using Data = EndpointData;
using Optional = flame_ide::templates::Optional<Data>;
using ActualInput = ActualData<MessageType, INPUT_SIZE>;
using ActualOutput = ActualData<MessageType, OUTPUT_SIZE>;

bool empty() const noexcept;
void attach(EndpointData &&data) noexcept;
EndpointData detach() noexcept;

Optional &endpoint() noexcept;
const Optional &endpoint() const noexcept;

ActualInput &input() noexcept;
ActualOutput &output() noexcept;

protected:
Optional osEndpoint;
ActualInput actualInput;
ActualOutput actualOutput;
};

}}}} // namespace flame_ide::handler::network::udp

namespace flame_ide
{namespace handler
{namespace network
{namespace udp
{

// Endpoint

template<
typename EndpointData
, typename MessageType
, ::flame_ide::Types::size_t INPUT_SIZE
, ::flame_ide::Types::size_t OUTPUT_SIZE
>
bool Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::empty() const noexcept
{
return !(osEndpoint);
}

template<
typename EndpointData
, typename MessageType
, ::flame_ide::Types::size_t INPUT_SIZE
, ::flame_ide::Types::size_t OUTPUT_SIZE
>
void Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::attach(
EndpointData &&data
) noexcept
{
if (!empty())
return;

osEndpoint.set(flame_ide::move(data));
}

template<
typename EndpointData
, typename MessageType
, ::flame_ide::Types::size_t INPUT_SIZE
, ::flame_ide::Types::size_t OUTPUT_SIZE
>
EndpointData
Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::detach() noexcept
{
if (empty())
return {};

Data data = flame_ide::move(osEndpoint.pull());
return data;
}

template<
typename EndpointData
, typename MessageType
, ::flame_ide::Types::size_t INPUT_SIZE
, ::flame_ide::Types::size_t OUTPUT_SIZE
>
typename Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::Optional &
Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::endpoint() noexcept
{
return osEndpoint;
}

template<
typename EndpointData
, typename MessageType
, ::flame_ide::Types::size_t INPUT_SIZE
, ::flame_ide::Types::size_t OUTPUT_SIZE
>
const typename Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::Optional &
Endpoint<EndpointData, MessageType, INPUT_SIZE, OUTPUT_SIZE>::endpoint() const noexcept
{
return osEndpoint;
}

}}}} // namespace flame_ide::handler::network::udp

#endif // HANDLERINTERNALUDPTYPES_HPP
Loading

0 comments on commit 40b4db8

Please sign in to comment.