Diag-Client-Lib
Public Types | Public Member Functions | Private Types | Private Attributes | List of all members
boost_support::socket::tcp::TcpSocket Class Referencefinal

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

#include <tcp_socket.h>

Public Types

enum class  SocketError : std::uint8_t { kOpenFailed , kBindingFailed , kRemoteDisconnected , kGenericError }
 Socket error code. More...
 
using TcpMessage = boost_support::message::tcp::TcpMessage
 Type alias for Tcp message. More...
 
using TcpMessagePtr = boost_support::message::tcp::TcpMessagePtr
 Type alias for Tcp message pointer. More...
 
using TcpMessageConstPtr = boost_support::message::tcp::TcpMessageConstPtr
 Type alias for Tcp message const pointer. More...
 
using Tcp = boost::asio::ip::tcp
 Type alias for tcp protocol. More...
 
using Socket = Tcp::socket
 Type alias for tcp socket. More...
 

Public Member Functions

 TcpSocket (std::string_view local_ip_address, std::uint16_t local_port_num, IoContext &io_context) noexcept
 Constructs an instance of TcpSocket. More...
 
 TcpSocket (Socket socket) noexcept
 Constructs an instance of TcpSocket. More...
 
 TcpSocket (const TcpSocket &other) noexcept=delete
 Deleted copy assignment and copy constructor. More...
 
TcpSocketoperator= (const TcpSocket &other) noexcept=delete
 
 TcpSocket (TcpSocket &&other) noexcept=default
 Move assignment and Move constructor. More...
 
TcpSocketoperator= (TcpSocket &&other) noexcept=default
 
 ~TcpSocket () noexcept
 Destruct an instance of TcpSocket. More...
 
core_type::Result< void, SocketErrorOpen () noexcept
 Function to open and bind the socket to provided ip address & port. More...
 
core_type::Result< void, SocketErrorConnect (std::string_view host_ip_address, std::uint16_t host_port_num) noexcept
 Function to connect to remote ip address and port number. More...
 
core_type::Result< void, SocketErrorDisconnect () noexcept
 Function to Disconnect from host. More...
 
core_type::Result< void, SocketErrorTransmit (TcpMessageConstPtr tcp_message) noexcept
 Function to trigger transmission. More...
 
core_type::Result< TcpMessagePtr, SocketErrorRead () noexcept
 Function to read message from socket. More...
 
core_type::Result< void, SocketErrorClose () noexcept
 Function to destroy the socket. More...
 

Private Types

using TcpIpAddress = boost::asio::ip::address
 Type alias for tcp ip address. More...
 
using TcpErrorCodeType = boost::system::error_code
 Type alias for tcp error codes. More...
 

Private Attributes

Socket tcp_socket_
 Store the underlying tcp socket. More...
 
Tcp::endpoint local_endpoint_
 Store the local endpoints. More...
 

Detailed Description

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

Definition at line 24 of file tcp_socket.h.

Member Typedef Documentation

◆ Socket

Type alias for tcp socket.

Definition at line 59 of file tcp_socket.h.

◆ Tcp

using boost_support::socket::tcp::TcpSocket::Tcp = boost::asio::ip::tcp

Type alias for tcp protocol.

Definition at line 54 of file tcp_socket.h.

◆ TcpErrorCodeType

using boost_support::socket::tcp::TcpSocket::TcpErrorCodeType = boost::system::error_code
private

Type alias for tcp error codes.

Definition at line 150 of file tcp_socket.h.

◆ TcpIpAddress

using boost_support::socket::tcp::TcpSocket::TcpIpAddress = boost::asio::ip::address
private

Type alias for tcp ip address.

Definition at line 145 of file tcp_socket.h.

◆ TcpMessage

Type alias for Tcp message.

Definition at line 39 of file tcp_socket.h.

◆ TcpMessageConstPtr

Type alias for Tcp message const pointer.

Definition at line 49 of file tcp_socket.h.

◆ TcpMessagePtr

Type alias for Tcp message pointer.

Definition at line 44 of file tcp_socket.h.

Member Enumeration Documentation

◆ SocketError

Socket error code.

Enumerator
kOpenFailed 
kBindingFailed 
kRemoteDisconnected 
kGenericError 

Definition at line 29 of file tcp_socket.h.

29  : std::uint8_t {
30  kOpenFailed,
31  kBindingFailed,
32  kRemoteDisconnected,
34  };

Constructor & Destructor Documentation

◆ TcpSocket() [1/4]

boost_support::socket::tcp::TcpSocket::TcpSocket ( std::string_view  local_ip_address,
std::uint16_t  local_port_num,
IoContext io_context 
)
noexcept

Constructs an instance of TcpSocket.

Parameters
[in]local_ip_addressThe local ip address
[in]local_port_numThe local port number
[in]io_contextThe I/O context required to create socket

Definition at line 19 of file tcp_socket.cpp.

21  : tcp_socket_{io_context.GetContext()},
22  local_endpoint_{boost::asio::ip::make_address(local_ip_address), local_port_num} {}
Socket tcp_socket_
Store the underlying tcp socket.
Definition: tcp_socket.h:155
Tcp::endpoint local_endpoint_
Store the local endpoints.
Definition: tcp_socket.h:160

◆ TcpSocket() [2/4]

boost_support::socket::tcp::TcpSocket::TcpSocket ( TcpSocket::Socket  socket)
explicitnoexcept

Constructs an instance of TcpSocket.

Parameters
[in]socketThe socket

Definition at line 24 of file tcp_socket.cpp.

25  : tcp_socket_{std::move(socket)},
26  local_endpoint_{tcp_socket_.local_endpoint()} {}

◆ TcpSocket() [3/4]

boost_support::socket::tcp::TcpSocket::TcpSocket ( const TcpSocket other)
deletenoexcept

Deleted copy assignment and copy constructor.

◆ TcpSocket() [4/4]

boost_support::socket::tcp::TcpSocket::TcpSocket ( TcpSocket &&  other)
defaultnoexcept

Move assignment and Move constructor.

◆ ~TcpSocket()

boost_support::socket::tcp::TcpSocket::~TcpSocket ( )
defaultnoexcept

Destruct an instance of TcpSocket.

Member Function Documentation

◆ Close()

core_type::Result< void, TcpSocket::SocketError > boost_support::socket::tcp::TcpSocket::Close ( )
noexcept

Function to destroy the socket.

Returns
Empty result on success otherwise error code

Definition at line 141 of file tcp_socket.cpp.

141  {
143  // destroy the socket
144  tcp_socket_.shutdown(boost::asio::socket_base::shutdown_receive);
145  tcp_socket_.close();
146  result.EmplaceValue();
147  return result;
148 }
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29

References kGenericError, and tcp_socket_.

◆ Connect()

core_type::Result< void, TcpSocket::SocketError > boost_support::socket::tcp::TcpSocket::Connect ( std::string_view  host_ip_address,
std::uint16_t  host_port_num 
)
noexcept

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
Empty result on success otherwise error code

Definition at line 72 of file tcp_socket.cpp.

73  {
75  TcpErrorCodeType ec{};
76 
77  // Connect to provided Ip address
78  tcp_socket_.connect(
79  Tcp::endpoint(TcpIpAddress::from_string(std::string{host_ip_address}), host_port_num), ec);
80  if (ec.value() == boost::system::errc::success) {
82  FILE_NAME, __LINE__, __func__, [this](std::stringstream &msg) {
83  Tcp::endpoint const endpoint_{tcp_socket_.remote_endpoint()};
84  msg << "Tcp Socket connected to host "
85  << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
86  });
87  result.EmplaceValue();
88  } else {
90  FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
91  msg << "Tcp Socket connect to host failed with error: " << ec.message();
92  });
93  }
94  return result;
95 }
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
Definition: logger.h:20
boost::system::error_code TcpErrorCodeType
Type alias for tcp error codes.
Definition: tcp_socket.h:150
#define FILE_NAME
Definition: file_path.h:14

References FILE_NAME, and boost_support::common::logger::LibBoostLogger::GetLibBoostLogger().

Here is the call graph for this function:

◆ Disconnect()

core_type::Result< void, TcpSocket::SocketError > boost_support::socket::tcp::TcpSocket::Disconnect ( )
noexcept

Function to Disconnect from host.

Returns
Empty result on success otherwise error code

Definition at line 97 of file tcp_socket.cpp.

97  {
99  TcpErrorCodeType ec{};
100 
101  // Graceful shutdown
102  tcp_socket_.shutdown(Socket::shutdown_both, ec);
103  if (ec.value() == boost::system::errc::success) {
104  // Socket shutdown success
105  result.EmplaceValue();
106  } else {
108  FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
109  msg << "Tcp Socket disconnection from host failed with error: " << ec.message();
110  });
111  }
112  return result;
113 }

References FILE_NAME, boost_support::common::logger::LibBoostLogger::GetLibBoostLogger(), kGenericError, and tcp_socket_.

Here is the call graph for this function:

◆ Open()

core_type::Result< void, TcpSocket::SocketError > boost_support::socket::tcp::TcpSocket::Open ( )
noexcept

Function to open and bind the socket to provided ip address & port.

Returns
Empty result on success otherwise error code

Definition at line 30 of file tcp_socket.cpp.

30  {
32  TcpErrorCodeType ec{};
33 
34  // Open the socket
35  tcp_socket_.open(local_endpoint_.protocol(), ec);
36  if (ec.value() == boost::system::errc::success) {
37  // reuse address
38  tcp_socket_.set_option(boost::asio::socket_base::reuse_address{true});
39  // Set socket to non blocking
40  tcp_socket_.non_blocking(false);
41  // Bind to local ip address and random port
42  tcp_socket_.bind(local_endpoint_, ec);
43 
44  if (ec.value() == boost::system::errc::success) {
45  local_endpoint_ = tcp_socket_.local_endpoint();
46  // Socket binding success
48  FILE_NAME, __LINE__, __func__, [this](std::stringstream &msg) {
49  Tcp::endpoint const endpoint_{tcp_socket_.local_endpoint()};
50  msg << "Tcp Socket opened and bound to "
51  << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
52  });
53  result.EmplaceValue();
54  } else {
55  // Socket binding failed
57  FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
58  msg << "Tcp Socket binding failed with message: " << ec.message();
59  });
60  result.EmplaceError(SocketError::kBindingFailed);
61  }
62  } else {
64  FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
65  msg << "Tcp Socket opening failed with error: " << ec.message();
66  });
67  result.EmplaceError(SocketError::kOpenFailed);
68  }
69  return result;
70 }

References FILE_NAME, and boost_support::common::logger::LibBoostLogger::GetLibBoostLogger().

Here is the call graph for this function:

◆ operator=() [1/2]

TcpSocket& boost_support::socket::tcp::TcpSocket::operator= ( const TcpSocket other)
deletenoexcept

◆ operator=() [2/2]

TcpSocket& boost_support::socket::tcp::TcpSocket::operator= ( TcpSocket &&  other)
defaultnoexcept

◆ Read()

core_type::Result< TcpSocket::TcpMessagePtr, TcpSocket::SocketError > boost_support::socket::tcp::TcpSocket::Read ( )
noexcept

Function to read message from socket.

Returns
Tcp message on success otherwise error code

Definition at line 150 of file tcp_socket.cpp.

150  {
152  TcpErrorCodeType ec{};
153  // create and reserve the buffer
154  TcpMessage::BufferType rx_buffer{};
155  rx_buffer.resize(message::tcp::kDoipheadrSize);
156  // start blocking read to read Header first
157  boost::asio::read(tcp_socket_, boost::asio::buffer(&rx_buffer[0u], message::tcp::kDoipheadrSize),
158  ec);
159  // Check for error
160  if (ec.value() == boost::system::errc::success) {
161  // read the next bytes to read
162  std::uint32_t const read_next_bytes{[&rx_buffer]() noexcept -> std::uint32_t {
163  return static_cast<std::uint32_t>(
164  (static_cast<std::uint32_t>(rx_buffer[4u] << 24u) & 0xFF000000) |
165  (static_cast<std::uint32_t>(rx_buffer[5u] << 16u) & 0x00FF0000) |
166  (static_cast<std::uint32_t>(rx_buffer[6u] << 8u) & 0x0000FF00) |
167  (static_cast<std::uint32_t>(rx_buffer[7u] & 0x000000FF)));
168  }()};
169 
170  if (read_next_bytes != 0u) {
171  // reserve the buffer
172  rx_buffer.resize(message::tcp::kDoipheadrSize + std::size_t(read_next_bytes));
173  boost::asio::read(
174  tcp_socket_,
175  boost::asio::buffer(&rx_buffer[message::tcp::kDoipheadrSize], read_next_bytes), ec);
176 
177  Tcp::endpoint const remote_endpoint{tcp_socket_.remote_endpoint()};
178  TcpMessagePtr tcp_rx_message{std::make_unique<TcpMessage>(
179  remote_endpoint.address().to_string(), remote_endpoint.port(), std::move(rx_buffer))};
181  FILE_NAME, __LINE__, __func__, [&remote_endpoint](std::stringstream &msg) {
182  msg << "Tcp Message received from "
183  << "<" << remote_endpoint.address().to_string() << "," << remote_endpoint.port()
184  << ">";
185  });
186  result.EmplaceValue(std::move(tcp_rx_message));
187  } else {
189  FILE_NAME, __LINE__, __func__,
190  [](std::stringstream &msg) { msg << "Tcp Message read ignored as header size is zero"; });
191  }
192  } else if (ec.value() == boost::asio::error::eof) {
194  FILE_NAME, __LINE__, __func__,
195  [ec](std::stringstream &msg) { msg << "Remote Disconnected with: " << ec.message(); });
196  } else {
198  FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
199  msg << "Remote Disconnected with undefined error: " << ec.message();
200  });
201  }
202  return result;
203 }
std::vector< std::uint8_t > BufferType
Type alias for underlying buffer.
Definition: tcp_message.h:49
boost_support::message::tcp::TcpMessagePtr TcpMessagePtr
Type alias for Tcp message pointer.
Definition: tcp_socket.h:44
constexpr std::uint8_t kDoipheadrSize
Doip HeaderSize.
Definition: tcp_message.h:156

References FILE_NAME, boost_support::common::logger::LibBoostLogger::GetLibBoostLogger(), boost_support::message::tcp::kDoipheadrSize, kRemoteDisconnected, and tcp_socket_.

Here is the call graph for this function:

◆ Transmit()

core_type::Result< void, TcpSocket::SocketError > boost_support::socket::tcp::TcpSocket::Transmit ( TcpMessageConstPtr  tcp_message)
noexcept

Function to trigger transmission.

Parameters
[in]tcp_messageThe tcp message to be transmitted
Returns
Empty result on success otherwise error code

Definition at line 115 of file tcp_socket.cpp.

116  {
118  TcpErrorCodeType ec{};
119 
120  boost::asio::write(
121  tcp_socket_,
122  boost::asio::buffer(tcp_message->GetPayload().data(), tcp_message->GetPayload().size()), ec);
123  // Check for error
124  if (ec.value() == boost::system::errc::success) {
126  FILE_NAME, __LINE__, __func__, [this](std::stringstream &msg) {
127  Tcp::endpoint const endpoint_{tcp_socket_.remote_endpoint()};
128  msg << "Tcp message sent to "
129  << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
130  });
131  result.EmplaceValue();
132  } else {
134  FILE_NAME, __LINE__, __func__, [ec](std::stringstream &msg) {
135  msg << "Tcp message sending failed with error: " << ec.message();
136  });
137  }
138  return result;
139 }

References FILE_NAME, and boost_support::common::logger::LibBoostLogger::GetLibBoostLogger().

Here is the call graph for this function:

Member Data Documentation

◆ local_endpoint_

Tcp::endpoint boost_support::socket::tcp::TcpSocket::local_endpoint_
private

Store the local endpoints.

Definition at line 160 of file tcp_socket.h.

◆ tcp_socket_

Socket boost_support::socket::tcp::TcpSocket::tcp_socket_
private

Store the underlying tcp socket.

Definition at line 155 of file tcp_socket.h.

Referenced by Close(), Disconnect(), and Read().


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