xtd - Reference Guide 0.2.0
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 {
40  class core_export_ udp_client : public xtd::object {
41  class async_result_socket : public xtd::object, public xtd::iasync_result {
42  public:
43  explicit async_result_socket(std::any async_state) : async_state_(async_state) {}
44  std::any async_state() const noexcept override {return async_state_;}
45  std::shared_mutex& async_mutex() override {return async_mutex_;}
46  bool completed_synchronously() const noexcept override {return false;}
47  bool is_completed() const noexcept override {return is_completed_;};
48 
49  std::any async_state_;
50  bool is_completed_ = false;
51  std::shared_mutex async_mutex_;
53  std::exception_ptr exception_;
54  };
55 
56  class async_result_receive : public async_result_socket {
57  public:
58  explicit async_result_receive(std::any async_state) : async_result_socket(async_state) {}
59  std::vector<byte_t> buffer_;
60  xtd::net::ip_end_point remote_end_point_;
61  };
62 
63  class async_result_send : public async_result_socket {
64  public:
65  explicit async_result_send(std::any async_state) : async_result_socket(async_state) {}
66  size_t number_of_bytes_sent_ = 0;
67  };
68 
69  public:
71 
78  udp_client();
85  explicit udp_client(uint16_t port);
92  explicit udp_client(const xtd::net::ip_end_point& local_end_point);
101  explicit udp_client(xtd::net::sockets::address_family address_Family);
111  udp_client(uint16_t port, xtd::net::sockets::address_family addressFamily);
118  udp_client(const xtd::ustring& hostname, uint16_t port);
120 
122  udp_client(udp_client&&) = default;
123  udp_client(const udp_client&) = default;
124  ~udp_client();
125  udp_client& operator=(const udp_client&) = default;
126  friend std::ostream& operator <<(std::ostream& os, const udp_client& uc) noexcept {return os << uc.to_string();}
127  bool operator==(const udp_client& s) const {return data_ == s.data_;};
128  bool operator!=(const udp_client& s) const {return !operator==(s);};
130 
132 
141  size_t available() const;
142 
146  xtd::net::sockets::socket client() const noexcept;
151  udp_client& client(const xtd::net::sockets::socket& value) noexcept;
152 
159  bool dont_fragment() const;
167  udp_client& dont_fragment(bool value);
168 
175  bool enable_broadcast() const;
183  udp_client& enable_broadcast(bool value);
184 
192  bool exclusive_address_use() const;
201  udp_client& exclusive_address_use(bool value);
202 
209  bool multicast_loopback() const;
217  udp_client& multicast_loopback(bool value);
218 
225  byte_t ttl() const;
233  udp_client& ttl(byte_t value);
234 
236 
246  void allow_nat_traversal(bool allowed);
247 
254  std::shared_ptr<xtd::iasync_result> begin_receive(xtd::async_callback callback, const std::any& state);
255 
266  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);
267 
277  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);
286  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);
287 
292  void close();
293 
304  void connect(const xtd::net::ip_end_point& end_point);
316  void connect(const xtd::net::ip_address& ip_address, uint16_t port);
328  void connect(const xtd::ustring& hostname, uint16_t port);
329 
336  void drop_multicast_group(const xtd::net::ip_address& multicast_address);
344  void drop_multicast_group(const xtd::net::ip_address& multicast_address, uint32_t if_index);
345 
356  std::vector<byte_t> end_receive(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::ip_end_point& remote_end_point);
357 
367  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result);
368 
381  void join_multicast_group(const xtd::net::ip_address& multicast_address);
394  void join_multicast_group(uint32_t if_index, const xtd::net::ip_address& multicast_address);
407  void join_multicast_group(const xtd::net::ip_address& multicast_address, byte_t ttl);
417  void join_multicast_group(const xtd::net::ip_address& multicast_address, const xtd::net::ip_address& local_address);
418 
429  std::vector<byte_t> receive(xtd::net::ip_end_point& remote_end_point);
430 
443  size_t send(const std::vector<byte_t>& dgram, size_t bytes, const xtd::ustring& hostname, uint16_t port);
455  size_t send(const std::vector<byte_t>& dgram, size_t bytes, const xtd::net::ip_end_point& end_point);
465  size_t send(const std::vector<byte_t>& dgram, size_t bytes);
467 
468  protected:
470 
475  bool active() const noexcept;
480  udp_client& active(bool value) noexcept;
482 
483  private:
485  struct data;
486  std::shared_ptr<data> data_;
487  };
488  }
489  }
490 }
Implements the Berkeley sockets interface.
Definition: socket.h:68
virtual xtd::ustring to_string() const noexcept
Returns a sxd::ustring that represents the current object.
Provides an Internet Protocol (IP) address.
Definition: ip_address.h:34
Represents the status of an asynchronous operation.
Definition: iasync_result.h:21
Identifies a network address. This is an abstract class.
Definition: end_point.h:24
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Enables a socket to be bound for exclusive access.
Represents a network endpoint as an IP address and a port number.
Definition: ip_end_point.h:20
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
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
socket_error
Defines error codes for the xtd::net::sockets::socket class.
Definition: socket_error.h:27
Supports datagrams, which are connectionless, unreliable messages of a fixed (typically small) maximu...
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
The xtd::uri::port data.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:30
Provides User Datagram Protocol (UDP) network services.
Definition: udp_client.h:40
Contains xtd::net::sockets::socket class.
address_family
Specifies the addressing scheme that an instance of the xtd::net::sockets::socket class can use...
Definition: address_family.h:27