-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10e8013
commit c50ac5c
Showing
24 changed files
with
585 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 0 additions & 227 deletions
227
diag-client-lib/lib/boost-support/include/boost-support/client/tcp/tcp_client.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,227 +0,0 @@ | ||
/* 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/. | ||
*/ | ||
#ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_CLIENT_TCP_TCP_CLIENT_H_ | ||
#define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_CLIENT_TCP_TCP_CLIENT_H_ | ||
|
||
#include <atomic> | ||
#include <condition_variable> | ||
#include <functional> | ||
#include <memory> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
#include "core/include/result.h" | ||
|
||
namespace boost_support { | ||
namespace client { | ||
namespace tcp { | ||
|
||
/** | ||
* @brief Client class used to handle transmission and reception of tcp message from socket | ||
* @tparam SocketType | ||
* The type of socket to read and write from/to | ||
*/ | ||
template<typename SocketType> | ||
class TcpClient final { | ||
public: | ||
/** | ||
* @brief Type alias for Tcp message | ||
*/ | ||
using TcpMessage = typename SocketType::TcpMessage; | ||
|
||
/** | ||
* @brief Type alias for Tcp message pointer | ||
*/ | ||
using TcpMessagePtr = typename SocketType::TcpMessagePtr; | ||
|
||
/** | ||
* @brief Type alias for Tcp message const pointer | ||
*/ | ||
using TcpMessageConstPtr = typename SocketType::TcpMessageConstPtr; | ||
|
||
/** | ||
* @brief Tcp function template used for reception | ||
*/ | ||
using HandlerRead = std::function<void(TcpMessagePtr)>; | ||
|
||
public: | ||
/** | ||
* @brief Constructs an instance of TcpClient | ||
* @param[in] socket | ||
* The socket used for read and writing messages | ||
* @param[in] handler_read | ||
* The handler to send received data to user | ||
*/ | ||
explicit TcpClient(SocketType socket) noexcept | ||
: socket_{std::move(socket)}, | ||
handler_read_{}, | ||
exit_request_{false}, | ||
running_{false}, | ||
cond_var_{}, | ||
thread_{}, | ||
mutex_{} {} | ||
|
||
/** | ||
* @brief Deleted copy assignment and copy constructor | ||
*/ | ||
TcpClient(const TcpClient &other) noexcept = delete; | ||
TcpClient &operator=(const TcpClient &other) &noexcept = delete; | ||
|
||
/** | ||
* @brief Deleted move assignment and move constructor | ||
*/ | ||
TcpClient(TcpClient &&other) noexcept = delete; | ||
TcpClient &operator=(TcpClient &&other) &noexcept = delete; | ||
|
||
/** | ||
* @brief Destruct an instance of TcpClientSocket | ||
*/ | ||
~TcpClient() noexcept = default; | ||
|
||
/** | ||
* @brief Function to set the read handler that is invoked when message is received | ||
* @details The ownership of provided read handler is moved | ||
* @param[in] read_handler | ||
* The handler to be set | ||
*/ | ||
void SetReadHandler(HandlerRead read_handler) { handler_read_ = std::move(read_handler); } | ||
|
||
/** | ||
* @brief Initialize the client | ||
* @return Empty result on success otherwise error code | ||
*/ | ||
void Initialize() noexcept { | ||
// Open socket | ||
socket_.Open(); | ||
// Start thread to receive messages | ||
thread_ = std::thread([this]() { | ||
std::unique_lock<std::mutex> lck(mutex_); | ||
while (!exit_request_) { | ||
if (!running_) { | ||
cond_var_.wait(lck, [this]() { return exit_request_ || running_; }); | ||
} | ||
if (!exit_request_.load()) { | ||
if (running_) { | ||
lck.unlock(); | ||
running_ = ReadMessage(); | ||
lck.lock(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @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 result on success otherwise error code | ||
*/ | ||
auto ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num) noexcept -> bool { | ||
return socket_.Connect(host_ip_address, host_port_num) | ||
.AndThen([this]() noexcept { | ||
{ // start reading | ||
std::lock_guard<std::mutex> lock{mutex_}; | ||
running_ = true; | ||
cond_var_.notify_all(); | ||
} | ||
}) | ||
.HasValue(); | ||
} | ||
|
||
/** | ||
* @brief Function to Disconnect from host | ||
* @return Empty result on success otherwise error code | ||
*/ | ||
void DisconnectFromHost() noexcept { | ||
{ // stop reading | ||
std::lock_guard<std::mutex> lock{mutex_}; | ||
running_ = false; | ||
} | ||
socket_.Disconnect(); | ||
} | ||
|
||
/** | ||
* @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 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 | ||
*/ | ||
SocketType socket_; | ||
|
||
/** | ||
* @brief Store the handler | ||
*/ | ||
HandlerRead handler_read_; | ||
|
||
/** | ||
* @brief Flag to terminate the thread | ||
*/ | ||
std::atomic_bool exit_request_; | ||
|
||
/** | ||
* @brief Flag to start the thread | ||
*/ | ||
std::atomic_bool running_; | ||
|
||
/** | ||
* @brief Conditional variable to block the thread | ||
*/ | ||
std::condition_variable cond_var_; | ||
|
||
/** | ||
* @brief Store the thread | ||
*/ | ||
std::thread thread_; | ||
|
||
/** | ||
* @brief mutex to lock critical section | ||
*/ | ||
std::mutex mutex_; | ||
|
||
private: | ||
/** | ||
* @brief Function to send the received message to stored handler | ||
* @return True if message is read successfully, otherwise False | ||
*/ | ||
auto ReadMessage() -> bool { | ||
// Try reading from socket | ||
core_type::Result<TcpMessagePtr, typename SocketType::SocketError> read_result{socket_.Read()}; | ||
if (read_result.HasValue()) { | ||
if (handler_read_) { handler_read_(std::move(read_result).Value()); } | ||
} | ||
return read_result.HasValue(); | ||
} | ||
}; | ||
} // namespace tcp | ||
} // namespace client | ||
} // namespace boost_support | ||
#endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_CLIENT_TCP_TCP_CLIENT_H_ | ||
Empty file.
49 changes: 49 additions & 0 deletions
49
diag-client-lib/lib/boost-support/src/boost-support/connection/tcp/tcp_acceptor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* 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/connection/tcp/tcp_acceptor.h" | ||
|
||
#include "boost-support/common/logger.h" | ||
|
||
namespace boost_support { | ||
namespace connection { | ||
namespace tcp { | ||
|
||
TcpAcceptor::TcpAcceptor(std::string_view local_ip_address, std::uint16_t local_port_num, | ||
boost::asio::io_context io_context) noexcept | ||
: acceptor_{io_context, | ||
Tcp::endpoint(TcpIpAddress::from_string(std::string{local_ip_address}.c_str()), local_port_num)} { | ||
acceptor_.listen(); | ||
} | ||
|
||
std::optional<TcpAcceptor::Connection> TcpAcceptor::GetConnection(TcpAcceptor::HandlerRead read_handler) noexcept { | ||
using TcpErrorCodeType = boost::system::error_code; | ||
std::optional<Connection> tcp_connection{}; | ||
TcpErrorCodeType ec{}; | ||
Tcp::endpoint endpoint{}; | ||
|
||
//blocking accept | ||
TcpSocket::Socket accepted_socket{acceptor_.accept(endpoint, ec)}; | ||
if (ec.value() == boost::system::errc::success) { | ||
tcp_connection.emplace(TcpSocket{std::move(accepted_socket)}, std::move(read_handler)); | ||
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogDebug( | ||
__FILE__, __LINE__, __func__, [&endpoint](std::stringstream &msg) { | ||
msg << "Tcp socket connection received from client " | ||
<< "<" << endpoint.address().to_string() << "," << endpoint.port() << ">"; | ||
}); | ||
} else { | ||
common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogError( | ||
__FILE__, __LINE__, __func__, | ||
[ec](std::stringstream &msg) { msg << "Tcp socket accept failed with error: " << ec.message(); }); | ||
} | ||
return tcp_connection; | ||
} | ||
|
||
} // namespace tcp | ||
} // namespace connection | ||
} // namespace boost_support |
Oops, something went wrong.