xtd - Reference Guide  0.1.1
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:
77  explicit udp_client(uint16_t port);
84  explicit udp_client(const xtd::net::ip_end_point& local_end_point);
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 
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;
172 
180  bool exclusive_address_use() const;
190 
197  bool multicast_loopback() const;
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:
464  udp_client(const xtd::net::sockets::socket& socket);
465  struct data;
466  std::shared_ptr<data> data_;
467  };
468  }
469  }
470 }
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
Provides an Internet Protocol (IP) address.
Definition: ip_address.h:30
Represents a network endpoint as an IP address and a port number.
Definition: ip_end_point.h:20
Implements the Berkeley sockets interface.
Definition: socket.h:63
Provides User Datagram Protocol (UDP) network services.
Definition: udp_client.h:35
void connect(const xtd::net::ip_end_point &end_point)
Establishes a default remote host using the specified network endpoint.
bool exclusive_address_use() const
Gets a boolean value that specifies whether the xtd::net::sockets::udp_client allows only one client ...
bool enable_broadcast() const
Gets a boolean value that specifies whether the xtd::net::sockets::udp_client may send or receive bro...
udp_client(uint16_t port, xtd::net::sockets::address_family addressFamily)
Initializes a new instance of the xtd::net::sockets::udp_client class and binds it to the local port ...
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.
size_t end_send(std::shared_ptr< xtd::iasync_result > async_result)
Ends a pending asynchronous send.
udp_client(const xtd::net::ip_end_point &local_end_point)
Initializes a new instance of the xtd::net::sockets::udp_client class and binds it to the specified l...
void close()
Closes the UDP connection.
bool active() const noexcept
Gets a value that indicates whether a connection has been made.
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.
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...
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.
void drop_multicast_group(const xtd::net::ip_address &multicast_address)
Leaves a multicast group.
udp_client(const xtd::ustring &hostname, uint16_t port)
Initializes a new instance of the xtd::net::sockets::udp_client class and establishes a default remot...
xtd::net::sockets::socket client() const noexcept
Gets the underlying network xtd::net::sockets::socket.
udp_client()
Initializes a new instance of the xtd::net::sockets::udp_client class.
void join_multicast_group(const xtd::net::ip_address &multicast_address)
Adds a xtd::net::sockets::udp_client to a multicast group.
bool multicast_loopback() const
Gets a boolean value that specifies whether outgoing multicast packets are delivered to the sending a...
void allow_nat_traversal(bool allowed)
Enables or disables Network Address Translation (NAT) traversal on a xtd::net::sockets::udp_client in...
size_t available() const
Gets the amount of data received from the network that is available to read.
std::vector< byte_t > receive(xtd::net::ip_end_point &remote_end_point)
Returns a UDP datagram that was sent by a remote host.
udp_client(xtd::net::sockets::address_family address_Family)
Initializes a new instance of the xtd::net::sockets::udp_client class.
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...
udp_client(uint16_t port)
Initializes a new instance of the xtd::net::sockets::udp_client class and binds it to the local port ...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition: object.h:26
virtual xtd::ustring to_string() const noexcept
Returns a std::string that represents the current object.
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
address_family
Specifies the addressing scheme that an instance of the xtd::net::sockets::socket class can use.
Definition: address_family.h:24
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
@ success
Operation successful.
@ s
The S key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::net::sockets::socket class.