descriptor_ops.ipp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // detail/impl/descriptor_ops.ipp
  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_IMPL_DESCRIPTOR_OPS_IPP
  11. #define ASIO_DETAIL_IMPL_DESCRIPTOR_OPS_IPP
  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 <cerrno>
  17. #include "asio/detail/descriptor_ops.hpp"
  18. #include "asio/error.hpp"
  19. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. namespace descriptor_ops {
  24. ASIO_DECL
  25. int open(const char* path, int flags, asio::error_code& ec)
  26. {
  27. errno = 0;
  28. int result = error_wrapper(::open(path, flags), ec);
  29. if (result >= 0)
  30. ec = asio::error_code();
  31. return result;
  32. }
  33. ASIO_DECL
  34. int close(int d, state_type& state, asio::error_code& ec)
  35. {
  36. int result = 0;
  37. if (d != -1)
  38. {
  39. if (state & internal_non_blocking)
  40. {
  41. #if defined(__SYMBIAN32__)
  42. int flags = ::fcntl(d, F_GETFL, 0);
  43. if (flags >= 0)
  44. ::fcntl(d, F_SETFL, flags & ~O_NONBLOCK);
  45. #else // defined(__SYMBIAN32__)
  46. ioctl_arg_type arg = 0;
  47. ::ioctl(d, FIONBIO, &arg);
  48. #endif // defined(__SYMBIAN32__)
  49. state &= ~internal_non_blocking;
  50. }
  51. errno = 0;
  52. result = error_wrapper(::close(d), ec);
  53. }
  54. if (result == 0)
  55. ec = asio::error_code();
  56. return result;
  57. }
  58. ASIO_DECL
  59. bool set_internal_non_blocking(int d,
  60. state_type& state, asio::error_code& ec)
  61. {
  62. if (d == -1)
  63. {
  64. ec = asio::error::bad_descriptor;
  65. return false;
  66. }
  67. errno = 0;
  68. #if defined(__SYMBIAN32__)
  69. int result = error_wrapper(::fcntl(d, F_GETFL, 0), ec);
  70. if (result >= 0)
  71. {
  72. errno = 0;
  73. result = error_wrapper(::fcntl(d, F_SETFL, result | O_NONBLOCK), ec);
  74. }
  75. #else // defined(__SYMBIAN32__)
  76. ioctl_arg_type arg = 1;
  77. int result = error_wrapper(::ioctl(d, FIONBIO, &arg), ec);
  78. #endif // defined(__SYMBIAN32__)
  79. if (result >= 0)
  80. {
  81. ec = asio::error_code();
  82. state |= internal_non_blocking;
  83. return true;
  84. }
  85. return false;
  86. }
  87. ASIO_DECL
  88. std::size_t sync_read(int d, state_type state, buf* bufs,
  89. std::size_t count, bool all_empty, asio::error_code& ec)
  90. {
  91. if (d == -1)
  92. {
  93. ec = asio::error::bad_descriptor;
  94. return 0;
  95. }
  96. // A request to read 0 bytes on a stream is a no-op.
  97. if (all_empty)
  98. {
  99. ec = asio::error_code();
  100. return 0;
  101. }
  102. // Read some data.
  103. for (;;)
  104. {
  105. // Try to complete the operation without blocking.
  106. errno = 0;
  107. int bytes = error_wrapper(::readv(d, bufs, static_cast<int>(count)), ec);
  108. // Check if operation succeeded.
  109. if (bytes > 0)
  110. return bytes;
  111. // Check for EOF.
  112. if (bytes == 0)
  113. {
  114. ec = asio::error::eof;
  115. return 0;
  116. }
  117. // Operation failed.
  118. if ((state & user_set_non_blocking)
  119. || (ec != asio::error::would_block
  120. && ec != asio::error::try_again))
  121. return 0;
  122. // Wait for descriptor to become ready.
  123. if (descriptor_ops::poll_read(d, ec) < 0)
  124. return 0;
  125. }
  126. }
  127. ASIO_DECL
  128. bool non_blocking_read(int d, buf* bufs, std::size_t count,
  129. asio::error_code& ec, std::size_t& bytes_transferred)
  130. {
  131. for (;;)
  132. {
  133. // Read some data.
  134. errno = 0;
  135. int bytes = error_wrapper(::readv(d, bufs, static_cast<int>(count)), ec);
  136. // Check for end of stream.
  137. if (bytes == 0)
  138. {
  139. ec = asio::error::eof;
  140. return true;
  141. }
  142. // Retry operation if interrupted by signal.
  143. if (ec == asio::error::interrupted)
  144. continue;
  145. // Check if we need to run the operation again.
  146. if (ec == asio::error::would_block
  147. || ec == asio::error::try_again)
  148. return false;
  149. // Operation is complete.
  150. if (bytes > 0)
  151. {
  152. ec = asio::error_code();
  153. bytes_transferred = bytes;
  154. }
  155. else
  156. bytes_transferred = 0;
  157. return true;
  158. }
  159. }
  160. ASIO_DECL
  161. std::size_t sync_write(int d, state_type state, const buf* bufs,
  162. std::size_t count, bool all_empty, asio::error_code& ec)
  163. {
  164. if (d == -1)
  165. {
  166. ec = asio::error::bad_descriptor;
  167. return 0;
  168. }
  169. // A request to write 0 bytes on a stream is a no-op.
  170. if (all_empty)
  171. {
  172. ec = asio::error_code();
  173. return 0;
  174. }
  175. // Write some data.
  176. for (;;)
  177. {
  178. // Try to complete the operation without blocking.
  179. errno = 0;
  180. int bytes = error_wrapper(::writev(d, bufs, static_cast<int>(count)), ec);
  181. // Check if operation succeeded.
  182. if (bytes > 0)
  183. return bytes;
  184. // Operation failed.
  185. if ((state & user_set_non_blocking)
  186. || (ec != asio::error::would_block
  187. && ec != asio::error::try_again))
  188. return 0;
  189. // Wait for descriptor to become ready.
  190. if (descriptor_ops::poll_write(d, ec) < 0)
  191. return 0;
  192. }
  193. }
  194. ASIO_DECL
  195. bool non_blocking_write(int d, const buf* bufs, std::size_t count,
  196. asio::error_code& ec, std::size_t& bytes_transferred)
  197. {
  198. for (;;)
  199. {
  200. // Write some data.
  201. errno = 0;
  202. int bytes = error_wrapper(::writev(d, bufs, static_cast<int>(count)), ec);
  203. // Retry operation if interrupted by signal.
  204. if (ec == asio::error::interrupted)
  205. continue;
  206. // Check if we need to run the operation again.
  207. if (ec == asio::error::would_block
  208. || ec == asio::error::try_again)
  209. return false;
  210. // Operation is complete.
  211. if (bytes >= 0)
  212. {
  213. ec = asio::error_code();
  214. bytes_transferred = bytes;
  215. }
  216. else
  217. bytes_transferred = 0;
  218. return true;
  219. }
  220. }
  221. ASIO_DECL
  222. int ioctl(int d, state_type& state, long cmd,
  223. ioctl_arg_type* arg, asio::error_code& ec)
  224. {
  225. if (d == -1)
  226. {
  227. ec = asio::error::bad_descriptor;
  228. return -1;
  229. }
  230. errno = 0;
  231. int result = error_wrapper(::ioctl(d, cmd, arg), ec);
  232. if (result >= 0)
  233. {
  234. ec = asio::error_code();
  235. // When updating the non-blocking mode we always perform the ioctl syscall,
  236. // even if the flags would otherwise indicate that the descriptor is
  237. // already in the correct state. This ensures that the underlying
  238. // descriptor is put into the state that has been requested by the user. If
  239. // the ioctl syscall was successful then we need to update the flags to
  240. // match.
  241. if (cmd == static_cast<long>(FIONBIO))
  242. {
  243. if (*arg)
  244. {
  245. state |= user_set_non_blocking;
  246. }
  247. else
  248. {
  249. // Clearing the non-blocking mode always overrides any internally-set
  250. // non-blocking flag. Any subsequent asynchronous operations will need
  251. // to re-enable non-blocking I/O.
  252. state &= ~(user_set_non_blocking | internal_non_blocking);
  253. }
  254. }
  255. }
  256. return result;
  257. }
  258. ASIO_DECL
  259. int fcntl(int d, long cmd, asio::error_code& ec)
  260. {
  261. if (d == -1)
  262. {
  263. ec = asio::error::bad_descriptor;
  264. return -1;
  265. }
  266. errno = 0;
  267. int result = error_wrapper(::fcntl(d, cmd), ec);
  268. if (result != -1)
  269. ec = asio::error_code();
  270. return result;
  271. }
  272. ASIO_DECL
  273. int fcntl(int d, long cmd, long arg, asio::error_code& ec)
  274. {
  275. if (d == -1)
  276. {
  277. ec = asio::error::bad_descriptor;
  278. return -1;
  279. }
  280. errno = 0;
  281. int result = error_wrapper(::fcntl(d, cmd, arg), ec);
  282. if (result != -1)
  283. ec = asio::error_code();
  284. return result;
  285. }
  286. ASIO_DECL
  287. int poll_read(int d, asio::error_code& ec)
  288. {
  289. if (d == -1)
  290. {
  291. ec = asio::error::bad_descriptor;
  292. return -1;
  293. }
  294. pollfd fds;
  295. fds.fd = d;
  296. fds.events = POLLIN;
  297. fds.revents = 0;
  298. errno = 0;
  299. int result = error_wrapper(::poll(&fds, 1, -1), ec);
  300. if (result >= 0)
  301. ec = asio::error_code();
  302. return result;
  303. }
  304. ASIO_DECL
  305. int poll_write(int d, asio::error_code& ec)
  306. {
  307. if (d == -1)
  308. {
  309. ec = asio::error::bad_descriptor;
  310. return -1;
  311. }
  312. pollfd fds;
  313. fds.fd = d;
  314. fds.events = POLLOUT;
  315. fds.revents = 0;
  316. errno = 0;
  317. int result = error_wrapper(::poll(&fds, 1, -1), ec);
  318. if (result >= 0)
  319. ec = asio::error_code();
  320. return result;
  321. }
  322. } // namespace descriptor_ops
  323. } // namespace detail
  324. } // namespace asio
  325. #include "asio/detail/pop_options.hpp"
  326. #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  327. #endif // ASIO_DETAIL_IMPL_DESCRIPTOR_OPS_IPP