1*3716f815SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*3716f815SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*3716f815SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*3716f815SAndrew Rist * distributed with this work for additional information 6*3716f815SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*3716f815SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*3716f815SAndrew Rist * "License"); you may not use this file except in compliance 9*3716f815SAndrew Rist * with the License. You may obtain a copy of the License at 10*3716f815SAndrew Rist * 11*3716f815SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*3716f815SAndrew Rist * 13*3716f815SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*3716f815SAndrew Rist * software distributed under the License is distributed on an 15*3716f815SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*3716f815SAndrew Rist * KIND, either express or implied. See the License for the 17*3716f815SAndrew Rist * specific language governing permissions and limitations 18*3716f815SAndrew Rist * under the License. 19*3716f815SAndrew Rist * 20*3716f815SAndrew Rist *************************************************************/ 21*3716f815SAndrew Rist 22*3716f815SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_io.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "connector.hxx" 28cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 29cdf0e10cSrcweir #include <algorithm> 30cdf0e10cSrcweir 31cdf0e10cSrcweir using namespace ::osl; 32cdf0e10cSrcweir using namespace ::rtl; 33cdf0e10cSrcweir using namespace ::com::sun::star::uno; 34cdf0e10cSrcweir using namespace ::com::sun::star::io; 35cdf0e10cSrcweir using namespace ::com::sun::star::connection; 36cdf0e10cSrcweir 37cdf0e10cSrcweir 38cdf0e10cSrcweir namespace stoc_connector { 39cdf0e10cSrcweir template<class T> notifyListeners(SocketConnection * pCon,sal_Bool * notified,T t)40cdf0e10cSrcweir void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t) 41cdf0e10cSrcweir { 42cdf0e10cSrcweir XStreamListener_hash_set listeners; 43cdf0e10cSrcweir 44cdf0e10cSrcweir { 45cdf0e10cSrcweir ::osl::MutexGuard guard(pCon->_mutex); 46cdf0e10cSrcweir if(!*notified) 47cdf0e10cSrcweir { 48cdf0e10cSrcweir *notified = sal_True; 49cdf0e10cSrcweir listeners = pCon->_listeners; 50cdf0e10cSrcweir } 51cdf0e10cSrcweir } 52cdf0e10cSrcweir 53cdf0e10cSrcweir ::std::for_each(listeners.begin(), listeners.end(), t); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir callStarted(Reference<XStreamListener> xStreamListener)57cdf0e10cSrcweir static void callStarted(Reference<XStreamListener> xStreamListener) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir xStreamListener->started(); 60cdf0e10cSrcweir } 61cdf0e10cSrcweir 62cdf0e10cSrcweir struct callError { 63cdf0e10cSrcweir const Any & any; 64cdf0e10cSrcweir 65cdf0e10cSrcweir callError(const Any & any); 66cdf0e10cSrcweir 67cdf0e10cSrcweir void operator () (Reference<XStreamListener> xStreamListener); 68cdf0e10cSrcweir }; 69cdf0e10cSrcweir callError(const Any & aAny)70cdf0e10cSrcweir callError::callError(const Any & aAny) 71cdf0e10cSrcweir : any(aAny) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir } 74cdf0e10cSrcweir operator ()(Reference<XStreamListener> xStreamListener)75cdf0e10cSrcweir void callError::operator () (Reference<XStreamListener> xStreamListener) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir xStreamListener->error(any); 78cdf0e10cSrcweir } 79cdf0e10cSrcweir callClosed(Reference<XStreamListener> xStreamListener)80cdf0e10cSrcweir static void callClosed(Reference<XStreamListener> xStreamListener) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir xStreamListener->closed(); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir SocketConnection(const OUString & sConnectionDescription)86cdf0e10cSrcweir SocketConnection::SocketConnection( const OUString &sConnectionDescription ) : 87cdf0e10cSrcweir m_nStatus( 0 ), 88cdf0e10cSrcweir m_sDescription( sConnectionDescription ), 89cdf0e10cSrcweir _started(sal_False), 90cdf0e10cSrcweir _closed(sal_False), 91cdf0e10cSrcweir _error(sal_False) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir // make it unique 94cdf0e10cSrcweir g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); 95cdf0e10cSrcweir m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) ); 96cdf0e10cSrcweir m_sDescription += OUString::valueOf( 97cdf0e10cSrcweir sal::static_int_cast< sal_Int64 >( 98cdf0e10cSrcweir reinterpret_cast< sal_IntPtr >(&m_socket)), 99cdf0e10cSrcweir 10 ); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir ~SocketConnection()102cdf0e10cSrcweir SocketConnection::~SocketConnection() 103cdf0e10cSrcweir { 104cdf0e10cSrcweir g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); 105cdf0e10cSrcweir } 106cdf0e10cSrcweir completeConnectionString()107cdf0e10cSrcweir void SocketConnection::completeConnectionString() 108cdf0e10cSrcweir { 109cdf0e10cSrcweir sal_Int32 nPort; 110cdf0e10cSrcweir 111cdf0e10cSrcweir nPort = m_socket.getPeerPort(); 112cdf0e10cSrcweir 113cdf0e10cSrcweir OUStringBuffer buf( 256 ); 114cdf0e10cSrcweir buf.appendAscii( ",peerPort=" ); 115cdf0e10cSrcweir buf.append( (sal_Int32) nPort ); 116cdf0e10cSrcweir buf.appendAscii( ",peerHost=" ); 117cdf0e10cSrcweir buf.append( m_socket.getPeerHost() ); 118cdf0e10cSrcweir 119cdf0e10cSrcweir buf.appendAscii( ",localPort=" ); 120cdf0e10cSrcweir buf.append( (sal_Int32) nPort ); 121cdf0e10cSrcweir buf.appendAscii( ",localHost=" ); 122cdf0e10cSrcweir buf.append( m_socket.getLocalHost( ) ); 123cdf0e10cSrcweir 124cdf0e10cSrcweir m_sDescription += buf.makeStringAndClear(); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir read(Sequence<sal_Int8> & aReadBytes,sal_Int32 nBytesToRead)127cdf0e10cSrcweir sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead ) 128cdf0e10cSrcweir throw(::com::sun::star::io::IOException, 129cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir if( ! m_nStatus ) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir notifyListeners(this, &_started, callStarted); 134cdf0e10cSrcweir 135cdf0e10cSrcweir if( aReadBytes.getLength() != nBytesToRead ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir aReadBytes.realloc( nBytesToRead ); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir sal_Int32 i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() ); 140cdf0e10cSrcweir 141cdf0e10cSrcweir if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::read: error - ")); 144cdf0e10cSrcweir message += m_socket.getErrorAsString(); 145cdf0e10cSrcweir 146cdf0e10cSrcweir IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); 147cdf0e10cSrcweir 148cdf0e10cSrcweir Any any; 149cdf0e10cSrcweir any <<= ioException; 150cdf0e10cSrcweir 151cdf0e10cSrcweir notifyListeners(this, &_error, callError(any)); 152cdf0e10cSrcweir 153cdf0e10cSrcweir throw ioException; 154cdf0e10cSrcweir } 155cdf0e10cSrcweir 156cdf0e10cSrcweir return i; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir else 159cdf0e10cSrcweir { 160cdf0e10cSrcweir OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::read: error - connection already closed")); 161cdf0e10cSrcweir 162cdf0e10cSrcweir IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); 163cdf0e10cSrcweir 164cdf0e10cSrcweir Any any; 165cdf0e10cSrcweir any <<= ioException; 166cdf0e10cSrcweir 167cdf0e10cSrcweir notifyListeners(this, &_error, callError(any)); 168cdf0e10cSrcweir 169cdf0e10cSrcweir throw ioException; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir } 172cdf0e10cSrcweir write(const Sequence<sal_Int8> & seq)173cdf0e10cSrcweir void SocketConnection::write( const Sequence < sal_Int8 > &seq ) 174cdf0e10cSrcweir throw(::com::sun::star::io::IOException, 175cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir if( ! m_nStatus ) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() ) 180cdf0e10cSrcweir { 181cdf0e10cSrcweir OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::write: error - ")); 182cdf0e10cSrcweir message += m_socket.getErrorAsString(); 183cdf0e10cSrcweir 184cdf0e10cSrcweir IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); 185cdf0e10cSrcweir 186cdf0e10cSrcweir Any any; 187cdf0e10cSrcweir any <<= ioException; 188cdf0e10cSrcweir 189cdf0e10cSrcweir notifyListeners(this, &_error, callError(any)); 190cdf0e10cSrcweir 191cdf0e10cSrcweir throw ioException; 192cdf0e10cSrcweir } 193cdf0e10cSrcweir } 194cdf0e10cSrcweir else 195cdf0e10cSrcweir { 196cdf0e10cSrcweir OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::write: error - connection already closed")); 197cdf0e10cSrcweir 198cdf0e10cSrcweir IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); 199cdf0e10cSrcweir 200cdf0e10cSrcweir Any any; 201cdf0e10cSrcweir any <<= ioException; 202cdf0e10cSrcweir 203cdf0e10cSrcweir notifyListeners(this, &_error, callError(any)); 204cdf0e10cSrcweir 205cdf0e10cSrcweir throw ioException; 206cdf0e10cSrcweir } 207cdf0e10cSrcweir } 208cdf0e10cSrcweir flush()209cdf0e10cSrcweir void SocketConnection::flush( ) 210cdf0e10cSrcweir throw(::com::sun::star::io::IOException, 211cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException) 212cdf0e10cSrcweir { 213cdf0e10cSrcweir 214cdf0e10cSrcweir } 215cdf0e10cSrcweir close()216cdf0e10cSrcweir void SocketConnection::close() 217cdf0e10cSrcweir throw(::com::sun::star::io::IOException, 218cdf0e10cSrcweir ::com::sun::star::uno::RuntimeException) 219cdf0e10cSrcweir { 220cdf0e10cSrcweir // ensure that close is called only once 221cdf0e10cSrcweir if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) ) 222cdf0e10cSrcweir { 223cdf0e10cSrcweir m_socket.shutdown(); 224cdf0e10cSrcweir notifyListeners(this, &_closed, callClosed); 225cdf0e10cSrcweir } 226cdf0e10cSrcweir } 227cdf0e10cSrcweir getDescription()228cdf0e10cSrcweir OUString SocketConnection::getDescription() 229cdf0e10cSrcweir throw( ::com::sun::star::uno::RuntimeException) 230cdf0e10cSrcweir { 231cdf0e10cSrcweir return m_sDescription; 232cdf0e10cSrcweir } 233cdf0e10cSrcweir 234cdf0e10cSrcweir 235cdf0e10cSrcweir 236cdf0e10cSrcweir // XConnectionBroadcaster addStreamListener(const Reference<XStreamListener> & aListener)237cdf0e10cSrcweir void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException) 238cdf0e10cSrcweir { 239cdf0e10cSrcweir MutexGuard guard(_mutex); 240cdf0e10cSrcweir 241cdf0e10cSrcweir _listeners.insert(aListener); 242cdf0e10cSrcweir } 243cdf0e10cSrcweir removeStreamListener(const Reference<XStreamListener> & aListener)244cdf0e10cSrcweir void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException) 245cdf0e10cSrcweir { 246cdf0e10cSrcweir MutexGuard guard(_mutex); 247cdf0e10cSrcweir 248cdf0e10cSrcweir _listeners.erase(aListener); 249cdf0e10cSrcweir } 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252