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