Diag-Client-Lib
Public Types | Public Member Functions | Private Types | Private Attributes | List of all members
doip_client::sockets::TcpSocketHandler Class Referencefinal

Class used to create a tcp socket for handling transmission and reception of tcp message from driver. More...

#include <tcp_socket_handler.h>

Collaboration diagram for doip_client::sockets::TcpSocketHandler:
Collaboration graph
[legend]

Public Types

enum class  SocketHandlerState : std::uint8_t { kSocketOffline = 0U , kSocketOnline = 1U , kSocketConnected = 2U , kSocketDisconnected = 4U }
 Definitions of different socket state. More...
 
using TcpMessage = boost_support::socket::tcp::TcpMessage
 Type alias for Tcp message. More...
 
using TcpMessagePtr = boost_support::socket::tcp::TcpMessagePtr
 Type alias for Tcp message pointer. More...
 
using TcpMessageConstPtr = boost_support::socket::tcp::TcpMessageConstPtr
 Type alias for Tcp message const pointer. More...
 
using TcpChannel = channel::tcp_channel::DoipTcpChannel
 Type alias for Tcp message. More...
 

Public Member Functions

 TcpSocketHandler (std::string_view local_ip_address, TcpChannel &channel)
 Constructs an instance of TcpSocketHandler. More...
 
 ~TcpSocketHandler ()=default
 Destruct an instance of TcpSocketHandler. More...
 
void Start ()
 Function to start the socket handler. More...
 
void Stop ()
 Function to stop the socket handler. More...
 
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. More...
 
core_type::Result< void > DisconnectFromHost ()
 Function to disconnect from remote host if already connected. More...
 
core_type::Result< void > Transmit (TcpMessageConstPtr tcp_message)
 Function to transmit the provided tcp message. More...
 
SocketHandlerState GetSocketHandlerState () const
 Function to get the current state of socket handler. More...
 

Private Types

using TcpSocket = boost_support::socket::tcp::TcpClientSocket
 Type alias for tcp client socket. More...
 

Private Attributes

std::string local_ip_address_
 Store the local ip address. More...
 
std::uint16_t local_port_num_
 Store the local port number. More...
 
std::optional< TcpSockettcp_socket_
 Store the socket object. More...
 
TcpChannelchannel_
 Store the reference to tcp channel. More...
 
std::atomic< SocketHandlerStatestate_
 Store the state of handler. More...
 

Detailed Description

Class used to create a tcp socket for handling transmission and reception of tcp message from driver.

Definition at line 32 of file tcp_socket_handler.h.

Member Typedef Documentation

◆ TcpChannel

Type alias for Tcp message.

Definition at line 62 of file tcp_socket_handler.h.

◆ TcpMessage

Type alias for Tcp message.

Definition at line 47 of file tcp_socket_handler.h.

◆ TcpMessageConstPtr

Type alias for Tcp message const pointer.

Definition at line 57 of file tcp_socket_handler.h.

◆ TcpMessagePtr

Type alias for Tcp message pointer.

Definition at line 52 of file tcp_socket_handler.h.

◆ TcpSocket

Type alias for tcp client socket.

Definition at line 122 of file tcp_socket_handler.h.

Member Enumeration Documentation

◆ SocketHandlerState

Definitions of different socket state.

Enumerator
kSocketOffline 

Socket offline state

kSocketOnline 

Socket online state

kSocketConnected 

Socket connected to remote server

kSocketDisconnected 

Socket disconnected from remote server

Definition at line 37 of file tcp_socket_handler.h.

37  : std::uint8_t {
38  kSocketOffline = 0U,
39  kSocketOnline = 1U,
40  kSocketConnected = 2U,
41  kSocketDisconnected = 4U
42  };

Constructor & Destructor Documentation

◆ TcpSocketHandler()

doip_client::sockets::TcpSocketHandler::TcpSocketHandler ( std::string_view  local_ip_address,
TcpChannel channel 
)

Constructs an instance of TcpSocketHandler.

Parameters
[in]local_ip_addressThe local ip address
[in]channelThe reference to tcp transport handler

Definition at line 19 of file tcp_socket_handler.cpp.

20  : local_ip_address_{local_ip_address},
21  local_port_num_{0U}, // port number with "0" will create socket with random port number at client side
22  tcp_socket_{},
23  channel_{channel},
std::optional< TcpSocket > tcp_socket_
Store the socket object.
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.

◆ ~TcpSocketHandler()

doip_client::sockets::TcpSocketHandler::~TcpSocketHandler ( )
default

Destruct an instance of TcpSocketHandler.

Member Function Documentation

◆ ConnectToHost()

core_type::Result< void > doip_client::sockets::TcpSocketHandler::ConnectToHost ( std::string_view  host_ip_address,
std::uint16_t  host_port_num 
)

Function to connect to remote ip address and port number.

Parameters
[in]host_ip_addressThe host ip address
[in]host_port_numThe host port number
Returns
The

Definition at line 37 of file tcp_socket_handler.cpp.

37  {
40  tcp_socket_->Open()
41  .AndThen([this]() noexcept { state_.store(SocketHandlerState::kSocketOnline); })
42  .AndThen([this, &result, host_ip_address, host_port_num]() {
43  return tcp_socket_->ConnectToHost(host_ip_address, host_port_num).AndThen([this, &result]() {
45  result.EmplaceValue();
46  });
47  });
48  } else {
49  // already connected
50  result.EmplaceValue();
51  logger::DoipClientLogger::GetDiagClientLogger().GetLogger().LogVerbose(
52  __FILE__, __LINE__, __func__, [](std::stringstream &msg) { msg << "Tcp socket socket already connected"; });
53  }
54  return result;
55 }
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
static auto GetDiagClientLogger() noexcept -> DoipClientLogger &
Definition: logger.h:20
core_type::ErrorCode MakeErrorCode(DoipErrorErrc, core_type::ErrorDomain::SupportDataType data) noexcept
Create a new ErrorCode within DoipErrorDomain.

References doip_client::logger::DoipClientLogger::GetDiagClientLogger(), doip_client::error_domain::kGenericError, kSocketConnected, kSocketOnline, doip_client::error_domain::MakeErrorCode(), state_, and tcp_socket_.

Referenced by doip_client::channel::tcp_channel::DoipTcpChannel::ConnectToHost().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DisconnectFromHost()

core_type::Result< void > doip_client::sockets::TcpSocketHandler::DisconnectFromHost ( )

Function to disconnect from remote host if already connected.

Returns
The

Definition at line 57 of file tcp_socket_handler.cpp.

57  {
60  tcp_socket_->DisconnectFromHost()
61  .AndThen([this]() noexcept { state_.store(SocketHandlerState::kSocketDisconnected); })
62  .AndThen([this, &result]() noexcept {
63  return tcp_socket_->Destroy().AndThen([this, &result]() {
65  result.EmplaceValue();
66  });
67  });
68  } else {
69  // not connected
70  logger::DoipClientLogger::GetDiagClientLogger().GetLogger().LogDebug(
71  __FILE__, __LINE__, __func__,
72  [](std::stringstream &msg) { msg << "Tcp socket already in not connected state"; });
73  }
74  return result;
75 }

References doip_client::logger::DoipClientLogger::GetDiagClientLogger(), doip_client::error_domain::kGenericError, kSocketConnected, kSocketDisconnected, kSocketOffline, doip_client::error_domain::MakeErrorCode(), state_, and tcp_socket_.

Referenced by doip_client::channel::tcp_channel::DoipTcpChannel::DisconnectFromHost(), and Stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSocketHandlerState()

TcpSocketHandler::SocketHandlerState doip_client::sockets::TcpSocketHandler::GetSocketHandlerState ( ) const

Function to get the current state of socket handler.

Returns
The socket handler state

Definition at line 90 of file tcp_socket_handler.cpp.

90 { return state_.load(); }

References state_.

Referenced by doip_client::channel::tcp_channel::DoipTcpChannel::IsConnectToHost().

Here is the caller graph for this function:

◆ Start()

void doip_client::sockets::TcpSocketHandler::Start ( )

Function to start the socket handler.

Definition at line 26 of file tcp_socket_handler.cpp.

26  {
27  tcp_socket_.emplace(local_ip_address_, local_port_num_, [this](TcpMessagePtr tcp_message) {
28  channel_.ProcessReceivedTcpMessage(std::move(tcp_message));
29  });
30 }
void ProcessReceivedTcpMessage(TcpMessagePtr tcp_rx_message)
Function to process the received Tcp message from socket layer.
boost_support::socket::tcp::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.

References channel_, local_ip_address_, local_port_num_, doip_client::channel::tcp_channel::DoipTcpChannel::ProcessReceivedTcpMessage(), and tcp_socket_.

Referenced by doip_client::channel::tcp_channel::DoipTcpChannel::Start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Stop()

void doip_client::sockets::TcpSocketHandler::Stop ( )

Function to stop the socket handler.

Definition at line 32 of file tcp_socket_handler.cpp.

32  {
34  tcp_socket_.reset();
35 }
core_type::Result< void > DisconnectFromHost()
Function to disconnect from remote host if already connected.

References DisconnectFromHost(), kSocketOffline, state_, and tcp_socket_.

Referenced by doip_client::channel::tcp_channel::DoipTcpChannel::Stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Transmit()

core_type::Result< void > doip_client::sockets::TcpSocketHandler::Transmit ( TcpMessageConstPtr  tcp_message)

Function to transmit the provided tcp message.

Parameters
[in]tcp_messageThe tcp message
Returns
The

Definition at line 77 of file tcp_socket_handler.cpp.

77  {
80  if (tcp_socket_->Transmit(std::move(tcp_message)).HasValue()) { result.EmplaceValue(); }
81  } else {
82  // not connected
83  logger::DoipClientLogger::GetDiagClientLogger().GetLogger().LogError(
84  __FILE__, __LINE__, __func__,
85  [](std::stringstream &msg) { msg << "Tcp socket Offline, please connect to server first"; });
86  }
87  return result;
88 }

References doip_client::logger::DoipClientLogger::GetDiagClientLogger(), doip_client::error_domain::kGenericError, kSocketConnected, doip_client::error_domain::MakeErrorCode(), state_, and tcp_socket_.

Here is the call graph for this function:

Member Data Documentation

◆ channel_

TcpChannel& doip_client::sockets::TcpSocketHandler::channel_
private

Store the reference to tcp channel.

Definition at line 142 of file tcp_socket_handler.h.

Referenced by Start().

◆ local_ip_address_

std::string doip_client::sockets::TcpSocketHandler::local_ip_address_
private

Store the local ip address.

Definition at line 127 of file tcp_socket_handler.h.

Referenced by Start().

◆ local_port_num_

std::uint16_t doip_client::sockets::TcpSocketHandler::local_port_num_
private

Store the local port number.

Definition at line 132 of file tcp_socket_handler.h.

Referenced by Start().

◆ state_

std::atomic<SocketHandlerState> doip_client::sockets::TcpSocketHandler::state_
private

Store the state of handler.

Definition at line 147 of file tcp_socket_handler.h.

Referenced by ConnectToHost(), DisconnectFromHost(), GetSocketHandlerState(), Stop(), and Transmit().

◆ tcp_socket_

std::optional<TcpSocket> doip_client::sockets::TcpSocketHandler::tcp_socket_
private

Store the socket object.

Definition at line 137 of file tcp_socket_handler.h.

Referenced by ConnectToHost(), DisconnectFromHost(), Start(), Stop(), and Transmit().


The documentation for this class was generated from the following files: