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