xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
udp_client.h
Go to the documentation of this file.
1 #pragma once
5 
6 #include "../../object.h"
7 #include "../../ustring.h"
8 #include "../ip_end_point.h"
9 #include "socket.h"
10 
12 namespace xtd {
14  namespace net {
16  namespace sockets {
35  class udp_client : public xtd::object {
36  class async_result_socket : public xtd::object, public xtd::iasync_result {
37  public:
38  async_result_socket(std::any async_state) : async_state_(async_state) {}
39  std::any async_state() const noexcept override {return async_state_;}
40  std::shared_mutex& async_mutex() override {return async_mutex_;}
41  bool completed_synchronously() const noexcept override {return false;}
42  bool is_completed() const noexcept override {return is_completed_;};
43 
44  std::any async_state_;
45  bool is_completed_ = false;
46  std::shared_mutex async_mutex_;
48  std::exception_ptr exception_;
49  };
50 
51  class async_result_receive : public async_result_socket {
52  public:
53  async_result_receive(std::any async_state) : async_result_socket(async_state) {}
54  std::vector<byte_t> buffer_;
55  xtd::net::ip_end_point remote_end_point_;
56  };
57 
58  class async_result_send : public async_result_socket {
59  public:
60  async_result_send(std::any async_state) : async_result_socket(async_state) {}
61  size_t number_of_bytes_sent_;
62  };
63 
64  public:
70  udp_client();
77  explicit udp_client(uint16_t port);
84  explicit udp_client(const xtd::net::ip_end_point& local_end_point);
93  explicit udp_client(xtd::net::sockets::address_family address_Family);
103  udp_client(uint16_t port, xtd::net::sockets::address_family addressFamily);
110  udp_client(const xtd::ustring& hostname, uint16_t port);
111 
113  udp_client(udp_client&&) = default;
114  udp_client(const udp_client&) = default;
115  ~udp_client();
116  udp_client& operator=(const udp_client&) = default;
117  friend std::ostream& operator <<(std::ostream& os, const udp_client& uc) noexcept {return os << uc.to_string();}
118  bool operator==(const udp_client& s) const {return data_ == s.data_;};
119  bool operator!=(const udp_client& s) const {return !operator==(s);};
121 
129  size_t available() const;
130 
134  xtd::net::sockets::socket client() const noexcept;
139  udp_client& client(const xtd::net::sockets::socket& value) noexcept;
140 
147  bool dont_fragment() const;
155  udp_client& dont_fragment(bool value);
156 
163  bool enable_broadcast() const;
171  udp_client& enable_broadcast(bool value);
172 
180  bool exclusive_address_use() const;
189  udp_client& exclusive_address_use(bool value);
190 
197  bool multicast_loopback() const;
205  udp_client& multicast_loopback(bool value);
206 
213  byte_t ttl() const;
221  udp_client& ttl(byte_t value);
222 
231  void allow_nat_traversal(bool allowed);
232 
239  std::shared_ptr<xtd::iasync_result> begin_receive(xtd::async_callback callback, const std::any& state);
240 
251  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<byte_t>& dgram, size_t bytes, const xtd::ustring& hostname, uint16_t port, xtd::async_callback callback, const std::any& state);
252 
262  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<byte_t>& dgram, size_t bytes, const xtd::net::ip_end_point& end_point, xtd::async_callback callback, const std::any& state);
271  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<byte_t>& dgram, size_t bytes, xtd::async_callback callback, const std::any& state);
272 
277  void close();
278 
289  void connect(const xtd::net::ip_end_point& end_point);
301  void connect(const xtd::net::ip_address& ip_address, uint16_t port);
313  void connect(const xtd::ustring& hostname, uint16_t port);
314 
321  void drop_multicast_group(const xtd::net::ip_address& multicast_address);
329  void drop_multicast_group(const xtd::net::ip_address& multicast_address, uint32_t if_index);
330 
341  std::vector<byte_t> end_receive(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::ip_end_point& remote_end_point);
342 
352  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result);
353 
366  void join_multicast_group(const xtd::net::ip_address& multicast_address);
379  void join_multicast_group(uint32_t if_index, const xtd::net::ip_address& multicast_address);
392  void join_multicast_group(const xtd::net::ip_address& multicast_address, byte_t ttl);
402  void join_multicast_group(const xtd::net::ip_address& multicast_address, const xtd::net::ip_address& local_address);
403 
414  std::vector<byte_t> receive(xtd::net::ip_end_point& remote_end_point);
415 
428  size_t send(const std::vector<byte_t>& dgram, size_t bytes, const xtd::ustring& hostname, uint16_t port);
440  size_t send(const std::vector<byte_t>& dgram, size_t bytes, const xtd::net::ip_end_point& end_point);
450  size_t send(const std::vector<byte_t>& dgram, size_t bytes);
451 
452  protected:
456  bool active() const noexcept;
461  udp_client& active(bool value) noexcept;
462 
463  private:
465  struct data;
466  std::shared_ptr<data> data_;
467  };
468  }
469  }
470 }
void allow_nat_traversal(bool allowed)
Enables or disables Network Address Translation (NAT) traversal on a xtd::net::sockets::udp_client in...
void join_multicast_group(const xtd::net::ip_address &multicast_address)
Adds a xtd::net::sockets::udp_client to a multicast group.
Implements the Berkeley sockets interface.
Definition: socket.h:63
bool exclusive_address_use() const
Gets a boolean value that specifies whether the xtd::net::sockets::udp_client allows only one client ...
virtual xtd::ustring to_string() const noexcept
Returns a std::string that represents the current object.
Provides an Internet Protocol (IP) address.
Definition: ip_address.h:30
void connect(const xtd::net::ip_end_point &end_point)
Establishes a default remote host using the specified network endpoint.
size_t end_send(std::shared_ptr< xtd::iasync_result > async_result)
Ends a pending asynchronous send.
Represents the status of an asynchronous operation.
Definition: iasync_result.h:21
std::shared_ptr< xtd::iasync_result > begin_send(const std::vector< byte_t > &dgram, size_t bytes, const xtd::ustring &hostname, uint16_t port, xtd::async_callback callback, const std::any &state)
xtd::net::sockets::udp_client::sends a datagram to a destination asynchronously. The destination is s...
bool multicast_loopback() const
Gets a boolean value that specifies whether outgoing multicast packets are delivered to the sending a...
Identifies a network address. This is an abstract class.
Definition: end_point.h:24
std::vector< byte_t > receive(xtd::net::ip_end_point &remote_end_point)
Returns a UDP datagram that was sent by a remote host.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
xtd::net::sockets::socket client() const noexcept
Gets the underlying network xtd::net::sockets::socket.
Represents a network endpoint as an IP address and a port number.
Definition: ip_end_point.h:20
size_t available() const
Gets the amount of data received from the network that is available to read.
delegate< void(std::shared_ptr< xtd::iasync_result > ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition: async_callback.h:18
void close()
Closes the UDP connection.
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
socket_error
Defines error codes for the xtd::net::sockets::socket class.
Definition: socket_error.h:24
udp_client()
Initializes a new instance of the xtd::net::sockets::udp_client class.
Supports datagrams, which are connectionless, unreliable messages of a fixed (typically small) maximu...
std::shared_ptr< xtd::iasync_result > begin_receive(xtd::async_callback callback, const std::any &state)
xtd::net::sockets::udp_client::receives a datagram from a remote host asynchronously.
bool dont_fragment() const
Gets a boolean value that specifies whether the xtd::net::sockets::udp_client allows Internet Protoco...
byte_t ttl() const
Gets a value that specifies the Time to Live (TTL) value of Internet Protocol (IP) packets sent by th...
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
size_t send(const std::vector< byte_t > &dgram, size_t bytes, const xtd::ustring &hostname, uint16_t port)
xtd::net::sockets::udp_client::sends a UDP datagram to a specified port on a specified remote host...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:26
std::vector< byte_t > end_receive(std::shared_ptr< xtd::iasync_result > async_result, xtd::net::ip_end_point &remote_end_point)
Ends a pending asynchronous receive.
Provides User Datagram Protocol (UDP) network services.
Definition: udp_client.h:35
Contains xtd::net::sockets::socket class.
bool enable_broadcast() const
Gets a boolean value that specifies whether the xtd::net::sockets::udp_client may send or receive bro...
bool active() const noexcept
Gets a value that indicates whether a connection has been made.
void drop_multicast_group(const xtd::net::ip_address &multicast_address)
Leaves a multicast group.
address_family
Specifies the addressing scheme that an instance of the xtd::net::sockets::socket class can use...
Definition: address_family.h:24