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

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

#include <udp_client.h>

Public Types

enum class  UdpErrorCode : std::uint8_t { kOpenFailed , kBindingFailed , kGenericError }
 Udp error code. More...
 
enum class  PortType : std::uint8_t { kUdp_Broadcast = 0 , kUdp_Unicast }
 Type of udp port to be used underneath. More...
 
using UdpHandlerRead = std::function< void(UdpMessagePtr)>
 Udp function template used for reception. More...
 

Public Member Functions

 UdpClientSocket (std::string_view local_ip_address, std::uint16_t local_port_num, PortType port_type, UdpHandlerRead udp_handler_read)
 Constructs an instance of UdpClientSocket. More...
 
virtual ~UdpClientSocket ()
 Destruct an instance of UdpClientSocket. More...
 
core_type::Result< void, UdpErrorCodeOpen ()
 Function to Open the socket. More...
 
core_type::Result< void, UdpErrorCodeTransmit (UdpMessageConstPtr udp_message)
 Function to trigger transmission. More...
 
core_type::Result< void, UdpErrorCodeDestroy ()
 Function to destroy the socket. More...
 

Private Types

using Udp = boost::asio::ip::udp
 Type alias for udp protocol. More...
 
using UdpSocket = Udp::socket
 Type alias for udp socket. More...
 
using UdpIpAddress = boost::asio::ip::address
 Type alias for udp ip address. More...
 
using UdpErrorCodeType = boost::system::error_code
 Type alias for udp error codes. More...
 

Private Member Functions

void HandleMessage (const UdpErrorCodeType &error, std::size_t bytes_received)
 Function to handle the reception of tcp message. More...
 

Private Attributes

std::string local_ip_address_
 Store local ip address. More...
 
std::uint16_t local_port_num_
 Store local port number. More...
 
boost::asio::io_context io_context_
 boost io context More...
 
UdpSocket udp_socket_
 Store tcp socket. More...
 
std::atomic_bool exit_request_
 Flag to terminate the thread. More...
 
std::atomic_bool running_
 Flag to start the thread. More...
 
std::condition_variable cond_var_
 Conditional variable to block the thread. More...
 
std::thread thread_
 The thread itself. More...
 
std::mutex mutex_
 mutex to lock critical section More...
 
Udp::endpoint remote_endpoint_
 Store the remote endpoint. More...
 
PortType port_type_
 Store the port type - broadcast / unicast. More...
 
UdpHandlerRead udp_handler_read_
 Store the handler. More...
 
std::array< std::uint8_t, kDoipUdpResSizerx_buffer_
 Reception buffer needed for async reception of udp data. More...
 

Detailed Description

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

Definition at line 27 of file udp_client.h.

Member Typedef Documentation

◆ Udp

using boost_support::socket::udp::UdpClientSocket::Udp = boost::asio::ip::udp
private

Type alias for udp protocol.

Definition at line 88 of file udp_client.h.

◆ UdpErrorCodeType

using boost_support::socket::udp::UdpClientSocket::UdpErrorCodeType = boost::system::error_code
private

Type alias for udp error codes.

Definition at line 103 of file udp_client.h.

◆ UdpHandlerRead

Udp function template used for reception.

Definition at line 42 of file udp_client.h.

◆ UdpIpAddress

using boost_support::socket::udp::UdpClientSocket::UdpIpAddress = boost::asio::ip::address
private

Type alias for udp ip address.

Definition at line 98 of file udp_client.h.

◆ UdpSocket

Type alias for udp socket.

Definition at line 93 of file udp_client.h.

Member Enumeration Documentation

◆ PortType

Type of udp port to be used underneath.

Enumerator
kUdp_Broadcast 
kUdp_Unicast 

Definition at line 37 of file udp_client.h.

37 : std::uint8_t { kUdp_Broadcast = 0, kUdp_Unicast };

◆ UdpErrorCode

Udp error code.

Enumerator
kOpenFailed 
kBindingFailed 
kGenericError 

Definition at line 32 of file udp_client.h.

32 : std::uint8_t { kOpenFailed, kBindingFailed, kGenericError };

Constructor & Destructor Documentation

◆ UdpClientSocket()

boost_support::socket::udp::UdpClientSocket::UdpClientSocket ( std::string_view  local_ip_address,
std::uint16_t  local_port_num,
PortType  port_type,
UdpHandlerRead  udp_handler_read 
)

Constructs an instance of UdpClientSocket.

Parameters
[in]local_ip_addressThe local ip address
[in]local_port_numThe local port number
[in]port_typeThe type of socket port
[in]UdpHandlerReadThe handler to send received data to user

Definition at line 17 of file udp_client.cpp.

19  : local_ip_address_{local_ip_address},
20  local_port_num_{local_port_num},
21  io_context_{},
23  exit_request_{false},
24  running_{false},
25  cond_var_{},
26  mutex_{},
27  port_type_{port_type},
28  udp_handler_read_{std::move(udp_handler_read)},
29  rx_buffer_{} {
30  // Start thread to receive messages
31  thread_ = std::thread([this]() {
32  std::unique_lock<std::mutex> lck(mutex_);
33  while (!exit_request_) {
34  if (!running_) {
35  cond_var_.wait(lck, [this]() { return exit_request_ || running_; });
36  }
37  if (!exit_request_) {
38  if (running_) {
39  io_context_.restart();
40  io_context_.run();
41  }
42  }
43  }
44  });
45 }
std::atomic_bool running_
Flag to start the thread.
Definition: udp_client.h:133
std::atomic_bool exit_request_
Flag to terminate the thread.
Definition: udp_client.h:128
PortType port_type_
Store the port type - broadcast / unicast.
Definition: udp_client.h:158
std::string local_ip_address_
Store local ip address.
Definition: udp_client.h:108
std::condition_variable cond_var_
Conditional variable to block the thread.
Definition: udp_client.h:138
UdpSocket udp_socket_
Store tcp socket.
Definition: udp_client.h:123
std::thread thread_
The thread itself.
Definition: udp_client.h:143
std::uint16_t local_port_num_
Store local port number.
Definition: udp_client.h:113
boost::asio::io_context io_context_
boost io context
Definition: udp_client.h:118
std::array< std::uint8_t, kDoipUdpResSize > rx_buffer_
Reception buffer needed for async reception of udp data.
Definition: udp_client.h:168
std::mutex mutex_
mutex to lock critical section
Definition: udp_client.h:148
UdpHandlerRead udp_handler_read_
Store the handler.
Definition: udp_client.h:163

References cond_var_, exit_request_, io_context_, mutex_, running_, and thread_.

◆ ~UdpClientSocket()

boost_support::socket::udp::UdpClientSocket::~UdpClientSocket ( )
virtual

Destruct an instance of UdpClientSocket.

Definition at line 47 of file udp_client.cpp.

47  {
48  exit_request_ = true;
49  running_ = false;
50  cond_var_.notify_all();
51  thread_.join();
52 }

References cond_var_, exit_request_, running_, and thread_.

Member Function Documentation

◆ Destroy()

core_type::Result< void, UdpClientSocket::UdpErrorCode > boost_support::socket::udp::UdpClientSocket::Destroy ( )

Function to destroy the socket.

Returns
Empty result on success otherwise error code

Definition at line 147 of file udp_client.cpp.

147  {
149  // destroy the socket
150  udp_socket_.close();
151  running_ = false;
152  io_context_.stop();
153  result.EmplaceValue();
154  return result;
155 }
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29

References core_type::Result< T, E >::EmplaceValue().

Here is the call graph for this function:

◆ HandleMessage()

void boost_support::socket::udp::UdpClientSocket::HandleMessage ( const UdpErrorCodeType error,
std::size_t  bytes_received 
)
private

Function to handle the reception of tcp message.

Definition at line 158 of file udp_client.cpp.

158  {
159  // Check for error
160  if (error.value() == boost::system::errc::success) {
161  if (local_ip_address_ != remote_endpoint_.address().to_string()) {
162  UdpMessage::BufferType received_data{};
163  received_data.reserve(total_bytes_received);
164  // copy the received bytes into local buffer
165  received_data.insert(received_data.begin(), rx_buffer_.begin(), rx_buffer_.begin() + total_bytes_received);
166 
167  UdpMessagePtr udp_rx_message{std::make_unique<UdpMessage>(remote_endpoint_.address().to_string(),
168  remote_endpoint_.port(), std::move(received_data))};
169 
171  __FILE__, __LINE__, __func__, [this, &udp_rx_message](std::stringstream &msg) {
172  msg << "Udp Message received: "
173  << "<" << udp_rx_message->GetHostIpAddress() << "," << udp_rx_message->GetHostPortNumber() << ">"
174  << " -> "
175  << "<" << local_ip_address_ << "," << local_port_num_ << ">";
176  });
177 
178  // send data to upper layer
179  udp_handler_read_(std::move(udp_rx_message));
180  // start async receive
181  udp_socket_.async_receive_from(boost::asio::buffer(rx_buffer_), remote_endpoint_,
182  [this](const UdpErrorCodeType &error_received, std::size_t bytes_received) {
183  HandleMessage(error_received, bytes_received);
184  });
185  } else {
186  Udp::endpoint endpoint_{remote_endpoint_};
187  common::logger::LibBoostLogger::GetLibBoostLogger().GetLogger().LogVerbose(
188  __FILE__, __LINE__, __func__, [endpoint_, this](std::stringstream &msg) {
189  msg << "Udp Message received from "
190  << "<" << endpoint_.address().to_string() << "," << endpoint_.port() << ">"
191  << " ignored as received by self ip"
192  << " <" << local_ip_address_ << ">";
193  });
194  }
195  } else {
196  if (error.value() != boost::asio::error::operation_aborted) {
198  __FILE__, __LINE__, __func__, [error, this](std::stringstream &msg) {
199  msg << "<" << local_ip_address_ << ">: "
200  << "Remote Disconnected with undefined error: " << error.message();
201  });
202  }
203  }
204 }
static auto GetLibBoostLogger() noexcept -> LibBoostLogger &
Definition: logger.h:20
void HandleMessage(const UdpErrorCodeType &error, std::size_t bytes_received)
Function to handle the reception of tcp message.
Definition: udp_client.cpp:158
Udp::endpoint remote_endpoint_
Store the remote endpoint.
Definition: udp_client.h:153
boost::system::error_code UdpErrorCodeType
Type alias for udp error codes.
Definition: udp_client.h:103
std::vector< uint8_t > BufferType
Type alias for underlying buffer.
Definition: udp_message.h:36
std::unique_ptr< UdpMessage > UdpMessagePtr
The unique pointer to UdpMessage.
Definition: udp_message.h:139

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

Referenced by Open(), and Transmit().

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

◆ Open()

core_type::Result< void, UdpClientSocket::UdpErrorCode > boost_support::socket::udp::UdpClientSocket::Open ( )

Function to Open the socket.

Returns
Empty result on success otherwise error code

Definition at line 54 of file udp_client.cpp.

54  {
56  UdpErrorCodeType ec{};
57 
58  // Open the socket
59  udp_socket_.open(Udp::v4(), ec);
60  if (ec.value() == boost::system::errc::success) {
61  // set broadcast option
62  boost::asio::socket_base::broadcast broadcast_option(true);
63  udp_socket_.set_option(broadcast_option);
64  // reuse address
65  boost::asio::socket_base::reuse_address reuse_address_option(true);
66  udp_socket_.set_option(reuse_address_option);
67 
69  // Todo : change the hardcoded value of port number 13400
70  udp_socket_.bind(Udp::endpoint(boost::asio::ip::address_v4::any(), 13400), ec);
71  } else {
72  //bind to local address and random port
73  udp_socket_.bind(Udp::endpoint(UdpIpAddress::from_string(local_ip_address_), local_port_num_), ec);
74  }
75 
76  if (ec.value() == boost::system::errc::success) {
77  Udp::endpoint endpoint{udp_socket_.local_endpoint()};
79  __FILE__, __LINE__, __func__, [endpoint](std::stringstream &msg) {
80  msg << "Udp Socket Opened and bound to "
81  << "<" << endpoint.address().to_string() << "," << endpoint.port() << ">";
82  });
83  // Update the port number with new one
84  local_port_num_ = udp_socket_.local_endpoint().port();
85  { // start reading
86  std::lock_guard<std::mutex> lock{mutex_};
87  running_ = true;
88  cond_var_.notify_all();
89  }
90  // start async receive
91  udp_socket_.async_receive_from(
92  boost::asio::buffer(rx_buffer_), remote_endpoint_,
93  [this](const UdpErrorCodeType &error, std::size_t bytes_recvd) { HandleMessage(error, bytes_recvd); });
94  result.EmplaceValue();
95  } else {
96  // Socket binding failed
98  __FILE__, __LINE__, __func__,
99  [ec](std::stringstream &msg) { msg << "Udp Socket Bind failed with message: " << ec.message(); });
100  }
101  } else {
103  __FILE__, __LINE__, __func__,
104  [ec](std::stringstream &msg) { msg << "Udp Socket Opening failed with error: " << ec.message(); });
105  }
106  return result;
107 }

References cond_var_, boost_support::common::logger::LibBoostLogger::GetLibBoostLogger(), HandleMessage(), kGenericError, kUdp_Broadcast, local_ip_address_, local_port_num_, mutex_, port_type_, remote_endpoint_, running_, rx_buffer_, and udp_socket_.

Here is the call graph for this function:

◆ Transmit()

core_type::Result< void, UdpClientSocket::UdpErrorCode > boost_support::socket::udp::UdpClientSocket::Transmit ( UdpMessageConstPtr  udp_message)

Function to trigger transmission.

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

Definition at line 109 of file udp_client.cpp.

109  {
111  try {
112  // Transmit to remote endpoints
113  std::size_t send_size{udp_socket_.send_to(
114  boost::asio::buffer(udp_message->GetTxBuffer(), std::size_t(udp_message->GetTxBuffer().size())),
115  Udp::endpoint{UdpIpAddress::from_string(std::string{udp_message->GetHostIpAddress()}),
116  udp_message->GetHostPortNumber()})};
117  // Check for error
118  if (send_size == udp_message->GetTxBuffer().size()) {
119  // successful
120  Udp::endpoint endpoint{udp_socket_.local_endpoint()};
122  __FILE__, __LINE__, __func__, [&udp_message, endpoint](std::stringstream &msg) {
123  msg << "Udp message sent : "
124  << "<" << endpoint.address().to_string() << "," << endpoint.port() << ">"
125  << " -> "
126  << "<" << udp_message->GetHostIpAddress() << "," << udp_message->GetHostPortNumber() << ">";
127  });
128  result.EmplaceValue();
129  // start async receive
130  udp_socket_.async_receive_from(
131  boost::asio::buffer(rx_buffer_), remote_endpoint_,
132  [this](const UdpErrorCodeType &error, std::size_t bytes_received) { HandleMessage(error, bytes_received); });
133  }
134  } catch (boost::system::system_error const &ec) {
135  UdpErrorCodeType error = ec.code();
136  std::cerr << error.message() << "\n";
138  __FILE__, __LINE__, __func__, [error, &udp_message](std::stringstream &msg) {
139  msg << "Udp message sending to "
140  << "<" << udp_message->GetHostIpAddress() << "> "
141  << "failed with error: " << error.message();
142  });
143  }
144  return result;
145 }

References boost_support::common::logger::LibBoostLogger::GetLibBoostLogger(), HandleMessage(), kGenericError, remote_endpoint_, rx_buffer_, and udp_socket_.

Here is the call graph for this function:

Member Data Documentation

◆ cond_var_

std::condition_variable boost_support::socket::udp::UdpClientSocket::cond_var_
private

Conditional variable to block the thread.

Definition at line 138 of file udp_client.h.

Referenced by Open(), UdpClientSocket(), and ~UdpClientSocket().

◆ exit_request_

std::atomic_bool boost_support::socket::udp::UdpClientSocket::exit_request_
private

Flag to terminate the thread.

Definition at line 128 of file udp_client.h.

Referenced by UdpClientSocket(), and ~UdpClientSocket().

◆ io_context_

boost::asio::io_context boost_support::socket::udp::UdpClientSocket::io_context_
private

boost io context

Definition at line 118 of file udp_client.h.

Referenced by UdpClientSocket().

◆ local_ip_address_

std::string boost_support::socket::udp::UdpClientSocket::local_ip_address_
private

Store local ip address.

Definition at line 108 of file udp_client.h.

Referenced by Open().

◆ local_port_num_

std::uint16_t boost_support::socket::udp::UdpClientSocket::local_port_num_
private

Store local port number.

Definition at line 113 of file udp_client.h.

Referenced by Open().

◆ mutex_

std::mutex boost_support::socket::udp::UdpClientSocket::mutex_
private

mutex to lock critical section

Definition at line 148 of file udp_client.h.

Referenced by Open(), and UdpClientSocket().

◆ port_type_

PortType boost_support::socket::udp::UdpClientSocket::port_type_
private

Store the port type - broadcast / unicast.

Definition at line 158 of file udp_client.h.

Referenced by Open().

◆ remote_endpoint_

Udp::endpoint boost_support::socket::udp::UdpClientSocket::remote_endpoint_
private

Store the remote endpoint.

Definition at line 153 of file udp_client.h.

Referenced by Open(), and Transmit().

◆ running_

std::atomic_bool boost_support::socket::udp::UdpClientSocket::running_
private

Flag to start the thread.

Definition at line 133 of file udp_client.h.

Referenced by Open(), UdpClientSocket(), and ~UdpClientSocket().

◆ rx_buffer_

std::array<std::uint8_t, kDoipUdpResSize> boost_support::socket::udp::UdpClientSocket::rx_buffer_
private

Reception buffer needed for async reception of udp data.

Definition at line 168 of file udp_client.h.

Referenced by Open(), and Transmit().

◆ thread_

std::thread boost_support::socket::udp::UdpClientSocket::thread_
private

The thread itself.

Definition at line 143 of file udp_client.h.

Referenced by UdpClientSocket(), and ~UdpClientSocket().

◆ udp_handler_read_

UdpHandlerRead boost_support::socket::udp::UdpClientSocket::udp_handler_read_
private

Store the handler.

Definition at line 163 of file udp_client.h.

◆ udp_socket_

UdpSocket boost_support::socket::udp::UdpClientSocket::udp_socket_
private

Store tcp socket.

Definition at line 123 of file udp_client.h.

Referenced by Open(), and Transmit().


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