/src/libreoffice/io/source/connector/connector.cxx
Line | Count | Source |
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 | | |
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> |
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 | | |
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; |
43 | | |
44 | | namespace { |
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 | | |
65 | | OConnector::OConnector(const Reference< XComponentContext > &xCtx) |
66 | 0 | : _xSMgr( xCtx->getServiceManager() ) |
67 | 0 | , _xCtx( xCtx ) |
68 | 0 | {} |
69 | | |
70 | | Reference< XConnection > SAL_CALL OConnector::connect( const OUString& sConnectionDescription ) |
71 | 0 | { |
72 | | // split string into tokens |
73 | 0 | try |
74 | 0 | { |
75 | 0 | cppu::UnoUrlDescriptor aDesc(sConnectionDescription); |
76 | |
|
77 | 0 | Reference< XConnection > r; |
78 | 0 | if ( aDesc.getName() == "pipe" ) |
79 | 0 | { |
80 | 0 | OUString aName(aDesc.getParameter(u"name"_ustr)); |
81 | |
|
82 | 0 | rtl::Reference<stoc_connector::PipeConnection> pConn(new stoc_connector::PipeConnection( sConnectionDescription )); |
83 | |
|
84 | 0 | if( pConn->m_pipe.create( aName.pData, osl_Pipe_OPEN, osl::Security() ) ) |
85 | 0 | { |
86 | 0 | r = pConn; |
87 | 0 | } |
88 | 0 | else |
89 | 0 | { |
90 | 0 | OUString const sMessage( |
91 | 0 | "Connector : couldn't connect to pipe \"" + aName + "\": " |
92 | 0 | + OUString::number(pConn->m_pipe.getError())); |
93 | 0 | SAL_WARN("io.connector", sMessage); |
94 | 0 | throw NoConnectException( sMessage ); |
95 | 0 | } |
96 | 0 | } |
97 | 0 | else if ( aDesc.getName() == "socket" ) |
98 | 0 | { |
99 | 0 | OUString aHost; |
100 | 0 | if (aDesc.hasParameter(u"host"_ustr)) |
101 | 0 | aHost = aDesc.getParameter(u"host"_ustr); |
102 | 0 | else |
103 | 0 | aHost = "localhost"; |
104 | 0 | sal_uInt16 nPort = static_cast< sal_uInt16 >( |
105 | 0 | aDesc.getParameter(u"port"_ustr). |
106 | 0 | toInt32()); |
107 | 0 | bool bTcpNoDelay |
108 | 0 | = aDesc.getParameter(u"tcpnodelay"_ustr).toInt32() != 0; |
109 | |
|
110 | 0 | rtl::Reference<stoc_connector::SocketConnection> pConn(new stoc_connector::SocketConnection( sConnectionDescription)); |
111 | |
|
112 | 0 | SocketAddr AddrTarget( aHost.pData, nPort ); |
113 | 0 | if(pConn->m_socket.connect(AddrTarget) != osl_Socket_Ok) |
114 | 0 | { |
115 | 0 | OUString sMessage(u"Connector : couldn't connect to socket ("_ustr); |
116 | 0 | OUString sError = pConn->m_socket.getErrorAsString(); |
117 | 0 | sMessage += sError + ")"; |
118 | 0 | throw NoConnectException( sMessage ); |
119 | 0 | } |
120 | | // we enable tcpNoDelay for loopback connections because |
121 | | // it can make a significant speed difference on linux boxes. |
122 | 0 | if( bTcpNoDelay || aHost == "localhost" || aHost.startsWith("127.0.0.") ) |
123 | 0 | { |
124 | 0 | sal_Int32 nTcpNoDelay = sal_Int32(true); |
125 | 0 | pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay, |
126 | 0 | sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp ); |
127 | 0 | } |
128 | 0 | pConn->completeConnectionString(); |
129 | 0 | r = pConn; |
130 | 0 | } |
131 | 0 | else |
132 | 0 | { |
133 | 0 | OUString delegatee= "com.sun.star.connection.Connector." + aDesc.getName(); |
134 | |
|
135 | 0 | Reference<XConnector> xConnector( |
136 | 0 | _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY ); |
137 | |
|
138 | 0 | if(!xConnector.is()) |
139 | 0 | throw ConnectionSetupException("Connector: unknown delegatee " + delegatee); |
140 | | |
141 | 0 | sal_Int32 index = sConnectionDescription.indexOf(','); |
142 | |
|
143 | 0 | r = xConnector->connect(OUString(o3tl::trim(sConnectionDescription.subView(index + 1)))); |
144 | 0 | } |
145 | 0 | return r; |
146 | 0 | } |
147 | 0 | catch (const rtl::MalformedUriException & rEx) |
148 | 0 | { |
149 | 0 | throw ConnectionSetupException(rEx.getMessage()); |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | OUString OConnector::getImplementationName() |
154 | 0 | { |
155 | 0 | return u"com.sun.star.comp.io.Connector"_ustr; |
156 | 0 | } |
157 | | |
158 | | sal_Bool OConnector::supportsService(const OUString& ServiceName) |
159 | 0 | { |
160 | 0 | return cppu::supportsService(this, ServiceName); |
161 | 0 | } |
162 | | |
163 | | Sequence< OUString > OConnector::getSupportedServiceNames() |
164 | 0 | { |
165 | 0 | return { u"com.sun.star.connection.Connector"_ustr }; |
166 | 0 | } |
167 | | |
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&) |
171 | 0 | { |
172 | 0 | return cppu::acquire(new OConnector(context)); |
173 | 0 | } |
174 | | |
175 | | |
176 | | |
177 | | |
178 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |