diff options
Diffstat (limited to '')
-rw-r--r-- | io/source/connector/connector.cxx | 191 | ||||
-rw-r--r-- | io/source/connector/connector.hxx | 95 | ||||
-rw-r--r-- | io/source/connector/ctr_pipe.cxx | 97 | ||||
-rw-r--r-- | io/source/connector/ctr_socket.cxx | 233 |
4 files changed, 616 insertions, 0 deletions
diff --git a/io/source/connector/connector.cxx b/io/source/connector/connector.cxx new file mode 100644 index 000000000..0f4792f3a --- /dev/null +++ b/io/source/connector/connector.cxx @@ -0,0 +1,191 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <osl/security.hxx> +#include <sal/log.hxx> + +#include <cppuhelper/implbase.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <cppuhelper/unourl.hxx> +#include <rtl/malformeduriexception.hxx> + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/connection/ConnectionSetupException.hpp> +#include <com/sun/star/connection/NoConnectException.hpp> +#include <com/sun/star/connection/XConnector.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> + +#include <services.hxx> +#include "connector.hxx" + +#define IMPLEMENTATION_NAME "com.sun.star.comp.io.Connector" +#define SERVICE_NAME "com.sun.star.connection.Connector" + +using namespace ::osl; +using namespace ::cppu; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::lang; +using namespace ::com::sun::star::connection; + +namespace stoc_connector +{ + namespace { + + class OConnector : public WeakImplHelper< XConnector, XServiceInfo > + { + Reference< XMultiComponentFactory > _xSMgr; + Reference< XComponentContext > _xCtx; + public: + explicit OConnector(const Reference< XComponentContext > &xCtx); + + // Methods + virtual Reference< XConnection > SAL_CALL connect( + const OUString& sConnectionDescription ) override; + + public: // XServiceInfo + virtual OUString SAL_CALL getImplementationName() override; + virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override; + virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override; + }; + + } + + OConnector::OConnector(const Reference< XComponentContext > &xCtx) + : _xSMgr( xCtx->getServiceManager() ) + , _xCtx( xCtx ) + {} + + Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription ) + { + // split string into tokens + try + { + cppu::UnoUrlDescriptor aDesc(sConnectionDescription); + + Reference< XConnection > r; + if ( aDesc.getName() == "pipe" ) + { + OUString aName(aDesc.getParameter("name")); + + std::unique_ptr<PipeConnection> pConn(new PipeConnection( sConnectionDescription )); + + if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) ) + { + r.set( static_cast<XConnection *>(pConn.release()) ); + } + else + { + OUString const sMessage( + "Connector : couldn't connect to pipe " + aName + "(" + + OUString::number(pConn->m_pipe.getError()) + ")"); + SAL_WARN("io.connector", sMessage); + throw NoConnectException( sMessage ); + } + } + else if ( aDesc.getName() == "socket" ) + { + OUString aHost; + if (aDesc.hasParameter("host")) + aHost = aDesc.getParameter("host"); + else + aHost = "localhost"; + sal_uInt16 nPort = static_cast< sal_uInt16 >( + aDesc.getParameter("port"). + toInt32()); + bool bTcpNoDelay + = aDesc.getParameter("tcpnodelay").toInt32() != 0; + + std::unique_ptr<SocketConnection> pConn(new SocketConnection( sConnectionDescription)); + + SocketAddr AddrTarget( aHost.pData, nPort ); + if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok) + { + OUString sMessage("Connector : couldn't connect to socket ("); + OUString sError = pConn->m_socket.getErrorAsString(); + sMessage += sError + ")"; + throw NoConnectException( sMessage ); + } + // we enable tcpNoDelay for loopback connections because + // it can make a significant speed difference on linux boxes. + if( bTcpNoDelay || aHost == "localhost" || aHost.startsWith("127.0.0.") ) + { + sal_Int32 nTcpNoDelay = sal_Int32(true); + pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay, + sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp ); + } + pConn->completeConnectionString(); + r.set( static_cast<XConnection *>(pConn.release()) ); + } + else + { + OUString delegatee= "com.sun.star.connection.Connector." + aDesc.getName(); + + Reference<XConnector> xConnector( + _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY ); + + if(!xConnector.is()) + throw ConnectionSetupException("Connector: unknown delegatee " + delegatee); + + sal_Int32 index = sConnectionDescription.indexOf(','); + + r = xConnector->connect(sConnectionDescription.copy(index + 1).trim()); + } + return r; + } + catch (const rtl::MalformedUriException & rEx) + { + throw ConnectionSetupException(rEx.getMessage()); + } + } + + Sequence< OUString > connector_getSupportedServiceNames() + { + Sequence< OUString > seqNames { SERVICE_NAME }; + return seqNames; + } + + OUString connector_getImplementationName() + { + return IMPLEMENTATION_NAME; + } + + OUString OConnector::getImplementationName() + { + return connector_getImplementationName(); + } + + sal_Bool OConnector::supportsService(const OUString& ServiceName) + { + return cppu::supportsService(this, ServiceName); + } + + Sequence< OUString > OConnector::getSupportedServiceNames() + { + return connector_getSupportedServiceNames(); + } + + Reference< XInterface > connector_CreateInstance( const Reference< XComponentContext > & xCtx) + { + return Reference < XInterface >( static_cast<OWeakObject *>(new OConnector(xCtx)) ); + } + + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/io/source/connector/connector.hxx b/io/source/connector/connector.hxx new file mode 100644 index 000000000..9aad55a45 --- /dev/null +++ b/io/source/connector/connector.hxx @@ -0,0 +1,95 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_IO_SOURCE_CONNECTOR_CONNECTOR_HXX +#define INCLUDED_IO_SOURCE_CONNECTOR_CONNECTOR_HXX + +#include <cppuhelper/implbase.hxx> + +#include <com/sun/star/connection/XConnection.hpp> +#include <com/sun/star/connection/XConnectionBroadcaster.hpp> + +#include <unordered_set> +#include <osl/socket.hxx> +#include <osl/pipe.hxx> + +namespace stoc_connector +{ + typedef std::unordered_set< css::uno::Reference< css::io::XStreamListener> > + XStreamListener_hash_set; + + class PipeConnection : + public ::cppu::WeakImplHelper< css::connection::XConnection > + + { + public: + explicit PipeConnection( const OUString &sConnectionDescription ); + virtual ~PipeConnection() override; + + virtual sal_Int32 SAL_CALL read( css::uno::Sequence< sal_Int8 >& aReadBytes, + sal_Int32 nBytesToRead ) override; + virtual void SAL_CALL write( const css::uno::Sequence< sal_Int8 >& aData ) override; + virtual void SAL_CALL flush( ) override; + virtual void SAL_CALL close( ) override; + virtual OUString SAL_CALL getDescription( ) override; + public: + ::osl::StreamPipe m_pipe; + oslInterlockedCount m_nStatus; + OUString m_sDescription; + }; + + class SocketConnection : + public ::cppu::WeakImplHelper< css::connection::XConnection, css::connection::XConnectionBroadcaster > + + { + public: + explicit SocketConnection( const OUString & sConnectionDescription ); + virtual ~SocketConnection() override; + + virtual sal_Int32 SAL_CALL read( css::uno::Sequence< sal_Int8 >& aReadBytes, + sal_Int32 nBytesToRead ) override; + virtual void SAL_CALL write( const css::uno::Sequence< sal_Int8 >& aData ) override; + virtual void SAL_CALL flush( ) override; + virtual void SAL_CALL close( ) override; + virtual OUString SAL_CALL getDescription( ) override; + + + // XConnectionBroadcaster + virtual void SAL_CALL addStreamListener(const css::uno::Reference< css::io::XStreamListener>& aListener) override; + virtual void SAL_CALL removeStreamListener(const css::uno::Reference< css::io::XStreamListener>& aListener) override; + + public: + void completeConnectionString(); + + ::osl::ConnectorSocket m_socket; + oslInterlockedCount m_nStatus; + OUString m_sDescription; + + ::osl::Mutex _mutex; + bool _started; + bool _closed; + bool _error; + + XStreamListener_hash_set _listeners; + }; +} + +#endif // INCLUDED_IO_SOURCE_CONNECTOR_CONNECTOR_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/io/source/connector/ctr_pipe.cxx b/io/source/connector/ctr_pipe.cxx new file mode 100644 index 000000000..8d66dea7b --- /dev/null +++ b/io/source/connector/ctr_pipe.cxx @@ -0,0 +1,97 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <com/sun/star/io/IOException.hpp> + +#include "connector.hxx" +#include <osl/pipe.hxx> + +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::connection; + + +namespace stoc_connector { + + PipeConnection::PipeConnection( const OUString & sConnectionDescription ) : + m_nStatus( 0 ), + m_sDescription( sConnectionDescription ) + { + // make it unique + m_sDescription += ",uniqueValue="; + m_sDescription += OUString::number( + sal::static_int_cast< sal_Int64 >( + reinterpret_cast< sal_IntPtr >(&m_pipe)) ); + } + + PipeConnection::~PipeConnection() + { + } + + sal_Int32 PipeConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead ) + { + if( m_nStatus ) + { + throw IOException(); + } + if( aReadBytes.getLength() != nBytesToRead ) + { + aReadBytes.realloc( nBytesToRead ); + } + return m_pipe.read( aReadBytes.getArray() , aReadBytes.getLength() ); + + } + + void PipeConnection::write( const Sequence < sal_Int8 > &seq ) + { + if( m_nStatus ) + { + throw IOException(); + } + if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() ) + { + throw IOException(); + } + } + + void PipeConnection::flush( ) + { + + } + + void PipeConnection::close() + { + // ensure that close is called only once + if(1 == osl_atomic_increment( (&m_nStatus) ) ) + { + m_pipe.close(); + } + } + + OUString PipeConnection::getDescription() + { + return m_sDescription; + } + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/io/source/connector/ctr_socket.cxx b/io/source/connector/ctr_socket.cxx new file mode 100644 index 000000000..164cd4f6f --- /dev/null +++ b/io/source/connector/ctr_socket.cxx @@ -0,0 +1,233 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "connector.hxx" +#include <com/sun/star/io/IOException.hpp> +#include <rtl/ustrbuf.hxx> + +using namespace ::osl; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::io; +using namespace ::com::sun::star::connection; + + +namespace stoc_connector { + template<class T> + static void notifyListeners(SocketConnection * pCon, bool * notified, T t) + { + XStreamListener_hash_set listeners; + + { + ::osl::MutexGuard guard(pCon->_mutex); + if(!*notified) + { + *notified = true; + listeners = pCon->_listeners; + } + } + + for(auto& listener : listeners) + t(listener); + } + + + static void callStarted(const Reference<XStreamListener>& xStreamListener) + { + xStreamListener->started(); + } + + namespace { + + struct callError { + const Any & any; + + explicit callError(const Any & any); + + void operator () (const Reference<XStreamListener>& xStreamListener); + }; + + } + + callError::callError(const Any & aAny) + : any(aAny) + { + } + + void callError::operator () (const Reference<XStreamListener>& xStreamListener) + { + xStreamListener->error(any); + } + + static void callClosed(const Reference<XStreamListener>& xStreamListener) + { + xStreamListener->closed(); + } + + + SocketConnection::SocketConnection( const OUString &sConnectionDescription ) : + m_nStatus( 0 ), + m_sDescription( sConnectionDescription ), + _started(false), + _closed(false), + _error(false) + { + // make it unique + m_sDescription += ",uniqueValue="; + m_sDescription += OUString::number( + sal::static_int_cast< sal_Int64 >( + reinterpret_cast< sal_IntPtr >(&m_socket)) ); + } + + SocketConnection::~SocketConnection() + { + } + + void SocketConnection::completeConnectionString() + { + sal_Int32 nPort; + + nPort = m_socket.getPeerPort(); + + OUStringBuffer buf( 256 ); + buf.append( ",peerPort=" ); + buf.append( nPort ); + buf.append( ",peerHost=" ); + buf.append( m_socket.getPeerHost() ); + + buf.append( ",localPort=" ); + buf.append( nPort ); + buf.append( ",localHost=" ); + buf.append( m_socket.getLocalHost( ) ); + + m_sDescription += buf; + } + + sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead ) + { + if( ! m_nStatus ) + { + notifyListeners(this, &_started, callStarted); + + if( aReadBytes.getLength() != nBytesToRead ) + { + aReadBytes.realloc( nBytesToRead ); + } + sal_Int32 i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() ); + + if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None) + { + OUString message = "ctr_socket.cxx:SocketConnection::read: error - " + + m_socket.getErrorAsString(); + + IOException ioException(message, static_cast<XConnection *>(this)); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + + return i; + } + else + { + IOException ioException("ctr_socket.cxx:SocketConnection::read: error - connection already closed", static_cast<XConnection *>(this)); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + } + + void SocketConnection::write( const Sequence < sal_Int8 > &seq ) + { + if( ! m_nStatus ) + { + if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() ) + { + OUString message = "ctr_socket.cxx:SocketConnection::write: error - " + + m_socket.getErrorAsString(); + + IOException ioException(message, static_cast<XConnection *>(this)); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + } + else + { + IOException ioException("ctr_socket.cxx:SocketConnection::write: error - connection already closed", static_cast<XConnection *>(this)); + + Any any; + any <<= ioException; + + notifyListeners(this, &_error, callError(any)); + + throw ioException; + } + } + + void SocketConnection::flush( ) + { + + } + + void SocketConnection::close() + { + // ensure that close is called only once + if( 1 == osl_atomic_increment( (&m_nStatus) ) ) + { + m_socket.shutdown(); + notifyListeners(this, &_closed, callClosed); + } + } + + OUString SocketConnection::getDescription() + { + return m_sDescription; + } + + + // XConnectionBroadcaster + void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) + { + MutexGuard guard(_mutex); + + _listeners.insert(aListener); + } + + void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) + { + MutexGuard guard(_mutex); + + _listeners.erase(aListener); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |