Diag-Client-Lib
udp_client.h
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  */
8 #ifndef DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_UDP_UDP_CLIENT_H_
9 #define DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_UDP_UDP_CLIENT_H_
10 // includes
11 #include <boost/asio.hpp>
12 #include <string>
13 #include <string_view>
14 #include <thread>
15 #include <vector>
16 
17 #include "core/include/result.h"
18 #include "socket/udp/udp_message.h"
19 
20 namespace boost_support {
21 namespace socket {
22 namespace udp {
23 
27 class UdpClientSocket final {
28  public:
32  enum class UdpErrorCode : std::uint8_t { kOpenFailed, kBindingFailed, kGenericError };
33 
37  enum class PortType : std::uint8_t { kUdp_Broadcast = 0, kUdp_Unicast };
38 
42  using UdpHandlerRead = std::function<void(UdpMessagePtr)>;
43 
44  public:
56  UdpClientSocket(std::string_view local_ip_address, std::uint16_t local_port_num, PortType port_type,
57  UdpHandlerRead udp_handler_read);
58 
62  virtual ~UdpClientSocket();
63 
69 
77 
83 
84  private:
88  using Udp = boost::asio::ip::udp;
89 
93  using UdpSocket = Udp::socket;
94 
98  using UdpIpAddress = boost::asio::ip::address;
99 
103  using UdpErrorCodeType = boost::system::error_code;
104 
108  std::string local_ip_address_;
109 
113  std::uint16_t local_port_num_;
114 
118  boost::asio::io_context io_context_;
119 
124 
128  std::atomic_bool exit_request_;
129 
133  std::atomic_bool running_;
134 
138  std::condition_variable cond_var_;
139 
143  std::thread thread_;
144 
148  std::mutex mutex_;
149 
153  Udp::endpoint remote_endpoint_;
154 
159 
164 
168  std::array<std::uint8_t, kDoipUdpResSize> rx_buffer_;
169 
170  private:
174  void HandleMessage(const UdpErrorCodeType &error, std::size_t bytes_received);
175 };
176 } // namespace udp
177 } // namespace socket
178 } // namespace boost_support
179 #endif // DIAG_CLIENT_LIB_LIB_BOOST_SUPPORT_SOCKET_UDP_UDP_CLIENT_H_
Class used to create a udp socket for handling transmission and reception of udp message from driver.
Definition: udp_client.h:27
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
virtual ~UdpClientSocket()
Destruct an instance of UdpClientSocket.
Definition: udp_client.cpp:47
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
std::function< void(UdpMessagePtr)> UdpHandlerRead
Udp function template used for reception.
Definition: udp_client.h:42
core_type::Result< void, UdpErrorCode > Destroy()
Function to destroy the socket.
Definition: udp_client.cpp:147
PortType port_type_
Store the port type - broadcast / unicast.
Definition: udp_client.h:158
PortType
Type of udp port to be used underneath.
Definition: udp_client.h:37
std::string local_ip_address_
Store local ip address.
Definition: udp_client.h:108
boost::asio::ip::address UdpIpAddress
Type alias for udp ip address.
Definition: udp_client.h:98
std::condition_variable cond_var_
Conditional variable to block the thread.
Definition: udp_client.h:138
boost::system::error_code UdpErrorCodeType
Type alias for udp error codes.
Definition: udp_client.h:103
boost::asio::ip::udp Udp
Type alias for udp protocol.
Definition: udp_client.h:88
UdpSocket udp_socket_
Store tcp socket.
Definition: udp_client.h:123
core_type::Result< void, UdpErrorCode > Transmit(UdpMessageConstPtr udp_message)
Function to trigger transmission.
Definition: udp_client.cpp:109
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
core_type::Result< void, UdpErrorCode > Open()
Function to Open the socket.
Definition: udp_client.cpp:54
Udp::socket UdpSocket
Type alias for udp socket.
Definition: udp_client.h:93
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.
Definition: udp_client.cpp:17
UdpHandlerRead udp_handler_read_
Store the handler.
Definition: udp_client.h:163
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
std::unique_ptr< const UdpMessage > UdpMessageConstPtr
The unique pointer to const UdpMessage.
Definition: udp_message.h:134
std::unique_ptr< UdpMessage > UdpMessagePtr
The unique pointer to UdpMessage.
Definition: udp_message.h:139