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 c8c3b38
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 150 deletions.
11 changes: 11 additions & 0 deletions src/Handler/Network/Internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ namespace flame_ide
Handler::ExpectedServerHandle Handler::Udp::push(os::network::UdpServer &&server) noexcept
{
flame_ide::unused(server);

udp::Server *udpServer = udp.push(move(server));
flame_ide::unused(udpServer);
return { os::STATUS_FAILED };
}

Handler::ExpectedSessionHandle Handler::Udp::push(os::network::UdpClient &&client) noexcept
{
flame_ide::unused(client);

udp::Client *udpClient = udp.push(move(client));
flame_ide::unused(udpClient);
return { os::STATUS_FAILED };
}

Expand Down Expand Up @@ -85,6 +91,11 @@ Handler::Tcp &Handler::Internal::tcp() noexcept
return tcpData;
}

os::async::network::Registrar &Handler::Internal::registrar() noexcept
{
return registration;
}

os::Status Handler::Internal::start() noexcept
{
return workers.start();
Expand Down
4 changes: 4 additions & 0 deletions src/Handler/Network/Internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class Handler::Internal
/// @return
Tcp &tcp() noexcept;

/// @brief registrar
/// @return
os::async::network::Registrar &registrar() noexcept;

/// @brief start
/// @return
// TODO
Expand Down
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
45 changes: 22 additions & 23 deletions src/Handler/Network/Udp/InternalData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,14 @@ namespace flame_ide
{namespace callbacks
{

// client

// TODO
Types::ssize_t clientBytesToRead(void */*data*/) noexcept
{
return os::STATUS_FAILED;
}

// TODO
Types::ssize_t clientReceive(void */*data*/, templates::Range<byte_t *>) noexcept
{
return os::STATUS_FAILED;
}

// TODO
Types::ssize_t clientSend(void */*data*/, templates::Range<const byte_t *>) noexcept
{
return os::STATUS_FAILED;
}

// server

Types::ssize_t serverBytesToRead(const udp::ServerCommunicationData *data) noexcept
{
if (!data || !data->message)
return os::STATUS_FAILED;

os::threads::Locker{ data->message->spin };

os::threads::Locker locker{ data->message->spin };
return data->message->size;
}

Expand Down Expand Up @@ -83,7 +62,7 @@ serverSend(
return os::STATUS_FAILED;

auto &output = *data->output;
Server::Message *message = nullptr;
ServerMessage *message = nullptr;
{
os::threads::Locker locker{ output.spin };
message = output.last->pointer();
Expand Down Expand Up @@ -111,4 +90,24 @@ serverSend(
return rangeSize;
}

// client

// TODO
Types::ssize_t clientBytesToRead(void */*data*/) noexcept
{
return os::STATUS_FAILED;
}

// TODO
Types::ssize_t clientReceive(void */*data*/, templates::Range<byte_t *>) noexcept
{
return os::STATUS_FAILED;
}

// TODO
Types::ssize_t clientSend(void */*data*/, templates::Range<const byte_t *>) noexcept
{
return os::STATUS_FAILED;
}

}}}}} // namespace flame_ide::os::network::udp::callbacks
26 changes: 13 additions & 13 deletions src/Handler/Network/Udp/InternalData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ namespace flame_ide
struct ServerCommunicationData
{
os::network::UdpServer::WithClient client;
Server::Message *message;
ServerMessage *message;
Server::ActualOutput *output;
};

struct ClientCommunicationData
{
os::network::UdpClient *client;
Server::Message *message;
ServerMessage *message;
Server::ActualOutput *output;
};

Expand All @@ -33,17 +33,6 @@ namespace flame_ide
{namespace callbacks
{

// client

// TODO
Types::ssize_t clientBytesToRead(void *data) noexcept;

// TODO
Types::ssize_t clientReceive(void *data, templates::Range<byte_t *>) noexcept;

// TODO
Types::ssize_t clientSend(void *data, templates::Range<const byte_t *>) noexcept;

// server

Types::ssize_t serverBytesToRead(const udp::ServerCommunicationData *message) noexcept;
Expand All @@ -56,6 +45,17 @@ Types::ssize_t serverSend(
udp::ServerCommunicationData *data, templates::Range<const byte_t *>
) noexcept;

// client

// TODO
Types::ssize_t clientBytesToRead(void *data) noexcept;

// TODO
Types::ssize_t clientReceive(void *data, templates::Range<byte_t *>) noexcept;

// TODO
Types::ssize_t clientSend(void *data, templates::Range<const byte_t *>) noexcept;

}}}}} // namespace flame_ide::os::network::udp::callbacks

#endif // HANDLERINTERNALUDPDATA_HPP
Loading

0 comments on commit c8c3b38

Please sign in to comment.