summaryrefslogtreecommitdiffstats
path: root/io/source/connector
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /io/source/connector
parentInitial commit. (diff)
downloadlibreoffice-upstream.tar.xz
libreoffice-upstream.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'io/source/connector')
-rw-r--r--io/source/connector/connector.cxx178
-rw-r--r--io/source/connector/connector.hxx94
-rw-r--r--io/source/connector/ctr_pipe.cxx97
-rw-r--r--io/source/connector/ctr_socket.cxx225
4 files changed, 594 insertions, 0 deletions
diff --git a/io/source/connector/connector.cxx b/io/source/connector/connector.cxx
new file mode 100644
index 000000000..15720b242
--- /dev/null
+++ b/io/source/connector/connector.cxx
@@ -0,0 +1,178 @@
+/* -*- 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 <rtl/ref.hxx>
+#include <o3tl/string_view.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 "connector.hxx"
+
+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 {
+
+ 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"));
+
+ rtl::Reference<stoc_connector::PipeConnection> pConn(new stoc_connector::PipeConnection( sConnectionDescription ));
+
+ if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
+ {
+ r = pConn;
+ }
+ 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;
+
+ rtl::Reference<stoc_connector::SocketConnection> pConn(new stoc_connector::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 = pConn;
+ }
+ 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(OUString(o3tl::trim(sConnectionDescription.subView(index + 1))));
+ }
+ return r;
+ }
+ catch (const rtl::MalformedUriException & rEx)
+ {
+ throw ConnectionSetupException(rEx.getMessage());
+ }
+}
+
+OUString OConnector::getImplementationName()
+{
+ return "com.sun.star.comp.io.Connector";
+}
+
+sal_Bool OConnector::supportsService(const OUString& ServiceName)
+{
+ return cppu::supportsService(this, ServiceName);
+}
+
+Sequence< OUString > OConnector::getSupportedServiceNames()
+{
+ return { "com.sun.star.connection.Connector" };
+}
+
+extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
+io_OConnector_get_implementation(
+ css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
+{
+ return cppu::acquire(new OConnector(context));
+}
+
+
+
+
+/* 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..691fc7a88
--- /dev/null
+++ b/io/source/connector/connector.hxx
@@ -0,0 +1,94 @@
+/* -*- 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 .
+ */
+
+#pragma once
+
+#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>
+#include <mutex>
+
+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;
+
+ std::mutex _mutex;
+ bool _started;
+ bool _closed;
+ bool _error;
+
+ XStreamListener_hash_set _listeners;
+ };
+}
+
+
+/* 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..5ee0b70bc
--- /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("pipe already closed");
+ }
+ 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("pipe already closed");
+ }
+ if( m_pipe.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
+ {
+ throw IOException("short write");
+ }
+ }
+
+ 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..dcdeef07f
--- /dev/null
+++ b/io/source/connector/ctr_socket.cxx
@@ -0,0 +1,225 @@
+/* -*- 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>
+
+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;
+
+ {
+ std::unique_lock 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();
+
+ m_sDescription +=
+ ",peerPort=" + OUString::number( nPort ) +
+ ",peerHost=" + m_socket.getPeerHost() +
+ ",localPort=" + OUString::number( nPort ) +
+ ",localHost=" + m_socket.getLocalHost( );
+ }
+
+ 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)
+ {
+ std::unique_lock guard(_mutex);
+
+ _listeners.insert(aListener);
+ }
+
+ void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener)
+ {
+ std::unique_lock guard(_mutex);
+
+ _listeners.erase(aListener);
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */