Woman, Life, Freedom


Async BSD Sockets Library
Library protected and public interface header documentation
udp_client.h
1#ifndef UDP_CLIENT_H
2#define UDP_CLIENT_H
3
4#include <sys/socket.h>
5#include <arpa/inet.h>
6#include <array>
7#include "./network_socket.h"
8
9namespace AsyncBsdSocketLib
10{
12 class UdpClient : public NetworkSocket
13 {
14 private:
15 std::string mNicIpAddress;
16 std::string mMulticastIpAddress;
17 bool mIsMulticast;
18 bool mShareAddress;
19
20 public:
21 UdpClient() = delete;
22
26 UdpClient(std::string ipAddress, uint16_t port);
27
35 std::string ipAddress,
36 uint16_t port,
37 std::string nicIpAddress,
38 std::string multicastIpAddress,
39 bool shareAddress = true);
40
41 int Connection() const noexcept override;
42
43 bool TrySetup() noexcept override;
44
51 template <std::size_t N>
52 ssize_t Send(
53 const std::array<uint8_t, N> &buffer,
54 std::string ipAddress,
55 uint16_t port) const noexcept
56 {
57 // No flag for sending
58 const int _flags = 0;
59
60 struct sockaddr_in _destinationAddress;
61 _destinationAddress.sin_addr.s_addr = inet_addr(ipAddress.c_str());
62 _destinationAddress.sin_family = AF_INET;
63 _destinationAddress.sin_port = htons(port);
64
65 ssize_t _result =
66 sendto(
68 buffer.data(),
69 N,
70 _flags,
71 (struct sockaddr *)&_destinationAddress,
72 sizeof(_destinationAddress));
73
74 return _result;
75 }
76
84 template <std::size_t N>
85 ssize_t Receive(
86 std::array<uint8_t, N> &buffer,
87 std::string &ipAddress,
88 uint16_t &port) const noexcept
89 {
90 // No flag for receiving
91 const int _flags = 0;
92
93 struct sockaddr_in _sourceAddress;
94 socklen_t _sourceAddressLength = sizeof(_sourceAddress);
95
96 ssize_t _result =
97 recvfrom(
99 buffer.data(),
100 N,
101 _flags,
102 (struct sockaddr *)&_sourceAddress,
103 &_sourceAddressLength);
104
105 // Convert IP address
106 char _ipAddress[INET_ADDRSTRLEN];
107 inet_ntop(AF_INET, &(_sourceAddress.sin_addr), _ipAddress, INET_ADDRSTRLEN);
108 ipAddress = std::string(_ipAddress);
109
110 // Convert port number
111 port = htons(_sourceAddress.sin_port);
112
113 return _result;
114 }
115 };
116}
117
118#endif
int FileDescriptor
File descriptor.
Definition: communicator.h:13
TCP/IP network socket.
Definition: network_socket.h:13
UDP client which can play both client and listener roles.
Definition: udp_client.h:13
ssize_t Send(const std::array< uint8_t, N > &buffer, std::string ipAddress, uint16_t port) const noexcept
Send a byte array to a destination.
Definition: udp_client.h:52
int Connection() const noexcept override
Connection descriptor for sending and receiving.
UdpClient(std::string ipAddress, uint16_t port)
Unicast UDP socket constructor.
bool TrySetup() noexcept override
Try to setup the communicator.
UdpClient(std::string ipAddress, uint16_t port, std::string nicIpAddress, std::string multicastIpAddress, bool shareAddress=true)
Multicast UDP socket constructor.
ssize_t Receive(std::array< uint8_t, N > &buffer, std::string &ipAddress, uint16_t &port) const noexcept
Receive a byte array from a source.
Definition: udp_client.h:85