xtd 0.2.0
udp_client.h
Go to the documentation of this file.
1 #pragma once
5 #include "socket.h"
6 #include "../ip_end_point.h"
7 #include "../../threading/manual_reset_event.h"
8 #include "../../iequatable.h"
9 #include "../../object.h"
10 #include "../../ustring.h"
11 
13 namespace xtd {
15  namespace net {
17  namespace sockets {
43  class core_export_ udp_client : public xtd::object, public xtd::iequatable<udp_client> {
44  class async_result_socket : public xtd::object, public xtd::iasync_result {
45  public:
46  explicit async_result_socket(std::any async_state) : async_state_(async_state) {}
47  std::any async_state() const noexcept override {return async_state_;}
48  xtd::threading::wait_handle& async_wait_handle() noexcept override {return async_event_;}
49  bool completed_synchronously() const noexcept override {return false;}
50  bool is_completed() const noexcept override {return is_completed_;};
51 
52  std::any async_state_;
53  bool is_completed_ = false;
56  std::exception_ptr exception_;
57  };
58 
59  class async_result_receive : public async_result_socket {
60  public:
61  explicit async_result_receive(std::any async_state) : async_result_socket(async_state) {}
62  std::vector<xtd::byte> buffer_;
63  xtd::net::ip_end_point remote_end_point_;
64  };
65 
66  class async_result_send : public async_result_socket {
67  public:
68  explicit async_result_send(std::any async_state) : async_result_socket(async_state) {}
69  size_t number_of_bytes_sent_ = 0;
70  };
71 
72  struct data;
73 
74  public:
76 
83  udp_client();
90  explicit udp_client(uint16 port);
97  explicit udp_client(const xtd::net::ip_end_point& local_end_point);
106  explicit udp_client(xtd::net::sockets::address_family address_Family);
123  udp_client(const xtd::ustring& hostname, uint16 port);
125 
127  udp_client(udp_client&&) = default;
128  udp_client(const udp_client&) = default;
129  ~udp_client();
130  udp_client& operator =(const udp_client&) = default;
132 
134 
143  size_t available() const;
144 
148  xtd::net::sockets::socket client() const noexcept;
153  udp_client& client(const xtd::net::sockets::socket& value) noexcept;
154 
161  bool dont_fragment() const;
169  udp_client& dont_fragment(bool value);
170 
177  bool enable_broadcast() const;
185  udp_client& enable_broadcast(bool value);
186 
194  bool exclusive_address_use() const;
203  udp_client& exclusive_address_use(bool value);
204 
211  bool multicast_loopback() const;
219  udp_client& multicast_loopback(bool value);
220 
227  xtd::byte ttl() const;
235  udp_client& ttl(xtd::byte value);
236 
238 
248  void allow_nat_traversal(bool allowed);
249 
256  std::shared_ptr<xtd::iasync_result> begin_receive(xtd::async_callback callback, const std::any& state);
257 
268  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<xtd::byte>& dgram, size_t bytes, const xtd::ustring& hostname, uint16 port, xtd::async_callback callback, const std::any& state);
269 
279  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<xtd::byte>& dgram, size_t bytes, const xtd::net::ip_end_point& end_point, xtd::async_callback callback, const std::any& state);
288  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<xtd::byte>& dgram, size_t bytes, xtd::async_callback callback, const std::any& state);
289 
294  void close();
295 
306  void connect(const xtd::net::ip_end_point& end_point);
318  void connect(const xtd::net::ip_address& ip_address, uint16 port);
330  void connect(const xtd::ustring& hostname, uint16 port);
331 
338  void drop_multicast_group(const xtd::net::ip_address& multicast_address);
346  void drop_multicast_group(const xtd::net::ip_address& multicast_address, uint32 if_index);
347 
358  std::vector<xtd::byte> end_receive(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::ip_end_point& remote_end_point);
359 
369  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result);
370 
371  bool equals(const udp_client& s) const noexcept override;
372 
385  void join_multicast_group(const xtd::net::ip_address& multicast_address);
398  void join_multicast_group(uint32 if_index, const xtd::net::ip_address& multicast_address);
411  void join_multicast_group(const xtd::net::ip_address& multicast_address, xtd::byte ttl);
421  void join_multicast_group(const xtd::net::ip_address& multicast_address, const xtd::net::ip_address& local_address);
422 
433  std::vector<xtd::byte> receive(xtd::net::ip_end_point& remote_end_point);
434 
447  size_t send(const std::vector<xtd::byte>& dgram, size_t bytes, const xtd::ustring& hostname, uint16 port);
459  size_t send(const std::vector<xtd::byte>& dgram, size_t bytes, const xtd::net::ip_end_point& end_point);
469  size_t send(const std::vector<xtd::byte>& dgram, size_t bytes);
471 
472  protected:
474 
479  bool active() const noexcept;
484  udp_client& active(bool value) noexcept;
486 
487  private:
489 
490  std::shared_ptr<data> data_;
491  };
492  }
493  }
494 }
Implements the Berkeley sockets interface.
Definition: socket.h:71
Provides an Internet Protocol (IP) address.
Definition: ip_address.h:35
delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition: delegate.h:34
Represents the status of an asynchronous operation.
Definition: iasync_result.h:22
Identifies a network address. This is an abstract class.
Definition: end_point.h:25
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
std::shared_ptr< xtd::iasync_result > async_result
Represents the status of an asynchronous operation.
Definition: async_result.h:16
Enables a socket to be bound for exclusive access.
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition: types.h:228
Represents a network endpoint as an IP address and a port number.
Definition: ip_end_point.h:21
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
socket_error
Defines error codes for the xtd::net::sockets::socket class.
Definition: socket_error.h:28
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:18
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:32
Provides User Datagram Protocol (UDP) network services.
Definition: udp_client.h:43
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:39
Contains xtd::net::sockets::socket class.
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
address_family
Specifies the addressing scheme that an instance of the xtd::net::sockets::socket class can use...
Definition: address_family.h:28
Represents a thread synchronization event that, when signaled, must be reset manually. This class cannot be inherited.
Definition: manual_reset_event.h:33
Encapsulates operating system specific objects that wait for exclusive access to shared resources...
Definition: wait_handle.h:48