Diag-Client-Lib
tcp_socket_handler.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_DOIP_CLIENT_SOCKETS_TCP_SOCKET_HANDLER_H_
9 #define DIAG_CLIENT_LIB_LIB_DOIP_CLIENT_SOCKETS_TCP_SOCKET_HANDLER_H_
10 
11 #include <atomic>
12 #include <optional>
13 #include <string>
14 #include <string_view>
15 
16 #include "core/include/result.h"
17 #include "socket/tcp/tcp_client.h"
18 
19 namespace doip_client {
20 // forward declaration
21 namespace channel {
22 namespace tcp_channel {
23 class DoipTcpChannel;
24 } // namespace tcp_channel
25 } // namespace channel
26 
27 namespace sockets {
28 
32 class TcpSocketHandler final {
33  public:
37  enum class SocketHandlerState : std::uint8_t {
38  kSocketOffline = 0U,
39  kSocketOnline = 1U,
40  kSocketConnected = 2U,
42  };
43 
48 
53 
58 
63 
71  TcpSocketHandler(std::string_view local_ip_address, TcpChannel &channel);
72 
76  ~TcpSocketHandler() = default;
77 
81  void Start();
82 
86  void Stop();
87 
96  core_type::Result<void> ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num);
97 
103 
111 
117 
118  private:
123 
127  std::string local_ip_address_;
128 
132  std::uint16_t local_port_num_;
133 
137  std::optional<TcpSocket> tcp_socket_;
138 
143 
147  std::atomic<SocketHandlerState> state_;
148 };
149 } // namespace sockets
150 } // namespace doip_client
151 #endif // DIAG_CLIENT_LIB_LIB_DOIP_CLIENT_SOCKETS_TCP_SOCKET_HANDLER_H_
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
Definition: tcp_client.h:26
Immutable class to store received tcp message.
Definition: tcp_message.h:25
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
Class to manage a tcp channel as per DoIP protocol.
Class used to create a tcp socket for handling transmission and reception of tcp message from driver.
boost_support::socket::tcp::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.
boost_support::socket::tcp::TcpMessageConstPtr TcpMessageConstPtr
Type alias for Tcp message const pointer.
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.
SocketHandlerState
Definitions of different socket state.
TcpSocketHandler(std::string_view local_ip_address, TcpChannel &channel)
Constructs an instance of TcpSocketHandler.
void Stop()
Function to stop the socket handler.
SocketHandlerState GetSocketHandlerState() const
Function to get the current state of socket handler.
void Start()
Function to start the socket handler.
~TcpSocketHandler()=default
Destruct an instance of TcpSocketHandler.
std::optional< TcpSocket > tcp_socket_
Store the socket object.
core_type::Result< void > Transmit(TcpMessageConstPtr tcp_message)
Function to transmit the provided tcp message.
core_type::Result< void > ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num)
Function to connect to remote ip address and port number.
std::string local_ip_address_
Store the local ip address.
TcpChannel & channel_
Store the reference to tcp channel.
std::uint16_t local_port_num_
Store the local port number.
std::atomic< SocketHandlerState > state_
Store the state of handler.
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