UDK 3.2.7 C/C++ API Reference
osl/socket.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 #ifndef _OSL_SOCKET_HXX_
00029 #define _OSL_SOCKET_HXX_
00030 
00031 #include <osl/socket_decl.hxx>
00032 
00033 namespace osl
00034 {
00035     //______________________________________________________________________________
00036     inline SocketAddr::SocketAddr()
00037         : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
00038     {}
00039 
00040     //______________________________________________________________________________
00041     inline SocketAddr::SocketAddr(const SocketAddr& Addr)
00042         : m_handle( osl_copySocketAddr( Addr.m_handle ) )
00043     {
00044     }
00045 
00046     //______________________________________________________________________________
00047     inline SocketAddr::SocketAddr(oslSocketAddr Addr)
00048         : m_handle( osl_copySocketAddr( Addr ) )
00049     {
00050     }
00051 
00052     //______________________________________________________________________________
00053     inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
00054         : m_handle( Addr )
00055     {
00056     }
00057 
00058     //______________________________________________________________________________
00059     inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
00060         : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
00061     {
00062         if(! m_handle )
00063         {
00064             m_handle = osl_resolveHostname(strAddrOrHostName.pData);
00065 
00066             // host found?
00067             if(m_handle)
00068             {
00069                 osl_setInetPortOfSocketAddr(m_handle, nPort);
00070             }
00071             else
00072             {
00073                 osl_destroySocketAddr( m_handle );
00074                 m_handle = 0;
00075             }
00076         }
00077     }
00078 
00079     //______________________________________________________________________________
00080     inline SocketAddr::~SocketAddr()
00081     {
00082         if( m_handle )
00083             osl_destroySocketAddr( m_handle );
00084     }
00085 
00086     //______________________________________________________________________________
00087     inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
00088     {
00089         ::rtl::OUString hostname;
00090         oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
00091         if( pResult )
00092             *pResult = result;
00093         return hostname;
00094     }
00095 
00096     //______________________________________________________________________________
00097     inline sal_Int32 SAL_CALL SocketAddr::getPort() const
00098     {
00099         return osl_getInetPortOfSocketAddr(m_handle);
00100     }
00101 
00102     //______________________________________________________________________________
00103     inline sal_Bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
00104     {
00105         return osl_setInetPortOfSocketAddr(m_handle, nPort );
00106     }
00107 
00108     inline sal_Bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
00109     {
00110         *this = SocketAddr( sDottedIpOrHostname , getPort() );
00111         return is();
00112     }
00113 
00114     //______________________________________________________________________________
00115     inline sal_Bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
00116     {
00117         return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
00118             == osl_Socket_Ok;
00119     }
00120 
00121     inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
00122     {
00123         ::rtl::ByteSequence sequence;
00124         oslSocketResult result = osl_getAddrOfSocketAddr( m_handle,(sal_Sequence **) &sequence );
00125         if( pResult )
00126             *pResult = result;
00127         return sequence;
00128     }
00129 
00130     //______________________________________________________________________________
00131     inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
00132     {
00133         oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
00134         if( m_handle )
00135             osl_destroySocketAddr( m_handle );
00136         m_handle = pNewAddr;
00137         return *this;
00138     }
00139 
00140     //______________________________________________________________________________
00141     inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
00142     {
00143         *this = (Addr.getHandle());
00144         return *this;
00145     }
00146 
00147     inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
00148     {
00149         if( m_handle )
00150             osl_destroySocketAddr( m_handle );
00151         m_handle = Addr;
00152         return *this;
00153     }
00154 
00155     //______________________________________________________________________________
00156     inline sal_Bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
00157     {
00158         return osl_isEqualSocketAddr( m_handle, Addr );
00159     }
00160 
00161     inline oslSocketAddr SocketAddr::getHandle() const
00162     {
00163         return m_handle;
00164     }
00165 
00166     //______________________________________________________________________________
00167     inline sal_Bool SocketAddr::is() const
00168     {
00169         return m_handle != 0;
00170     }
00171 
00172     // (static method)______________________________________________________________
00173     inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
00174     {
00175         ::rtl::OUString hostname;
00176         oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
00177         if(pResult )
00178             *pResult = result;
00179         return hostname;
00180     }
00181 
00182     // (static method)______________________________________________________________
00183     inline void SAL_CALL SocketAddr::resolveHostname(
00184         const ::rtl::OUString & strHostName, SocketAddr &Addr)
00185     {
00186         Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
00187     }
00188 
00189     // (static method)______________________________________________________________
00190     inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
00191             const ::rtl::OUString& strServiceName,
00192             const ::rtl::OUString & strProtocolName )
00193     {
00194         return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
00195     }
00196 
00197     //______________________________________________________________________________
00198     inline Socket::Socket(oslSocketType Type,
00199                           oslAddrFamily Family,
00200                           oslProtocol   Protocol)
00201         : m_handle( osl_createSocket(Family, Type, Protocol) )
00202     {}
00203 
00204     //______________________________________________________________________________
00205     inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
00206         : m_handle( socketHandle )
00207     {}
00208 
00209     //______________________________________________________________________________
00210     inline Socket::Socket( oslSocket socketHandle )
00211         : m_handle( socketHandle )
00212     {
00213         osl_acquireSocket( m_handle );
00214     }
00215 
00216     //______________________________________________________________________________
00217     inline Socket::Socket( const Socket & socket )
00218         : m_handle( socket.getHandle() )
00219     {
00220         osl_acquireSocket( m_handle );
00221     }
00222 
00223     //______________________________________________________________________________
00224     inline Socket::~Socket()
00225     {
00226         osl_releaseSocket( m_handle );
00227     }
00228 
00229     //______________________________________________________________________________
00230     inline Socket& Socket::operator= ( oslSocket socketHandle)
00231     {
00232         osl_acquireSocket( socketHandle );
00233         osl_releaseSocket( m_handle );
00234         m_handle = socketHandle;
00235         return *this;
00236     }
00237 
00238     //______________________________________________________________________________
00239     inline Socket&  Socket::operator= (const Socket& sock)
00240     {
00241         return (*this) = sock.getHandle();
00242     }
00243 
00244     //______________________________________________________________________________
00245     inline sal_Bool Socket::operator==( const Socket& rSocket ) const
00246     {
00247         return m_handle == rSocket.getHandle();
00248     }
00249 
00250     //______________________________________________________________________________
00251     inline sal_Bool Socket::operator==( const oslSocket socketHandle ) const
00252     {
00253         return m_handle == socketHandle;
00254     }
00255 
00256     //______________________________________________________________________________
00257     inline void Socket::shutdown( oslSocketDirection Direction )
00258     {
00259         osl_shutdownSocket( m_handle , Direction );
00260     }
00261 
00262     //______________________________________________________________________________
00263     inline void Socket::close()
00264     {
00265         osl_closeSocket( m_handle );
00266     }
00267 
00268     //______________________________________________________________________________
00269     inline void Socket::getLocalAddr( SocketAddr & addr) const
00270     {
00271         addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
00272     }
00273 
00274     //______________________________________________________________________________
00275     inline sal_Int32 Socket::getLocalPort() const
00276     {
00277         SocketAddr addr( 0 );
00278         getLocalAddr( addr );
00279         return addr.getPort();
00280     }
00281 
00282     //______________________________________________________________________________
00283     inline ::rtl::OUString Socket::getLocalHost() const
00284     {
00285         SocketAddr addr( 0 );
00286         getLocalAddr( addr );
00287         return addr.getHostname();
00288     }
00289 
00290     //______________________________________________________________________________
00291     inline void Socket::getPeerAddr( SocketAddr &addr ) const
00292     {
00293         addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
00294     }
00295 
00296     //______________________________________________________________________________
00297     inline sal_Int32 Socket::getPeerPort() const
00298     {
00299         SocketAddr addr( 0 );
00300         getPeerAddr( addr );
00301         return addr.getPort();
00302     }
00303 
00304     //______________________________________________________________________________
00305     inline ::rtl::OUString Socket::getPeerHost() const
00306     {
00307         SocketAddr addr( 0 );
00308         getPeerAddr( addr );
00309         return addr.getHostname();
00310     }
00311 
00312     //______________________________________________________________________________
00313     inline sal_Bool Socket::bind(const SocketAddr& LocalInterface)
00314     {
00315         return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
00316     }
00317 
00318     //______________________________________________________________________________
00319     inline sal_Bool Socket::isRecvReady(const TimeValue *pTimeout ) const
00320     {
00321         return osl_isReceiveReady( m_handle , pTimeout );
00322     }
00323 
00324     //______________________________________________________________________________
00325     inline sal_Bool Socket::isSendReady(const TimeValue *pTimeout ) const
00326     {
00327         return osl_isSendReady( m_handle, pTimeout );
00328     }
00329 
00330     //______________________________________________________________________________
00331     inline sal_Bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
00332     {
00333         return osl_isExceptionPending( m_handle, pTimeout );
00334     }
00335 
00336     //______________________________________________________________________________
00337     inline oslSocketType Socket::getType() const
00338     {
00339         return osl_getSocketType( m_handle );
00340     }
00341 
00342     //______________________________________________________________________________
00343     inline sal_Int32  Socket::getOption(
00344         oslSocketOption Option,
00345         void* pBuffer,
00346         sal_uInt32 BufferLen,
00347         oslSocketOptionLevel Level) const
00348     {
00349         return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
00350     }
00351 
00352     //______________________________________________________________________________
00353     inline sal_Bool Socket::setOption(  oslSocketOption Option,
00354                                         void* pBuffer,
00355                                         sal_uInt32 BufferLen,
00356                                         oslSocketOptionLevel Level ) const
00357     {
00358         return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
00359     }
00360 
00361     //______________________________________________________________________________
00362     inline sal_Bool Socket::setOption( oslSocketOption option, sal_Int32 nValue  )
00363     {
00364         return setOption( option, &nValue, sizeof( nValue ) );
00365     }
00366 
00367     //______________________________________________________________________________
00368     inline sal_Int32 Socket::getOption( oslSocketOption option ) const
00369     {
00370         sal_Int32 n;
00371         getOption( option, &n, sizeof( n ) );
00372         return n;
00373     }
00374 
00375     //______________________________________________________________________________
00376     inline sal_Bool Socket::enableNonBlockingMode( sal_Bool bNonBlockingMode)
00377     {
00378         return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
00379     }
00380 
00381     //______________________________________________________________________________
00382     inline sal_Bool Socket::isNonBlockingMode() const
00383     {
00384         return osl_isNonBlockingMode( m_handle );
00385     }
00386 
00387     //______________________________________________________________________________
00388     inline void SAL_CALL Socket::clearError() const
00389     {
00390         sal_Int32 err = 0;
00391         getOption(osl_Socket_OptionError, &err, sizeof(err));
00392     }
00393 
00394     //______________________________________________________________________________
00395     inline oslSocketError Socket::getError() const
00396     {
00397         return osl_getLastSocketError( m_handle );
00398     }
00399 
00400     //______________________________________________________________________________
00401     inline ::rtl::OUString Socket::getErrorAsString( ) const
00402     {
00403         ::rtl::OUString error;
00404         osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
00405         return error;
00406     }
00407 
00408     //______________________________________________________________________________
00409     inline oslSocket Socket::getHandle() const
00410     {
00411         return m_handle;
00412     }
00413 
00414     //______________________________________________________________________________
00415     inline StreamSocket::StreamSocket(oslAddrFamily Family,
00416                                       oslProtocol Protocol,
00417                                       oslSocketType Type )
00418         : Socket( Type, Family, Protocol )
00419     {}
00420 
00421     //______________________________________________________________________________
00422     inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
00423         : Socket( socketHandle, noacquire )
00424     {}
00425 
00426     //______________________________________________________________________________
00427     inline StreamSocket::StreamSocket( oslSocket socketHandle )
00428         : Socket( socketHandle )
00429     {}
00430 
00431     //______________________________________________________________________________
00432     inline StreamSocket::StreamSocket( const StreamSocket & socket )
00433         : Socket( socket )
00434     {}
00435 
00436     //______________________________________________________________________________
00437     inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
00438     {
00439         return osl_readSocket( m_handle, pBuffer, n );
00440     }
00441 
00442     //______________________________________________________________________________
00443     inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
00444     {
00445         return osl_writeSocket( m_handle, pBuffer, n );
00446     }
00447 
00448 
00449     //______________________________________________________________________________
00450     inline sal_Int32 StreamSocket::recv(void* pBuffer,
00451                                         sal_uInt32 BytesToRead,
00452                                         oslSocketMsgFlag Flag)
00453     {
00454         return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
00455     }
00456 
00457     //______________________________________________________________________________
00458     inline sal_Int32 StreamSocket::send(const void* pBuffer,
00459                                         sal_uInt32 BytesToSend,
00460                                         oslSocketMsgFlag Flag)
00461     {
00462         return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
00463     }
00464 
00465     //______________________________________________________________________________
00466       inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
00467                                             oslProtocol Protocol,
00468                                             oslSocketType   Type)
00469         : StreamSocket( Family, Protocol ,Type )
00470     {}
00471 
00472     //______________________________________________________________________________
00473     inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
00474                                                      const TimeValue* pTimeout )
00475     {
00476         return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
00477     }
00478 
00479     //______________________________________________________________________________
00480     inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
00481                                           oslProtocol   Protocol ,
00482                                           oslSocketType Type )
00483         : Socket( Type, Family, Protocol )
00484     {}
00485 
00486     //______________________________________________________________________________
00487     inline sal_Bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
00488     {
00489         return osl_listenOnSocket( m_handle, MaxPendingConnections );
00490     }
00491 
00492     //______________________________________________________________________________
00493     inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
00494     {
00495         oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 );
00496         oslSocketResult status = osl_Socket_Ok;
00497         if( o )
00498         {
00499             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
00500         }
00501         else
00502         {
00503             Connection = StreamSocket();
00504             status = osl_Socket_Error;
00505         }
00506         return status;
00507     }
00508 
00509     //______________________________________________________________________________
00510     inline oslSocketResult AcceptorSocket::acceptConnection(
00511         StreamSocket&   Connection, SocketAddr & PeerAddr)
00512     {
00513         // TODO change in/OUT parameter
00514         oslSocket o = osl_acceptConnectionOnSocket( m_handle, (oslSocketAddr *)&PeerAddr );
00515         oslSocketResult status = osl_Socket_Ok;
00516         if( o )
00517         {
00518             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
00519         }
00520         else
00521         {
00522             Connection = StreamSocket();
00523             status = osl_Socket_Error;
00524         }
00525         return status;
00526     }
00527 
00528     //______________________________________________________________________________
00529     inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
00530                                           oslProtocol   Protocol,
00531                                           oslSocketType Type)
00532         : Socket( Type, Family, Protocol )
00533     {}
00534 
00535     //______________________________________________________________________________
00536     inline sal_Int32 DatagramSocket::recvFrom(void*  pBuffer,
00537                                               sal_uInt32 BufferSize,
00538                                               SocketAddr* pSenderAddr,
00539                                               oslSocketMsgFlag Flag )
00540     {
00541         sal_Int32 nByteRead;
00542         if( pSenderAddr )
00543         {
00544             // TODO : correct the out-parameter pSenderAddr outparameter
00545               nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
00546                                                  BufferSize, Flag);
00547 //              nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
00548 //                                                 BufferSize, Flag);
00549         }
00550         else
00551         {
00552             nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize ,  Flag );
00553         }
00554         return nByteRead;
00555     }
00556 
00557     //______________________________________________________________________________
00558     inline sal_Int32  DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
00559                                               const void* pBuffer,
00560                                               sal_uInt32 BufferSize,
00561                                               oslSocketMsgFlag Flag )
00562     {
00563         return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
00564     }
00565 }
00566 #endif
00567 
00568 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines