xref: /aoo42x/main/vos/source/pipe.cxx (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 #include <vos/pipe.hxx>
30 #include <vos/diagnose.hxx>
31 
32 using namespace vos;
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // Pipe
36 
37 
38 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OPipe, vos),
39 						VOS_NAMESPACE(OPipe, vos),
40 						VOS_NAMESPACE(OObject, vos), 0);
41 
42 /*****************************************************************************/
43 // OPipe()
44 /*****************************************************************************/
45 OPipe::OPipe()
46 {
47 	m_pPipeRef= 0;
48 }
49 
50 /*****************************************************************************/
51 // OPipe()
52 /*****************************************************************************/
53 
54 OPipe::OPipe( const rtl::OUString& strName, TPipeOption Options)
55 {
56 	m_pPipeRef =
57 		new PipeRef( osl_createPipe(strName.pData,
58 									(oslPipeOptions)Options,
59 									NULL) );
60 
61 	VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
62 	VOS_POSTCOND((*m_pPipeRef)(), "OPipe(): creation of pipe failed!\n");
63 }
64 
65 /*****************************************************************************/
66 // OPipe()
67 /*****************************************************************************/
68 
69 OPipe::OPipe( const rtl::OUString& strName,
70 			  TPipeOption Options,
71 			  const OSecurity& rSecurity)
72 {
73 	m_pPipeRef=
74 		new PipeRef(osl_createPipe(strName.pData,
75 								   (oslPipeOptions)Options,
76 								   (oslSecurity)rSecurity));
77 
78 	VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
79 	VOS_POSTCOND((*m_pPipeRef)(), "OPipe(): creation of pipe failed!\n");
80 }
81 
82 /*****************************************************************************/
83 // OPipe()
84 /*****************************************************************************/
85 OPipe::OPipe(const OPipe& pipe) :
86 OReference(), OObject()
87 {
88 
89 	VOS_ASSERT(pipe.m_pPipeRef != 0);
90 
91 	m_pPipeRef= pipe.m_pPipeRef;
92 
93 	m_pPipeRef->acquire();
94 }
95 
96 /*****************************************************************************/
97 // OPipe()
98 /*****************************************************************************/
99 OPipe::OPipe(oslPipe Pipe)
100 {
101 	m_pPipeRef = new PipeRef(Pipe);
102 }
103 
104 
105 /*****************************************************************************/
106 // ~OPipe()
107 /*****************************************************************************/
108 OPipe::~OPipe()
109 {
110 	close();
111 }
112 
113 /*****************************************************************************/
114 // create
115 /*****************************************************************************/
116 sal_Bool OPipe::create( const rtl::OUString& strName, TPipeOption Options )
117 {
118 	// if this was a valid pipe, decrease reference
119 	if ((m_pPipeRef) && (m_pPipeRef->release() == 0))
120 	{
121 		osl_releasePipe((*m_pPipeRef)());
122 		delete m_pPipeRef;
123 		m_pPipeRef= 0;
124 	}
125 
126 	m_pPipeRef=
127 		new PipeRef(osl_createPipe(strName.pData,
128 								   (oslPipeOptions)Options,
129 								   NULL));
130 
131 	VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
132 
133 	return (*m_pPipeRef)() != 0;
134 }
135 
136 /*****************************************************************************/
137 // create
138 /*****************************************************************************/
139 sal_Bool OPipe::create( const rtl::OUString& strName,
140 						TPipeOption Options,
141 						const vos::OSecurity& rSecurity )
142 {
143 	// if this was a valid pipe, decrease reference
144 	if ((m_pPipeRef) && (m_pPipeRef->release() == 0))
145 	{
146 		osl_releasePipe((*m_pPipeRef)());
147 		delete m_pPipeRef;
148 		m_pPipeRef= 0;
149 	}
150 
151 	m_pPipeRef=
152 		new PipeRef(osl_createPipe(strName.pData,
153 								   (oslPipeOptions)Options,
154 								   (oslSecurity)rSecurity));
155 
156 	VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
157 
158 	return (*m_pPipeRef)() != 0;
159 }
160 
161 /*****************************************************************************/
162 // operator=
163 /*****************************************************************************/
164 OPipe& OPipe::operator= (const OPipe& pipe)
165 {
166 	VOS_PRECOND(pipe.m_pPipeRef != 0, "OPipe::operator=: tried to assign an empty/invalid pipe\n");
167 
168 	if (m_pPipeRef == pipe.m_pPipeRef)
169 		return *this;
170 
171 	// if this was a valid pipe, decrease reference
172 	if ((m_pPipeRef) && (m_pPipeRef->release() == 0))
173 	{
174 		osl_releasePipe((*m_pPipeRef)());
175 		delete m_pPipeRef;
176 		m_pPipeRef= 0;
177 	}
178 
179 	m_pPipeRef= pipe.m_pPipeRef;
180 
181 	m_pPipeRef->acquire();
182 
183 	return *this;
184 }
185 
186 /*****************************************************************************/
187 // operator oslPipe()
188 /*****************************************************************************/
189 OPipe::operator oslPipe() const
190 {
191 	VOS_ASSERT(m_pPipeRef);
192 	return (*m_pPipeRef)();
193 }
194 
195 /*****************************************************************************/
196 // isValid()
197 /*****************************************************************************/
198 sal_Bool OPipe::isValid() const
199 {
200 	return m_pPipeRef != 0 && (*m_pPipeRef)() != 0;
201 }
202 
203 
204 /*****************************************************************************/
205 // close
206 /*****************************************************************************/
207 void OPipe::close()
208 {
209 	if (m_pPipeRef && (m_pPipeRef->release() == 0))
210 	{
211 		osl_releasePipe((*m_pPipeRef)());
212 		delete m_pPipeRef;
213 	}
214 	m_pPipeRef= 0;
215 }
216 
217 /*****************************************************************************/
218 // accept
219 /*****************************************************************************/
220 OPipe::TPipeError OPipe::accept(OStreamPipe& Connection)
221 {
222 	if ( isValid() )
223 	{
224 		Connection = osl_acceptPipe((*m_pPipeRef)());
225 
226 		if(Connection.isValid())
227 			return E_None;
228 	}
229 
230 	return getError();
231 }
232 
233 /*****************************************************************************/
234 // recv
235 /*****************************************************************************/
236 sal_Int32 OPipe::recv(void* pBuffer, sal_uInt32 BytesToRead)
237 {
238 	if ( isValid() )
239 		return osl_receivePipe((*m_pPipeRef)(),
240 					 		pBuffer,
241 							BytesToRead);
242 	else
243 		return -1;
244 
245 }
246 
247 /*****************************************************************************/
248 // send
249 /*****************************************************************************/
250 sal_Int32 OPipe::send(const void* pBuffer, sal_uInt32 BytesToSend)
251 {
252 	if ( isValid() )
253 		return osl_sendPipe((*m_pPipeRef)(),
254 							pBuffer,
255 							BytesToSend);
256 	else
257 		return -1;
258 }
259 
260 /*****************************************************************************/
261 // getError
262 /*****************************************************************************/
263 OPipe::TPipeError OPipe::getError() const
264 {
265 	if (m_pPipeRef)
266 		return (TPipeError)osl_getLastPipeError((*m_pPipeRef)());
267 	else
268 		return (TPipeError)osl_getLastPipeError(NULL);
269 }
270 
271 
272 
273 VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OStreamPipe, vos),
274 						VOS_NAMESPACE(OStreamPipe, vos),
275 						VOS_NAMESPACE(OPipe, vos), 0);
276 
277 
278 
279 /*****************************************************************************/
280 // OStreamPipe
281 /*****************************************************************************/
282 OStreamPipe::OStreamPipe()
283 {
284 }
285 
286 /*****************************************************************************/
287 // OStreamPipe
288 /*****************************************************************************/
289 OStreamPipe::OStreamPipe(oslPipe Pipe) :
290 	OPipe(Pipe)
291 {
292 }
293 
294 /*****************************************************************************/
295 // OStreamPipe
296 // copy constructor
297 /*****************************************************************************/
298 OStreamPipe::OStreamPipe(const OStreamPipe& pipe) :
299 OPipe(), IStream()
300 {
301 	VOS_ASSERT(pipe.m_pPipeRef != 0);
302 
303 	m_pPipeRef= pipe.m_pPipeRef;
304 
305 	m_pPipeRef->acquire();
306 }
307 
308 /*****************************************************************************/
309 // ~OStreamPipe
310 /*****************************************************************************/
311 OStreamPipe::~OStreamPipe()
312 {
313 }
314 
315 /*****************************************************************************/
316 // operator=(oslPipe)
317 /*****************************************************************************/
318 OStreamPipe& OStreamPipe::operator=(oslPipe Pipe)
319 {
320 
321 	// if this was a valid pipe, decrease reference
322 	if (m_pPipeRef && (m_pPipeRef->release() == 0))
323 	{
324 		osl_releasePipe((*m_pPipeRef)());
325 		delete m_pPipeRef;
326 		m_pPipeRef= 0;
327 	}
328 
329 	m_pPipeRef= new PipeRef(Pipe);
330 
331 	VOS_POSTCOND(m_pPipeRef != 0, "OPipe(): new failed.\n");
332 
333 	return *this;
334 }
335 
336 /*****************************************************************************/
337 // operator=OPipe
338 /*****************************************************************************/
339 
340 OStreamPipe& OStreamPipe::operator= (const OPipe& pipe)
341 {
342 	OPipe::operator= ( pipe );
343 	return *this;
344 }
345 
346 /*****************************************************************************/
347 // read
348 /*****************************************************************************/
349 sal_Int32 OStreamPipe::read(void* pBuffer, sal_uInt32 n) const
350 {
351 	VOS_ASSERT(m_pPipeRef && (*m_pPipeRef)());
352 
353 	/* loop until all desired bytes were read or an error occured */
354 	sal_Int32 BytesRead= 0;
355 	sal_Int32 BytesToRead= n;
356 	while (BytesToRead > 0)
357 	{
358 		sal_Int32 RetVal;
359 		RetVal= osl_receivePipe((*m_pPipeRef)(),
360 						 	    pBuffer,
361 								BytesToRead);
362 
363 		/* error occured? */
364 		if(RetVal <= 0)
365 		{
366 			break;
367 		}
368 
369 		BytesToRead -= RetVal;
370 		BytesRead += RetVal;
371 		pBuffer= (sal_Char*)pBuffer + RetVal;
372 	}
373 
374 	return BytesRead;
375 }
376 
377 /*****************************************************************************/
378 // write
379 /*****************************************************************************/
380 sal_Int32 OStreamPipe::write(const void* pBuffer, sal_uInt32 n)
381 {
382 	VOS_ASSERT(m_pPipeRef && (*m_pPipeRef)());
383 
384 	/* loop until all desired bytes were send or an error occured */
385 	sal_Int32 BytesSend= 0;
386 	sal_Int32 BytesToSend= n;
387 	while (BytesToSend > 0)
388 	{
389 		sal_Int32 RetVal;
390 
391 		RetVal= osl_sendPipe((*m_pPipeRef)(),
392 								pBuffer,
393 								BytesToSend);
394 
395 		/* error occured? */
396 		if(RetVal <= 0)
397 		{
398 			break;
399 		}
400 
401 		BytesToSend -= RetVal;
402 		BytesSend += RetVal;
403 		pBuffer= (sal_Char*)pBuffer + RetVal;
404 	}
405 
406 	return BytesSend;
407 }
408 
409 sal_Bool OStreamPipe::isEof() const
410 {
411 	return isValid();
412 }
413 
414 
415 
416 
417