Skip to content

Commit

Permalink
Implement public api TcpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Iandiehard committed Feb 5, 2024
1 parent af9c45b commit 132217f
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,110 @@
#ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_CLIENT_TCP_TCP_CLIENT_H_
#define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_INCLUDE_BOOST_SUPPORT_CLIENT_TCP_TCP_CLIENT_H_

#include "boost-support/connection/tcp/tcp_connection.h"
#include "boost-support/socket/tcp/tcp_socket.h"
#include "boost-support/socket/tls/tls_socket.h"
#include <functional>
#include <string_view>

#include "boost-support/client/tcp/tcp_message.h"
#include "core/include/result.h"

namespace boost_support {
namespace client {
namespace tcp {

using TcpClientUnsecure =
connection::tcp::TcpConnection<connection::tcp::ConnectionType::kClient, socket::tcp::TcpSocket>;
/**
* @brief Strong type for TLS version 1.2
*/
struct TlsVersion12 {};

/**
* @brief Strong type for TLS version 1.3
*/
struct TlsVersion13 {};

/**
* @brief Client that manages unsecured/ secured tcp connection
*/
class TcpClient final {
public:
/**
* @brief Tcp function template used for reception
*/
using HandlerRead = std::function<void(TcpMessagePtr)>;

public:
/**
* @brief Constructs an instance of TcpClient
* @param[in] ClientType
* The type of client
*/
TcpClient(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept;


TcpClient(std::string_view local_ip_address, std::uint16_t local_port_num, TlsVersion12,
std::string_view ca_certification_path) noexcept;

/**
* @brief Deleted copy assignment and copy constructor
*/
TcpClient(const TcpClient &other) noexcept = delete;
TcpClient &operator=(const TcpClient &other) noexcept = delete;

/**
* @brief Defaulted move assignment and move constructor
*/
TcpClient(TcpClient &&other) noexcept = default;
TcpClient &operator=(TcpClient &&other) noexcept = default;

/**
* @brief Destruct an instance of TcpClient
*/
~TcpClient() noexcept;

/**
* @brief Initialize the client
*/
void Initialize() noexcept;

/**
* @brief De-initialize the client
*/
void DeInitialize() noexcept;

/**
* @brief Function to connect to remote ip address and port number
* @param[in] host_ip_address
* The host ip address
* @param[in] host_port_num
* The host port number
* @return Empty void on success, otherwise error is returned
*/
core_type::Result<void> ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num);

/**
* @brief Function to disconnect from remote host if already connected
* @return Empty void on success, otherwise error is returned
*/
core_type::Result<void> DisconnectFromHost();

/**
* @brief Function to transmit the provided tcp message
* @param[in] tcp_message
* The tcp message
* @return Empty void on success, otherwise error is returned
*/
core_type::Result<void> Transmit(TcpMessageConstPtr tcp_message);

private:
/**
* @brief Forward declaration of tcp client implementation
*/
class TcpClientImpl;

using TcpClientSecured =
connection::tcp::TcpConnection<connection::tcp::ConnectionType::kServer, socket::tcp::TcpSocket>;
/**
* @brief Unique pointer to tcp client implementation
*/
std::unique_ptr<TcpClientImpl> tcp_client_impl_;
};

} // namespace tcp
} // namespace client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "core/include/span.h"

namespace boost_support {
namespace socket {
namespace client {
namespace tcp {

/**
Expand Down Expand Up @@ -182,6 +182,6 @@ using TcpMessagePtr = std::unique_ptr<TcpMessage>;
constexpr std::uint8_t kDoipheadrSize = 8U;

} // namespace tcp
} // namespace socket
} // namespace client
} // namespace boost_support
#endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_MESSAGE_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/* Diagnostic Client library
* Copyright (C) 2023 Avijit Dey
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "boost-support/client/tcp/tcp_client.h"

#include <variant>

#include "boost-support/connection/tcp/tcp_connection.h"
#include "boost-support/socket/io_context.h"
#include "boost-support/socket/tcp/tcp_socket.h"
#include "boost-support/socket/tls/tls_socket.h"

namespace boost_support {
namespace client {
namespace tcp {

/**
* @brief Class to provide implementation of tcp client
*/
class TcpClient::TcpClientImpl final {
private:
/**
* @brief Type alias for unsecured tcp connection
*/
using TcpConnectionUnsecured =
connection::tcp::TcpConnection<connection::tcp::ConnectionType::kClient, socket::tcp::TcpSocket>;

using TlsSocket12 = socket::tls::TlsSocket<socket::tls::TlsVersionType::kTls12>;

/**
* @brief Type alias for secured tcp connection with tls version 1.2
*/
using TcpConnectionSecuredTls12 =
connection::tcp::TcpConnection<connection::tcp::ConnectionType::kClient, TlsSocket12>;

/**
* @brief Type alias for secured tcp connection with tls version 1.3
*/
using TcpConnectionSecuredTls13 =
connection::tcp::TcpConnection<connection::tcp::ConnectionType::kClient,
socket::tls::TlsSocket<socket::tls::TlsVersionType::kTls13>>;

/**
* @brief Type alias for boost context
*/
using IoContext = boost_support::socket::IoContext;

public:
/**
* @brief Constructs an instance of TcpClient
* @param[in] ClientType
* The type of client
*/
TcpClientImpl(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept
: io_context_{},
tcp_connection_{std::in_place_type<TcpConnectionUnsecured>,
socket::tcp::TcpSocket{local_ip_address, local_port_num, io_context_.GetContext()}} {}

TcpClientImpl(std::string_view local_ip_address, std::uint16_t local_port_num, TlsVersion12,
std::string_view ca_certification_path) noexcept
: io_context_{},
tcp_connection_{std::in_place_type<TcpConnectionSecuredTls12>,
TlsSocket12{local_ip_address, local_port_num, ca_certification_path, io_context_.GetContext()}} {
//TcpConnectionSecuredTls12 test{TlsSocket12{local_ip_address, local_port_num, ca_certification_path, io_context_.GetContext()}};



//tcp_connection_.emplace<TcpConnectionSecuredTls12>(TlsSocket12{local_ip_address, local_port_num, ca_certification_path, io_context_.GetContext()});
}

/**
* @brief Deleted copy assignment and copy constructor
*/
TcpClientImpl(const TcpClientImpl &other) noexcept = delete;
TcpClientImpl &operator=(const TcpClientImpl &other) noexcept = delete;

/**
* @brief Deleted move assignment and move constructor
*/
TcpClientImpl(TcpClientImpl &&other) noexcept = delete;
TcpClientImpl &operator=(TcpClientImpl &&other) noexcept = delete;

/**
* @brief Destruct an instance of TcpClient
*/
~TcpClientImpl() noexcept = default;

private:
/**
* @brief Stores the io context
*/
IoContext io_context_;

/**
* @brief Store the tcp connection
*/
std::variant<std::monostate, TcpConnectionUnsecured, TcpConnectionSecuredTls12, TcpConnectionSecuredTls13> tcp_connection_;
};

TcpClient::TcpClient(std::string_view local_ip_address, std::uint16_t local_port_num) noexcept {}

TcpClient::TcpClient(std::string_view local_ip_address, std::uint16_t local_port_num, TlsVersion12,
std::string_view ca_certification_path) noexcept {}

TcpClient::~TcpClient() noexcept = default;

void TcpClient::Initialize() noexcept {
}

void TcpClient::DeInitialize() noexcept {}

core_type::Result<void> TcpClient::ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num) {
return core_type::Result<void>();
}

core_type::Result<void> TcpClient::DisconnectFromHost() { return core_type::Result<void>(); }

core_type::Result<void> TcpClient::Transmit(TcpMessageConstPtr tcp_message) { return core_type::Result<void>(); }

} // namespace tcp
} // namespace client
} // namespace boost_support
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class TcpConnection<ConnectionType::kClient, Socket> final {

/**
* @brief Initialize the client
* @return Empty result on success otherwise error code
*/
void Initialize() noexcept {
// Open socket
Expand All @@ -128,6 +127,20 @@ class TcpConnection<ConnectionType::kClient, Socket> final {
});
}

/**
* @brief De-initialize the client
*/
void DeInitialize() noexcept {
socket_.Close();
{
std::unique_lock<std::mutex> lck(mutex_);
exit_request_ = true;
running_ = false;
}
cond_var_.notify_all();
thread_.join();
}

/**
* @brief Function to connect to remote ip address and port number
* @param[in] host_ip_address
Expand Down Expand Up @@ -168,21 +181,6 @@ class TcpConnection<ConnectionType::kClient, Socket> final {
*/
auto Transmit(TcpMessageConstPtr message) noexcept -> bool { return socket_.Transmit(std::move(message)).HasValue(); }

/**
* @brief De-initialize the client
* @return Empty result on success otherwise error code
*/
void DeInitialize() noexcept {
socket_.Close();
{
std::unique_lock<std::mutex> lck(mutex_);
exit_request_ = true;
running_ = false;
}
cond_var_.notify_all();
thread_.join();
}

private:
/**
* @brief Store socket used for reading and writing tcp message
Expand Down Expand Up @@ -307,8 +305,7 @@ class TcpConnection<ConnectionType::kServer, Socket> final {
~TcpConnection() noexcept = default;

/**
* @brief Initialize the client
* @return Empty result on success otherwise error code
* @brief Initialize the Server
*/
void Initialize() noexcept {
// Open socket
Expand All @@ -329,17 +326,8 @@ class TcpConnection<ConnectionType::kServer, Socket> final {
});
}

/**
* @brief Function to trigger transmission
* @param[in] message
* The tcp message to be transmitted
* @return Empty result on success otherwise error code
*/
auto Transmit(TcpMessageConstPtr message) noexcept -> bool { return socket_.Transmit(std::move(message)).HasValue(); }

/**
* @brief De-initialize the server
* @return Empty result on success otherwise error code
*/
void DeInitialize() noexcept {
socket_.Close();
Expand All @@ -352,6 +340,14 @@ class TcpConnection<ConnectionType::kServer, Socket> final {
thread_.join();
}

/**
* @brief Function to trigger transmission
* @param[in] message
* The tcp message to be transmitted
* @return Empty result on success otherwise error code
*/
auto Transmit(TcpMessageConstPtr message) noexcept -> bool { return socket_.Transmit(std::move(message)).HasValue(); }

private:
/**
* @brief Store socket used for reading and writing tcp message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <string_view>
#include <vector>

#include "boost-support/socket/tcp/tcp_message.h"
#include "boost-support/client/tcp/tcp_message.h"

namespace boost_support {
namespace socket {
Expand Down
Loading

0 comments on commit 132217f

Please sign in to comment.