LibreOffice Module io (master) 1
acceptor.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
22#include <cppuhelper/unourl.hxx>
23#include <rtl/malformeduriexception.hxx>
24
25#include <com/sun/star/connection/AlreadyAcceptingException.hpp>
26#include <com/sun/star/connection/ConnectionSetupException.hpp>
27#include <com/sun/star/connection/XAcceptor.hpp>
28#include <com/sun/star/lang/IllegalArgumentException.hpp>
29#include <com/sun/star/lang/XServiceInfo.hpp>
30#include <com/sun/star/uno/XComponentContext.hpp>
31
32#include "acceptor.hxx"
33#include <memory>
34#include <mutex>
35#include <string_view>
36
37using namespace ::osl;
38using namespace ::cppu;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::lang;
41using namespace ::com::sun::star::connection;
42
43namespace {
44
45 class OAcceptor : public WeakImplHelper< XAcceptor, XServiceInfo >
46 {
47 public:
48 explicit OAcceptor(const Reference< XComponentContext > & xCtx);
49 virtual ~OAcceptor() override;
50 public:
51 // Methods
52 virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription ) override;
53 virtual void SAL_CALL stopAccepting( ) override;
54
55 public: // XServiceInfo
56 virtual OUString SAL_CALL getImplementationName() override;
57 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
58 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
59
60 private:
61 std::unique_ptr<io_acceptor::PipeAcceptor> m_pPipe;
62 std::unique_ptr<io_acceptor::SocketAcceptor> m_pSocket;
63 std::mutex m_mutex;
64 OUString m_sLastDescription;
65 bool m_bInAccept;
66
67 Reference< XMultiComponentFactory > _xSMgr;
68 Reference< XComponentContext > _xCtx;
69 Reference<XAcceptor> _xAcceptor;
70 };
71
72}
73
74OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
75 : m_bInAccept( false )
76 , _xSMgr( xCtx->getServiceManager() )
77 , _xCtx( xCtx )
78{}
79
80OAcceptor::~OAcceptor()
81{
82 m_pPipe.reset();
83}
84
85namespace {
86struct BeingInAccept
87{
89 BeingInAccept( bool *pFlag,std::u16string_view sConnectionDescription )
90 : m_pFlag( pFlag )
91 {
92 if( *m_pFlag )
93 throw AlreadyAcceptingException( OUString::Concat("AlreadyAcceptingException :") + sConnectionDescription );
94 *m_pFlag = true;
95 }
96 ~BeingInAccept()
97 {
98 *m_pFlag = false;
99 }
100 bool *m_pFlag;
101};
102}
103
104Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
105{
106 // if there is a thread already accepting in this object, throw an exception.
107 struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
108
109 Reference< XConnection > r;
110 if( !m_sLastDescription.isEmpty() &&
111 m_sLastDescription != sConnectionDescription )
112 {
113 // instantiate another acceptor for different ports
114 throw ConnectionSetupException( "acceptor::accept called multiple times with different connection strings\n" );
115 }
116
117 if( m_sLastDescription.isEmpty() )
118 {
119 // setup the acceptor
120 try
121 {
122 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
123 if ( aDesc.getName() == "pipe" )
124 {
125 OUString aName(
126 aDesc.getParameter(
127 "name"));
128
129 m_pPipe.reset(new io_acceptor::PipeAcceptor(aName, sConnectionDescription));
130
131 try
132 {
133 m_pPipe->init();
134 }
135 catch( ... )
136 {
137 {
138 std::unique_lock g( m_mutex );
139 m_pPipe.reset();
140 }
141 throw;
142 }
143 }
144 else if ( aDesc.getName() == "socket" )
145 {
146 OUString aHost;
147 if (aDesc.hasParameter(
148 "host"))
149 aHost = aDesc.getParameter(
150 "host");
151 else
152 aHost = "localhost";
153 sal_uInt16 nPort = static_cast< sal_uInt16 >(
154 aDesc.getParameter(
155 "port").
156 toInt32());
157 bool bTcpNoDelay
158 = aDesc.getParameter(
159 "tcpnodelay").toInt32() != 0;
160
161 m_pSocket.reset(new io_acceptor::SocketAcceptor(
162 aHost, nPort, bTcpNoDelay, sConnectionDescription));
163
164 try
165 {
166 m_pSocket->init();
167 }
168 catch( ... )
169 {
170 {
171 std::unique_lock g( m_mutex );
172 m_pSocket.reset();
173 }
174 throw;
175 }
176 }
177 else
178 {
179 OUString delegatee = "com.sun.star.connection.Acceptor." + aDesc.getName();
180 _xAcceptor.set(_xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
181
182 if(!_xAcceptor.is())
183 throw ConnectionSetupException("Acceptor: unknown delegatee " + delegatee);
184 }
185 }
186 catch (const rtl::MalformedUriException & rEx)
187 {
188 throw IllegalArgumentException(
189 rEx.getMessage(),
190 Reference< XInterface > (),
191 0 );
192 }
193 m_sLastDescription = sConnectionDescription;
194 }
195
196 if( m_pPipe )
197 {
198 r = m_pPipe->accept();
199 }
200 else if( m_pSocket )
201 {
202 r = m_pSocket->accept();
203 }
204 else
205 {
206 r = _xAcceptor->accept(sConnectionDescription);
207 }
208
209 return r;
210}
211
212void SAL_CALL OAcceptor::stopAccepting( )
213{
214 std::unique_lock guard( m_mutex );
215
216 if( m_pPipe )
217 {
218 m_pPipe->stopAccepting();
219 }
220 else if ( m_pSocket )
221 {
222 m_pSocket->stopAccepting();
223 }
224 else if( _xAcceptor.is() )
225 {
226 _xAcceptor->stopAccepting();
227 }
228
229}
230
231OUString OAcceptor::getImplementationName()
232{
233 return "com.sun.star.comp.io.Acceptor";
234}
235
236sal_Bool OAcceptor::supportsService(const OUString& ServiceName)
237{
238 return cppu::supportsService(this, ServiceName);
239}
240
241Sequence< OUString > OAcceptor::getSupportedServiceNames()
242{
243 return { "com.sun.star.connection.Acceptor" };
244}
245
246extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
248 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
249{
250 return cppu::acquire(new OAcceptor(context));
251}
252
253/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * io_OAcceptor_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: acceptor.cxx:247
OUString aName
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
sal_Int32 toInt32(std::u16string_view rStr)
std::mutex m_mutex
Definition: omark.cxx:122
unsigned char sal_Bool
Reference< XMultiComponentFactory > _xSMgr
Reference< XComponentContext > _xCtx