LCOV - code coverage report
Current view: top level - io/source/acceptor - acc_pipe.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 57 67 85.1 %
Date: 2014-04-11 Functions: 11 12 91.7 %
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 "osl/security.hxx"
      21             : #include "acceptor.hxx"
      22             : #include <com/sun/star/connection/ConnectionSetupException.hpp>
      23             : 
      24             : #include <cppuhelper/implbase1.hxx>
      25             : 
      26             : using namespace ::rtl;
      27             : using namespace ::osl;
      28             : using namespace ::cppu;
      29             : using namespace ::com::sun::star::uno;
      30             : using namespace ::com::sun::star::lang;
      31             : using namespace ::com::sun::star::connection;
      32             : using namespace ::com::sun::star::io;
      33             : 
      34             : 
      35             : namespace io_acceptor
      36             : {
      37             : 
      38             :     typedef WeakImplHelper1< XConnection > MyPipeConnection;
      39             : 
      40             :     class PipeConnection :
      41             :         public MyPipeConnection
      42             :     {
      43             :     public:
      44             :         PipeConnection( const OUString &sConnectionDescription);
      45             :         virtual ~PipeConnection();
      46             : 
      47             :         virtual sal_Int32 SAL_CALL read( Sequence< sal_Int8 >& aReadBytes, sal_Int32 nBytesToRead )
      48             :             throw(::com::sun::star::io::IOException,
      49             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      50             :         virtual void SAL_CALL write( const Sequence< sal_Int8 >& aData )
      51             :             throw(::com::sun::star::io::IOException,
      52             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      53             :         virtual void SAL_CALL flush(  ) throw(
      54             :             ::com::sun::star::io::IOException,
      55             :             ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      56             :         virtual void SAL_CALL close(  )
      57             :             throw(::com::sun::star::io::IOException,
      58             :                   ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      59             :         virtual OUString SAL_CALL getDescription(  )
      60             :             throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      61             :     public:
      62             :         ::osl::StreamPipe m_pipe;
      63             :         oslInterlockedCount m_nStatus;
      64             :         OUString m_sDescription;
      65             :     };
      66             : 
      67             : 
      68             : 
      69          97 :     PipeConnection::PipeConnection( const OUString &sConnectionDescription) :
      70             :         m_nStatus( 0 ),
      71          97 :         m_sDescription( sConnectionDescription )
      72             :     {
      73             :         // make it unique
      74          97 :         m_sDescription += ",uniqueValue=";
      75         194 :         m_sDescription += OUString::number(
      76             :             sal::static_int_cast<sal_Int64 >(
      77             :                 reinterpret_cast< sal_IntPtr >(&m_pipe)),
      78          97 :             10 );
      79          97 :     }
      80             : 
      81         188 :     PipeConnection::~PipeConnection()
      82             :     {
      83         188 :     }
      84             : 
      85      514181 :     sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
      86             :         throw(::com::sun::star::io::IOException,
      87             :               ::com::sun::star::uno::RuntimeException, std::exception)
      88             :     {
      89      514181 :         if( ! m_nStatus )
      90             :         {
      91      514181 :             if( aReadBytes.getLength() < nBytesToRead )
      92             :             {
      93      514181 :                 aReadBytes.realloc( nBytesToRead );
      94             :             }
      95      514181 :             sal_Int32 n = m_pipe.read( aReadBytes.getArray(), nBytesToRead );
      96             :             OSL_ASSERT( n >= 0 && n <= aReadBytes.getLength() );
      97      514181 :             if( n < aReadBytes.getLength() )
      98             :             {
      99          49 :                 aReadBytes.realloc( n );
     100             :             }
     101     1028362 :             return n;
     102             :         }
     103             :         else {
     104           0 :             throw IOException();
     105             :         }
     106             :     }
     107             : 
     108      207411 :     void PipeConnection::write( const Sequence < sal_Int8 > &seq )
     109             :             throw(::com::sun::star::io::IOException,
     110             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     111             :     {
     112      207411 :         if( ! m_nStatus )
     113             :         {
     114      207409 :             if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
     115             :             {
     116           0 :                 throw IOException();
     117             :             }
     118             :         }
     119             :         else {
     120           2 :             throw IOException();
     121             :         }
     122      207409 :     }
     123             : 
     124           0 :     void PipeConnection::flush( )
     125             :         throw(  ::com::sun::star::io::IOException,
     126             :                 ::com::sun::star::uno::RuntimeException, std::exception)
     127             :     {
     128           0 :     }
     129             : 
     130          49 :     void PipeConnection::close()
     131             :         throw( ::com::sun::star::io::IOException,
     132             :                ::com::sun::star::uno::RuntimeException, std::exception)
     133             :     {
     134          49 :         if(  1 == osl_atomic_increment( (&m_nStatus) ) )
     135             :         {
     136          49 :             m_pipe.close();
     137             :         }
     138          49 :     }
     139             : 
     140          48 :     OUString PipeConnection::getDescription()
     141             :             throw(::com::sun::star::uno::RuntimeException, std::exception)
     142             :     {
     143          48 :         return m_sDescription;
     144             :     }
     145             : 
     146             :     /***************
     147             :      * PipeAcceptor
     148             :      **************/
     149          49 :     PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) :
     150             :         m_sPipeName( sPipeName ),
     151             :         m_sConnectionDescription( sConnectionDescription ),
     152          49 :         m_bClosed( sal_False )
     153             :     {
     154          49 :     }
     155             : 
     156             : 
     157          49 :     void PipeAcceptor::init()
     158             :     {
     159          49 :         m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() );
     160          49 :         if( ! m_pipe.is() )
     161             :         {
     162           0 :             OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
     163           0 :             throw ConnectionSetupException( error, Reference< XInterface > () );
     164             :         }
     165          49 :     }
     166             : 
     167          97 :     Reference< XConnection > PipeAcceptor::accept( )
     168             :     {
     169          97 :         Pipe pipe;
     170             :         {
     171          97 :             MutexGuard guard( m_mutex );
     172          97 :             pipe = m_pipe;
     173             :         }
     174          97 :         if( ! pipe.is() )
     175             :         {
     176           0 :             OUString error = "io.acceptor: pipe already closed" + m_sPipeName;
     177           0 :             throw ConnectionSetupException( error, Reference< XInterface > () );
     178             :         }
     179          97 :         PipeConnection *pConn = new PipeConnection( m_sConnectionDescription );
     180             : 
     181          97 :         oslPipeError status = pipe.accept( pConn->m_pipe );
     182             : 
     183          97 :         if( m_bClosed )
     184             :         {
     185             :             // stopAccepting was called !
     186          48 :             delete pConn;
     187          48 :             return Reference < XConnection >();
     188             :         }
     189          49 :         else if( osl_Pipe_E_None == status )
     190             :         {
     191          49 :             return Reference < XConnection > ( (XConnection * ) pConn );
     192             :         }
     193             :         else
     194             :         {
     195           0 :             OUString error = "io.acceptor: Couldn't setup pipe " + m_sPipeName;
     196           0 :             throw ConnectionSetupException( error, Reference< XInterface > ());
     197          97 :         }
     198             :     }
     199             : 
     200          48 :     void PipeAcceptor::stopAccepting()
     201             :     {
     202          48 :         m_bClosed = sal_True;
     203          48 :         Pipe pipe;
     204             :         {
     205          48 :             MutexGuard guard( m_mutex );
     206          48 :             pipe = m_pipe;
     207          48 :             m_pipe.clear();
     208             :         }
     209          48 :         if( pipe.is() )
     210             :         {
     211          48 :             pipe.close();
     212          48 :         }
     213          48 :     }
     214             : }
     215             : 
     216             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10