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