xtd 0.2.0
socket.h
Go to the documentation of this file.
1 #pragma once
5 #include "address_family.h"
6 #include "io_control_code.h"
8 #include "ip_protection_level.h"
9 #include "linger_option.h"
10 #include "multicast_option.h"
11 #include "ip_v6_multicast_option.h"
12 #include "protocol_type.h"
13 #include "select_mode.h"
14 #include "socket_error.h"
15 #include "socket_flags.h"
16 #include "socket_information.h"
17 #include "socket_option_level.h"
18 #include "socket_option_name.h"
19 #include "socket_shutdown.h"
20 #include "socket_type.h"
21 #include "../end_point.h"
22 #include "../ip_address.h"
23 #include "../../threading/manual_reset_event.h"
24 #include "../../core_export.h"
25 #include "../../argument_exception.h"
26 #include "../../async_callback.h"
27 #include "../../iasync_result.h"
28 #include "../../iequatable.h"
29 #include "../../not_implemented_exception.h"
30 #include "../../not_supported_exception.h"
31 #include "../../object.h"
32 #include "../../types.h"
33 #include "../../ustring.h"
34 #include <memory>
35 
37 namespace xtd {
39  namespace net {
41  namespace sockets {
43  class socket_async_event_args;
45 
71  class core_export_ socket : public xtd::object, public xtd::iequatable<socket> {
73  class async_result_socket : public xtd::object, public xtd::iasync_result {
74  public:
75  explicit async_result_socket(std::any async_state) : async_state_(async_state) {}
76  std::any async_state() const noexcept override {return async_state_;}
77  xtd::threading::wait_handle& async_wait_handle() noexcept override {return async_event_;}
78  bool completed_synchronously() const noexcept override {return false;}
79  bool is_completed() const noexcept override {return is_completed_;};
80 
81  std::any async_state_;
82  bool is_completed_ = false;
85  std::exception_ptr exception_;
86  };
87 
89  class async_result_accept : public async_result_socket {
90  public:
91  explicit async_result_accept(std::any async_state) : async_result_socket(async_state) {}
92  std::any socket_;
93  };
94 
96  class async_result_connect : public async_result_socket {
97  public:
98  explicit async_result_connect(std::any async_state) : async_result_socket(async_state) {}
99  };
100 
102  class async_result_disconnect : public async_result_socket {
103  public:
104  explicit async_result_disconnect(std::any async_state) : async_result_socket(async_state) {}
105  };
106 
108  class async_result_receive : public async_result_socket {
109  public:
110  explicit async_result_receive(std::any async_state) : async_result_socket(async_state) {}
111  size_t number_of_bytes_received_ = 0;
112  };
113 
115  class async_result_receive_from : public async_result_socket {
116  public:
117  explicit async_result_receive_from(std::any async_state) : async_result_socket(async_state) {}
118  std::shared_ptr<xtd::net::end_point> end_point_;
119  size_t number_of_bytes_received_ = 0;
120  };
121 
123  class async_result_receive_message_from : public async_result_socket {
124  public:
125  explicit async_result_receive_message_from(std::any async_state) : async_result_socket(async_state) {}
126  std::shared_ptr<xtd::net::end_point> end_point_;
128  size_t number_of_bytes_received_ = 0;
129  xtd::net::sockets::ip_packet_information ip_packet_information_;
130  };
131 
133  class async_result_send : public async_result_socket {
134  public:
135  explicit async_result_send(std::any async_state) : async_result_socket(async_state) {}
136  size_t number_of_bytes_sent_ = 0;
137  };
138 
140  class async_result_send_to : public async_result_socket {
141  public:
142  explicit async_result_send_to(std::any async_state) : async_result_socket(async_state) {}
143  size_t number_of_bytes_sent_ = 0;
144  };
145 
146  struct data;
147 
148  public:
150 
153  socket();
154 
159  explicit socket(intptr handle);
180 
182  socket(socket&&) = default;
183  socket(const socket&) = default;
184  ~socket();
185  socket& operator =(const socket&) = default;
187 
189 
195 
200  size_t available() const;
201 
209  bool blocking() const;
218  socket& blocking(bool value);
219 
225  bool connected() const noexcept;
226 
233  bool dont_fragment() const;
241  socket& dont_fragment(bool value);
242 
249  bool dual_mode() const;
257  socket& dual_mode(bool value);
258 
266  bool enable_broadcast() const;
275  socket& enable_broadcast(bool value);
276 
285  bool exclusive_address_use() const;
296  socket& exclusive_address_use(bool value);
297 
300  intptr handle() const noexcept;
301 
305  bool is_bound() const noexcept;
306 
324  xtd::net::sockets::linger_option linger_state() const;
343  socket& linger_state(const xtd::net::sockets::linger_option& value);
344 
350  std::shared_ptr<xtd::net::end_point> local_end_point() const;
351 
360  bool multicast_loopback() const;
370  socket& multicast_loopback(bool value);
371 
380  bool no_delay() const;
390  socket& no_delay(bool value);
391 
395  static bool os_supports_ip_v4() noexcept;
396 
400  static bool os_supports_ip_v6() noexcept;
401 
406 
413  size_t receive_buffer_size() const;
421  socket& receive_buffer_size(size_t value);
422 
429  int32 receive_timeout() const;
438  socket& receive_timeout(int32 value);
439 
445  std::shared_ptr<xtd::net::end_point> remote_end_point() const;
446 
453  size_t send_buffer_size() const;
461  socket& send_buffer_size(size_t value);
462 
469  int32 send_timeout() const;
478  socket& send_timeout(int32 value);
479 
484 
494  xtd::byte ttl() const;
505  socket& ttl(xtd::byte value);
507 
509 
521  socket accept();
522 
540  bool accept_async(xtd::net::sockets::socket_async_event_args& e);
541 
556  std::shared_ptr<xtd::iasync_result> begin_accept(xtd::async_callback callback, const std::any& state);
557 
573  template<typename end_point_t>
574  std::shared_ptr<xtd::iasync_result> begin_connect(const end_point_t& remote_end_point, xtd::async_callback callback, const std::any& state) {
575  return begin_connect_(std::make_shared<end_point_t>(remote_end_point), callback, state);
576  }
590  std::shared_ptr<xtd::iasync_result> begin_connect(const xtd::net::ip_address& address, uint16 port, xtd::async_callback callback, const std::any& state);
604  std::shared_ptr<xtd::iasync_result> begin_connect(const std::vector<xtd::net::ip_address>& addresses, uint16 port, xtd::async_callback callback, const std::any& state);
618  std::shared_ptr<xtd::iasync_result> begin_connect(const xtd::ustring& host, uint16 port, xtd::async_callback callback, const std::any& state);
619 
630  std::shared_ptr<xtd::iasync_result> begin_disconnect(bool reuse_socket, xtd::async_callback callback, const std::any& state);
631 
649  std::shared_ptr<xtd::iasync_result> begin_receive(std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::async_callback callback, const std::any& state);
668  std::shared_ptr<xtd::iasync_result> begin_receive(std::vector<xtd::byte>& 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);
669 
692  std::shared_ptr<xtd::iasync_result> begin_receive_from(std::vector<xtd::byte>& 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);
693 
711  std::shared_ptr<xtd::iasync_result> begin_receive_message_from(std::vector<xtd::byte>& 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);
712 
733  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::async_callback callback, const std::any& state);
755  std::shared_ptr<xtd::iasync_result> begin_send(const std::vector<xtd::byte>& 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);
756 
777  std::shared_ptr<xtd::iasync_result> begin_send_to(const std::vector<xtd::byte>& 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);
778 
790  template<typename end_point_t>
791  void bind(const end_point_t& local_end_point) {
792  bind_(std::make_shared<end_point_t>(local_end_point));
793  }
794 
800  void close();
801 
811  template<typename end_point_t>
812  void connect(const end_point_t& remote_end_point) {
813  connect_(std::make_shared<end_point_t>(remote_end_point));
814  }
815 
826  void connect(const xtd::net::ip_address& address, uint16 port);
827 
839  void connect(const std::vector<xtd::net::ip_address>& addresses, uint16 port);
840 
851  void connect(const xtd::ustring& host, uint16 port);
852 
861  void disconnect(bool reuse_socket);
862 
872  socket end_accept(std::shared_ptr<xtd::iasync_result> async_result);
873 
883  void end_connect(std::shared_ptr<xtd::iasync_result> async_result);
884 
892  void end_disconnect(std::shared_ptr<xtd::iasync_result> async_result);
893 
908  size_t end_receive(std::shared_ptr<xtd::iasync_result> async_result);
923  size_t end_receive(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::sockets::socket_error& error_code);
924 
936  size_t end_receive_from(std::shared_ptr<xtd::iasync_result> async_result, std::shared_ptr<xtd::net::end_point>& end_point);
937 
951  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);
952 
967  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result);
983  size_t end_send(std::shared_ptr<xtd::iasync_result> async_result, xtd::net::sockets::socket_error& error_code);
984 
996  size_t end_send_to(std::shared_ptr<xtd::iasync_result> async_result);
997 
998  bool equals(const socket& s) const noexcept override;
999 
1008  size_t get_raw_socket_option(int32 socket_option_level, int32 socket_option_name, intptr option_value, size_t size_option_value) const;
1009 
1021  int32 get_socket_option(xtd::net::sockets::socket_option_level socket_option_level, xtd::net::sockets::socket_option_name socket_option_name) const;
1028  xtd::net::sockets::linger_option get_socket_linger_option() const;
1035  xtd::net::sockets::multicast_option get_socket_multicast_option(xtd::net::sockets::socket_option_name socket_option_name) const;
1042  xtd::net::sockets::ip_v6_multicast_option get_socket_ip_v6_multicast_option(xtd::net::sockets::socket_option_name socket_option_name) const;
1043 
1051  size_t io_control(int32 io_control_code, std::vector<xtd::byte>& option_in_value, std::vector<xtd::byte>& option_out_value);
1059  size_t io_control(xtd::net::sockets::io_control_code io_control_code, std::vector<xtd::byte>& option_in_value, std::vector<xtd::byte>& option_out_value);
1060 
1069  void listen(size_t backlog);
1070 
1079  void listen();
1080 
1094  bool poll(int32 micro_seconds, xtd::net::sockets::select_mode mode);
1095 
1108  size_t receive(std::vector<xtd::byte>& buffer);
1109 
1123  size_t receive(std::vector<xtd::byte>& buffer, xtd::net::sockets::socket_flags socket_flags);
1124 
1140  size_t receive(std::vector<xtd::byte>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags);
1141 
1157  size_t receive(std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags);
1158 
1174  size_t receive(std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::sockets::socket_error& error);
1175 
1191  size_t receive_from(std::vector<xtd::byte>& buffer, xtd::net::end_point& remote_end_point);
1192 
1209  size_t receive_from(std::vector<xtd::byte>& buffer, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point);
1210 
1229  size_t receive_from(std::vector<xtd::byte>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point);
1230 
1249  size_t receive_from(std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::end_point& remote_end_point);
1250 
1251  size_t receive_message_from(std::vector<xtd::byte>& 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);
1252 
1268  static size_t select(std::vector<socket>& check_read, std::vector<socket>& check_write, std::vector<socket>& check_error, int32 microseconds);
1269 
1282  size_t send(const std::vector<xtd::byte>& buffer);
1283 
1297  size_t send(const std::vector<xtd::byte>& buffer, xtd::net::sockets::socket_flags socket_flags);
1298 
1314  size_t send(const std::vector<xtd::byte>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags);
1315 
1332  size_t send(const std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags);
1333 
1349  size_t send(const std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, xtd::net::sockets::socket_error& error_code);
1350 
1363  size_t send_to(const std::vector<xtd::byte>& buffer, const xtd::net::end_point& remote_end_point);
1377  size_t send_to(const std::vector<xtd::byte>& buffer, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point);
1393  size_t send_to(const std::vector<xtd::byte>& buffer, size_t size, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point);
1410  size_t send_to(const std::vector<xtd::byte>& buffer, size_t offset, size_t size, xtd::net::sockets::socket_flags socket_flags, const xtd::net::end_point& remote_end_point);
1411 
1423  void set_ip_protection_level(xtd::net::sockets::ip_protection_level level);
1424 
1454  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);
1455 
1496  void set_socket_option(xtd::net::sockets::socket_option_level socket_option_level, xtd::net::sockets::socket_option_name socket_option_name, int32 option_value);
1502  void set_socket_option(xtd::net::sockets::linger_option option_value);
1509  void set_socket_option(xtd::net::sockets::socket_option_name socket_option_name, const xtd::net::sockets::multicast_option& option_value);
1516  void set_socket_option(xtd::net::sockets::socket_option_name socket_option_name, const xtd::net::sockets::ip_v6_multicast_option& option_value);
1517 
1528  void set_raw_socket_option(int32 socket_option_level, int32 socket_option_name, intptr option_value, size_t option_value_size);
1529 
1551 
1552  private:
1553  static xtd::net::sockets::socket_error get_last_error_();
1554  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);
1555  void bind_(std::shared_ptr<xtd::net::end_point> local_end_point);
1556  void connect_(std::shared_ptr<xtd::net::end_point> remote_end_point);
1557 
1558  std::shared_ptr<data> data_;
1559  };
1560  }
1561  }
1562 }
Implements the Berkeley sockets interface.
Definition: socket.h:71
The xtd::uri::host data.
protocol_type
Specifies the protocols that the xtd::net::sockets::socket class supports.
Definition: protocol_type.h:28
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:29
Provides an Internet Protocol (IP) address.
Definition: ip_address.h:35
Contains xtd::net::sockets::multicast_option class.
Contains xtd::net::sockets::io_control_code enum.
intmax_t intptr
Represent a pointer or a handle.
Definition: types.h:151
Gets the error status and clear.
The SELECT key.
io_control_code
Specifies the IO control codes supported by the xtd::net::sockets::socket::io_control method...
Definition: io_control_code.h:28
delegate< void(async_result ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition: delegate.h:34
Specifies whether a xtd::net::sockets::socket will remain connected after a call to the xtd::net::soc...
Definition: linger_option.h:32
Represents the status of an asynchronous operation.
Definition: iasync_result.h:22
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:574
socket_option_name
Defines configuration option names.
Definition: socket_option_name.h:29
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:25
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
std::shared_ptr< xtd::iasync_result > async_result
Represents the status of an asynchronous operation.
Definition: async_result.h:16
Gets the network interface information that is associated with a call to xtd::net::sockets::socket::r...
Definition: ip_packet_information.h:34
Contains xtd::net::sockets::socket_type enum.
Contains xtd::net::sockets::select_mode enum.
Enables a socket to be bound for exclusive access.
uint_least16_t uint16
Represents a 16-bit unsigned integer.
Definition: types.h:228
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
Contains IPAddress values used to join and drop multicast groups.
Definition: multicast_option.h:37
socket_error
Defines error codes for the xtd::net::sockets::socket class.
Definition: socket_error.h:28
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:791
select_mode
Defines the polling modes for the xtd::net::sockets::socket::poll method.
Definition: select_mode.h:29
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:18
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:28
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:32
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
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:29
socket_type
Specifies the type of socket that an instance of the xtd::net::sockets::socket class represents...
Definition: socket_type.h:30
Contains option values for joining an IPv6 multicast group.
Definition: ip_v6_multicast_option.h:30
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:29
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:812
The socket handle may be reused when the request completes. This flag is valid only if xtd::net::sock...
uint_least8_t byte
Represents a 8-bit unsigned integer.
Definition: types.h:39
Represents an asynchronous socket operation.
Definition: socket_async_event_args.h:52
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:31
Contains xtd::net::sockets::socket_option_level enum.
size_t size
Represents a size of any object in bytes.
Definition: types.h:195
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: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