1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: pipe.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <vos/pipe.hxx>
33 #include <vos/diagnose.hxx>
37 ///////////////////////////////////////////////////////////////////////////////
41 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OPipe
, vos
),
42 VOS_NAMESPACE(OPipe
, vos
),
43 VOS_NAMESPACE(OObject
, vos
), 0);
45 /*****************************************************************************/
47 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
57 OPipe::OPipe( const rtl::OUString
& strName
, TPipeOption Options
)
60 new PipeRef( osl_createPipe(strName
.pData
,
61 (oslPipeOptions
)Options
,
64 VOS_POSTCOND(m_pPipeRef
!= 0, "OPipe(): new failed.\n");
65 VOS_POSTCOND((*m_pPipeRef
)(), "OPipe(): creation of pipe failed!\n");
68 /*****************************************************************************/
70 /*****************************************************************************/
72 OPipe::OPipe( const rtl::OUString
& strName
,
74 const OSecurity
& rSecurity
)
77 new PipeRef(osl_createPipe(strName
.pData
,
78 (oslPipeOptions
)Options
,
79 (oslSecurity
)rSecurity
));
81 VOS_POSTCOND(m_pPipeRef
!= 0, "OPipe(): new failed.\n");
82 VOS_POSTCOND((*m_pPipeRef
)(), "OPipe(): creation of pipe failed!\n");
85 /*****************************************************************************/
87 /*****************************************************************************/
88 OPipe::OPipe(const OPipe
& pipe
) :
89 OReference(), OObject()
92 VOS_ASSERT(pipe
.m_pPipeRef
!= 0);
94 m_pPipeRef
= pipe
.m_pPipeRef
;
96 m_pPipeRef
->acquire();
99 /*****************************************************************************/
101 /*****************************************************************************/
102 OPipe::OPipe(oslPipe Pipe
)
104 m_pPipeRef
= new PipeRef(Pipe
);
108 /*****************************************************************************/
110 /*****************************************************************************/
116 /*****************************************************************************/
118 /*****************************************************************************/
119 sal_Bool
OPipe::create( const rtl::OUString
& strName
, TPipeOption Options
)
121 // if this was a valid pipe, decrease reference
122 if ((m_pPipeRef
) && (m_pPipeRef
->release() == 0))
124 osl_releasePipe((*m_pPipeRef
)());
130 new PipeRef(osl_createPipe(strName
.pData
,
131 (oslPipeOptions
)Options
,
134 VOS_POSTCOND(m_pPipeRef
!= 0, "OPipe(): new failed.\n");
136 return (*m_pPipeRef
)() != 0;
139 /*****************************************************************************/
141 /*****************************************************************************/
142 sal_Bool
OPipe::create( const rtl::OUString
& strName
,
144 const NAMESPACE_VOS(OSecurity
)& rSecurity
)
146 // if this was a valid pipe, decrease reference
147 if ((m_pPipeRef
) && (m_pPipeRef
->release() == 0))
149 osl_releasePipe((*m_pPipeRef
)());
155 new PipeRef(osl_createPipe(strName
.pData
,
156 (oslPipeOptions
)Options
,
157 (oslSecurity
)rSecurity
));
159 VOS_POSTCOND(m_pPipeRef
!= 0, "OPipe(): new failed.\n");
161 return (*m_pPipeRef
)() != 0;
164 /*****************************************************************************/
166 /*****************************************************************************/
167 OPipe
& OPipe::operator= (const OPipe
& pipe
)
169 VOS_PRECOND(pipe
.m_pPipeRef
!= 0, "OPipe::operator=: tried to assign an empty/invalid pipe\n");
171 if (m_pPipeRef
== pipe
.m_pPipeRef
)
174 // if this was a valid pipe, decrease reference
175 if ((m_pPipeRef
) && (m_pPipeRef
->release() == 0))
177 osl_releasePipe((*m_pPipeRef
)());
182 m_pPipeRef
= pipe
.m_pPipeRef
;
184 m_pPipeRef
->acquire();
189 /*****************************************************************************/
190 // operator oslPipe()
191 /*****************************************************************************/
192 OPipe::operator oslPipe() const
194 VOS_ASSERT(m_pPipeRef
);
195 return (*m_pPipeRef
)();
198 /*****************************************************************************/
200 /*****************************************************************************/
201 sal_Bool
OPipe::isValid() const
203 return m_pPipeRef
!= 0 && (*m_pPipeRef
)() != 0;
207 /*****************************************************************************/
209 /*****************************************************************************/
212 if (m_pPipeRef
&& (m_pPipeRef
->release() == 0))
214 osl_releasePipe((*m_pPipeRef
)());
220 /*****************************************************************************/
222 /*****************************************************************************/
223 OPipe::TPipeError
OPipe::accept(OStreamPipe
& Connection
)
227 Connection
= osl_acceptPipe((*m_pPipeRef
)());
229 if(Connection
.isValid())
236 /*****************************************************************************/
238 /*****************************************************************************/
239 sal_Int32
OPipe::recv(void* pBuffer
, sal_uInt32 BytesToRead
)
242 return osl_receivePipe((*m_pPipeRef
)(),
250 /*****************************************************************************/
252 /*****************************************************************************/
253 sal_Int32
OPipe::send(const void* pBuffer
, sal_uInt32 BytesToSend
)
256 return osl_sendPipe((*m_pPipeRef
)(),
263 /*****************************************************************************/
265 /*****************************************************************************/
266 OPipe::TPipeError
OPipe::getError() const
269 return (TPipeError
)osl_getLastPipeError((*m_pPipeRef
)());
271 return (TPipeError
)osl_getLastPipeError(NULL
);
276 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OStreamPipe
, vos
),
277 VOS_NAMESPACE(OStreamPipe
, vos
),
278 VOS_NAMESPACE(OPipe
, vos
), 0);
282 /*****************************************************************************/
284 /*****************************************************************************/
285 OStreamPipe::OStreamPipe()
289 /*****************************************************************************/
291 /*****************************************************************************/
292 OStreamPipe::OStreamPipe(oslPipe Pipe
) :
297 /*****************************************************************************/
300 /*****************************************************************************/
301 OStreamPipe::OStreamPipe(const OStreamPipe
& pipe
) :
304 VOS_ASSERT(pipe
.m_pPipeRef
!= 0);
306 m_pPipeRef
= pipe
.m_pPipeRef
;
308 m_pPipeRef
->acquire();
311 /*****************************************************************************/
313 /*****************************************************************************/
314 OStreamPipe::~OStreamPipe()
318 /*****************************************************************************/
319 // operator=(oslPipe)
320 /*****************************************************************************/
321 OStreamPipe
& OStreamPipe::operator=(oslPipe Pipe
)
324 // if this was a valid pipe, decrease reference
325 if (m_pPipeRef
&& (m_pPipeRef
->release() == 0))
327 osl_releasePipe((*m_pPipeRef
)());
332 m_pPipeRef
= new PipeRef(Pipe
);
334 VOS_POSTCOND(m_pPipeRef
!= 0, "OPipe(): new failed.\n");
339 /*****************************************************************************/
341 /*****************************************************************************/
343 OStreamPipe
& OStreamPipe::operator= (const OPipe
& pipe
)
345 OPipe::operator= ( pipe
);
349 /*****************************************************************************/
351 /*****************************************************************************/
352 sal_Int32
OStreamPipe::read(void* pBuffer
, sal_uInt32 n
) const
354 VOS_ASSERT(m_pPipeRef
&& (*m_pPipeRef
)());
356 /* loop until all desired bytes were read or an error occured */
357 sal_Int32 BytesRead
= 0;
358 sal_Int32 BytesToRead
= n
;
359 while (BytesToRead
> 0)
362 RetVal
= osl_receivePipe((*m_pPipeRef
)(),
372 BytesToRead
-= RetVal
;
374 pBuffer
= (sal_Char
*)pBuffer
+ RetVal
;
380 /*****************************************************************************/
382 /*****************************************************************************/
383 sal_Int32
OStreamPipe::write(const void* pBuffer
, sal_uInt32 n
)
385 VOS_ASSERT(m_pPipeRef
&& (*m_pPipeRef
)());
387 /* loop until all desired bytes were send or an error occured */
388 sal_Int32 BytesSend
= 0;
389 sal_Int32 BytesToSend
= n
;
390 while (BytesToSend
> 0)
394 RetVal
= osl_sendPipe((*m_pPipeRef
)(),
404 BytesToSend
-= RetVal
;
406 pBuffer
= (sal_Char
*)pBuffer
+ RetVal
;
412 sal_Bool
OStreamPipe::isEof() const