io_control.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // io_control.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_IO_CONTROL_HPP
  11. #define ASIO_DETAIL_IO_CONTROL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #include "asio/detail/socket_types.hpp"
  21. namespace asio {
  22. namespace detail {
  23. namespace io_control {
  24. // IO control command for non-blocking I/O.
  25. class non_blocking_io
  26. {
  27. public:
  28. // Default constructor.
  29. non_blocking_io()
  30. : value_(0)
  31. {
  32. }
  33. // Construct with a specific command value.
  34. non_blocking_io(bool value)
  35. : value_(value ? 1 : 0)
  36. {
  37. }
  38. // Get the name of the IO control command.
  39. int name() const
  40. {
  41. return FIONBIO;
  42. }
  43. // Set the value of the I/O control command.
  44. void set(bool value)
  45. {
  46. value_ = value ? 1 : 0;
  47. }
  48. // Get the current value of the I/O control command.
  49. bool get() const
  50. {
  51. return value_ != 0;
  52. }
  53. // Get the address of the command data.
  54. detail::ioctl_arg_type* data()
  55. {
  56. return &value_;
  57. }
  58. // Get the address of the command data.
  59. const detail::ioctl_arg_type* data() const
  60. {
  61. return &value_;
  62. }
  63. private:
  64. detail::ioctl_arg_type value_;
  65. };
  66. // I/O control command for getting number of bytes available.
  67. class bytes_readable
  68. {
  69. public:
  70. // Default constructor.
  71. bytes_readable()
  72. : value_(0)
  73. {
  74. }
  75. // Construct with a specific command value.
  76. bytes_readable(std::size_t value)
  77. : value_(static_cast<detail::ioctl_arg_type>(value))
  78. {
  79. }
  80. // Get the name of the IO control command.
  81. int name() const
  82. {
  83. return FIONREAD;
  84. }
  85. // Set the value of the I/O control command.
  86. void set(std::size_t value)
  87. {
  88. value_ = static_cast<detail::ioctl_arg_type>(value);
  89. }
  90. // Get the current value of the I/O control command.
  91. std::size_t get() const
  92. {
  93. return static_cast<std::size_t>(value_);
  94. }
  95. // Get the address of the command data.
  96. detail::ioctl_arg_type* data()
  97. {
  98. return &value_;
  99. }
  100. // Get the address of the command data.
  101. const detail::ioctl_arg_type* data() const
  102. {
  103. return &value_;
  104. }
  105. private:
  106. detail::ioctl_arg_type value_;
  107. };
  108. } // namespace io_control
  109. } // namespace detail
  110. } // namespace asio
  111. #include "asio/detail/pop_options.hpp"
  112. #endif // ASIO_DETAIL_IO_CONTROL_HPP