Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Implementation of network handler #13

Draft
wants to merge 25 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f984296
Add handler submodule
kachsheev Oct 13, 2023
9ce1b74
Add Expected class to Common. Fix 'Utils' -> 'UtilsTest'
kachsheev Dec 24, 2023
475470c
Add implementation of network hadler
kachsheev Dec 24, 2023
57636fd
Merge branch 'dev' into Handler/Network
kachsheev Sep 12, 2024
b6b2b99
Move files and fix headers
kachsheev Sep 16, 2024
d7b382c
Add 'Common/TypeMapper' with tests
kachsheev Sep 17, 2024
b392d0b
Redesign UDP part of Hanler
kachsheev Sep 23, 2024
81169f6
Add pull() member function to Optional
kachsheev Sep 23, 2024
2cd47f2
Fix compilation errors and warnings
kachsheev Sep 23, 2024
b9022ce
Rename Handler tests
kachsheev Sep 24, 2024
7a17097
Fix build on MSVC 2017
kachsheev Sep 24, 2024
dc93f32
Change returning type in 'Handler::ServerHandle::getSessionHandle()'
kachsheev Sep 24, 2024
745f99a
Multiple changes
kachsheev Sep 24, 2024
282640a
Common/Expected: add 'ifResultGet()' and 'ifErrorGet()' for change da…
kachsheev Dec 14, 2024
6f514e1
Common/ReferenceWrapper: update, add default copy/move ctorss add ass…
kachsheev Dec 14, 2024
429815c
Handler/Network/Tests: fix tests location
kachsheev Dec 14, 2024
c765f10
Dump changes
kachsheev Dec 14, 2024
d7a833f
Templates/Array: fix compilation error
kachsheev Dec 14, 2024
6cb32a1
Os/Async/Network/Registrar: remove useless header
kachsheev Dec 14, 2024
00c0ebe
Os/Threads/ConditionVariable: remove tryWait and unwait
kachsheev Dec 14, 2024
52c6fae
Os/Threads/Counter: update
kachsheev Dec 14, 2024
ff0eb23
Templates/Object: fix compilation error
kachsheev Dec 14, 2024
59e777a
Templates/SimpleAlgorithms: fix
kachsheev Dec 14, 2024
f24ae30
Common/Tests: small fixes in FunctionTrait and VoidType tests
kachsheev Dec 14, 2024
7860296
Handler/Tests/Network/Udp: add test templates; add tests for UDP storage
kachsheev Dec 16, 2024
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
1 change: 1 addition & 0 deletions include/FlameIDE/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(HEADER_MODULES
${FLAME_INCLUDE_SUBMODLUES_PATH}/Common
${FLAME_INCLUDE_SUBMODLUES_PATH}/Constants
${FLAME_INCLUDE_SUBMODLUES_PATH}/Crypto
${FLAME_INCLUDE_SUBMODLUES_PATH}/Handler
${FLAME_INCLUDE_SUBMODLUES_PATH}/Os
${FLAME_INCLUDE_SUBMODLUES_PATH}/Others
# ${FLAME_INCLUDE_SUBMODLUES_PATH}/Streams
Expand Down
4 changes: 2 additions & 2 deletions include/FlameIDE/Common/Expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ Expected<ResultType, ErrorType>::operator=(Me &&expected) noexcept
{
expected.ifResult([this](ResultType &&result)
{
operator=(move(result));
this->operator=(move(result));
}).ifError([this](ErrorType &&error)
{
operator=(move(error));
this->operator=(move(error));
}).done();
return *this;
}
Expand Down
1 change: 0 additions & 1 deletion include/FlameIDE/Common/PrimitiveTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ using long_t = ::int64_t;
using float_t = float;
using double_t = double;
using ldouble_t = long double;

using ptrint_t = ::intptr_t;
using ptruint_t = ::uintptr_t;

Expand Down
38 changes: 38 additions & 0 deletions include/FlameIDE/Common/Traits/Functional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,44 @@ struct DoSomeByIndex<T, SIZE, SIZE>
{}
};

///
/// @brief The TypeMappingTrait class
/// @tparam T1
/// @tparam T2
///
template<typename T1, typename T2>
struct TypeMappingTrait
{
using Type1 = T1;
using Type2 = T2;
};

///
/// @brief The TypeMapper class
/// @tparam T
/// @tparam TypeMappingTrait
/// @tparam ENABLE_STATIC_ASSERT
///
template<typename T, typename TypeMappingTrait, bool ENABLE_STATIC_ASSERT = true>
struct TypeMapper
{
using Type = typename ChooseType<
ComparingTypes<T, typename TypeMappingTrait::Type1>::VALUE
, typename TypeMappingTrait::Type2
, typename ChooseType<
ComparingTypes<T, typename TypeMappingTrait::Type2>::VALUE
, typename TypeMappingTrait::Type1
, Empty
>::Type
>::Type;

static_assert(
!ENABLE_STATIC_ASSERT
|| ComparingTypes<Type, Empty>::VALUE == FalseType::VALUE
, "Type does not include in chosen TypeMatchingTrait"
);
};

}

#endif // FLAMEIDE_COMMON_TRAITS_FUCTIONAL_HPP
8 changes: 8 additions & 0 deletions include/FlameIDE/Handler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.14)

set(SOURCE_MODULES
${CMAKE_CURRENT_LIST_DIR}/Network
)
foreach(module ${SOURCE_MODULES})
add_subdirectory(${module})
endforeach()
20 changes: 20 additions & 0 deletions include/FlameIDE/Handler/Network/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.14)

set(NAME "Handler.Network")
set(NAME_ALIAS "Handler::Network")
set(LIBRARY_ALIAS_NAME ${FLAME_NAMESPACE}::${NAME_ALIAS}::Headers)
set(DEPENDENCY_LIST
${FLAME_NAMESPACE}::Common::Headers
${FLAME_NAMESPACE}::Os::Headers
${FLAME_NAMESPACE}::Templates::Headers
)

get_sources(FILE_LIST)
flame_header_library(
NAME "${NAME}"
LIBRARY_ALIAS_NAME "${LIBRARY_ALIAS_NAME}"
HEADER_LIST "${FILE_LIST}"
DEPENDENCY_TARGET_LIST "${DEPENDENCY_LIST}"
INCLUDE_PATHS "${FLAME_INCLUDE_PATH}"
INSTALL_SUBDIR "${FLAME_NAMESPACE}/${NAME}"
)
216 changes: 216 additions & 0 deletions include/FlameIDE/Handler/Network/Handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
#ifndef FLAMEIDE_OS_NETWORK_HANDLER_HPP
#define FLAMEIDE_OS_NETWORK_HANDLER_HPP

#include <FlameIDE/Common/Expected.hpp>
#include <FlameIDE/Common/Traits.hpp>

#include <FlameIDE/Os/Types.hpp>
#include <FlameIDE/Templates/Range.hpp>
#include <FlameIDE/Templates/Object.hpp>

namespace flame_ide
{namespace os
{namespace network
{

class UdpServer;
class UdpClient;

class TcpServer;
class TcpClient;

}}} // namespace flame_ide::os::network

namespace flame_ide
{namespace handler
{namespace network
{

///
/// @brief The Handler class
///
class Handler
{
public:
class Internal;
class Tcp;
class Udp;

public:
class ServerHandle;
class SessionHandle;

using ExpectedServerHandle = Expected<ServerHandle, os::Status>;
using ExpectedSessionHandle = Expected<SessionHandle, os::Status>;

using ExpectedUdpServer = Expected<os::network::UdpServer, os::Status>;
using ExpectedUdpClient = Expected<os::network::UdpClient, os::Status>;

using ExpectedTcpServer = Expected<os::network::TcpServer, os::Status>;
using ExpectedTcpClient = Expected<os::network::TcpClient, os::Status>;

public:
Handler() noexcept;
Handler(const Handler &) noexcept = delete;
Handler(Handler &&handler) noexcept;
~Handler() noexcept;

Handler &operator=(const Handler &) noexcept = delete;

Handler &operator=(Handler &&handler) noexcept;

/// @brief pushUdp
/// @param server
/// @return
ExpectedServerHandle pushUdp(os::network::UdpServer &&server) noexcept;

/// @brief pushUdp
/// @param client
/// @return
ExpectedSessionHandle pushUdp(os::network::UdpClient &&client) noexcept;

/// @brief pushTcp
/// @param server
/// @return
ExpectedServerHandle pushTcp(os::network::TcpServer &&server) noexcept;

/// @brief pushTcp
/// @param client
/// @return
ExpectedSessionHandle pushTcp(os::network::TcpClient &&client) noexcept;

/// @brief pop
/// @param server
/// @return
ExpectedUdpServer popUdp(ServerHandle &handle) noexcept;

/// @brief pop
/// @param client
/// @return
ExpectedUdpClient popUdp(SessionHandle &handle) noexcept;

/// @brief pop
/// @param server
/// @return
ExpectedTcpServer popTcp(ServerHandle &handle) noexcept;

/// @brief pop
/// @param client
/// @return
ExpectedTcpClient popTcp(SessionHandle &handle) noexcept;

/// @brief start
/// @return
os::Status start() noexcept;

/// @brief stop
/// @return
os::Status stop() noexcept;

private:
Internal *internal = nullptr;
};

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

// Handler::ServerHandle
namespace flame_ide
{namespace handler
{namespace network
{

class Handler::ServerHandle
{
public:
ServerHandle() noexcept = default;
ServerHandle(ServerHandle &&) noexcept = default;
~ServerHandle() noexcept = default;

ServerHandle &operator=(const ServerHandle &) noexcept = default;
ServerHandle &operator=(ServerHandle &&) noexcept = default;

operator bool() const noexcept;

///
/// @brief getSessionHandle
/// @return
///
Handler::ExpectedSessionHandle getSessionHandle() noexcept;

private:
friend class Handler::Internal;
friend class Handler::Udp;
friend class Handler::Tcp;

using CallbackGetSessionHandle = Handler::ExpectedSessionHandle (*)(void *);

private:
void *data = nullptr;
CallbackGetSessionHandle callbackGetSessionHandle = nullptr;
};

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

// Handler::SessionHandle
namespace flame_ide
{namespace handler
{namespace network
{

class Handler::SessionHandle
{
public:
SessionHandle() noexcept = default;
SessionHandle(SessionHandle &&) noexcept = default;
~SessionHandle() noexcept = default;

SessionHandle &operator=(SessionHandle &&) noexcept = default;

operator bool() const noexcept;

///
/// @brief bytesToRead
/// @return
///
Types::ssize_t bytesToRead() const noexcept;

///
/// @brief receive
/// @return
///
Types::ssize_t receive(flame_ide::templates::Range<byte_t *>) noexcept;

///
/// @brief send
/// @return
///
Types::ssize_t send(flame_ide::templates::Range<const byte_t *>) noexcept;

private:
friend class Handler::Internal;
friend class Handler::Udp;
friend class Handler::Tcp;
friend class Handler::ServerHandle;

private:
static constexpr Types::size_t OBJECT_SIZE = 64;

using Object = ::flame_ide::templates::Object<OBJECT_SIZE>;
using CallbackBytesToRead = ::flame_ide::Types::ssize_t (*)(const Object *);
using CallbackReceive = ::flame_ide::Types::ssize_t (*)(
Object *, ::flame_ide::templates::Range<byte_t *>
);
using CallbackSend = ::flame_ide::Types::ssize_t (*)(
Object *, ::flame_ide::templates::Range<const byte_t *>
);

private:
Object object;
CallbackBytesToRead callbackBytesToRead = nullptr;
CallbackReceive callbackReceive = nullptr;
CallbackSend callbackSend = nullptr;
};

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

#endif // FLAMEIDE_OS_NETWORK_HANDLER_HPP
3 changes: 3 additions & 0 deletions include/FlameIDE/Handler/Network/Headers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set (HEADER_LIST
${CMAKE_CURRENT_SOURCE_DIR}/Handler.hpp
)
6 changes: 4 additions & 2 deletions include/FlameIDE/Os/Network/NetworkBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <FlameIDE/Os/Network/Ipv4.hpp>
#include <FlameIDE/Os/Types.hpp>
#include <FlameIDE/Os/Constants.hpp>

#include <FlameIDE/Templates/Range.hpp>

Expand Down Expand Up @@ -38,6 +39,7 @@ class NetworkBase: public NonCopy
static const NativeCallbacks &callbacks() noexcept;

protected:
NetworkBase() noexcept = default;
NetworkBase(Socket socket) noexcept;
NetworkBase(NetworkBase &&base) noexcept;

Expand All @@ -54,10 +56,10 @@ class NetworkBase: public NonCopy
static Types::int_t checkStatus(Status status) noexcept;

protected:
Socket socket;
Socket socket = SOCKET_INVALID;

private:
Status status = 0;
Status status = STATUS_SUCCESS;
};

}}} // namespace flame_ide::os::network
Expand Down
3 changes: 2 additions & 1 deletion include/FlameIDE/Os/Network/TcpClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class TcpClient final: public NetworkBase
struct NativeCallbacks;

public:
TcpClient() noexcept = delete;
TcpClient() noexcept : NetworkBase() {}

TcpClient(const TcpClient &) noexcept = delete;

TcpClient(TcpClient &&client) noexcept;
Expand Down
2 changes: 1 addition & 1 deletion include/FlameIDE/Os/Network/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TcpServer final: public NetworkBase
struct NativeCallbacks;

public:
TcpServer() noexcept = delete;
TcpServer() noexcept : NetworkBase() {}
TcpServer(const TcpServer &tcpServer) noexcept = delete;

TcpServer(TcpServer &&tcpServer) noexcept;
Expand Down
2 changes: 2 additions & 0 deletions include/FlameIDE/Os/Network/UdpClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class UdpClient final: public NetworkBase
struct NativeCallbacks;

public:
UdpClient() noexcept : NetworkBase() {}

UdpClient(UdpClient &&udpClient) noexcept;
UdpClient(Ipv4 ip) noexcept;

Expand Down
2 changes: 2 additions & 0 deletions include/FlameIDE/Os/Network/UdpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class UdpServer final: public NetworkBase
struct NativeCallbacks;

public:
UdpServer() noexcept : NetworkBase() {}

UdpServer(UdpServer &&udpServer) noexcept;
UdpServer(Ipv4::Port port) noexcept;
~UdpServer() noexcept = default;
Expand Down
Loading