LibreOffice Module io (master) 1
connector.cxx
Go to the documentation of this file.
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#include <osl/security.hxx>
21#include <sal/log.hxx>
22
25#include <cppuhelper/unourl.hxx>
26#include <rtl/malformeduriexception.hxx>
27#include <rtl/ref.hxx>
28#include <o3tl/string_view.hxx>
29
30#include <com/sun/star/lang/XServiceInfo.hpp>
31#include <com/sun/star/connection/ConnectionSetupException.hpp>
32#include <com/sun/star/connection/NoConnectException.hpp>
33#include <com/sun/star/connection/XConnector.hpp>
34#include <com/sun/star/uno/XComponentContext.hpp>
35
36#include "connector.hxx"
37
38using namespace ::osl;
39using namespace ::cppu;
40using namespace ::com::sun::star::uno;
41using namespace ::com::sun::star::lang;
42using namespace ::com::sun::star::connection;
43
44namespace {
45
46 class OConnector : public WeakImplHelper< XConnector, XServiceInfo >
47 {
48 Reference< XMultiComponentFactory > _xSMgr;
49 Reference< XComponentContext > _xCtx;
50 public:
51 explicit OConnector(const Reference< XComponentContext > &xCtx);
52
53 // Methods
54 virtual Reference< XConnection > SAL_CALL connect(
55 const OUString& sConnectionDescription ) override;
56
57 public: // XServiceInfo
58 virtual OUString SAL_CALL getImplementationName() override;
59 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
60 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
61 };
62
63}
64
65OConnector::OConnector(const Reference< XComponentContext > &xCtx)
66 : _xSMgr( xCtx->getServiceManager() )
67 , _xCtx( xCtx )
68{}
69
70Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription )
71{
72 // split string into tokens
73 try
74 {
75 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
76
77 Reference< XConnection > r;
78 if ( aDesc.getName() == "pipe" )
79 {
80 OUString aName(aDesc.getParameter("name"));
81
83
84 if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) )
85 {
86 r = pConn;
87 }
88 else
89 {
90 OUString const sMessage(
91 "Connector : couldn't connect to pipe \"" + aName + "\": "
92 + OUString::number(pConn->m_pipe.getError()));
93 SAL_WARN("io.connector", sMessage);
94 throw NoConnectException( sMessage );
95 }
96 }
97 else if ( aDesc.getName() == "socket" )
98 {
99 OUString aHost;
100 if (aDesc.hasParameter("host"))
101 aHost = aDesc.getParameter("host");
102 else
103 aHost = "localhost";
104 sal_uInt16 nPort = static_cast< sal_uInt16 >(
105 aDesc.getParameter("port").
106 toInt32());
107 bool bTcpNoDelay
108 = aDesc.getParameter("tcpnodelay").toInt32() != 0;
109
111
112 SocketAddr AddrTarget( aHost.pData, nPort );
113 if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok)
114 {
115 OUString sMessage("Connector : couldn't connect to socket (");
116 OUString sError = pConn->m_socket.getErrorAsString();
117 sMessage += sError + ")";
118 throw NoConnectException( sMessage );
119 }
120 // we enable tcpNoDelay for loopback connections because
121 // it can make a significant speed difference on linux boxes.
122 if( bTcpNoDelay || aHost == "localhost" || aHost.startsWith("127.0.0.") )
123 {
124 sal_Int32 nTcpNoDelay = sal_Int32(true);
125 pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
126 sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
127 }
128 pConn->completeConnectionString();
129 r = pConn;
130 }
131 else
132 {
133 OUString delegatee= "com.sun.star.connection.Connector." + aDesc.getName();
134
135 Reference<XConnector> xConnector(
136 _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY );
137
138 if(!xConnector.is())
139 throw ConnectionSetupException("Connector: unknown delegatee " + delegatee);
140
141 sal_Int32 index = sConnectionDescription.indexOf(',');
142
143 r = xConnector->connect(OUString(o3tl::trim(sConnectionDescription.subView(index + 1))));
144 }
145 return r;
146 }
147 catch (const rtl::MalformedUriException & rEx)
148 {
149 throw ConnectionSetupException(rEx.getMessage());
150 }
151}
152
153OUString OConnector::getImplementationName()
154{
155 return "com.sun.star.comp.io.Connector";
156}
157
158sal_Bool OConnector::supportsService(const OUString& ServiceName)
159{
160 return cppu::supportsService(this, ServiceName);
161}
162
163Sequence< OUString > OConnector::getSupportedServiceNames()
164{
165 return { "com.sun.star.connection.Connector" };
166}
167
168extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
170 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
171{
172 return cppu::acquire(new OConnector(context));
173}
174
175
176
177
178/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * io_OConnector_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: connector.cxx:169
OUString aName
#define SAL_WARN(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
index
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
sal_Int32 toInt32(std::u16string_view rStr)
OUString sMessage
unsigned char sal_Bool
Reference< XMultiComponentFactory > _xSMgr
Reference< XComponentContext > _xCtx