diff options
Diffstat (limited to 'io/source/acceptor')
-rw-r--r-- | io/source/acceptor/acc_pipe.cxx | 224 | ||||
-rw-r--r-- | io/source/acceptor/acc_socket.cxx | 416 | ||||
-rw-r--r-- | io/source/acceptor/acceptor.cxx | 383 | ||||
-rw-r--r-- | io/source/acceptor/acceptor.hxx | 78 | ||||
-rw-r--r-- | io/source/acceptor/acceptor.xml | 44 | ||||
-rw-r--r-- | io/source/acceptor/makefile.mk | 73 |
6 files changed, 1218 insertions, 0 deletions
diff --git a/io/source/acceptor/acc_pipe.cxx b/io/source/acceptor/acc_pipe.cxx new file mode 100644 index 000000000000..6d0b77484fef --- /dev/null +++ b/io/source/acceptor/acc_pipe.cxx @@ -0,0 +1,224 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_io.hxx" +#include "osl/security.hxx" +#include "acceptor.hxx" +#include <com/sun/star/connection/ConnectionSetupException.hpp> + +#include <cppuhelper/implbase1.hxx> + +using namespace ::rtl; +using namespace ::osl; +using namespace ::cppu; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::connection; +using namespace ::com::sun::star::io; + + +namespace io_acceptor +{ + + typedef WeakImplHelper1< XConnection > MyPipeConnection; + + class PipeConnection : + public MyPipeConnection + { + public: + PipeConnection( const OUString &sConnectionDescription); + ~PipeConnection(); + + virtual sal_Int32 SAL_CALL read( Sequence< sal_Int8 >& aReadBytes, sal_Int32 nBytesToRead ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL write( const Sequence< sal_Int8 >& aData ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL flush( ) throw( + ::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL close( ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual ::rtl::OUString SAL_CALL getDescription( ) + throw(::com::sun::star::uno::RuntimeException); + public: + ::osl::StreamPipe m_pipe; + oslInterlockedCount m_nStatus; + OUString m_sDescription; + }; + + + + PipeConnection::PipeConnection( const OUString &sConnectionDescription) : + m_nStatus( 0 ), + m_sDescription( sConnectionDescription ) + { + g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); + + // make it unique + m_sDescription += OUString::createFromAscii( ",uniqueValue=" ); + m_sDescription += OUString::valueOf( + sal::static_int_cast<sal_Int64 >( + reinterpret_cast< sal_IntPtr >(&m_pipe)), + 10 ); + } + + PipeConnection::~PipeConnection() + { + g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); + } + + sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + if( ! m_nStatus ) + { + if( aReadBytes.getLength() != nBytesToRead ) + { + aReadBytes.realloc( nBytesToRead ); + } + return m_pipe.read( aReadBytes.getArray() , aReadBytes.getLength() ); + } + else { + throw IOException(); + } + } + + void PipeConnection::write( const Sequence < sal_Int8 > &seq ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + if( ! m_nStatus ) + { + if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() ) + { + throw IOException(); + } + } + else { + throw IOException(); + } + } + + void PipeConnection::flush( ) + throw( ::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + } + + void PipeConnection::close() + throw( ::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) ) + { + m_pipe.close(); + } + } + + OUString PipeConnection::getDescription() + throw(::com::sun::star::uno::RuntimeException) + { + return m_sDescription; + } + + /*************** + * PipeAcceptor + **************/ + PipeAcceptor::PipeAcceptor( const OUString &sPipeName , const OUString & sConnectionDescription) : + m_sPipeName( sPipeName ), + m_sConnectionDescription( sConnectionDescription ), + m_bClosed( sal_False ) + { + } + + + void PipeAcceptor::init() + { + m_pipe = Pipe( m_sPipeName.pData , osl_Pipe_CREATE , osl::Security() ); + if( ! m_pipe.is() ) + { + OUString error = OUString::createFromAscii( "io.acceptor: Couldn't setup pipe " ); + error += m_sPipeName; + throw ConnectionSetupException( error, Reference< XInterface > () ); + } + } + + Reference< XConnection > PipeAcceptor::accept( ) + { + Pipe pipe; + { + MutexGuard guard( m_mutex ); + pipe = m_pipe; + } + if( ! pipe.is() ) + { + OUString error = OUString::createFromAscii( "io.acceptor: pipe already closed" ); + error += m_sPipeName; + throw ConnectionSetupException( error, Reference< XInterface > () ); + } + PipeConnection *pConn = new PipeConnection( m_sConnectionDescription ); + + oslPipeError status = pipe.accept( pConn->m_pipe ); + + if( m_bClosed ) + { + // stopAccepting was called ! + delete pConn; + return Reference < XConnection >(); + } + else if( osl_Pipe_E_None == status ) + { + return Reference < XConnection > ( (XConnection * ) pConn ); + } + else + { + OUString error = OUString::createFromAscii( "io.acceptor: Couldn't setup pipe " ); + error += m_sPipeName; + throw ConnectionSetupException( error, Reference< XInterface > ()); + } + } + + void PipeAcceptor::stopAccepting() + { + m_bClosed = sal_True; + Pipe pipe; + { + MutexGuard guard( m_mutex ); + pipe = m_pipe; + m_pipe.clear(); + } + if( pipe.is() ) + { + pipe.close(); + } + } +} diff --git a/io/source/acceptor/acc_socket.cxx b/io/source/acceptor/acc_socket.cxx new file mode 100644 index 000000000000..ecdf59495f3d --- /dev/null +++ b/io/source/acceptor/acc_socket.cxx @@ -0,0 +1,416 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_io.hxx" +#include "acceptor.hxx" + +#include <hash_set> +#include <algorithm> + +#include <rtl/ustrbuf.hxx> +#include <com/sun/star/connection/XConnectionBroadcaster.hpp> +#include <com/sun/star/connection/ConnectionSetupException.hpp> + +#include <cppuhelper/implbase2.hxx> + +using namespace ::osl; +using namespace ::rtl; +using namespace ::cppu; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::connection; + + +namespace io_acceptor { + template<class T> + struct ReferenceHash + { + size_t operator () (const ::com::sun::star::uno::Reference<T> & ref) const + { + return (size_t)ref.get(); + } + }; + + template<class T> + struct ReferenceEqual + { + sal_Bool operator () (const ::com::sun::star::uno::Reference<T> & op1, + const ::com::sun::star::uno::Reference<T> & op2) const + { + return op1.get() == op2.get(); + } + }; + + + typedef ::std::hash_set< ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>, + ReferenceHash< ::com::sun::star::io::XStreamListener>, + ReferenceEqual< ::com::sun::star::io::XStreamListener> > + XStreamListener_hash_set; + + + class SocketConnection : public ::cppu::WeakImplHelper2< + ::com::sun::star::connection::XConnection, + ::com::sun::star::connection::XConnectionBroadcaster> + + { + public: + SocketConnection( const OUString & sConnectionDescription ); + ~SocketConnection(); + + virtual sal_Int32 SAL_CALL read( ::com::sun::star::uno::Sequence< sal_Int8 >& aReadBytes, + sal_Int32 nBytesToRead ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL write( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL flush( ) throw( + ::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL close( ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException); + virtual ::rtl::OUString SAL_CALL getDescription( ) + throw(::com::sun::star::uno::RuntimeException); + + // XConnectionBroadcaster + virtual void SAL_CALL addStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener) + throw(::com::sun::star::uno::RuntimeException); + virtual void SAL_CALL removeStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener) + throw(::com::sun::star::uno::RuntimeException); + + public: + void completeConnectionString(); + + ::osl::StreamSocket m_socket; + ::osl::SocketAddr m_addr; + oslInterlockedCount m_nStatus; + ::rtl::OUString m_sDescription; + + ::osl::Mutex _mutex; + sal_Bool _started; + sal_Bool _closed; + sal_Bool _error; + XStreamListener_hash_set _listeners; + }; + + template<class T> + void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t) + { + XStreamListener_hash_set listeners; + + { + ::osl::MutexGuard guard(pCon->_mutex); + if(!*notified) + { + *notified = sal_True; + listeners = pCon->_listeners; + } + } + + ::std::for_each(listeners.begin(), listeners.end(), t); + } + + static void callStarted(Reference<XStreamListener> xStreamListener) + { + xStreamListener->started(); + } + + struct callError { + const Any & any; + + callError(const Any & any); + + void operator () (Reference<XStreamListener> xStreamListener); + }; + + callError::callError(const Any & aAny) + : any(aAny) + { + } + + void callError::operator () (Reference<XStreamListener> xStreamListener) + { + xStreamListener->error(any); + } + + static void callClosed(Reference<XStreamListener> xStreamListener) + { + xStreamListener->closed(); + } + + + SocketConnection::SocketConnection( const OUString &sConnectionDescription) : + m_nStatus( 0 ), + m_sDescription( sConnectionDescription ), + _started(sal_False), + _closed(sal_False), + _error(sal_False) + { + g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); + // make it unique + m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) ); + m_sDescription += OUString::valueOf( + sal::static_int_cast< sal_Int64 >( + reinterpret_cast< sal_IntPtr >(&m_socket)), + 10 ); + } + + SocketConnection::~SocketConnection() + { + g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); + } + + void SocketConnection::completeConnectionString() + { + OUStringBuffer buf( 256 ); + buf.appendAscii( ",peerPort=" ); + buf.append( (sal_Int32) m_socket.getPeerPort() ); + buf.appendAscii( ",peerHost=" ); + buf.append( m_socket.getPeerHost( ) ); + + buf.appendAscii( ",localPort=" ); + buf.append( (sal_Int32) m_socket.getLocalPort() ); + buf.appendAscii( ",localHost=" ); + buf.append( m_socket.getLocalHost() ); + + m_sDescription += buf.makeStringAndClear(); + } + + sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + if( ! m_nStatus ) + { + notifyListeners(this, &_started, callStarted); + + if( aReadBytes.getLength() != nBytesToRead ) + { + aReadBytes.realloc( nBytesToRead ); + } + + sal_Int32 i = 0; + i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() ); + + if(i != nBytesToRead) + { + OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - ")); + message += m_socket.getErrorAsString(); + + IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + + return i; + } + else + { + OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - connection already closed")); + + IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + } + + void SocketConnection::write( const Sequence < sal_Int8 > &seq ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + if( ! m_nStatus ) + { + if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() ) + { + OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - ")); + message += m_socket.getErrorAsString(); + + IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + } + else + { + OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - connection already closed")); + + IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this))); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + } + + void SocketConnection::flush( ) + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + + } + + void SocketConnection::close() + throw(::com::sun::star::io::IOException, + ::com::sun::star::uno::RuntimeException) + { + // enshure close is called only once + if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) ) + { + m_socket.shutdown(); + notifyListeners(this, &_closed, callClosed); + } + } + + OUString SocketConnection::getDescription() + throw( ::com::sun::star::uno::RuntimeException) + { + return m_sDescription; + } + + + // XConnectionBroadcaster + void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException) + { + MutexGuard guard(_mutex); + + _listeners.insert(aListener); + } + + void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException) + { + MutexGuard guard(_mutex); + + _listeners.erase(aListener); + } + + SocketAcceptor::SocketAcceptor( const OUString &sSocketName, + sal_uInt16 nPort, + sal_Bool bTcpNoDelay, + const OUString &sConnectionDescription) : + m_sSocketName( sSocketName ), + m_sConnectionDescription( sConnectionDescription ), + m_nPort( nPort ), + m_bTcpNoDelay( bTcpNoDelay ), + m_bClosed( sal_False ) + { + } + + + void SocketAcceptor::init() + { + if( ! m_addr.setPort( m_nPort ) ) + { + OUStringBuffer message( 128 ); + message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " ); + message.append( (sal_Int32) m_nPort ); + throw ConnectionSetupException( + message.makeStringAndClear() , Reference< XInterface> () ); + } + if( ! m_addr.setHostname( m_sSocketName.pData ) ) + { + OUStringBuffer message( 128 ); + message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid host " ); + message.append( m_sSocketName ); + throw ConnectionSetupException( + message.makeStringAndClear(), Reference< XInterface > () ); + } + m_socket.setOption( osl_Socket_OptionReuseAddr, 1); + + if(! m_socket.bind(m_addr) ) + { + OUStringBuffer message( 128 ); + message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " ); + message.append( m_sSocketName ).appendAscii( ":" ).append((sal_Int32)m_nPort); + throw ConnectionSetupException( + message.makeStringAndClear(), + Reference<XInterface>()); + } + + if(! m_socket.listen() ) + { + OUStringBuffer message( 128 ); + message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " ); + message.append( m_sSocketName ).appendAscii( ":" ).append( (sal_Int32) m_nPort); + throw ConnectionSetupException( message.makeStringAndClear(),Reference<XInterface>() ); + } + } + + Reference< XConnection > SocketAcceptor::accept( ) + { + SocketConnection *pConn = new SocketConnection( m_sConnectionDescription ); + + if( m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok ) + { + // stopAccepting was called + delete pConn; + return Reference < XConnection > (); + } + if( m_bClosed ) + { + delete pConn; + return Reference < XConnection > (); + } + + pConn->completeConnectionString(); + if( m_bTcpNoDelay ) + { + sal_Int32 nTcpNoDelay = sal_True; + pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay, + sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp ); + } + + return Reference < XConnection > ( (XConnection * ) pConn ); + } + + void SocketAcceptor::stopAccepting() + { + m_bClosed = sal_True; + m_socket.close(); + } +} + + diff --git a/io/source/acceptor/acceptor.cxx b/io/source/acceptor/acceptor.cxx new file mode 100644 index 000000000000..4f973065899a --- /dev/null +++ b/io/source/acceptor/acceptor.cxx @@ -0,0 +1,383 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_io.hxx" +#include <osl/mutex.hxx> + +#include <uno/mapping.hxx> + +#include <cppuhelper/factory.hxx> +#include <cppuhelper/implbase2.hxx> +#include <cppuhelper/implementationentry.hxx> +#include "cppuhelper/unourl.hxx" +#include "rtl/malformeduriexception.hxx" + +#include <com/sun/star/connection/XAcceptor.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> + +#include "acceptor.hxx" + +#define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor" +#define SERVICE_NAME "com.sun.star.connection.Acceptor" + +using namespace ::osl; +using namespace ::rtl; +using namespace ::cppu; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::registry; +using namespace ::com::sun::star::connection; + +namespace io_acceptor +{ + rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT; + + class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo > + { + public: + OAcceptor(const Reference< XComponentContext > & xCtx); + virtual ~OAcceptor(); + public: + // Methods + virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription ) + throw( AlreadyAcceptingException, + ConnectionSetupException, + IllegalArgumentException, + RuntimeException); + virtual void SAL_CALL stopAccepting( ) throw( RuntimeException); + + public: // XServiceInfo + virtual OUString SAL_CALL getImplementationName() throw(); + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw(); + virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(); + + private: + PipeAcceptor *m_pPipe; + SocketAcceptor *m_pSocket; + Mutex m_mutex; + OUString m_sLastDescription; + sal_Bool m_bInAccept; + + Reference< XMultiComponentFactory > _xSMgr; + Reference< XComponentContext > _xCtx; + Reference<XAcceptor> _xAcceptor; + }; + + + OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx ) + : m_pPipe( 0 ) + , m_pSocket( 0 ) + , m_bInAccept( sal_False ) + , _xSMgr( xCtx->getServiceManager() ) + , _xCtx( xCtx ) + { + g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); + } + + OAcceptor::~OAcceptor() + { + if( m_pPipe ) + { + delete m_pPipe; + } + if( m_pSocket ) + { + delete m_pSocket; + } + g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); + } + + struct BeingInAccept + { + BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription ) throw( AlreadyAcceptingException) + : m_pFlag( pFlag ) + { + if( *m_pFlag ) + { + OUString sMessage( RTL_CONSTASCII_USTRINGPARAM( "AlreadyAcceptingException :" ) ); + sMessage += sConnectionDescription; + throw AlreadyAcceptingException( sMessage , Reference< XInterface > () ); + } + *m_pFlag = sal_True; + } + ~BeingInAccept() + { + *m_pFlag = sal_False; + } + sal_Bool *m_pFlag; + }; + + Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription ) + throw( AlreadyAcceptingException, + ConnectionSetupException, + IllegalArgumentException, + RuntimeException) + { + OSL_TRACE( + "acceptor %s\n", + OUStringToOString( + sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr()); + // if there is a thread alread accepting in this object, throw an exception. + struct BeingInAccept guard( &m_bInAccept, sConnectionDescription ); + + Reference< XConnection > r; + if( m_sLastDescription.getLength() && + m_sLastDescription != sConnectionDescription ) + { + // instantiate another acceptor for different ports + OUString sMessage = OUString( RTL_CONSTASCII_USTRINGPARAM( + "acceptor::accept called multiple times with different conncetion strings\n" ) ); + throw ConnectionSetupException( sMessage, Reference< XInterface > () ); + } + + if( ! m_sLastDescription.getLength() ) + { + // setup the acceptor + try + { + cppu::UnoUrlDescriptor aDesc(sConnectionDescription); + if (aDesc.getName().equalsAsciiL( + RTL_CONSTASCII_STRINGPARAM("pipe"))) + { + rtl::OUString aName( + aDesc.getParameter( + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "name")))); + + m_pPipe = new PipeAcceptor(aName, sConnectionDescription); + + try + { + m_pPipe->init(); + } + catch( ... ) + { + { + MutexGuard g( m_mutex ); + delete m_pPipe; + m_pPipe = 0; + } + throw; + } + } + else if (aDesc.getName().equalsAsciiL( + RTL_CONSTASCII_STRINGPARAM("socket"))) + { + rtl::OUString aHost; + if (aDesc.hasParameter( + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")))) + aHost = aDesc.getParameter( + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))); + else + aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "localhost")); + sal_uInt16 nPort = static_cast< sal_uInt16 >( + aDesc.getParameter( + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))). + toInt32()); + bool bTcpNoDelay + = aDesc.getParameter( + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( + "tcpnodelay"))).toInt32() != 0; + + m_pSocket = new SocketAcceptor( + aHost, nPort, bTcpNoDelay, sConnectionDescription); + + try + { + m_pSocket->init(); + } + catch( ... ) + { + { + MutexGuard g( m_mutex ); + delete m_pSocket; + m_pSocket = 0; + } + throw; + } + } + else + { + OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor.")); + delegatee += aDesc.getName(); + + OSL_TRACE( + "trying to get service %s\n", + OUStringToOString( + delegatee, RTL_TEXTENCODING_ASCII_US).getStr()); + _xAcceptor = Reference<XAcceptor>( + _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY); + + if(!_xAcceptor.is()) + { + OUString message(RTL_CONSTASCII_USTRINGPARAM("Acceptor: unknown delegatee ")); + message += delegatee; + + throw ConnectionSetupException(message, Reference<XInterface>()); + } + } + } + catch (rtl::MalformedUriException & rEx) + { + throw IllegalArgumentException( + rEx.getMessage(), + Reference< XInterface > (), + 0 ); + } + m_sLastDescription = sConnectionDescription; + } + + if( m_pPipe ) + { + r = m_pPipe->accept(); + } + else if( m_pSocket ) + { + r = m_pSocket->accept(); + } + else + { + r = _xAcceptor->accept(sConnectionDescription); + } + + return r; + } + + void SAL_CALL OAcceptor::stopAccepting( ) throw( RuntimeException) + { + MutexGuard guard( m_mutex ); + + if( m_pPipe ) + { + m_pPipe->stopAccepting(); + } + else if ( m_pSocket ) + { + m_pSocket->stopAccepting(); + } + else if( _xAcceptor.is() ) + { + _xAcceptor->stopAccepting(); + } + + } + + OUString acceptor_getImplementationName() + { + return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) ); + } + + Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx) + { + return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) ); + } + + Sequence< OUString > acceptor_getSupportedServiceNames() + { + static Sequence < OUString > *pNames = 0; + if( ! pNames ) + { + MutexGuard guard( Mutex::getGlobalMutex() ); + if( !pNames ) + { + static Sequence< OUString > seqNames(1); + seqNames.getArray()[0] = OUString::createFromAscii( SERVICE_NAME ); + pNames = &seqNames; + } + } + return *pNames; + } + + OUString OAcceptor::getImplementationName() throw() + { + return acceptor_getImplementationName(); + } + + sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw() + { + Sequence< OUString > aSNL = getSupportedServiceNames(); + const OUString * pArray = aSNL.getConstArray(); + + for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) + if( pArray[i] == ServiceName ) + return sal_True; + + return sal_False; + } + + Sequence< OUString > OAcceptor::getSupportedServiceNames(void) throw() + { + return acceptor_getSupportedServiceNames(); + } + + +} + +using namespace io_acceptor; + +static struct ImplementationEntry g_entries[] = +{ + { + acceptor_CreateInstance, acceptor_getImplementationName , + acceptor_getSupportedServiceNames, createSingleComponentFactory , + &g_moduleCount.modCnt , 0 + }, + { 0, 0, 0, 0, 0, 0 } +}; + +extern "C" +{ + +sal_Bool SAL_CALL component_canUnload( TimeValue *pTime ) +{ + return g_moduleCount.canUnload( &g_moduleCount , pTime ); +} + +//================================================================================================== +void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** ) +{ + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; +} +//================================================================================================== +sal_Bool SAL_CALL component_writeInfo( + void * pServiceManager, void * pRegistryKey ) +{ + return component_writeInfoHelper( pServiceManager, pRegistryKey, g_entries ); +} +//================================================================================================== +void * SAL_CALL component_getFactory( + const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) +{ + return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries ); +} +} + + + diff --git a/io/source/acceptor/acceptor.hxx b/io/source/acceptor/acceptor.hxx new file mode 100644 index 000000000000..11b01e6dac7d --- /dev/null +++ b/io/source/acceptor/acceptor.hxx @@ -0,0 +1,78 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <osl/pipe.hxx> +#include <osl/socket.hxx> +#include <rtl/unload.h> + +#include <com/sun/star/connection/XConnection.hpp> + +namespace io_acceptor { + + extern rtl_StandardModuleCount g_moduleCount; + + class PipeAcceptor + { + public: + PipeAcceptor( const ::rtl::OUString &sPipeName , const ::rtl::OUString &sConnectionDescription ); + + void init(); + ::com::sun::star::uno::Reference < ::com::sun::star::connection::XConnection > accept( ); + + void stopAccepting(); + + ::osl::Mutex m_mutex; + ::osl::Pipe m_pipe; + ::rtl::OUString m_sPipeName; + ::rtl::OUString m_sConnectionDescription; + sal_Bool m_bClosed; + }; + + class SocketAcceptor + { + public: + SocketAcceptor( const ::rtl::OUString & sSocketName , + sal_uInt16 nPort, + sal_Bool bTcpNoDelay, + const ::rtl::OUString &sConnectionDescription ); + + void init(); + ::com::sun::star::uno::Reference < ::com::sun::star::connection::XConnection > accept(); + + void stopAccepting(); + + ::osl::SocketAddr m_addr; + ::osl::AcceptorSocket m_socket; + ::rtl::OUString m_sSocketName; + ::rtl::OUString m_sConnectionDescription; + sal_uInt16 m_nPort; + sal_Bool m_bTcpNoDelay; + sal_Bool m_bClosed; + }; + +} + diff --git a/io/source/acceptor/acceptor.xml b/io/source/acceptor/acceptor.xml new file mode 100644 index 000000000000..82a25d3d5926 --- /dev/null +++ b/io/source/acceptor/acceptor.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE module-description PUBLIC "-//StarOffice//DTD ComponentDescription 1.0//EN" "module-description.dtd"> +<module-description xmlns:xlink="http://www.w3.org/1999/xlink"> + <module-name> acceptor.uno </module-name> + <component-description> + <author> Joerg Budischewski </author> + <name> com.sun.star.comp.io.Acceptor </name> + <description> + This component allows + to accept an connect-attempt from a different process. + </description> + <loader-name> com.sun.star.loader.SharedLibrary </loader-name> + <language> c++ </language> + <status value="final"/> + <supported-service> com.sun.star.connection.Acceptor </supported-service> + <service-dependency> ... </service-dependency> + <type> com.sun.star.connection.XAcceptor </type> + <type> com.sun.star.connection.XConnectionBroadcaster </type> + <type> com.sun.star.io.UnexpectedEOFException </type> + <type> com.sun.star.io.WrongFormatException </type> + <type> com.sun.star.lang.XComponent </type> + <type> com.sun.star.lang.XMultiServiceFactory </type> + <type> com.sun.star.lang.XSingleServiceFactory </type> + <type> com.sun.star.lang.XServiceInfo </type> + <type> com.sun.star.lang.XTypeProvider </type> + <type> com.sun.star.lang.IllegalArgumentException </type> + <type> com.sun.star.registry.XRegistryKey </type> + <type> com.sun.star.registry.XImplementationRegistration </type> + <type> com.sun.star.test.XSimpleTest </type> + <type> com.sun.star.lang.XSingleComponentFactory </type> + <type> com.sun.star.uno.XComponentContext </type> + <type> com.sun.star.uno.TypeClass </type> + <type> com.sun.star.uno.XWeak </type> + <type> com.sun.star.uno.XAggregation </type> + </component-description> + <project-build-dependency> cppuhelper </project-build-dependency> + <project-build-dependency> cppu </project-build-dependency> + <project-build-dependency> vos </project-build-dependency> + <project-build-dependency> sal </project-build-dependency> + <runtime-module-dependency> cppuhelper </runtime-module-dependency> + <runtime-module-dependency> cppu2 </runtime-module-dependency> + <runtime-module-dependency> vos2MSC </runtime-module-dependency> + <runtime-module-dependency> sal2 </runtime-module-dependency> +</module-description> diff --git a/io/source/acceptor/makefile.mk b/io/source/acceptor/makefile.mk new file mode 100644 index 000000000000..3da3fbe18913 --- /dev/null +++ b/io/source/acceptor/makefile.mk @@ -0,0 +1,73 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=io +TARGET = acceptor.uno +ENABLE_EXCEPTIONS=TRUE +NO_BSYMBOLIC=TRUE +COMP1TYPELIST = acceptor +COMPRDB=$(SOLARBINDIR)$/udkapi.rdb + +# --- Settings ----------------------------------------------------- +.INCLUDE : settings.mk +.IF "$(L10N_framework)"=="" +DLLPRE = +# ------------------------------------------------------------------ + +UNOUCRDEP=$(SOLARBINDIR)$/udkapi.rdb +UNOUCRRDB=$(SOLARBINDIR)$/udkapi.rdb +UNOUCROUT=$(OUT)$/inc$/acceptor +INCPRE+= $(UNOUCROUT) + + +SLOFILES= \ + $(SLO)$/acceptor.obj \ + $(SLO)$/acc_pipe.obj \ + $(SLO)$/acc_socket.obj + +SHL1TARGET= $(TARGET) +SHL1VERSIONMAP = $(SOLARENV)/src/unloadablecomponent.map +SHL1RPATH=URELIB + +SHL1STDLIBS= \ + $(SALLIB) \ + $(CPPULIB) \ + $(CPPUHELPERLIB) + +SHL1DEPN= +SHL1IMPLIB= i$(TARGET) +SHL1LIBS= $(SLB)$/$(TARGET).lib +SHL1DEF= $(MISC)$/$(SHL1TARGET).def + +DEF1NAME= $(SHL1TARGET) + +# --- Targets ------------------------------------------------------ +.ENDIF # L10N_framework + +.INCLUDE : target.mk |