Pipe.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Pipe.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Pipe.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: Pipe
  9. //
  10. // Definition of the Pipe class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Pipe_INCLUDED
  18. #define Foundation_Pipe_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/PipeImpl.h"
  21. namespace Poco {
  22. class Foundation_API Pipe
  23. /// This class implements an anonymous pipe.
  24. ///
  25. /// Pipes are a common method of inter-process communication -
  26. /// on Unix, pipes are the oldest form of IPC.
  27. ///
  28. /// A pipe is a half-duplex communication channel, which means
  29. /// that data only flows in one direction.
  30. /// Pipes have a read-end and a write-end. One process writes to
  31. /// the pipe and another process reads the data written by
  32. /// its peer.
  33. /// Read and write operations are always synchronous. A read will
  34. /// block until data is available and a write will block until
  35. /// the reader reads the data.
  36. ///
  37. /// The sendBytes() and readBytes() methods of Pipe are usually
  38. /// used through a PipeOutputStream or PipeInputStream and are
  39. /// not called directly.
  40. ///
  41. /// Pipe objects have value semantics; the actual work is delegated
  42. /// to a reference-counted PipeImpl object.
  43. {
  44. public:
  45. typedef PipeImpl::Handle Handle; /// The read/write handle or file descriptor.
  46. enum CloseMode /// used by close()
  47. {
  48. CLOSE_READ = 0x01, /// Close reading end of pipe.
  49. CLOSE_WRITE = 0x02, /// Close writing end of pipe.
  50. CLOSE_BOTH = 0x03 /// Close both ends of pipe.
  51. };
  52. Pipe();
  53. /// Creates the Pipe.
  54. ///
  55. /// Throws a CreateFileException if the pipe cannot be
  56. /// created.
  57. Pipe(const Pipe& pipe);
  58. /// Creates the Pipe using the PipeImpl from another one.
  59. ~Pipe();
  60. /// Closes and destroys the Pipe.
  61. Pipe& operator = (const Pipe& pipe);
  62. /// Releases the Pipe's PipeImpl and assigns another one.
  63. int writeBytes(const void* buffer, int length);
  64. /// Sends the contents of the given buffer through
  65. /// the pipe. Blocks until the receiver is ready
  66. /// to read the data.
  67. ///
  68. /// Returns the number of bytes sent.
  69. ///
  70. /// Throws a WriteFileException if the data cannot be written.
  71. int readBytes(void* buffer, int length);
  72. /// Receives data from the pipe and stores it
  73. /// in buffer. Up to length bytes are received.
  74. /// Blocks until data becomes available.
  75. ///
  76. /// Returns the number of bytes received, or 0
  77. /// if the pipe has been closed.
  78. ///
  79. /// Throws a ReadFileException if nothing can be read.
  80. Handle readHandle() const;
  81. /// Returns the read handle or file descriptor
  82. /// for the Pipe. For internal use only.
  83. Handle writeHandle() const;
  84. /// Returns the write handle or file descriptor
  85. /// for the Pipe. For internal use only.
  86. void close(CloseMode mode = CLOSE_BOTH);
  87. /// Depending on the argument, closes either the
  88. /// reading end, the writing end, or both ends
  89. /// of the Pipe.
  90. private:
  91. PipeImpl* _pImpl;
  92. };
  93. //
  94. // inlines
  95. //
  96. inline int Pipe::writeBytes(const void* buffer, int length)
  97. {
  98. return _pImpl->writeBytes(buffer, length);
  99. }
  100. inline int Pipe::readBytes(void* buffer, int length)
  101. {
  102. return _pImpl->readBytes(buffer, length);
  103. }
  104. inline Pipe::Handle Pipe::readHandle() const
  105. {
  106. return _pImpl->readHandle();
  107. }
  108. inline Pipe::Handle Pipe::writeHandle() const
  109. {
  110. return _pImpl->writeHandle();
  111. }
  112. } // namespace Poco
  113. #endif // Foundation_Pipe_INCLUDED