socket_ops.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // detail/socket_ops.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_SOCKET_OPS_HPP
  11. #define ASIO_DETAIL_SOCKET_OPS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/error_code.hpp"
  17. #include "asio/detail/shared_ptr.hpp"
  18. #include "asio/detail/socket_types.hpp"
  19. #include "asio/detail/weak_ptr.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. namespace socket_ops {
  24. // Socket state bits.
  25. enum
  26. {
  27. // The user wants a non-blocking socket.
  28. user_set_non_blocking = 1,
  29. // The socket has been set non-blocking.
  30. internal_non_blocking = 2,
  31. // Helper "state" used to determine whether the socket is non-blocking.
  32. non_blocking = user_set_non_blocking | internal_non_blocking,
  33. // User wants connection_aborted errors, which are disabled by default.
  34. enable_connection_aborted = 4,
  35. // The user set the linger option. Needs to be checked when closing.
  36. user_set_linger = 8,
  37. // The socket is stream-oriented.
  38. stream_oriented = 16,
  39. // The socket is datagram-oriented.
  40. datagram_oriented = 32
  41. };
  42. typedef unsigned char state_type;
  43. struct noop_deleter { void operator()(void*) {} };
  44. typedef shared_ptr<void> shared_cancel_token_type;
  45. typedef weak_ptr<void> weak_cancel_token_type;
  46. ASIO_DECL socket_type accept(socket_type s, socket_addr_type* addr,
  47. std::size_t* addrlen, asio::error_code& ec);
  48. ASIO_DECL socket_type sync_accept(socket_type s,
  49. state_type state, socket_addr_type* addr,
  50. std::size_t* addrlen, asio::error_code& ec);
  51. #if defined(ASIO_HAS_IOCP)
  52. ASIO_DECL void complete_iocp_accept(socket_type s,
  53. void* output_buffer, DWORD address_length,
  54. socket_addr_type* addr, std::size_t* addrlen,
  55. socket_type new_socket, asio::error_code& ec);
  56. #else // defined(ASIO_HAS_IOCP)
  57. ASIO_DECL bool non_blocking_accept(socket_type s,
  58. state_type state, socket_addr_type* addr, std::size_t* addrlen,
  59. asio::error_code& ec, socket_type& new_socket);
  60. #endif // defined(ASIO_HAS_IOCP)
  61. ASIO_DECL int bind(socket_type s, const socket_addr_type* addr,
  62. std::size_t addrlen, asio::error_code& ec);
  63. ASIO_DECL int close(socket_type s, state_type& state,
  64. bool destruction, asio::error_code& ec);
  65. ASIO_DECL bool set_internal_non_blocking(socket_type s,
  66. state_type& state, asio::error_code& ec);
  67. ASIO_DECL int shutdown(socket_type s,
  68. int what, asio::error_code& ec);
  69. ASIO_DECL int connect(socket_type s, const socket_addr_type* addr,
  70. std::size_t addrlen, asio::error_code& ec);
  71. ASIO_DECL void sync_connect(socket_type s, const socket_addr_type* addr,
  72. std::size_t addrlen, asio::error_code& ec);
  73. ASIO_DECL bool non_blocking_connect(
  74. socket_type s, asio::error_code& ec);
  75. ASIO_DECL int socketpair(int af, int type, int protocol,
  76. socket_type sv[2], asio::error_code& ec);
  77. ASIO_DECL bool sockatmark(socket_type s, asio::error_code& ec);
  78. ASIO_DECL size_t available(socket_type s, asio::error_code& ec);
  79. ASIO_DECL int listen(socket_type s,
  80. int backlog, asio::error_code& ec);
  81. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  82. typedef WSABUF buf;
  83. #else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  84. typedef iovec buf;
  85. #endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  86. ASIO_DECL void init_buf(buf& b, void* data, size_t size);
  87. ASIO_DECL void init_buf(buf& b, const void* data, size_t size);
  88. ASIO_DECL int recv(socket_type s, buf* bufs, size_t count, int flags,
  89. asio::error_code& ec);
  90. ASIO_DECL size_t sync_recv(socket_type s, state_type state, buf* bufs,
  91. size_t count, int flags, bool all_empty, asio::error_code& ec);
  92. #if defined(ASIO_HAS_IOCP)
  93. ASIO_DECL void complete_iocp_recv(state_type state,
  94. const weak_cancel_token_type& cancel_token, bool all_empty,
  95. asio::error_code& ec, size_t bytes_transferred);
  96. #else // defined(ASIO_HAS_IOCP)
  97. ASIO_DECL bool non_blocking_recv(socket_type s,
  98. buf* bufs, size_t count, int flags, bool is_stream,
  99. asio::error_code& ec, size_t& bytes_transferred);
  100. #endif // defined(ASIO_HAS_IOCP)
  101. ASIO_DECL int recvfrom(socket_type s, buf* bufs, size_t count, int flags,
  102. socket_addr_type* addr, std::size_t* addrlen,
  103. asio::error_code& ec);
  104. ASIO_DECL size_t sync_recvfrom(socket_type s, state_type state,
  105. buf* bufs, size_t count, int flags, socket_addr_type* addr,
  106. std::size_t* addrlen, asio::error_code& ec);
  107. #if defined(ASIO_HAS_IOCP)
  108. ASIO_DECL void complete_iocp_recvfrom(
  109. const weak_cancel_token_type& cancel_token,
  110. asio::error_code& ec);
  111. #else // defined(ASIO_HAS_IOCP)
  112. ASIO_DECL bool non_blocking_recvfrom(socket_type s,
  113. buf* bufs, size_t count, int flags,
  114. socket_addr_type* addr, std::size_t* addrlen,
  115. asio::error_code& ec, size_t& bytes_transferred);
  116. #endif // defined(ASIO_HAS_IOCP)
  117. ASIO_DECL int send(socket_type s, const buf* bufs,
  118. size_t count, int flags, asio::error_code& ec);
  119. ASIO_DECL size_t sync_send(socket_type s, state_type state,
  120. const buf* bufs, size_t count, int flags,
  121. bool all_empty, asio::error_code& ec);
  122. #if defined(ASIO_HAS_IOCP)
  123. ASIO_DECL void complete_iocp_send(
  124. const weak_cancel_token_type& cancel_token,
  125. asio::error_code& ec);
  126. #else // defined(ASIO_HAS_IOCP)
  127. ASIO_DECL bool non_blocking_send(socket_type s,
  128. const buf* bufs, size_t count, int flags,
  129. asio::error_code& ec, size_t& bytes_transferred);
  130. #endif // defined(ASIO_HAS_IOCP)
  131. ASIO_DECL int sendto(socket_type s, const buf* bufs, size_t count,
  132. int flags, const socket_addr_type* addr, std::size_t addrlen,
  133. asio::error_code& ec);
  134. ASIO_DECL size_t sync_sendto(socket_type s, state_type state,
  135. const buf* bufs, size_t count, int flags, const socket_addr_type* addr,
  136. std::size_t addrlen, asio::error_code& ec);
  137. #if !defined(ASIO_HAS_IOCP)
  138. ASIO_DECL bool non_blocking_sendto(socket_type s,
  139. const buf* bufs, size_t count, int flags,
  140. const socket_addr_type* addr, std::size_t addrlen,
  141. asio::error_code& ec, size_t& bytes_transferred);
  142. #endif // !defined(ASIO_HAS_IOCP)
  143. ASIO_DECL socket_type socket(int af, int type, int protocol,
  144. asio::error_code& ec);
  145. ASIO_DECL int setsockopt(socket_type s, state_type& state,
  146. int level, int optname, const void* optval,
  147. std::size_t optlen, asio::error_code& ec);
  148. ASIO_DECL int getsockopt(socket_type s, state_type state,
  149. int level, int optname, void* optval,
  150. size_t* optlen, asio::error_code& ec);
  151. ASIO_DECL int getpeername(socket_type s, socket_addr_type* addr,
  152. std::size_t* addrlen, bool cached, asio::error_code& ec);
  153. ASIO_DECL int getsockname(socket_type s, socket_addr_type* addr,
  154. std::size_t* addrlen, asio::error_code& ec);
  155. ASIO_DECL int ioctl(socket_type s, state_type& state,
  156. int cmd, ioctl_arg_type* arg, asio::error_code& ec);
  157. ASIO_DECL int select(int nfds, fd_set* readfds, fd_set* writefds,
  158. fd_set* exceptfds, timeval* timeout, asio::error_code& ec);
  159. ASIO_DECL int poll_read(socket_type s, asio::error_code& ec);
  160. ASIO_DECL int poll_write(socket_type s, asio::error_code& ec);
  161. ASIO_DECL int poll_connect(socket_type s, asio::error_code& ec);
  162. ASIO_DECL const char* inet_ntop(int af, const void* src, char* dest,
  163. size_t length, unsigned long scope_id, asio::error_code& ec);
  164. ASIO_DECL int inet_pton(int af, const char* src, void* dest,
  165. unsigned long* scope_id, asio::error_code& ec);
  166. ASIO_DECL int gethostname(char* name,
  167. int namelen, asio::error_code& ec);
  168. ASIO_DECL asio::error_code getaddrinfo(const char* host,
  169. const char* service, const addrinfo_type& hints,
  170. addrinfo_type** result, asio::error_code& ec);
  171. ASIO_DECL asio::error_code background_getaddrinfo(
  172. const weak_cancel_token_type& cancel_token, const char* host,
  173. const char* service, const addrinfo_type& hints,
  174. addrinfo_type** result, asio::error_code& ec);
  175. ASIO_DECL void freeaddrinfo(addrinfo_type* ai);
  176. ASIO_DECL asio::error_code getnameinfo(
  177. const socket_addr_type* addr, std::size_t addrlen,
  178. char* host, std::size_t hostlen, char* serv,
  179. std::size_t servlen, int flags, asio::error_code& ec);
  180. ASIO_DECL asio::error_code sync_getnameinfo(
  181. const socket_addr_type* addr, std::size_t addrlen,
  182. char* host, std::size_t hostlen, char* serv,
  183. std::size_t servlen, int sock_type, asio::error_code& ec);
  184. ASIO_DECL asio::error_code background_getnameinfo(
  185. const weak_cancel_token_type& cancel_token,
  186. const socket_addr_type* addr, std::size_t addrlen,
  187. char* host, std::size_t hostlen, char* serv,
  188. std::size_t servlen, int sock_type, asio::error_code& ec);
  189. ASIO_DECL u_long_type network_to_host_long(u_long_type value);
  190. ASIO_DECL u_long_type host_to_network_long(u_long_type value);
  191. ASIO_DECL u_short_type network_to_host_short(u_short_type value);
  192. ASIO_DECL u_short_type host_to_network_short(u_short_type value);
  193. } // namespace socket_ops
  194. } // namespace detail
  195. } // namespace asio
  196. #include "asio/detail/pop_options.hpp"
  197. #if defined(ASIO_HEADER_ONLY)
  198. # include "asio/detail/impl/socket_ops.ipp"
  199. #endif // defined(ASIO_HEADER_ONLY)
  200. #endif // ASIO_DETAIL_SOCKET_OPS_HPP