Diag-Client-Lib
udp_socket_handler.cpp
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  */
9 
12 
13 namespace doip_client {
14 namespace sockets {
15 UdpSocketHandler::UdpSocketHandler(std::string_view local_ip_address, std::uint16_t port_num, PortType port_type,
16  DoipUdpChannel &channel)
17  : local_ip_address_{local_ip_address},
18  local_port_num_{port_num},
19  port_type_{port_type},
20  channel_{channel} {
21  // create sockets and start receiving
22  if (port_type == UdpSocket::PortType::kUdp_Broadcast) {
23  udp_socket_ = std::make_unique<UdpSocket>(
25  [this](UdpMessagePtr udp_rx_message) { channel_.ProcessReceivedUdpBroadcast(std::move(udp_rx_message)); });
26  } else {
27  udp_socket_ = std::make_unique<UdpSocket>(
29  [this](UdpMessagePtr udp_rx_message) { channel_.ProcessReceivedUdpUnicast(std::move(udp_rx_message)); });
30  }
31 }
32 
34 
35 void UdpSocketHandler::Stop() { udp_socket_->Destroy(); }
36 
39  if (udp_socket_->Transmit(std::move(udp_message)).HasValue()) { result.EmplaceValue(); }
40  return result;
41 }
42 } // namespace sockets
43 } // namespace doip_client
PortType
Type of udp port to be used underneath.
Definition: udp_client.h:37
Class type to contains a value (of type ValueType), or an error (of type ErrorType)
Definition: result.h:29
Class to manage a udp channel as per DoIP protocol.
void ProcessReceivedUdpUnicast(UdpMessagePtr udp_rx_message)
Function to process the received Udp unicast message from socket layer.
void ProcessReceivedUdpBroadcast(UdpMessagePtr udp_rx_message)
Function to process the received Udp broadcast message from socket layer.
UdpSocketHandler(std::string_view local_ip_address, std::uint16_t port_num, PortType port_type, DoipUdpChannel &channel)
Constructs an instance of UdpSocketHandler.
core_type::Result< void > Transmit(UdpMessageConstPtr udp_message)
Function to transmit the provided udp message.
boost_support::socket::udp::UdpMessagePtr UdpMessagePtr
Type alias for Udp message pointer.
std::uint16_t local_port_num_
Store the local port number.
boost_support::socket::udp::UdpMessageConstPtr UdpMessageConstPtr
Type alias for Udp message const pointer.
std::string local_ip_address_
Store the local ip address.
DoipUdpChannel & channel_
Store the reference to tcp channel.
std::unique_ptr< UdpSocket > udp_socket_
Store the socket object.
void Stop()
Function to stop the socket handler.
void Start()
Function to start the socket handler.
UdpSocket::PortType port_type_
Store the port type.
core_type::ErrorCode MakeErrorCode(DoipErrorErrc, core_type::ErrorDomain::SupportDataType data) noexcept
Create a new ErrorCode within DoipErrorDomain.