PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/3rdparty/libprocess/include/process/io.hpp

https://gitlab.com/wilane/mesos
C++ Header | 189 lines | 31 code | 24 blank | 134 comment | 0 complexity | c487f177b35d90621115c76497c44acc MD5 | raw file
  1. // Licensed under the Apache License, Version 2.0 (the "License");
  2. // you may not use this file except in compliance with the License.
  3. // You may obtain a copy of the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS,
  9. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. // See the License for the specific language governing permissions and
  11. // limitations under the License
  12. #ifndef __PROCESS_IO_HPP__
  13. #define __PROCESS_IO_HPP__
  14. #include <cstring> // For size_t.
  15. #include <string>
  16. #include <process/future.hpp>
  17. #include <stout/nothing.hpp>
  18. #ifdef __WINDOWS__
  19. #include <stout/windows.hpp>
  20. #endif // __WINDOWS__
  21. namespace process {
  22. namespace io {
  23. /**
  24. * A possible event while polling.
  25. *
  26. * @see process::io::poll
  27. */
  28. const short READ = 0x01;
  29. /**
  30. * @copydoc process::io::READ
  31. */
  32. const short WRITE = 0x02;
  33. /**
  34. * Buffered read chunk size.
  35. *
  36. * Roughly 16 pages.
  37. */
  38. const size_t BUFFERED_READ_SIZE = 16*4096;
  39. /**
  40. * Returns the events (a subset of the events specified) that can be
  41. * performed on the specified file descriptor without blocking.
  42. *
  43. * @see process::io::READ
  44. * @see process::io::WRITE
  45. */
  46. // TODO(benh): Add a version which takes multiple file descriptors.
  47. Future<short> poll(int fd, short events);
  48. /**
  49. * Performs a single non-blocking read by polling on the specified
  50. * file descriptor until any data can be be read.
  51. *
  52. * The future will become ready when some data is read (may be less than
  53. * the specified size).
  54. *
  55. * @return The number of bytes read or zero on EOF.
  56. * A failure will be returned if an error is detected.
  57. */
  58. Future<size_t> read(int fd, void* data, size_t size);
  59. /**
  60. * Performs a series of asynchronous reads, until EOF is reached.
  61. *
  62. * **NOTE**: when using this, ensure the sender will close the connection
  63. * so that EOF can be reached.
  64. *
  65. * @return The concatentated result of the reads.
  66. * A failure will be returned if the file descriptor is bad, or if the
  67. * file descriptor cannot be duplicated, set to close-on-exec,
  68. * or made non-blocking.
  69. */
  70. Future<std::string> read(int fd);
  71. #ifdef __WINDOWS__
  72. // Version of this function compatible with Windows `HANDLE`.
  73. Future<std::string> read(HANDLE fd);
  74. #endif // __WINDOWS__
  75. /**
  76. * Performs a single non-blocking write by polling on the specified
  77. * file descriptor until data can be be written.
  78. *
  79. * The future will become ready when some data is written (may be less than
  80. * the specified size of the data).
  81. *
  82. * @return The number of bytes written.
  83. * A failure will be returned if an error is detected.
  84. * If writing to a socket or pipe, an error will be returned if the
  85. * the read end of the socket or pipe has been closed.
  86. */
  87. Future<size_t> write(int fd, const void* data, size_t size);
  88. /**
  89. * Performs a series of asynchronous writes, until all of data has been
  90. * written.
  91. *
  92. * @return Nothing or a failure if an error occurred.
  93. * A failure will be returned if the file descriptor is bad, or if the
  94. * file descriptor cannot be duplicated, set to close-on-exec,
  95. * or made non-blocking.
  96. */
  97. Future<Nothing> write(int fd, const std::string& data);
  98. /**
  99. * Redirect output from the 'from' file descriptor to the 'to' file
  100. * descriptor (or /dev/null if 'to' is None).
  101. *
  102. * The 'to' and 'from' file descriptors will be duplicated so that the
  103. * file descriptors' lifetimes can be controlled within this function.
  104. *
  105. * @return Nothing after EOF has been encountered on 'from' or if a
  106. * failure has occurred. A failure will be returned if the file
  107. * descriptor is bad, or if the file descriptor cannot be duplicated,
  108. * set to close-on-exec, or made non-blocking.
  109. */
  110. Future<Nothing> redirect(int from, Option<int> to, size_t chunk = 4096);
  111. #ifdef __WINDOWS__
  112. // Version of this function compatible with Windows `HANDLE`.
  113. Future<Nothing> redirect(HANDLE from, Option<int> to, size_t chunk = 4096);
  114. #endif // __WINDOWS__
  115. /**
  116. * Performs a single non-blocking peek by polling on the specified
  117. * file descriptor until any data can be be peeked.
  118. *
  119. * The future will become ready when some data is peeked (may be less
  120. * than specified by the limit). A failure will be returned if an error
  121. * is detected. If end-of-file is reached, value zero will be returned.
  122. *
  123. * **NOTE**: This function is inspired by the MSG_PEEK flag of recv()
  124. * in that it does not remove the peeked data from the queue. Thus, a
  125. * subsequent io::read or io::peek() call will return the same data.
  126. *
  127. * TODO(hartem): This function will currently return an error if fd
  128. * is not a socket descriptor. Chnages need to be made to support
  129. * ordinary files and pipes as well.
  130. *
  131. * @param fd socket descriptor.
  132. * @param data buffer to which peek'd bytes will be copied.
  133. * @param size size of the buffer.
  134. * @param limit maximum number of bytes to peek.
  135. * @return The number of bytes peeked.
  136. * A failure will be returned if an error is detected.
  137. */
  138. Future<size_t> peek(int fd, void* data, size_t size, size_t limit);
  139. /**
  140. * A more convenient version of io::peek that does not require
  141. * allocating the buffer.
  142. *
  143. * **NOTE**: this function treats the limit parameter merely as an
  144. * upper bound for the size of the data to peek. It does not wait
  145. * until the specified amount of bytes is peeked. It returns as soon
  146. * as some amount of data becomes available.
  147. * It can not concatenate data from subsequent peeks because MSG_PEEK
  148. * has known limitations when it comes to spanning message boundaries.
  149. *
  150. * **NOTE**: this function will return an error if the limit is
  151. * greater than the internal peek buffer size (64k as of writing this
  152. * comment, io::BUFFERED_READ_SIZE. The caller should use the overlaod
  153. * of io::peek that allows to supply a bigger buffer.
  154. * TODO(hartem): It will be possible to fix this once SO_PEEK_OFF
  155. * (introduced in 3.4 kernels) becomes universally available.
  156. *
  157. * @param fd socket descriptor.
  158. * @param limit maximum number of bytes to peek.
  159. * @return Peeked bytes.
  160. * A failure will be returned if an error is detected.
  161. */
  162. Future<std::string> peek(int fd, size_t limit);
  163. } // namespace io {
  164. } // namespace process {
  165. #endif // __PROCESS_IO_HPP__