LCOV - code coverage report
Current view: top level - io/source/connector - ctr_socket.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 97 0.0 %
Date: 2014-11-03 Functions: 0 17 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             : 
      21             : #include "connector.hxx"
      22             : #include <rtl/ustrbuf.hxx>
      23             : #include <algorithm>
      24             : 
      25             : using namespace ::osl;
      26             : using namespace ::com::sun::star::uno;
      27             : using namespace ::com::sun::star::io;
      28             : using namespace ::com::sun::star::connection;
      29             : 
      30             : 
      31             : namespace stoc_connector {
      32             :     template<class T>
      33           0 :     void notifyListeners(SocketConnection * pCon, bool * notified, T t)
      34             :     {
      35           0 :           XStreamListener_hash_set listeners;
      36             : 
      37             :         {
      38           0 :             ::osl::MutexGuard guard(pCon->_mutex);
      39           0 :             if(!*notified)
      40             :             {
      41           0 :                 *notified = true;
      42           0 :                 listeners = pCon->_listeners;
      43           0 :             }
      44             :         }
      45             : 
      46           0 :         ::std::for_each(listeners.begin(), listeners.end(), t);
      47           0 :     }
      48             : 
      49             : 
      50           0 :     static void callStarted(Reference<XStreamListener> xStreamListener)
      51             :     {
      52           0 :         xStreamListener->started();
      53           0 :     }
      54             : 
      55             :     struct callError {
      56             :         const Any & any;
      57             : 
      58             :         callError(const Any & any);
      59             : 
      60             :         void operator () (Reference<XStreamListener> xStreamListener);
      61             :     };
      62             : 
      63           0 :     callError::callError(const Any & aAny)
      64           0 :         : any(aAny)
      65             :     {
      66           0 :     }
      67             : 
      68           0 :     void callError::operator () (Reference<XStreamListener> xStreamListener)
      69             :     {
      70           0 :         xStreamListener->error(any);
      71           0 :     }
      72             : 
      73           0 :     static void callClosed(Reference<XStreamListener> xStreamListener)
      74             :     {
      75           0 :         xStreamListener->closed();
      76           0 :     }
      77             : 
      78             : 
      79           0 :     SocketConnection::SocketConnection( const OUString &sConnectionDescription ) :
      80             :         m_nStatus( 0 ),
      81             :         m_sDescription( sConnectionDescription ),
      82             :         _started(false),
      83             :         _closed(false),
      84           0 :         _error(false)
      85             :     {
      86             :         // make it unique
      87           0 :         m_sDescription += ",uniqueValue=";
      88           0 :         m_sDescription += OUString::number(
      89             :             sal::static_int_cast< sal_Int64 >(
      90             :                 reinterpret_cast< sal_IntPtr >(&m_socket)),
      91           0 :             10 );
      92           0 :     }
      93             : 
      94           0 :     SocketConnection::~SocketConnection()
      95             :     {
      96           0 :     }
      97             : 
      98           0 :     void SocketConnection::completeConnectionString()
      99             :     {
     100             :         sal_Int32 nPort;
     101             : 
     102           0 :         nPort = m_socket.getPeerPort();
     103             : 
     104           0 :         OUStringBuffer buf( 256 );
     105           0 :         buf.appendAscii( ",peerPort=" );
     106           0 :         buf.append( (sal_Int32) nPort );
     107           0 :         buf.appendAscii( ",peerHost=" );
     108           0 :         buf.append( m_socket.getPeerHost() );
     109             : 
     110           0 :         buf.appendAscii( ",localPort=" );
     111           0 :         buf.append( (sal_Int32) nPort );
     112           0 :         buf.appendAscii( ",localHost=" );
     113           0 :         buf.append( m_socket.getLocalHost( ) );
     114             : 
     115           0 :         m_sDescription += buf.makeStringAndClear();
     116           0 :     }
     117             : 
     118           0 :     sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
     119             :             throw(::com::sun::star::io::IOException,
     120             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     121             :     {
     122           0 :         if( ! m_nStatus )
     123             :         {
     124           0 :             notifyListeners(this, &_started, callStarted);
     125             : 
     126           0 :             if( aReadBytes.getLength() != nBytesToRead )
     127             :             {
     128           0 :                 aReadBytes.realloc( nBytesToRead );
     129             :             }
     130           0 :             sal_Int32 i = m_socket.read( aReadBytes.getArray()  , aReadBytes.getLength() );
     131             : 
     132           0 :             if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None)
     133             :             {
     134           0 :                 OUString message("ctr_socket.cxx:SocketConnection::read: error - ");
     135           0 :                 message += m_socket.getErrorAsString();
     136             : 
     137           0 :                 IOException ioException(message, static_cast<XConnection *>(this));
     138             : 
     139           0 :                 Any any;
     140           0 :                 any <<= ioException;
     141             : 
     142           0 :                 notifyListeners(this, &_error, callError(any));
     143             : 
     144           0 :                 throw ioException;
     145             :             }
     146             : 
     147           0 :             return i;
     148             :         }
     149             :         else
     150             :         {
     151           0 :             OUString message("ctr_socket.cxx:SocketConnection::read: error - connection already closed");
     152             : 
     153           0 :             IOException ioException(message, static_cast<XConnection *>(this));
     154             : 
     155           0 :             Any any;
     156           0 :             any <<= ioException;
     157             : 
     158           0 :             notifyListeners(this, &_error, callError(any));
     159             : 
     160           0 :             throw ioException;
     161             :         }
     162             :     }
     163             : 
     164           0 :     void SocketConnection::write( const Sequence < sal_Int8 > &seq )
     165             :             throw(::com::sun::star::io::IOException,
     166             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     167             :     {
     168           0 :         if( ! m_nStatus )
     169             :         {
     170           0 :             if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
     171             :             {
     172           0 :                 OUString message("ctr_socket.cxx:SocketConnection::write: error - ");
     173           0 :                 message += m_socket.getErrorAsString();
     174             : 
     175           0 :                 IOException ioException(message, static_cast<XConnection *>(this));
     176             : 
     177           0 :                 Any any;
     178           0 :                 any <<= ioException;
     179             : 
     180           0 :                 notifyListeners(this, &_error, callError(any));
     181             : 
     182           0 :                 throw ioException;
     183             :             }
     184             :         }
     185             :         else
     186             :         {
     187           0 :             OUString message("ctr_socket.cxx:SocketConnection::write: error - connection already closed");
     188             : 
     189           0 :             IOException ioException(message, static_cast<XConnection *>(this));
     190             : 
     191           0 :             Any any;
     192           0 :             any <<= ioException;
     193             : 
     194           0 :             notifyListeners(this, &_error, callError(any));
     195             : 
     196           0 :             throw ioException;
     197             :         }
     198           0 :     }
     199             : 
     200           0 :     void SocketConnection::flush( )
     201             :             throw(::com::sun::star::io::IOException,
     202             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     203             :     {
     204             : 
     205           0 :     }
     206             : 
     207           0 :     void SocketConnection::close()
     208             :             throw(::com::sun::star::io::IOException,
     209             :                   ::com::sun::star::uno::RuntimeException, std::exception)
     210             :     {
     211             :             // ensure that close is called only once
     212           0 :         if( 1 == osl_atomic_increment( (&m_nStatus) ) )
     213             :         {
     214           0 :             m_socket.shutdown();
     215           0 :             notifyListeners(this, &_closed, callClosed);
     216             :         }
     217           0 :     }
     218             : 
     219           0 :     OUString SocketConnection::getDescription()
     220             :             throw( ::com::sun::star::uno::RuntimeException, std::exception)
     221             :     {
     222           0 :         return m_sDescription;
     223             :     }
     224             : 
     225             : 
     226             : 
     227             :     // XConnectionBroadcaster
     228           0 :     void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException, std::exception)
     229             :     {
     230           0 :         MutexGuard guard(_mutex);
     231             : 
     232           0 :         _listeners.insert(aListener);
     233           0 :     }
     234             : 
     235           0 :     void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException, std::exception)
     236             :     {
     237           0 :         MutexGuard guard(_mutex);
     238             : 
     239           0 :         _listeners.erase(aListener);
     240           0 :     }
     241             : }
     242             : 
     243             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10