LCOV - code coverage report
Current view: top level - io/source/acceptor - acc_socket.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 144 0.0 %
Date: 2014-04-14 Functions: 0 23 0.0 %
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             : 
      20             : #include "acceptor.hxx"
      21             : 
      22             : #include <boost/unordered_set.hpp>
      23             : #include <algorithm>
      24             : 
      25             : #include <rtl/ustrbuf.hxx>
      26             : #include <com/sun/star/connection/XConnectionBroadcaster.hpp>
      27             : #include <com/sun/star/connection/ConnectionSetupException.hpp>
      28             : 
      29             : #include <cppuhelper/implbase2.hxx>
      30             : 
      31             : using namespace ::osl;
      32             : using namespace ::rtl;
      33             : using namespace ::cppu;
      34             : using namespace ::com::sun::star::uno;
      35             : using namespace ::com::sun::star::io;
      36             : using namespace ::com::sun::star::connection;
      37             : 
      38             : 
      39             : namespace io_acceptor {
      40             :     template<class T>
      41             :     struct ReferenceHash
      42             :     {
      43           0 :         size_t operator () (const ::com::sun::star::uno::Reference<T> & ref) const
      44             :         {
      45           0 :             return (size_t)ref.get();
      46             :         }
      47             :     };
      48             : 
      49             :     template<class T>
      50             :     struct ReferenceEqual
      51             :     {
      52           0 :         sal_Bool operator () (const ::com::sun::star::uno::Reference<T> & op1,
      53             :                               const ::com::sun::star::uno::Reference<T> & op2) const
      54             :         {
      55           0 :             return op1.get() == op2.get();
      56             :         }
      57             :     };
      58             : 
      59             : 
      60             :     typedef ::boost::unordered_set< ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>,
      61             :                              ReferenceHash< ::com::sun::star::io::XStreamListener>,
      62             :                              ReferenceEqual< ::com::sun::star::io::XStreamListener> >
      63             :             XStreamListener_hash_set;
      64             : 
      65             : 
      66             :     class SocketConnection : public ::cppu::WeakImplHelper2<
      67             :         ::com::sun::star::connection::XConnection,
      68             :         ::com::sun::star::connection::XConnectionBroadcaster>
      69             : 
      70             :     {
      71             :     public:
      72             :         SocketConnection( const OUString & sConnectionDescription );
      73             :         virtual ~SocketConnection();
      74             : 
      75             :         virtual sal_Int32 SAL_CALL read( ::com::sun::star::uno::Sequence< sal_Int8 >& aReadBytes,
      76             :                                          sal_Int32 nBytesToRead )
      77             :             throw(::com::sun::star::io::IOException,
      78             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      79             :         virtual void SAL_CALL write( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData )
      80             :             throw(::com::sun::star::io::IOException,
      81             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      82             :         virtual void SAL_CALL flush(  ) throw(
      83             :             ::com::sun::star::io::IOException,
      84             :             ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      85             :         virtual void SAL_CALL close(  )
      86             :             throw(::com::sun::star::io::IOException,
      87             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      88             :         virtual OUString SAL_CALL getDescription(  )
      89             :             throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      90             : 
      91             :         // XConnectionBroadcaster
      92             :         virtual void SAL_CALL addStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
      93             :             throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      94             :         virtual void SAL_CALL removeStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
      95             :             throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      96             : 
      97             :     public:
      98             :         void completeConnectionString();
      99             : 
     100             :         ::osl::StreamSocket m_socket;
     101             :         ::osl::SocketAddr m_addr;
     102             :         oslInterlockedCount m_nStatus;
     103             :         OUString m_sDescription;
     104             : 
     105             :         ::osl::Mutex _mutex;
     106             :         sal_Bool     _started;
     107             :         sal_Bool     _closed;
     108             :         sal_Bool     _error;
     109             :         XStreamListener_hash_set _listeners;
     110             :     };
     111             : 
     112             :     template<class T>
     113           0 :     void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
     114             :     {
     115           0 :           XStreamListener_hash_set listeners;
     116             : 
     117             :         {
     118           0 :             ::osl::MutexGuard guard(pCon->_mutex);
     119           0 :             if(!*notified)
     120             :             {
     121           0 :                 *notified = sal_True;
     122           0 :                 listeners = pCon->_listeners;
     123           0 :             }
     124             :         }
     125             : 
     126           0 :         ::std::for_each(listeners.begin(), listeners.end(), t);
     127           0 :     }
     128             : 
     129           0 :     static void callStarted(Reference<XStreamListener> xStreamListener)
     130             :     {
     131           0 :         xStreamListener->started();
     132           0 :     }
     133             : 
     134             :     struct callError {
     135             :         const Any & any;
     136             : 
     137             :         callError(const Any & any);
     138             : 
     139             :         void operator () (Reference<XStreamListener> xStreamListener);
     140             :     };
     141             : 
     142           0 :     callError::callError(const Any & aAny)
     143           0 :         : any(aAny)
     144             :     {
     145           0 :     }
     146             : 
     147           0 :     void callError::operator () (Reference<XStreamListener> xStreamListener)
     148             :     {
     149           0 :         xStreamListener->error(any);
     150           0 :     }
     151             : 
     152           0 :     static void callClosed(Reference<XStreamListener> xStreamListener)
     153             :     {
     154           0 :         xStreamListener->closed();
     155           0 :     }
     156             : 
     157             : 
     158           0 :     SocketConnection::SocketConnection( const OUString &sConnectionDescription) :
     159             :         m_nStatus( 0 ),
     160             :         m_sDescription( sConnectionDescription ),
     161             :         _started(sal_False),
     162             :         _closed(sal_False),
     163           0 :         _error(sal_False)
     164             :     {
     165             :         // make it unique
     166           0 :         m_sDescription += ",uniqueValue=" ;
     167           0 :         m_sDescription += OUString::number(
     168             :             sal::static_int_cast< sal_Int64 >(
     169             :                 reinterpret_cast< sal_IntPtr >(&m_socket)),
     170           0 :             10 );
     171           0 :     }
     172             : 
     173           0 :     SocketConnection::~SocketConnection()
     174             :     {
     175           0 :     }
     176             : 
     177           0 :     void SocketConnection::completeConnectionString()
     178             :     {
     179           0 :         OUStringBuffer buf( 256 );
     180           0 :         buf.appendAscii( ",peerPort=" );
     181           0 :         buf.append( (sal_Int32) m_socket.getPeerPort() );
     182           0 :         buf.appendAscii( ",peerHost=" );
     183           0 :         buf.append( m_socket.getPeerHost( ) );
     184             : 
     185           0 :         buf.appendAscii( ",localPort=" );
     186           0 :         buf.append( (sal_Int32) m_socket.getLocalPort() );
     187           0 :         buf.appendAscii( ",localHost=" );
     188           0 :         buf.append( m_socket.getLocalHost() );
     189             : 
     190           0 :         m_sDescription += buf.makeStringAndClear();
     191           0 :     }
     192             : 
     193           0 :     sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
     194             :             throw(::com::sun::star::io::IOException,
     195             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     196             :     {
     197           0 :         if( ! m_nStatus )
     198             :         {
     199           0 :             notifyListeners(this, &_started, callStarted);
     200             : 
     201           0 :             if( aReadBytes.getLength() != nBytesToRead )
     202             :             {
     203           0 :                 aReadBytes.realloc( nBytesToRead );
     204             :             }
     205             : 
     206           0 :             sal_Int32 i = 0;
     207           0 :             i = m_socket.read( aReadBytes.getArray()  , aReadBytes.getLength() );
     208             : 
     209           0 :             if(i != nBytesToRead)
     210             :             {
     211           0 :                 OUString message("acc_socket.cxx:SocketConnection::read: error - ");
     212           0 :                 message +=  m_socket.getErrorAsString();
     213             : 
     214           0 :                 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
     215             : 
     216           0 :                 Any any;
     217           0 :                 any <<= ioException;
     218             : 
     219           0 :                 notifyListeners(this, &_error, callError(any));
     220             : 
     221           0 :                 throw ioException;
     222             :             }
     223             : 
     224           0 :             return i;
     225             :         }
     226             :         else
     227             :         {
     228           0 :             OUString message("acc_socket.cxx:SocketConnection::read: error - connection already closed");
     229             : 
     230           0 :             IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
     231             : 
     232           0 :             Any any;
     233           0 :             any <<= ioException;
     234             : 
     235           0 :             notifyListeners(this, &_error, callError(any));
     236             : 
     237           0 :             throw ioException;
     238             :         }
     239             :     }
     240             : 
     241           0 :     void SocketConnection::write( const Sequence < sal_Int8 > &seq )
     242             :             throw(::com::sun::star::io::IOException,
     243             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     244             :     {
     245           0 :         if( ! m_nStatus )
     246             :         {
     247           0 :             if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
     248             :             {
     249           0 :                 OUString message("acc_socket.cxx:SocketConnection::write: error - ");
     250           0 :                 message += m_socket.getErrorAsString();
     251             : 
     252           0 :                 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
     253             : 
     254           0 :                 Any any;
     255           0 :                 any <<= ioException;
     256             : 
     257           0 :                 notifyListeners(this, &_error, callError(any));
     258             : 
     259           0 :                 throw ioException;
     260             :             }
     261             :         }
     262             :         else
     263             :         {
     264           0 :             OUString message("acc_socket.cxx:SocketConnection::write: error - connection already closed");
     265             : 
     266           0 :             IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
     267             : 
     268           0 :             Any any;
     269           0 :             any <<= ioException;
     270             : 
     271           0 :             notifyListeners(this, &_error, callError(any));
     272             : 
     273           0 :             throw ioException;
     274             :         }
     275           0 :     }
     276             : 
     277           0 :     void SocketConnection::flush( )
     278             :             throw(::com::sun::star::io::IOException,
     279             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     280             :     {
     281             : 
     282           0 :     }
     283             : 
     284           0 :     void SocketConnection::close()
     285             :             throw(::com::sun::star::io::IOException,
     286             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     287             :     {
     288             :         // enshure close is called only once
     289           0 :         if(  1 == osl_atomic_increment( (&m_nStatus) ) )
     290             :         {
     291           0 :             m_socket.shutdown();
     292           0 :             notifyListeners(this, &_closed, callClosed);
     293             :         }
     294           0 :     }
     295             : 
     296           0 :     OUString SocketConnection::getDescription()
     297             :             throw( ::com::sun::star::uno::RuntimeException, std::exception)
     298             :     {
     299           0 :         return m_sDescription;
     300             :     }
     301             : 
     302             : 
     303             :     // XConnectionBroadcaster
     304           0 :     void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException, std::exception)
     305             :     {
     306           0 :         MutexGuard guard(_mutex);
     307             : 
     308           0 :         _listeners.insert(aListener);
     309           0 :     }
     310             : 
     311           0 :     void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException, std::exception)
     312             :     {
     313           0 :         MutexGuard guard(_mutex);
     314             : 
     315           0 :         _listeners.erase(aListener);
     316           0 :     }
     317             : 
     318           0 :     SocketAcceptor::SocketAcceptor( const OUString &sSocketName,
     319             :                                     sal_uInt16 nPort,
     320             :                                     sal_Bool bTcpNoDelay,
     321             :                                     const OUString &sConnectionDescription) :
     322             :         m_sSocketName( sSocketName ),
     323             :         m_sConnectionDescription( sConnectionDescription ),
     324             :         m_nPort( nPort ),
     325             :         m_bTcpNoDelay( bTcpNoDelay ),
     326           0 :         m_bClosed( sal_False )
     327             :     {
     328           0 :     }
     329             : 
     330             : 
     331           0 :     void SocketAcceptor::init()
     332             :     {
     333           0 :         if( ! m_addr.setPort( m_nPort ) )
     334             :         {
     335           0 :             OUStringBuffer message( 128 );
     336           0 :             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " );
     337           0 :             message.append( (sal_Int32) m_nPort );
     338             :             throw ConnectionSetupException(
     339           0 :                 message.makeStringAndClear() , Reference< XInterface> () );
     340             :         }
     341           0 :         if( ! m_addr.setHostname( m_sSocketName.pData ) )
     342             :         {
     343           0 :             OUStringBuffer message( 128 );
     344           0 :             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid host " );
     345           0 :             message.append( m_sSocketName );
     346             :             throw ConnectionSetupException(
     347           0 :                 message.makeStringAndClear(), Reference< XInterface > () );
     348             :         }
     349           0 :         m_socket.setOption( osl_Socket_OptionReuseAddr, 1);
     350             : 
     351           0 :         if(! m_socket.bind(m_addr) )
     352             :         {
     353           0 :             OUStringBuffer message( 128 );
     354           0 :             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " );
     355           0 :             message.append( m_sSocketName ).appendAscii( ":" ).append((sal_Int32)m_nPort);
     356             :             throw ConnectionSetupException(
     357             :                 message.makeStringAndClear(),
     358           0 :                 Reference<XInterface>());
     359             :         }
     360             : 
     361           0 :         if(! m_socket.listen() )
     362             :         {
     363           0 :             OUStringBuffer message( 128 );
     364           0 :             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " );
     365           0 :             message.append( m_sSocketName ).appendAscii( ":" ).append( (sal_Int32) m_nPort);
     366           0 :             throw ConnectionSetupException( message.makeStringAndClear(),Reference<XInterface>() );
     367             :         }
     368           0 :     }
     369             : 
     370           0 :     Reference< XConnection > SocketAcceptor::accept( )
     371             :     {
     372           0 :         SocketConnection *pConn = new SocketConnection( m_sConnectionDescription );
     373             : 
     374           0 :         if( m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok )
     375             :         {
     376             :             // stopAccepting was called
     377           0 :             delete pConn;
     378           0 :             return Reference < XConnection > ();
     379             :         }
     380           0 :         if( m_bClosed )
     381             :         {
     382           0 :             delete pConn;
     383           0 :             return Reference < XConnection > ();
     384             :         }
     385             : 
     386           0 :         pConn->completeConnectionString();
     387           0 :         if( m_bTcpNoDelay )
     388             :         {
     389           0 :             sal_Int32 nTcpNoDelay = sal_True;
     390             :             pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
     391           0 :                                        sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
     392             :         }
     393             : 
     394           0 :         return Reference < XConnection > ( (XConnection * ) pConn );
     395             :     }
     396             : 
     397           0 :     void SocketAcceptor::stopAccepting()
     398             :     {
     399           0 :         m_bClosed = sal_True;
     400           0 :         m_socket.close();
     401           0 :     }
     402             : }
     403             : 
     404             : 
     405             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10