Diag-Client-Lib
Public Member Functions | Private Attributes | List of all members
boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection Class Reference

#include <tcp_server.h>

Public Member Functions

 TcpServerConnection (boost::asio::io_context &io_context, TcpHandlerRead &&tcp_handler_read)
 
 ~TcpServerConnection ()=default
 
 TcpServerConnection (TcpServerConnection &&)=default
 
TcpServerConnectionoperator= (TcpServerConnection &&)=default
 
 TcpServerConnection (TcpServerConnection &)=delete
 
TcpServerConnectionoperator= (TcpServerConnection &)=delete
 
TcpSocketGetSocket ()
 
bool Transmit (TcpMessageConstPtr udp_tx_message)
 
bool ReceivedMessage ()
 
bool Shutdown ()
 

Private Attributes

TcpSocket tcp_socket_
 
TcpHandlerRead tcp_handler_read_
 

Detailed Description

Definition at line 31 of file tcp_server.h.

Constructor & Destructor Documentation

◆ TcpServerConnection() [1/3]

boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::TcpServerConnection ( boost::asio::io_context &  io_context,
TcpHandlerRead &&  tcp_handler_read 
)

◆ ~TcpServerConnection()

boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::~TcpServerConnection ( )
default

◆ TcpServerConnection() [2/3]

boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::TcpServerConnection ( TcpServerConnection &&  )
default

◆ TcpServerConnection() [3/3]

boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::TcpServerConnection ( TcpServerConnection )
delete

Member Function Documentation

◆ GetSocket()

TcpSocket & boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::GetSocket ( )

Definition at line 58 of file tcp_server.cpp.

58 { return tcp_socket_; }

◆ operator=() [1/2]

TcpServerConnection& boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::operator= ( TcpServerConnection &&  )
default

◆ operator=() [2/2]

TcpServerConnection& boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::operator= ( TcpServerConnection )
delete

◆ ReceivedMessage()

bool boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::ReceivedMessage ( )

Definition at line 83 of file tcp_server.cpp.

83  {
85  bool connection_closed{false};
86 
87  // create and reserve the buffer
88  TcpMessage::BufferType rx_buffer{};
89  rx_buffer.resize(kDoipheadrSize);
90  // start blocking read to read Header first
91  boost::asio::read(tcp_socket_, boost::asio::buffer(&rx_buffer[0], kDoipheadrSize), ec);
92  // Check for error
93  if (ec.value() == boost::system::errc::success) {
94  // read the next bytes to read
95  std::uint32_t const read_next_bytes = [&rx_buffer]() noexcept -> std::uint32_t {
96  return static_cast<std::uint32_t>((static_cast<std::uint32_t>(rx_buffer[4u] << 24u) & 0xFF000000) |
97  (static_cast<std::uint32_t>(rx_buffer[5u] << 16u) & 0x00FF0000) |
98  (static_cast<std::uint32_t>(rx_buffer[6u] << 8u) & 0x0000FF00) |
99  (static_cast<std::uint32_t>(rx_buffer[7u] & 0x000000FF)));
100  }();
101  // reserve the buffer
102  rx_buffer.resize(kDoipheadrSize + std::size_t(read_next_bytes));
103  boost::asio::read(tcp_socket_, boost::asio::buffer(&rx_buffer[kDoipheadrSize], read_next_bytes), ec);
104 
105  // all message received, transfer to upper layer
106  Tcp::endpoint endpoint_{tcp_socket_.remote_endpoint()};
107  TcpMessagePtr tcp_rx_message{
108  std::make_unique<TcpMessage>(endpoint_.address().to_string(), endpoint_.port(), std::move(rx_buffer))};
110  __FILE__, __LINE__, __func__, [endpoint_](std::stringstream &msg) {
111  msg << "Tcp Message received from "
112  << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
113  });
114  // send data to upper layer
115  tcp_handler_read_(std::move(tcp_rx_message));
116  } else if (ec.value() == boost::asio::error::eof) {
118  __FILE__, __LINE__, __func__,
119  [ec](std::stringstream &msg) { msg << "Remote Disconnected with: " << ec.message(); });
120  connection_closed = true;
121  } else {
123  __FILE__, __LINE__, __func__,
124  [ec](std::stringstream &msg) { msg << "Remote Disconnected with undefined error: " << ec.message(); });
125  connection_closed = true;
126  }
127  return connection_closed;
128 }
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
Definition: logger.h:20
std::vector< uint8_t > BufferType
Type alias for underlying buffer.
Definition: tcp_message.h:48
boost::system::error_code TcpErrorCodeType
Definition: tcp_server.cpp:17
constexpr std::uint8_t kDoipheadrSize
Doip HeaderSize.
Definition: tcp_message.h:182
std::unique_ptr< TcpMessage > TcpMessagePtr
The unique pointer to TcpMessage.
Definition: tcp_message.h:177

References boost_support::common::logger::LibBoostLogger::GetLibBoostLogger(), and boost_support::socket::tcp::kDoipheadrSize.

Here is the call graph for this function:

◆ Shutdown()

bool boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::Shutdown ( )

Definition at line 130 of file tcp_server.cpp.

130  {
131  TcpErrorCodeType ec{};
132  bool ret_val{false};
133  // Graceful shutdown
134  if (tcp_socket_.is_open()) {
135  tcp_socket_.shutdown(TcpSocket::shutdown_both, ec);
136  if (ec.value() == boost::system::errc::success) {
137  ret_val = true;
138  } else {
140  __FILE__, __LINE__, __func__,
141  [ec](std::stringstream &msg) { msg << "Tcp Socket Disconnection failed with error: " << ec.message(); });
142  }
143  tcp_socket_.close();
144  }
145  return ret_val;
146 }

References boost_support::common::logger::LibBoostLogger::GetLibBoostLogger().

Here is the call graph for this function:

◆ Transmit()

bool boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::Transmit ( TcpMessageConstPtr  udp_tx_message)

Definition at line 60 of file tcp_server.cpp.

60  {
61  TcpErrorCodeType ec{};
62  bool ret_val{false};
63  boost::asio::write(
65  boost::asio::buffer(tcp_tx_message->GetTxBuffer(), std::size_t(tcp_tx_message->GetTxBuffer().size())), ec);
66  // Check for error
67  if (ec.value() == boost::system::errc::success) {
68  Tcp::endpoint endpoint_{tcp_socket_.remote_endpoint()};
70  __FILE__, __LINE__, __func__, [endpoint_](std::stringstream &msg) {
71  msg << "Tcp message sent to "
72  << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">";
73  });
74  ret_val = true;
75  } else {
77  __FILE__, __LINE__, __func__,
78  [ec](std::stringstream &msg) { msg << "Tcp message sending failed with error: " << ec.message(); });
79  }
80  return ret_val;
81 }

References boost_support::common::logger::LibBoostLogger::GetLibBoostLogger().

Here is the call graph for this function:

Member Data Documentation

◆ tcp_handler_read_

TcpHandlerRead boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::tcp_handler_read_
private

Definition at line 66 of file tcp_server.h.

◆ tcp_socket_

TcpSocket boost_support::socket::tcp::CreateTcpServerSocket::TcpServerConnection::tcp_socket_
private

Definition at line 63 of file tcp_server.h.


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