Diag-Client-Lib
tcp_socket_handler.cpp
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  */
9 
10 #include <utility>
11 
13 #include "common/logger.h"
15 
16 namespace doip_client {
17 namespace sockets {
18 
19 TcpSocketHandler::TcpSocketHandler(std::string_view local_ip_address, TcpChannel &channel)
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},
24  state_{SocketHandlerState::kSocketOffline} {}
25 
27  tcp_socket_.emplace(local_ip_address_, local_port_num_, [this](TcpMessagePtr tcp_message) {
28  channel_.ProcessReceivedTcpMessage(std::move(tcp_message));
29  });
30 }
31 
34  tcp_socket_.reset();
35 }
36 
37 core_type::Result<void> TcpSocketHandler::ConnectToHost(std::string_view host_ip_address, std::uint16_t host_port_num) {
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 }
56 
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 }
76 
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 }
89 
91 
92 } // namespace sockets
93 } // namespace doip_client
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.
void ProcessReceivedTcpMessage(TcpMessagePtr tcp_rx_message)
Function to process the received Tcp message from socket layer.
static auto GetDiagClientLogger() noexcept -> DoipClientLogger &
Definition: logger.h:20
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.
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.
core_type::ErrorCode MakeErrorCode(DoipErrorErrc, core_type::ErrorDomain::SupportDataType data) noexcept
Create a new ErrorCode within DoipErrorDomain.