descriptor_ops.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // descriptor_ops.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 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 BOOST_ASIO_DETAIL_DESCRIPTOR_OPS_HPP
  11. #define BOOST_ASIO_DETAIL_DESCRIPTOR_OPS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <cerrno>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/detail/socket_types.hpp>
  22. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. namespace descriptor_ops {
  27. inline void clear_error(boost::system::error_code& ec)
  28. {
  29. errno = 0;
  30. ec = boost::system::error_code();
  31. }
  32. template <typename ReturnType>
  33. inline ReturnType error_wrapper(ReturnType return_value,
  34. boost::system::error_code& ec)
  35. {
  36. ec = boost::system::error_code(errno,
  37. boost::asio::error::get_system_category());
  38. return return_value;
  39. }
  40. inline int open(const char* path, int flags, boost::system::error_code& ec)
  41. {
  42. clear_error(ec);
  43. int result = error_wrapper(::open(path, flags), ec);
  44. if (result >= 0)
  45. clear_error(ec);
  46. return result;
  47. }
  48. inline int close(int d, boost::system::error_code& ec)
  49. {
  50. clear_error(ec);
  51. int result = error_wrapper(::close(d), ec);
  52. if (result == 0)
  53. clear_error(ec);
  54. return result;
  55. }
  56. inline void init_buf_iov_base(void*& base, void* addr)
  57. {
  58. base = addr;
  59. }
  60. template <typename T>
  61. inline void init_buf_iov_base(T& base, void* addr)
  62. {
  63. base = static_cast<T>(addr);
  64. }
  65. typedef iovec buf;
  66. inline void init_buf(buf& b, void* data, size_t size)
  67. {
  68. init_buf_iov_base(b.iov_base, data);
  69. b.iov_len = size;
  70. }
  71. inline void init_buf(buf& b, const void* data, size_t size)
  72. {
  73. init_buf_iov_base(b.iov_base, const_cast<void*>(data));
  74. b.iov_len = size;
  75. }
  76. inline int scatter_read(int d, buf* bufs, size_t count,
  77. boost::system::error_code& ec)
  78. {
  79. clear_error(ec);
  80. int result = error_wrapper(::readv(d, bufs, static_cast<int>(count)), ec);
  81. if (result >= 0)
  82. clear_error(ec);
  83. return result;
  84. }
  85. inline int gather_write(int d, const buf* bufs, size_t count,
  86. boost::system::error_code& ec)
  87. {
  88. clear_error(ec);
  89. int result = error_wrapper(::writev(d, bufs, static_cast<int>(count)), ec);
  90. if (result >= 0)
  91. clear_error(ec);
  92. return result;
  93. }
  94. inline int ioctl(int d, long cmd, ioctl_arg_type* arg,
  95. boost::system::error_code& ec)
  96. {
  97. clear_error(ec);
  98. int result = error_wrapper(::ioctl(d, cmd, arg), ec);
  99. if (result >= 0)
  100. clear_error(ec);
  101. return result;
  102. }
  103. inline int fcntl(int d, long cmd, boost::system::error_code& ec)
  104. {
  105. clear_error(ec);
  106. int result = error_wrapper(::fcntl(d, cmd), ec);
  107. if (result != -1)
  108. clear_error(ec);
  109. return result;
  110. }
  111. inline int fcntl(int d, long cmd, long arg, boost::system::error_code& ec)
  112. {
  113. clear_error(ec);
  114. int result = error_wrapper(::fcntl(d, cmd, arg), ec);
  115. if (result != -1)
  116. clear_error(ec);
  117. return result;
  118. }
  119. inline int poll_read(int d, boost::system::error_code& ec)
  120. {
  121. clear_error(ec);
  122. pollfd fds;
  123. fds.fd = d;
  124. fds.events = POLLIN;
  125. fds.revents = 0;
  126. clear_error(ec);
  127. int result = error_wrapper(::poll(&fds, 1, -1), ec);
  128. if (result >= 0)
  129. clear_error(ec);
  130. return result;
  131. }
  132. inline int poll_write(int d, boost::system::error_code& ec)
  133. {
  134. clear_error(ec);
  135. pollfd fds;
  136. fds.fd = d;
  137. fds.events = POLLOUT;
  138. fds.revents = 0;
  139. clear_error(ec);
  140. int result = error_wrapper(::poll(&fds, 1, -1), ec);
  141. if (result >= 0)
  142. clear_error(ec);
  143. return result;
  144. }
  145. } // namespace descriptor_ops
  146. } // namespace detail
  147. } // namespace asio
  148. } // namespace boost
  149. #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  150. #include <boost/asio/detail/pop_options.hpp>
  151. #endif // BOOST_ASIO_DETAIL_DESCRIPTOR_OPS_HPP