Diag-Client-Lib
tcp_client.h
Go to the documentation of this file.
1 /* Diagnostic Client library
2  * Copyright (C) 2024 Avijit Dey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 #ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_CLIENT_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_CLIENT_H_
10 // includes
11 #include <boost/asio.hpp>
12 #include <string>
13 #include <string_view>
14 #include <thread>
15 
16 #include "core/include/result.h"
17 #include "socket/tcp/tcp_message.h"
18 
19 namespace boost_support {
20 namespace socket {
21 namespace tcp {
22 
26 class TcpClientSocket final {
27  public:
31  enum class TcpErrorCode : std::uint8_t { kOpenFailed, kBindingFailed, kGenericError };
32 
36  using TcpHandlerRead = std::function<void(TcpMessagePtr)>;
37 
38  public:
48  TcpClientSocket(std::string_view local_ip_address, std::uint16_t local_port_num, TcpHandlerRead tcp_handler_read);
49 
54 
60 
69  core_type::Result<void, TcpErrorCode> ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num);
70 
76 
84 
90 
91  private:
95  using Tcp = boost::asio::ip::tcp;
96 
100  using TcpSocket = Tcp::socket;
101 
105  using TcpIpAddress = boost::asio::ip::address;
106 
110  using TcpErrorCodeType = boost::system::error_code;
111 
115  std::string local_ip_address_;
116 
120  std::uint16_t local_port_num_;
121 
125  boost::asio::io_context io_context_;
126 
131 
135  std::atomic_bool exit_request_;
136 
140  std::atomic_bool running_;
141 
145  std::condition_variable cond_var_;
146 
150  std::thread thread_;
151 
155  std::mutex mutex_;
156 
161 
162  private:
166  void HandleMessage();
167 };
168 } // namespace tcp
169 } // namespace socket
170 } // namespace boost_support
171 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_TCP_TCP_CLIENT_H_
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tcp_client.h:26
boost::system::error_code TcpErrorCodeType
Type alias for tcp error codes.
Definition: tcp_client.h:110
std::uint16_t local_port_num_
Store local port number.
Definition: tcp_client.h:120
std::function< void(TcpMessagePtr)> TcpHandlerRead
Tcp function template used for reception.
Definition: tcp_client.h:36
std::string local_ip_address_
Store local ip address.
Definition: tcp_client.h:115
TcpSocket tcp_socket_
Store tcp socket.
Definition: tcp_client.h:130
std::mutex mutex_
mutex to lock critical section
Definition: tcp_client.h:155
core_type::Result< void, TcpErrorCode > DisconnectFromHost()
Function to Disconnect from host.
Definition: tcp_client.cpp:122
TcpHandlerRead tcp_handler_read_
Store the handler.
Definition: tcp_client.h:160
boost::asio::io_context io_context_
boost io context
Definition: tcp_client.h:125
core_type::Result< void, TcpErrorCode > Destroy()
Function to destroy the socket.
Definition: tcp_client.cpp:165
Tcp::socket TcpSocket
Type alias for tcp socket.
Definition: tcp_client.h:100
core_type::Result< void, TcpErrorCode > Open()
Function to Open the socket.
Definition: tcp_client.cpp:55
std::atomic_bool exit_request_
Flag to terminate the thread.
Definition: tcp_client.h:135
std::atomic_bool running_
Flag to start the thread.
Definition: tcp_client.h:140
std::condition_variable cond_var_
Conditional variable to block the thread.
Definition: tcp_client.h:145
~TcpClientSocket()
Destruct an instance of TcpClientSocket.
Definition: tcp_client.cpp:48
boost::asio::ip::tcp Tcp
Type alias for tcp protocol.
Definition: tcp_client.h:95
core_type::Result< void, TcpErrorCode > Transmit(TcpMessageConstPtr tcp_message)
Function to trigger transmission.
Definition: tcp_client.cpp:142
void HandleMessage()
Function to handle the reception of tcp message.
Definition: tcp_client.cpp:173
core_type::Result< void, TcpErrorCode > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
Definition: tcp_client.cpp:94
TcpClientSocket(std::string_view local_ip_address, std::uint16_t local_port_num, TcpHandlerRead tcp_handler_read)
Constructs an instance of TcpClientSocket.
Definition: tcp_client.cpp:19
std::thread thread_
The thread itself.
Definition: tcp_client.h:150
boost::asio::ip::address TcpIpAddress
Type alias for tcp ip address.
Definition: tcp_client.h:105
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
std::unique_ptr< const TcpMessage > TcpMessageConstPtr
The unique pointer to const TcpMessage.
Definition: tcp_message.h:172
std::unique_ptr< TcpMessage > TcpMessagePtr
The unique pointer to TcpMessage.
Definition: tcp_message.h:177