10 #include "common/logger.h"
20 : local_ip_address_{local_ip_address},
21 local_port_num_{local_port_num} {
25 __FILE__, __LINE__, __func__, [&local_ip_address, &local_port_num](std::stringstream &msg) {
26 msg <<
"Tcp Socket Accepter created at "
27 <<
"<" << local_ip_address <<
"," << local_port_num <<
">";
34 Tcp::endpoint endpoint{};
38 static_cast<void>(
tcp_accepter_->accept(tcp_connection.GetSocket(), endpoint, ec));
39 if (ec.value() == boost::system::errc::success) {
41 __FILE__, __LINE__, __func__, [endpoint](std::stringstream &msg) {
42 msg <<
"Tcp Socket connection received from client "
43 <<
"<" << endpoint.address().to_string() <<
"," << endpoint.port() <<
">";
47 __FILE__, __LINE__, __func__,
48 [ec](std::stringstream &msg) { msg <<
"Tcp Socket Connect to client failed with error: " << ec.message(); });
50 return tcp_connection;
55 : tcp_socket_{io_context},
56 tcp_handler_read_{tcp_handler_read} {}
65 boost::asio::buffer(tcp_tx_message->GetTxBuffer(), std::size_t(tcp_tx_message->GetTxBuffer().size())), ec);
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() <<
">";
77 __FILE__, __LINE__, __func__,
78 [ec](std::stringstream &msg) { msg <<
"Tcp message sending failed with error: " << ec.message(); });
85 bool connection_closed{
false};
91 boost::asio::read(tcp_socket_, boost::asio::buffer(&rx_buffer[0],
kDoipheadrSize), ec);
93 if (ec.value() == boost::system::errc::success) {
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)));
103 boost::asio::read(tcp_socket_, boost::asio::buffer(&rx_buffer[
kDoipheadrSize], read_next_bytes), ec);
106 Tcp::endpoint endpoint_{tcp_socket_.remote_endpoint()};
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() <<
">";
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;
123 __FILE__, __LINE__, __func__,
124 [ec](std::stringstream &msg) { msg <<
"Remote Disconnected with undefined error: " << ec.message(); });
125 connection_closed =
true;
127 return connection_closed;
134 if (tcp_socket_.is_open()) {
135 tcp_socket_.shutdown(TcpSocket::shutdown_both, ec);
136 if (ec.value() == boost::system::errc::success) {
140 __FILE__, __LINE__, __func__,
141 [ec](std::stringstream &msg) { msg <<
"Tcp Socket Disconnection failed with error: " << ec.message(); });
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
TcpServerConnection(boost::asio::io_context &io_context, TcpHandlerRead &&tcp_handler_read)
bool Transmit(TcpMessageConstPtr udp_tx_message)
std::unique_ptr< TcpAccepter > tcp_accepter_
CreateTcpServerSocket(std::string_view local_ip_address, uint16_t local_port_num)
boost::asio::io_context io_context_
std::function< void(TcpMessagePtr)> TcpHandlerRead
TcpServerConnection GetTcpServerConnection(TcpHandlerRead &&tcp_handler_read)
std::vector< uint8_t > BufferType
Type alias for underlying buffer.
std::unique_ptr< const TcpMessage > TcpMessageConstPtr
The unique pointer to const TcpMessage.
boost::asio::ip::address TcpIpAddress
boost::system::error_code TcpErrorCodeType
constexpr std::uint8_t kDoipheadrSize
Doip HeaderSize.
std::unique_ptr< TcpMessage > TcpMessagePtr
The unique pointer to TcpMessage.