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.
socket.h
Go to the documentation of this file.
1 #pragma once
5 
6 #include <memory>
7 #include "../../core_export.h"
8 #include "../../argument_exception.h"
9 #include "../../async_callback.h"
10 #include "../../iasync_result.h"
11 #include "../../not_implemented_exception.h"
12 #include "../../not_supported_exception.h"
13 #include "../../object.h"
14 #include "../../types.h"
15 #include "../../ustring.h"
16 #include "../end_point.h"
17 #include "../ip_address.h"
18 #include "address_family.h"
19 #include "io_control_code.h"
20 #include "ip_packet_information.h"
21 #include "ip_protection_level.h"
22 #include "linger_option.h"
23 #include "multicast_option.h"
24 #include "ip_v6_multicast_option.h"
25 #include "protocol_type.h"
26 #include "select_mode.h"
27 #include "socket_error.h"
28 #include "socket_flags.h"
29 #include "socket_information.h"
30 #include "socket_option_level.h"
31 #include "socket_option_name.h"
32 #include "socket_shutdown.h"
33 #include "socket_type.h"
34 
36 namespace xtd {
38  namespace net {
40  namespace sockets {
42  class socket_async_event_args;
44 
68  class core_export_ socket : public xtd::object {
69  class async_result_socket : public xtd::object, public xtd::iasync_result {
70  public:
71  explicit async_result_socket(std::any async_state) : async_state_(async_state) {}
72  std::any async_state() const noexcept override {return async_state_;}
73  std::shared_mutex& async_mutex() override {return async_mutex_;}
74  bool completed_synchronously() const noexcept override {return false;}
75  bool is_completed() const noexcept override {return is_completed_;};
76 
77  std::any async_state_;
78  bool is_completed_ = false;
79  std::shared_mutex async_mutex_;
81  std::exception_ptr exception_;
82  };
83 
84  class async_result_accept : public async_result_socket {
85  public:
86  explicit async_result_accept(std::any async_state) : async_result_socket(async_state) {}
87  std::any socket_;
88  };
89 
90  class async_result_connect : public async_result_socket {
91  public:
92  explicit async_result_connect(std::any async_state) : async_result_socket(async_state) {}
93  };
94 
95  class async_result_disconnect : public async_result_socket {
96  public:
97  explicit async_result_disconnect(std::any async_state) : async_result_socket(async_state) {}
98  };
99 
100  class async_result_receive : public async_result_socket {
101  public:
102  explicit async_result_receive(std::any async_state) : async_result_socket(async_state) {}
103  size_t number_of_bytes_received_ = 0;
104  };
105 
106  class async_result_receive_from : public async_result_socket {
107  public:
108  explicit async_result_receive_from(std::any async_state) : async_result_socket(async_state) {}
109  std::shared_ptr<xtd::net::end_point> end_point_;
110  size_t number_of_bytes_received_ = 0;
111  };
112 
113  class async_result_receive_message_from : public async_result_socket {
114  public:
115  explicit async_result_receive_message_from(std::any async_state) : async_result_socket(async_state) {}
116  std::shared_ptr<xtd::net::end_point> end_point_;
118  size_t number_of_bytes_received_ = 0;
119  xtd::net::sockets::ip_packet_information ip_packet_information_;
120  };
121 
122  class async_result_send : public async_result_socket {
123  public:
124  explicit async_result_send(std::any async_state) : async_result_socket(async_state) {}
125  size_t number_of_bytes_sent_ = 0;
126  };
127 
128  class async_result_send_to : public async_result_socket {
129  public:
130  explicit async_result_send_to(std::any async_state) : async_result_socket(async_state) {}
131  size_t number_of_bytes_sent_ = 0;
132  };
133 
134  public:
136 
139  socket();
140 
145  explicit socket(intptr_t handle);
166 
168  socket(socket&&) = default;
169  socket(const socket&) = default;
170  ~socket();
171  socket& operator=(const socket&) = default;
172  friend std::ostream& operator <<(std::ostream& os, const socket& mo) noexcept {return os << mo.to_string();}
173  bool operator==(const socket& s) const {return data_ == s.data_;};
174  bool operator!=(const socket& s) const {return !operator==(s);};
176 
178 
184 
189  size_t available() const;
190 
198  bool blocking() const;
207  socket& blocking(bool value);
208 
214  bool connected() const noexcept;
215 
222  bool dont_fragment() const;
230  socket& dont_fragment(bool value);
231 
238  bool dual_mode() const;
246  socket& dual_mode(bool value);
247 
255  bool enable_broadcast() const;
264  socket& enable_broadcast(bool value);
265 
274  bool exclusive_address_use() const;
285  socket& exclusive_address_use(bool value);
286 
289  intptr_t handle() const noexcept;
290 
294  bool is_bound() const noexcept;
295 
313  xtd::net::sockets::linger_option linger_state() const;
332  socket& linger_state(const xtd::net::sockets::linger_option& value);
333 
339  std::shared_ptr<xtd::net::end_point> local_end_point() const;
340 
349  bool multicast_loopback() const;
359  socket& multicast_loopback(bool value);
360 
369  bool no_delay() const;
379  socket& no_delay(bool value);
380 
384  static bool os_supports_ip_v4() noexcept;
385 
389  static bool os_supports_ip_v6() noexcept;
390 
395 
402  size_t receive_buffer_size() const;
410  socket& receive_buffer_size(size_t value);
411 
418  int32_t receive_timeout() const;
427  socket& receive_timeout(int32_t value);
428 
434  std::shared_ptr<xtd::net::end_point> remote_end_point() const;
435 
442  size_t send_buffer_size() const;
450  socket& send_buffer_size(size_t value);
451 
458  int32_t send_timeout() const;
467  socket& send_timeout(int32_t value);
468 
473 
483  byte_t ttl() const;
494  socket& ttl(byte_t value);
496 
498 
510  socket accept();
511 
529  bool accept_async(xtd::net::sockets::socket_async_event_args& e);
530 
545  std::shared_ptr<xtd::iasync_result> begin_accept(xtd::async_callback callback, const std::any& state);
546 
562  template<typename end_point_t>
563  std::shared_ptr<xtd::iasync_result> begin_connect(const end_point_t& remote_end_point, xtd::async_callback callback, const std::any& state) {
564  return begin_connect_(std::make_shared<end_point_t>(remote_end_point), callback, state);
565  }
579  std::shared_ptr<xtd::iasync_result> begin_connect(const xtd::net::ip_address& address, uint16_t port, xtd::async_callback callback, const std::any& state);
593  std::shared_ptr<xtd::iasync_result> begin_connect(const std::vector<xtd::net::ip_address>& addresses, uint16_t port, xtd::async_callback callback, const std::any& state);
607  std::shared_ptr<xtd::iasync_result> begin_connect(const xtd::ustring& host, uint16_t port, xtd::async_callback callback, const std::any& state);
608 
619  std::shared_ptr<xtd::iasync_result> begin_disconnect(bool reuse_socket, xtd::async_callback callback, const std::any& state);
620 
638  std::shared_ptr<xtd::iasync_result> begin_receive(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::async_callback callback, const std::any& state);
657  std::shared_ptr<xtd::iasync_result> begin_receive(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::sockets::socket_error& error_code, xtd::async_callback callback, const std::any& state);
658 
681  std::shared_ptr<xtd::iasync_result> begin_receive_from(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point, xtd::async_callback callback, const std::any& state);
682 
700  std::shared_ptr<xtd::iasync_result> begin_receive_message_from(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point, xtd::async_callback callback, const std::any& state);
701 
722  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::async_callback callback, const std::any& state);
744  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::sockets::socket_error& error_code, xtd::async_callback callback, const std::any& state);
745 
766  std::shared_ptr<xtd::iasync_result> begin_send_to(const std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point, xtd::async_callback callback, const std::any& state);
767 
779  template<typename end_point_t>
780  void bind(const end_point_t& local_end_point) {
781  bind_(std::make_shared<end_point_t>(local_end_point));
782  }
783 
789  void close();
790 
800  template<typename end_point_t>
801  void connect(const end_point_t& remote_end_point) {
802  connect_(std::make_shared<end_point_t>(remote_end_point));
803  }
804 
815  void connect(const xtd::net::ip_address& address, uint16_t port);
816 
828  void connect(const std::vector<xtd::net::ip_address>& addresses, uint16_t port);
829 
840  void connect(const xtd::ustring& host, uint16_t port);
841 
850  void disconnect(bool reuse_socket);
851 
861  socket end_accept(std::shared_ptr<xtd::iasync_result> async_result);
862 
872  void end_connect(std::shared_ptr<xtd::iasync_result> async_result);
873 
881  void end_disconnect(std::shared_ptr<xtd::iasync_result> async_result);
882 
897  size_t end_receive(std::shared_ptr<xtd::iasync_result> async_result);
912  size_t end_receive(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::sockets::socket_error& error_code);
913 
925  size_t end_receive_from(std::shared_ptr<xtd::iasync_result> async_result, std::shared_ptr<xtd::net::end_point>& end_point);
926 
940  size_t end_receive_message_from(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::sockets::socket_flags& socket_flags, std::shared_ptr<xtd::net::end_point>& end_point, xtd::net::sockets::ip_packet_information& ip_packet_information);
941 
956  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result);
972  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::sockets::socket_error& error_code);
973 
985  size_t end_send_to(std::shared_ptr<xtd::iasync_result> async_result);
986 
995  size_t get_raw_socket_option(int32_t socket_option_level, int32_t socket_option_name, intptr_t option_value, size_t size_option_value) const;
996 
1008  int32_t get_socket_option(xtd::net::sockets::socket_option_level socket_option_level, xtd::net::sockets::socket_option_name socket_option_name) const;
1015  xtd::net::sockets::linger_option get_socket_linger_option() const;
1022  xtd::net::sockets::multicast_option get_socket_multicast_option(xtd::net::sockets::socket_option_name socket_option_name) const;
1029  xtd::net::sockets::ip_v6_multicast_option get_socket_ip_v6_multicast_option(xtd::net::sockets::socket_option_name socket_option_name) const;
1030 
1038  size_t io_control(int32_t io_control_code, std::vector<uint8_t>& option_in_value, std::vector<uint8_t>& option_out_value);
1046  size_t io_control(xtd::net::sockets::io_control_code io_control_code, std::vector<uint8_t>& option_in_value, std::vector<uint8_t>& option_out_value);
1047 
1056  void listen(size_t backlog);
1057 
1066  void listen();
1067 
1081  bool poll(int32_t micro_seconds, xtd::net::sockets::select_mode mode);
1082 
1095  size_t receive(std::vector<byte_t>& buffer);
1096 
1110  size_t receive(std::vector<byte_t>& buffer, xtd::net::sockets::socket_flags socket_flags);
1111 
1127  size_t receive(std::vector<byte_t>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags);
1128 
1144  size_t receive(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags);
1145 
1161  size_t receive(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::sockets::socket_error& error);
1162 
1178  size_t receive_from(std::vector<byte_t>& buffer, xtd::net::end_point& remote_end_point);
1179 
1196  size_t receive_from(std::vector<byte_t>& buffer, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point);
1197 
1216  size_t receive_from(std::vector<byte_t>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point);
1217 
1236  size_t receive_from(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point);
1237 
1238  size_t receive_message_from(std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point, xtd::net::sockets::ip_packet_information& ip_packet_information);
1239 
1255  static size_t select(std::vector<socket>& check_read, std::vector<socket>& check_write, std::vector<socket>& check_error, int32_t microseconds);
1256 
1269  size_t send(const std::vector<byte_t>& buffer);
1270 
1284  size_t send(const std::vector<byte_t>& buffer, xtd::net::sockets::socket_flags socket_flags);
1285 
1301  size_t send(const std::vector<byte_t>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags);
1302 
1319  size_t send(const std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags);
1320 
1336  size_t send(const std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::sockets::socket_error& error_code);
1337 
1350  size_t send_to(const std::vector<byte_t>& buffer, const xtd::net::end_point& remote_end_point);
1364  size_t send_to(const std::vector<byte_t>& buffer, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point);
1380  size_t send_to(const std::vector<byte_t>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point);
1397  size_t send_to(const std::vector<byte_t>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point);
1398 
1410  void set_ip_protection_level(xtd::net::sockets::ip_protection_level level);
1411 
1441  void set_socket_option(xtd::net::sockets::socket_option_level socket_option_level, xtd::net::sockets::socket_option_name socket_option_name, bool option_value);
1442 
1483  void set_socket_option(xtd::net::sockets::socket_option_level socket_option_level, xtd::net::sockets::socket_option_name socket_option_name, int32_t option_value);
1489  void set_socket_option(xtd::net::sockets::linger_option option_value);
1496  void set_socket_option(xtd::net::sockets::socket_option_name socket_option_name, const xtd::net::sockets::multicast_option& option_value);
1503  void set_socket_option(xtd::net::sockets::socket_option_name socket_option_name, const xtd::net::sockets::ip_v6_multicast_option& option_value);
1504 
1515  void set_raw_socket_option(int32_t socket_option_level, int32_t socket_option_name, intptr_t option_value, size_t option_value_size);
1516 
1538 
1539  private:
1540  static xtd::net::sockets::socket_error get_last_error_();
1541  std::shared_ptr<xtd::iasync_result> begin_connect_(std::shared_ptr<xtd::net::end_point> remote_end_point, xtd::async_callback callback, const std::any& state);
1542  void bind_(std::shared_ptr<xtd::net::end_point> local_end_point);
1543  void connect_(std::shared_ptr<xtd::net::end_point> remote_end_point);
1544  struct data;
1545 
1546  std::shared_ptr<data> data_;
1547  };
1548  }
1549  }
1550 }
Implements the Berkeley sockets interface.
Definition: socket.h:68
The xtd::uri::host data.
protocol_type
Specifies the protocols that the xtd::net::sockets::socket class supports.
Definition: protocol_type.h:27
Contains xtd::net::sockets::socket_option_name enum.
ip_protection_level
A value that enables restriction of an IPv6 socket to a specified scope, such as addresses with the s...
Definition: ip_protection_level.h:28
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
Contains xtd::net::sockets::multicast_option class.
Contains xtd::net::sockets::io_control_code enum.
Gets the error status and clear.
The SELECT key.
Specifies whether a xtd::net::sockets::socket will remain connected after a call to the xtd::net::soc...
Definition: linger_option.h:31
Represents the status of an asynchronous operation.
Definition: iasync_result.h:21
Use no flags for this call.
Receive a time-out. This option applies only to synchronous methods; it has no effect on asynchronous...
std::shared_ptr< xtd::iasync_result > begin_connect(const end_point_t &remote_end_point, xtd::async_callback callback, const std::any &state)
Begins an asynchronous request for a remote host connection.
Definition: socket.h:563
socket_option_name
Defines configuration option names.
Definition: socket_option_name.h:28
A request to send or receive data was disallowed because the socket is closed.
Contains xtd::net::sockets::socket_flags enum.
Identifies a network address. This is an abstract class.
Definition: end_point.h:24
Contains xtd::net::sockets::ip_v6_multicast_option class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Gets the network interface information that is associated with a call to xtd::net::sockets::socket::r...
Definition: ip_packet_information.h:33
Contains xtd::net::sockets::socket_type enum.
Contains xtd::net::sockets::select_mode enum.
Enables a socket to be bound for exclusive access.
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
Contains IPAddress values used to join and drop multicast groups.
Definition: multicast_option.h:36
socket_error
Defines error codes for the xtd::net::sockets::socket class.
Definition: socket_error.h:27
Send a time-out. This option applies only to synchronous methods; it has no effect on asynchronous me...
void bind(const end_point_t &local_end_point)
Associates a xtd::net::sockets::socket with a local endpoint.
Definition: socket.h:780
select_mode
Defines the polling modes for the xtd::net::sockets::socket::poll method.
Definition: select_mode.h:28
Contains xtd::net::sockets::socket_shutdown enum.
The protocol type is incorrect.
Socket options apply to all sockets.
Contains xtd::net::sockets::socket_error enum.
socket_flags
Specifies socket send and receive behaviors. This enumeration has a flags attribute that allows a bit...
Definition: socket_flags.h:27
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
The xtd::uri::port data.
Contains xtd::net::sockets::ip_protection_level enum.
Disables the Nagle algorithm for send coalescing.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:30
Contains xtd::net::sockets::address_family enum.
socket_shutdown
Defines constants that are used by the xtd::net::sockets::socket::shutdown method.
Definition: socket_shutdown.h:28
socket_type
Specifies the type of socket that an instance of the xtd::net::sockets::socket class represents...
Definition: socket_type.h:29
Contains option values for joining an IPv6 multicast group.
Definition: ip_v6_multicast_option.h:29
socket_option_level
Defines socket option levels for the xtd::net::sockets::socket::set_socket_option and xtd::net::socke...
Definition: socket_option_level.h:28
Contains xtd::net::sockets::linger_option class.
Contains xtd::net::sockets::socket_information class.
void connect(const end_point_t &remote_end_point)
Establishes a connection to a remote host.
Definition: socket.h:801
The socket handle may be reused when the request completes. This flag is valid only if xtd::net::sock...
io_control_code
Specifies the IO control codes supported by the xtd::net::sockets::socket::io_control method...
Definition: io_control_code.h:27
Represents an asynchronous socket operation.
Definition: socket_async_event_args.h:51
Contains xtd::net::sockets::ip_packet_information class.
Encapsulates the information that is necessary to duplicate a xtd::net::sockets::socket.
Definition: socket_information.h:30
Contains xtd::net::sockets::socket_option_level enum.
Contains xtd::net::sockets::protocol_type enum.
address_family
Specifies the addressing scheme that an instance of the xtd::net::sockets::socket class can use...
Definition: address_family.h:27