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

Generated by: LCOV version 1.11