LCOV - code coverage report
Current view: top level - include/osl - socket.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 5 157 3.2 %
Date: 2015-06-13 12:38:46 Functions: 1 47 2.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : #ifndef INCLUDED_OSL_SOCKET_HXX
      20             : #define INCLUDED_OSL_SOCKET_HXX
      21             : 
      22             : #include <osl/socket_decl.hxx>
      23             : 
      24             : namespace osl
      25             : {
      26             : 
      27           0 :     inline SocketAddr::SocketAddr()
      28           0 :         : m_handle( osl_createEmptySocketAddr( osl_Socket_FamilyInet ) )
      29           0 :     {}
      30             : 
      31             : 
      32             :     inline SocketAddr::SocketAddr(const SocketAddr& Addr)
      33             :         : m_handle( osl_copySocketAddr( Addr.m_handle ) )
      34             :     {
      35             :     }
      36             : 
      37             : 
      38           0 :     inline SocketAddr::SocketAddr(oslSocketAddr Addr)
      39           0 :         : m_handle( osl_copySocketAddr( Addr ) )
      40             :     {
      41           0 :     }
      42             : 
      43             : 
      44           0 :     inline SocketAddr::SocketAddr(oslSocketAddr Addr, __osl_socket_NoCopy )
      45           0 :         : m_handle( Addr )
      46             :     {
      47           0 :     }
      48             : 
      49             : 
      50           0 :     inline SocketAddr::SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort)
      51           0 :         : m_handle( osl_createInetSocketAddr( strAddrOrHostName.pData, nPort ) )
      52             :     {
      53           0 :         if(! m_handle )
      54             :         {
      55           0 :             m_handle = osl_resolveHostname(strAddrOrHostName.pData);
      56             : 
      57             :             // host found?
      58           0 :             if(m_handle)
      59             :             {
      60           0 :                 osl_setInetPortOfSocketAddr(m_handle, nPort);
      61             :             }
      62             :             else
      63             :             {
      64           0 :                 osl_destroySocketAddr( m_handle );
      65           0 :                 m_handle = 0;
      66             :             }
      67             :         }
      68           0 :     }
      69             : 
      70             : 
      71           0 :     inline SocketAddr::~SocketAddr()
      72             :     {
      73           0 :         if( m_handle )
      74           0 :             osl_destroySocketAddr( m_handle );
      75           0 :     }
      76             : 
      77             : 
      78           0 :     inline ::rtl::OUString SocketAddr::getHostname( oslSocketResult *pResult ) const
      79             :     {
      80           0 :         ::rtl::OUString hostname;
      81           0 :         oslSocketResult result = osl_getHostnameOfSocketAddr( m_handle, &(hostname.pData) );
      82           0 :         if( pResult )
      83           0 :             *pResult = result;
      84           0 :         return hostname;
      85             :     }
      86             : 
      87             : 
      88           0 :     inline sal_Int32 SAL_CALL SocketAddr::getPort() const
      89             :     {
      90           0 :         return osl_getInetPortOfSocketAddr(m_handle);
      91             :     }
      92             : 
      93             : 
      94           0 :     inline bool SAL_CALL SocketAddr::setPort( sal_Int32 nPort )
      95             :     {
      96           0 :         return osl_setInetPortOfSocketAddr(m_handle, nPort );
      97             :     }
      98             : 
      99           0 :     inline bool SAL_CALL SocketAddr::setHostname( const ::rtl::OUString &sDottedIpOrHostname )
     100             :     {
     101           0 :         *this = SocketAddr( sDottedIpOrHostname , getPort() );
     102           0 :         return is();
     103             :     }
     104             : 
     105             : 
     106             :     inline bool SAL_CALL SocketAddr::setAddr( const ::rtl::ByteSequence & address )
     107             :     {
     108             :         return osl_setAddrOfSocketAddr( m_handle, address.getHandle() )
     109             :             == osl_Socket_Ok;
     110             :     }
     111             : 
     112             :     inline ::rtl::ByteSequence SAL_CALL SocketAddr::getAddr( oslSocketResult *pResult ) const
     113             :     {
     114             :         ::rtl::ByteSequence sequence;
     115             :         oslSocketResult result = osl_getAddrOfSocketAddr( m_handle, reinterpret_cast<sal_Sequence **>(&sequence) );
     116             :         if( pResult )
     117             :             *pResult = result;
     118             :         return sequence;
     119             :     }
     120             : 
     121             : 
     122           0 :     inline SocketAddr & SAL_CALL SocketAddr::operator= (oslSocketAddr Addr)
     123             :     {
     124           0 :         oslSocketAddr pNewAddr = osl_copySocketAddr( Addr );
     125           0 :         if( m_handle )
     126           0 :             osl_destroySocketAddr( m_handle );
     127           0 :         m_handle = pNewAddr;
     128           0 :         return *this;
     129             :     }
     130             : 
     131             : 
     132           0 :     inline SocketAddr & SAL_CALL SocketAddr::operator= (const SocketAddr& Addr)
     133             :     {
     134           0 :         *this = (Addr.getHandle());
     135           0 :         return *this;
     136             :     }
     137             : 
     138           0 :     inline SocketAddr & SAL_CALL SocketAddr::assign( oslSocketAddr Addr, __osl_socket_NoCopy )
     139             :     {
     140           0 :         if( m_handle )
     141           0 :             osl_destroySocketAddr( m_handle );
     142           0 :         m_handle = Addr;
     143           0 :         return *this;
     144             :     }
     145             : 
     146             : 
     147             :     inline bool SAL_CALL SocketAddr::operator== (oslSocketAddr Addr) const
     148             :     {
     149             :         return osl_isEqualSocketAddr( m_handle, Addr );
     150             :     }
     151             : 
     152           0 :     inline oslSocketAddr SocketAddr::getHandle() const
     153             :     {
     154           0 :         return m_handle;
     155             :     }
     156             : 
     157             : 
     158           0 :     inline bool SocketAddr::is() const
     159             :     {
     160           0 :         return m_handle != 0;
     161             :     }
     162             : 
     163             :     // (static method)______________________________________________________________
     164          63 :     inline ::rtl::OUString SAL_CALL SocketAddr::getLocalHostname( oslSocketResult *pResult )
     165             :     {
     166          63 :         ::rtl::OUString hostname;
     167          63 :         oslSocketResult result = osl_getLocalHostname( &(hostname.pData) );
     168          63 :         if(pResult )
     169           0 :             *pResult = result;
     170          63 :         return hostname;
     171             :     }
     172             : 
     173             :     // (static method)______________________________________________________________
     174           0 :     inline void SAL_CALL SocketAddr::resolveHostname(
     175             :         const ::rtl::OUString & strHostName, SocketAddr &Addr)
     176             :     {
     177           0 :         Addr = SocketAddr( osl_resolveHostname( strHostName.pData ) , SAL_NO_COPY );
     178           0 :     }
     179             : 
     180             :     // (static method)______________________________________________________________
     181             :     inline sal_Int32 SAL_CALL SocketAddr::getServicePort(
     182             :             const ::rtl::OUString& strServiceName,
     183             :             const ::rtl::OUString & strProtocolName )
     184             :     {
     185             :         return osl_getServicePort( strServiceName.pData, strProtocolName.pData );
     186             :     }
     187             : 
     188             : 
     189           0 :     inline Socket::Socket(oslSocketType Type,
     190             :                           oslAddrFamily Family,
     191             :                           oslProtocol   Protocol)
     192           0 :         : m_handle( osl_createSocket(Family, Type, Protocol) )
     193           0 :     {}
     194             : 
     195             : 
     196           0 :     inline Socket::Socket( oslSocket socketHandle, __sal_NoAcquire )
     197           0 :         : m_handle( socketHandle )
     198           0 :     {}
     199             : 
     200             : 
     201             :     inline Socket::Socket( oslSocket socketHandle )
     202             :         : m_handle( socketHandle )
     203             :     {
     204             :         osl_acquireSocket( m_handle );
     205             :     }
     206             : 
     207             : 
     208           0 :     inline Socket::Socket( const Socket & socket )
     209           0 :         : m_handle( socket.getHandle() )
     210             :     {
     211           0 :         osl_acquireSocket( m_handle );
     212           0 :     }
     213             : 
     214             : 
     215           0 :     inline Socket::~Socket()
     216             :     {
     217           0 :         osl_releaseSocket( m_handle );
     218           0 :     }
     219             : 
     220             : 
     221           0 :     inline Socket& Socket::operator= ( oslSocket socketHandle)
     222             :     {
     223           0 :         osl_acquireSocket( socketHandle );
     224           0 :         osl_releaseSocket( m_handle );
     225           0 :         m_handle = socketHandle;
     226           0 :         return *this;
     227             :     }
     228             : 
     229             : 
     230           0 :     inline Socket&  Socket::operator= (const Socket& sock)
     231             :     {
     232           0 :         return (*this) = sock.getHandle();
     233             :     }
     234             : 
     235             : 
     236             :     inline bool Socket::operator==( const Socket& rSocket ) const
     237             :     {
     238             :         return m_handle == rSocket.getHandle();
     239             :     }
     240             : 
     241             : 
     242             :     inline bool Socket::operator==( const oslSocket socketHandle ) const
     243             :     {
     244             :         return m_handle == socketHandle;
     245             :     }
     246             : 
     247             : 
     248           0 :     inline void Socket::shutdown( oslSocketDirection Direction )
     249             :     {
     250           0 :         osl_shutdownSocket( m_handle , Direction );
     251           0 :     }
     252             : 
     253             : 
     254           0 :     inline void Socket::close()
     255             :     {
     256           0 :         osl_closeSocket( m_handle );
     257           0 :     }
     258             : 
     259             : 
     260           0 :     inline void Socket::getLocalAddr( SocketAddr & addr) const
     261             :     {
     262           0 :         addr.assign( osl_getLocalAddrOfSocket( m_handle ) , SAL_NO_COPY );
     263           0 :     }
     264             : 
     265             : 
     266           0 :     inline sal_Int32 Socket::getLocalPort() const
     267             :     {
     268           0 :         SocketAddr addr( 0 );
     269           0 :         getLocalAddr( addr );
     270           0 :         return addr.getPort();
     271             :     }
     272             : 
     273             : 
     274           0 :     inline ::rtl::OUString Socket::getLocalHost() const
     275             :     {
     276           0 :         SocketAddr addr( 0 );
     277           0 :         getLocalAddr( addr );
     278           0 :         return addr.getHostname();
     279             :     }
     280             : 
     281             : 
     282           0 :     inline void Socket::getPeerAddr( SocketAddr &addr ) const
     283             :     {
     284           0 :         addr.assign( osl_getPeerAddrOfSocket( m_handle ), SAL_NO_COPY );
     285           0 :     }
     286             : 
     287             : 
     288           0 :     inline sal_Int32 Socket::getPeerPort() const
     289             :     {
     290           0 :         SocketAddr addr( 0 );
     291           0 :         getPeerAddr( addr );
     292           0 :         return addr.getPort();
     293             :     }
     294             : 
     295             : 
     296           0 :     inline ::rtl::OUString Socket::getPeerHost() const
     297             :     {
     298           0 :         SocketAddr addr( 0 );
     299           0 :         getPeerAddr( addr );
     300           0 :         return addr.getHostname();
     301             :     }
     302             : 
     303             : 
     304           0 :     inline bool Socket::bind(const SocketAddr& LocalInterface)
     305             :     {
     306           0 :         return osl_bindAddrToSocket( m_handle , LocalInterface.getHandle() );
     307             :     }
     308             : 
     309             : 
     310             :     inline bool Socket::isRecvReady(const TimeValue *pTimeout ) const
     311             :     {
     312             :         return osl_isReceiveReady( m_handle , pTimeout );
     313             :     }
     314             : 
     315             : 
     316             :     inline bool Socket::isSendReady(const TimeValue *pTimeout ) const
     317             :     {
     318             :         return osl_isSendReady( m_handle, pTimeout );
     319             :     }
     320             : 
     321             : 
     322             :     inline bool Socket::isExceptionPending(const TimeValue *pTimeout ) const
     323             :     {
     324             :         return osl_isExceptionPending( m_handle, pTimeout );
     325             :     }
     326             : 
     327             : 
     328             :     inline oslSocketType Socket::getType() const
     329             :     {
     330             :         return osl_getSocketType( m_handle );
     331             :     }
     332             : 
     333             : 
     334             :     inline sal_Int32  Socket::getOption(
     335             :         oslSocketOption Option,
     336             :         void* pBuffer,
     337             :         sal_uInt32 BufferLen,
     338             :         oslSocketOptionLevel Level) const
     339             :     {
     340             :         return osl_getSocketOption( m_handle, Level, Option, pBuffer , BufferLen );
     341             :     }
     342             : 
     343             : 
     344           0 :     inline bool Socket::setOption(  oslSocketOption Option,
     345             :                                         void* pBuffer,
     346             :                                         sal_uInt32 BufferLen,
     347             :                                         oslSocketOptionLevel Level ) const
     348             :     {
     349           0 :         return osl_setSocketOption( m_handle, Level, Option , pBuffer, BufferLen );
     350             :     }
     351             : 
     352             : 
     353           0 :     inline bool Socket::setOption( oslSocketOption option, sal_Int32 nValue  )
     354             :     {
     355           0 :         return setOption( option, &nValue, sizeof( nValue ) );
     356             :     }
     357             : 
     358             : 
     359             :     inline sal_Int32 Socket::getOption( oslSocketOption option ) const
     360             :     {
     361             :         sal_Int32 n;
     362             :         getOption( option, &n, sizeof( n ) );
     363             :         return n;
     364             :     }
     365             : 
     366             : 
     367             :     inline bool Socket::enableNonBlockingMode( bool bNonBlockingMode)
     368             :     {
     369             :         return osl_enableNonBlockingMode( m_handle , bNonBlockingMode );
     370             :     }
     371             : 
     372             : 
     373             :     inline bool Socket::isNonBlockingMode() const
     374             :     {
     375             :         return osl_isNonBlockingMode( m_handle );
     376             :     }
     377             : 
     378             : 
     379             :     inline void SAL_CALL Socket::clearError() const
     380             :     {
     381             :         sal_Int32 err = 0;
     382             :         getOption(osl_Socket_OptionError, &err, sizeof(err));
     383             :     }
     384             : 
     385             : 
     386           0 :     inline oslSocketError Socket::getError() const
     387             :     {
     388           0 :         return osl_getLastSocketError( m_handle );
     389             :     }
     390             : 
     391             : 
     392           0 :     inline ::rtl::OUString Socket::getErrorAsString( ) const
     393             :     {
     394           0 :         ::rtl::OUString error;
     395           0 :         osl_getLastSocketErrorDescription( m_handle, &(error.pData) );
     396           0 :         return error;
     397             :     }
     398             : 
     399             : 
     400           0 :     inline oslSocket Socket::getHandle() const
     401             :     {
     402           0 :         return m_handle;
     403             :     }
     404             : 
     405             : 
     406           0 :     inline StreamSocket::StreamSocket(oslAddrFamily Family,
     407             :                                       oslProtocol Protocol,
     408             :                                       oslSocketType Type )
     409           0 :         : Socket( Type, Family, Protocol )
     410           0 :     {}
     411             : 
     412             : 
     413           0 :     inline StreamSocket::StreamSocket( oslSocket socketHandle, __sal_NoAcquire noacquire )
     414           0 :         : Socket( socketHandle, noacquire )
     415           0 :     {}
     416             : 
     417             : 
     418             :     inline StreamSocket::StreamSocket( oslSocket socketHandle )
     419             :         : Socket( socketHandle )
     420             :     {}
     421             : 
     422             : 
     423           0 :     inline StreamSocket::StreamSocket( const StreamSocket & socket )
     424           0 :         : Socket( socket )
     425           0 :     {}
     426             : 
     427             : 
     428           0 :     inline sal_Int32 StreamSocket::read(void* pBuffer, sal_uInt32 n)
     429             :     {
     430           0 :         return osl_readSocket( m_handle, pBuffer, n );
     431             :     }
     432             : 
     433             : 
     434           0 :     inline sal_Int32 StreamSocket::write(const void* pBuffer, sal_uInt32 n)
     435             :     {
     436           0 :         return osl_writeSocket( m_handle, pBuffer, n );
     437             :     }
     438             : 
     439             : 
     440             : 
     441           0 :     inline sal_Int32 StreamSocket::recv(void* pBuffer,
     442             :                                         sal_uInt32 BytesToRead,
     443             :                                         oslSocketMsgFlag Flag)
     444             :     {
     445           0 :         return osl_receiveSocket( m_handle, pBuffer,BytesToRead, Flag );
     446             :     }
     447             : 
     448             : 
     449             :     inline sal_Int32 StreamSocket::send(const void* pBuffer,
     450             :                                         sal_uInt32 BytesToSend,
     451             :                                         oslSocketMsgFlag Flag)
     452             :     {
     453             :         return osl_sendSocket( m_handle, pBuffer, BytesToSend, Flag );
     454             :     }
     455             : 
     456             : 
     457           0 :       inline ConnectorSocket::ConnectorSocket(oslAddrFamily Family,
     458             :                                             oslProtocol Protocol,
     459             :                                             oslSocketType   Type)
     460           0 :         : StreamSocket( Family, Protocol ,Type )
     461           0 :     {}
     462             : 
     463             : 
     464           0 :     inline oslSocketResult ConnectorSocket::connect( const SocketAddr& TargetHost,
     465             :                                                      const TimeValue* pTimeout )
     466             :     {
     467           0 :         return osl_connectSocketTo( m_handle , TargetHost.getHandle(), pTimeout );
     468             :     }
     469             : 
     470             : 
     471           0 :     inline AcceptorSocket::AcceptorSocket(oslAddrFamily Family ,
     472             :                                           oslProtocol   Protocol ,
     473             :                                           oslSocketType Type )
     474           0 :         : Socket( Type, Family, Protocol )
     475           0 :     {}
     476             : 
     477             : 
     478           0 :     inline bool AcceptorSocket::listen(sal_Int32 MaxPendingConnections)
     479             :     {
     480           0 :         return osl_listenOnSocket( m_handle, MaxPendingConnections );
     481             :     }
     482             : 
     483             : 
     484           0 :     inline oslSocketResult AcceptorSocket::acceptConnection( StreamSocket& Connection)
     485             :     {
     486           0 :         oslSocket o = osl_acceptConnectionOnSocket( m_handle, 0 );
     487           0 :         oslSocketResult status = osl_Socket_Ok;
     488           0 :         if( o )
     489             :         {
     490           0 :             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
     491             :         }
     492             :         else
     493             :         {
     494           0 :             Connection = StreamSocket();
     495           0 :             status = osl_Socket_Error;
     496             :         }
     497           0 :         return status;
     498             :     }
     499             : 
     500             : 
     501             :     inline oslSocketResult AcceptorSocket::acceptConnection(
     502             :         StreamSocket&   Connection, SocketAddr & PeerAddr)
     503             :     {
     504             :         // TODO change in/OUT parameter
     505             :         oslSocket o = osl_acceptConnectionOnSocket(
     506             :             m_handle, reinterpret_cast<oslSocketAddr *>(&PeerAddr));
     507             :         oslSocketResult status = osl_Socket_Ok;
     508             :         if( o )
     509             :         {
     510             :             Connection = StreamSocket( o , SAL_NO_ACQUIRE );
     511             :         }
     512             :         else
     513             :         {
     514             :             Connection = StreamSocket();
     515             :             status = osl_Socket_Error;
     516             :         }
     517             :         return status;
     518             :     }
     519             : 
     520             : 
     521             :     inline DatagramSocket::DatagramSocket(oslAddrFamily Family,
     522             :                                           oslProtocol   Protocol,
     523             :                                           oslSocketType Type)
     524             :         : Socket( Type, Family, Protocol )
     525             :     {}
     526             : 
     527             : 
     528             :     inline sal_Int32 DatagramSocket::recvFrom(void*  pBuffer,
     529             :                                               sal_uInt32 BufferSize,
     530             :                                               SocketAddr* pSenderAddr,
     531             :                                               oslSocketMsgFlag Flag )
     532             :     {
     533             :         sal_Int32 nByteRead;
     534             :         if( pSenderAddr )
     535             :         {
     536             :             // TODO : correct the out-parameter pSenderAddr outparameter
     537             :               nByteRead = osl_receiveFromSocket( m_handle, pSenderAddr->getHandle() , pBuffer,
     538             :                                                  BufferSize, Flag);
     539             : //              nByteRead = osl_receiveFromSocket( m_handle, *(oslSocketAddr**) &pSenderAddr , pBuffer,
     540             : //                                                 BufferSize, Flag);
     541             :         }
     542             :         else
     543             :         {
     544             :             nByteRead = osl_receiveFromSocket( m_handle, 0 , pBuffer , BufferSize ,  Flag );
     545             :         }
     546             :         return nByteRead;
     547             :     }
     548             : 
     549             : 
     550             :     inline sal_Int32  DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
     551             :                                               const void* pBuffer,
     552             :                                               sal_uInt32 BufferSize,
     553             :                                               oslSocketMsgFlag Flag )
     554             :     {
     555             :         return osl_sendToSocket( m_handle, ReceiverAddr.getHandle(), pBuffer, BufferSize, Flag );
     556             :     }
     557             : }
     558             : #endif
     559             : 
     560             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11