1 | // |
---|
2 | // Pipe.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Pipe_INCLUDED |
---|
40 | #define Foundation_Pipe_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/PipeImpl.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | class Foundation_API Pipe |
---|
51 | /// This class implements an anonymous pipe. |
---|
52 | /// |
---|
53 | /// Pipes are a common method of inter-process communication - |
---|
54 | /// on Unix, pipes are the oldest form of IPC. |
---|
55 | /// |
---|
56 | /// A pipe is a half-duplex communication channel, which means |
---|
57 | /// that data only flows in one direction. |
---|
58 | /// Pipes have a read-end and a write-end. One process writes to |
---|
59 | /// the pipe and another process reads the data written by |
---|
60 | /// its peer. |
---|
61 | /// Read and write operations are always synchronous. A read will |
---|
62 | /// block until data is available and a write will block until |
---|
63 | /// the reader reads the data. |
---|
64 | /// |
---|
65 | /// The sendBytes() and readBytes() methods of Pipe are usually |
---|
66 | /// used through a PipeOutputStream or PipeInputStream and are |
---|
67 | /// not called directly. |
---|
68 | /// |
---|
69 | /// Pipe objects have value semantics; the actual work is delegated |
---|
70 | /// to a reference-counted PipeImpl object. |
---|
71 | { |
---|
72 | public: |
---|
73 | typedef PipeImpl::Handle Handle; /// The read/write handle or file descriptor. |
---|
74 | |
---|
75 | enum CloseMode /// used by close() |
---|
76 | { |
---|
77 | CLOSE_READ = 0x01, /// Close reading end of pipe. |
---|
78 | CLOSE_WRITE = 0x02, /// Close writing end of pipe. |
---|
79 | CLOSE_BOTH = 0x03 /// Close both ends of pipe. |
---|
80 | }; |
---|
81 | |
---|
82 | Pipe(); |
---|
83 | /// Creates the Pipe. |
---|
84 | /// |
---|
85 | /// Throws a CreateFileException if the pipe cannot be |
---|
86 | /// created. |
---|
87 | |
---|
88 | Pipe(const Pipe& pipe); |
---|
89 | /// Creates the Pipe using the PipeImpl from another one. |
---|
90 | |
---|
91 | ~Pipe(); |
---|
92 | /// Closes and destroys the Pipe. |
---|
93 | |
---|
94 | Pipe& operator = (const Pipe& pipe); |
---|
95 | /// Releases the Pipe's PipeImpl and assigns another one. |
---|
96 | |
---|
97 | int writeBytes(const void* buffer, int length); |
---|
98 | /// Sends the contents of the given buffer through |
---|
99 | /// the pipe. Blocks until the receiver is ready |
---|
100 | /// to read the data. |
---|
101 | /// |
---|
102 | /// Returns the number of bytes sent. |
---|
103 | /// |
---|
104 | /// Throws a WriteFileException if the data cannot be written. |
---|
105 | |
---|
106 | int readBytes(void* buffer, int length); |
---|
107 | /// Receives data from the pipe and stores it |
---|
108 | /// in buffer. Up to length bytes are received. |
---|
109 | /// Blocks until data becomes available. |
---|
110 | /// |
---|
111 | /// Returns the number of bytes received, or 0 |
---|
112 | /// if the pipe has been closed. |
---|
113 | /// |
---|
114 | /// Throws a ReadFileException if nothing can be read. |
---|
115 | |
---|
116 | Handle readHandle() const; |
---|
117 | /// Returns the read handle or file descriptor |
---|
118 | /// for the Pipe. For internal use only. |
---|
119 | |
---|
120 | Handle writeHandle() const; |
---|
121 | /// Returns the write handle or file descriptor |
---|
122 | /// for the Pipe. For internal use only. |
---|
123 | |
---|
124 | void close(CloseMode mode = CLOSE_BOTH); |
---|
125 | /// Depending on the argument, closes either the |
---|
126 | /// reading end, the writing end, or both ends |
---|
127 | /// of the Pipe. |
---|
128 | |
---|
129 | private: |
---|
130 | PipeImpl* _pImpl; |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | // |
---|
135 | // inlines |
---|
136 | // |
---|
137 | inline int Pipe::writeBytes(const void* buffer, int length) |
---|
138 | { |
---|
139 | return _pImpl->writeBytes(buffer, length); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | inline int Pipe::readBytes(void* buffer, int length) |
---|
144 | { |
---|
145 | return _pImpl->readBytes(buffer, length); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | inline Pipe::Handle Pipe::readHandle() const |
---|
150 | { |
---|
151 | return _pImpl->readHandle(); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | inline Pipe::Handle Pipe::writeHandle() const |
---|
156 | { |
---|
157 | return _pImpl->writeHandle(); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | } // namespace Poco |
---|
162 | |
---|
163 | |
---|
164 | #endif // Foundation_Pipe_INCLUDED |
---|