1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/security.hxx>
21 #include <sal/log.hxx>
23 #include <cppuhelper/implbase.hxx>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <cppuhelper/unourl.hxx>
26 #include <rtl/malformeduriexception.hxx>
27 #include <rtl/ref.hxx>
28 #include <o3tl/string_view.hxx>
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>
36 #include "connector.hxx"
38 using namespace ::osl
;
39 using namespace ::cppu
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::connection
;
46 class OConnector
: public WeakImplHelper
< XConnector
, XServiceInfo
>
48 Reference
< XMultiComponentFactory
> _xSMgr
;
49 Reference
< XComponentContext
> _xCtx
;
51 explicit OConnector(const Reference
< XComponentContext
> &xCtx
);
54 virtual Reference
< XConnection
> SAL_CALL
connect(
55 const OUString
& sConnectionDescription
) override
;
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
;
65 OConnector::OConnector(const Reference
< XComponentContext
> &xCtx
)
66 : _xSMgr( xCtx
->getServiceManager() )
70 Reference
< XConnection
> SAL_CALL
OConnector::connect( const OUString
& sConnectionDescription
)
72 // split string into tokens
75 cppu::UnoUrlDescriptor
aDesc(sConnectionDescription
);
77 Reference
< XConnection
> r
;
78 if ( aDesc
.getName() == "pipe" )
80 OUString
aName(aDesc
.getParameter(u
"name"_ustr
));
82 rtl::Reference
<stoc_connector::PipeConnection
> pConn(new stoc_connector::PipeConnection( sConnectionDescription
));
84 if( pConn
->m_pipe
.create( aName
.pData
, osl_Pipe_OPEN
, osl::Security() ) )
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
);
97 else if ( aDesc
.getName() == "socket" )
100 if (aDesc
.hasParameter(u
"host"_ustr
))
101 aHost
= aDesc
.getParameter(u
"host"_ustr
);
104 sal_uInt16 nPort
= static_cast< sal_uInt16
>(
105 aDesc
.getParameter(u
"port"_ustr
).
108 = aDesc
.getParameter(u
"tcpnodelay"_ustr
).toInt32() != 0;
110 rtl::Reference
<stoc_connector::SocketConnection
> pConn(new stoc_connector::SocketConnection( sConnectionDescription
));
112 SocketAddr
AddrTarget( aHost
.pData
, nPort
);
113 if(pConn
->m_socket
.connect(AddrTarget
) != osl_Socket_Ok
)
115 OUString
sMessage(u
"Connector : couldn't connect to socket ("_ustr
);
116 OUString sError
= pConn
->m_socket
.getErrorAsString();
117 sMessage
+= sError
+ ")";
118 throw NoConnectException( sMessage
);
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.") )
124 sal_Int32 nTcpNoDelay
= sal_Int32(true);
125 pConn
->m_socket
.setOption( osl_Socket_OptionTcpNoDelay
, &nTcpNoDelay
,
126 sizeof( nTcpNoDelay
) , osl_Socket_LevelTcp
);
128 pConn
->completeConnectionString();
133 OUString delegatee
= "com.sun.star.connection.Connector." + aDesc
.getName();
135 Reference
<XConnector
> xConnector(
136 _xSMgr
->createInstanceWithContext(delegatee
, _xCtx
), UNO_QUERY
);
139 throw ConnectionSetupException("Connector: unknown delegatee " + delegatee
);
141 sal_Int32 index
= sConnectionDescription
.indexOf(',');
143 r
= xConnector
->connect(OUString(o3tl::trim(sConnectionDescription
.subView(index
+ 1))));
147 catch (const rtl::MalformedUriException
& rEx
)
149 throw ConnectionSetupException(rEx
.getMessage());
153 OUString
OConnector::getImplementationName()
155 return u
"com.sun.star.comp.io.Connector"_ustr
;
158 sal_Bool
OConnector::supportsService(const OUString
& ServiceName
)
160 return cppu::supportsService(this, ServiceName
);
163 Sequence
< OUString
> OConnector::getSupportedServiceNames()
165 return { u
"com.sun.star.connection.Connector"_ustr
};
168 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
169 io_OConnector_get_implementation(
170 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
172 return cppu::acquire(new OConnector(context
));
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */