xref: /trunk/main/vos/inc/vos/pipe.hxx (revision d98e0520) !
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #ifndef _VOS_PIPE_HXX_
26 #define _VOS_PIPE_HXX_
27 
28 #   include <osl/pipe.h>
29 #   include <vos/types.hxx>
30 #   include <vos/object.hxx>
31 #   include <vos/istream.hxx>
32 #ifndef _VOS_REFERMCE_HXX_
33 #   include <vos/refernce.hxx>
34 #endif
35 #   include <vos/refobj.hxx>
36 #   include <vos/security.hxx>
37 #   include <rtl/ustring.hxx>
38 
39 
40 namespace vos
41 {
42 
43 class OStreamPipe;
44 
45 /** Represents a pipe.
46 */
47 class VOS_DLLPUBLIC OPipe : public vos::OReference,
48               public vos::OObject
49 {
50     VOS_DECLARE_CLASSINFO(vos::OPipe);
51 
52 public:
53     /*
54         Represents pipe-options
55     */
56     enum TPipeOption {
57         TOption_Open   = osl_Pipe_OPEN,         /* open existing pipe */
58         TOption_Create = osl_Pipe_CREATE        /* create pipe and open it, */
59                                                 /*  fails if already existst */
60     };
61 
62     enum TPipeError {
63         E_None              = osl_Pipe_E_None,              /* no error */
64         E_NotFound          = osl_Pipe_E_NotFound,          /* Pipe could not be found */
65         E_AlreadyExists     = osl_Pipe_E_AlreadyExists,     /* Pipe already exists */
66         E_NoProtocol        = osl_Pipe_E_NoProtocol,        /* Protocol not available */
67         E_NetworkReset      = osl_Pipe_E_NetworkReset,      /* Network dropped connection because of reset */
68         E_ConnectionAbort   = osl_Pipe_E_ConnectionAbort,   /* Software caused connection abort */
69         E_ConnectionReset   = osl_Pipe_E_ConnectionReset,   /* Connection reset by peer */
70         E_NoBufferSpace     = osl_Pipe_E_NoBufferSpace,     /* No buffer space available */
71         E_TimedOut          = osl_Pipe_E_TimedOut,          /* Connection timed out */
72         E_ConnectionRefused = osl_Pipe_E_ConnectionRefused, /* Connection refused */
73         E_invalidError      = osl_Pipe_E_invalidError       /* unmapped error */
74     };
75 
76 protected:
77     typedef ORefObj<oslPipe> PipeRef;
78 
79     PipeRef* m_pPipeRef;
80 
81 public:
82 
83     /** Does not create a pipe. Use assignment operator to
84         make this a useable pipe.
85     */
86     OPipe();
87 
88     /** Creates a pipe.
89         @param strName
90         @param Options
91     */
92     OPipe(const ::rtl::OUString& strName, TPipeOption Options = TOption_Open);
93 
94     /** Creates a pipe.
95         @param strName
96         @param Options
97         @param Security
98     */
99     OPipe(const ::rtl::OUString& strName, TPipeOption Options,
100           const vos::OSecurity& rSecurity);
101 
102     /** Copy constructor.
103     */
104     OPipe(const OPipe& pipe);
105 
106     /** Creates pipe as wrapper around the underlying oslPipe.
107         @param Pipe
108     */
109     OPipe(oslPipe Pipe);
110 
111     /** Destructor. Destroys the underlying oslPipe.
112     */
113     virtual ~OPipe();
114 
115     /** Create a pipe with the given attributes.
116         If socket was already created, the old one will be discarded.
117         @param strName
118         @param Options
119         @return True if socket was successfully created.
120     */
121     sal_Bool SAL_CALL create(const ::rtl::OUString& strName, TPipeOption Options = TOption_Open);
122 
123     /** Create a pipe with the given attributes.
124         If socket was already created, the old one will be discarded.
125         @param strName
126         @param Options
127         @param Security
128         @return True if socket was successfully created.
129     */
130     sal_Bool SAL_CALL create(const ::rtl::OUString& strName, TPipeOption Options,
131                    const vos::OSecurity& rSecurity);
132 
133     /** Assignment operator. If pipe was already created, the old one will
134         be discarded.
135     */
136     OPipe& SAL_CALL operator= (const OPipe& pipe);
137 
138     /** Allow cast to underlying oslPipe.
139     */
140     SAL_CALL operator oslPipe() const;
141 
142     /** Checks if the pipe is valid.
143         @return True if the object represents a valid pipe.
144     */
145     sal_Bool SAL_CALL isValid() const;
146 
operator ==(const OPipe & rPipe)147     sal_Bool SAL_CALL operator==( const OPipe& rPipe )
148     {
149         return m_pPipeRef == rPipe.m_pPipeRef;
150     }
151 
152     /** Closes the pipe.
153     */
154     virtual void SAL_CALL close();
155 
156     /** Accept connection on an existing pipe
157     */
158     TPipeError SAL_CALL accept(OStreamPipe& Connection);
159 
160     /** Tries to receives BytesToRead data from the connected pipe,
161 
162         @param pBuffer [out] Points to a buffer that will be filled with the received
163         data.
164         @param BytesToRead [in] The number of bytes to read. pBuffer must have at least
165         this size.
166         @return the number of received bytes.
167     */
168     sal_Int32   SAL_CALL recv(void* pBuffer, sal_uInt32 BytesToRead);
169 
170     /** Tries to sends BytesToSend data from the connected pipe.
171 
172         @param pBuffer [in] Points to a buffer that contains the send-data.
173         @param BytesToSend [in] The number of bytes to send. pBuffer must have at least
174         this size.
175         @return the number of transferred bytes.
176     */
177     sal_Int32 SAL_CALL send(const void* pBuffer, sal_uInt32 BytesToSend);
178 
179     /** Delivers a constant describing the last error for the pipe system.
180         @return ENONE if no error occurred, invalid_PipeError if
181         an unknown (unmapped) error occurred, otherwise an enum describing the
182         error.
183     */
184     TPipeError SAL_CALL getError() const;
185 
186 };
187 
188 /** A pipe to send or receive a stream of data.
189 */
190 class VOS_DLLPUBLIC OStreamPipe : public vos::OPipe,
191                     public vos::IStream
192 {
193     VOS_DECLARE_CLASSINFO(vos::OStreamPipe);
194 public:
195 
196     /** Creates an unattached pipe. You must attach the pipe to an oslPipe
197         e.g. by using the operator=(oslPipe), before you can use the stream-
198         functionality of the object.
199     */
200     OStreamPipe();
201 
202     /** Creates pipe as wrapper around the underlying oslPipe.
203         @param Pipe
204     */
205     OStreamPipe(oslPipe Pipe);
206 
207     /** Copy constructor.
208         @param Pipe
209     */
210     OStreamPipe(const OStreamPipe& Pipe);
211 
212     /** Destructor. Calls shutdown(readwrite) and close().
213     */
214     virtual ~OStreamPipe();
215 
216     /** Attaches the oslPipe to this object. If the object
217         already was attached to an oslPipe, the old one will
218         be closed and destroyed.
219         @param Pipe.
220     */
221     OStreamPipe& SAL_CALL operator=(oslPipe Pipe);
222 
223     /** Assignment operator
224     */
225     OStreamPipe& SAL_CALL operator=(const OPipe& pipe);
226 
227     /** Retrieves n bytes from the stream and copies them into pBuffer.
228         The method avoids incomplete reads due to packet boundaries.
229         @param pBuffer receives the read data.
230         @param n the number of bytes to read. pBuffer must be large enough
231         to hold the n bytes!
232         @return the number of read bytes. The number will only be smaller than
233         n if an exceptional condition (e.g. connection closed) occurs.
234     */
235     virtual sal_Int32 SAL_CALL read(void* pBuffer, sal_uInt32 n) const;
236 
237     /** Writes n bytes from pBuffer to the stream. The method avoids
238         incomplete writes due to packet boundaries.
239         @param pBuffer contains the data to be written.
240         @param n the number of bytes to write.
241         @return the number of written bytes. The number will only be smaller than
242         n if an exceptional condition (e.g. connection closed) occurs.
243     */
244     virtual sal_Int32 SAL_CALL write(const void* pBuffer, sal_uInt32 n);
245 
246     /** Checks if pipe is closed.
247         @return True if pipe is closed.
248     */
249     virtual sal_Bool SAL_CALL isEof() const;
250 };
251 
252 }
253 
254 #endif // _VOS_PIPE_HXX_
255 
256