use insert function instead of for loop
[LibreOffice.git] / io / source / acceptor / acceptor.cxx
blob96e23da356310ce26d7d5126e34466f45413c351
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <cppuhelper/implbase.hxx>
21 #include <cppuhelper/supportsservice.hxx>
22 #include <cppuhelper/unourl.hxx>
23 #include <rtl/malformeduriexception.hxx>
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>
32 #include "acceptor.hxx"
33 #include <memory>
34 #include <mutex>
35 #include <string_view>
37 using namespace ::osl;
38 using namespace ::cppu;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::connection;
43 namespace {
45 class OAcceptor : public WeakImplHelper< XAcceptor, XServiceInfo >
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;
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;
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;
67 Reference< XMultiComponentFactory > _xSMgr;
68 Reference< XComponentContext > _xCtx;
69 Reference<XAcceptor> _xAcceptor;
74 OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
75 : m_bInAccept( false )
76 , _xSMgr( xCtx->getServiceManager() )
77 , _xCtx( xCtx )
80 OAcceptor::~OAcceptor()
82 m_pPipe.reset();
85 namespace {
86 struct BeingInAccept
88 /// @throws AlreadyAcceptingException
89 BeingInAccept( bool *pFlag,std::u16string_view sConnectionDescription )
90 : m_pFlag( pFlag )
92 if( *m_pFlag )
93 throw AlreadyAcceptingException( OUString::Concat("AlreadyAcceptingException :") + sConnectionDescription );
94 *m_pFlag = true;
96 ~BeingInAccept()
98 *m_pFlag = false;
100 bool *m_pFlag;
104 Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
106 // if there is a thread already accepting in this object, throw an exception.
107 struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
109 Reference< XConnection > r;
110 if( !m_sLastDescription.isEmpty() &&
111 m_sLastDescription != sConnectionDescription )
113 // instantiate another acceptor for different ports
114 throw ConnectionSetupException( u"acceptor::accept called multiple times with different connection strings\n"_ustr );
117 if( m_sLastDescription.isEmpty() )
119 // setup the acceptor
122 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
123 if ( aDesc.getName() == "pipe" )
125 OUString aName(
126 aDesc.getParameter(
127 u"name"_ustr));
129 m_pPipe.reset(new io_acceptor::PipeAcceptor(aName, sConnectionDescription));
133 m_pPipe->init();
135 catch( ... )
138 std::unique_lock g( m_mutex );
139 m_pPipe.reset();
141 throw;
144 else if ( aDesc.getName() == "socket" )
146 OUString aHost;
147 if (aDesc.hasParameter(
148 u"host"_ustr))
149 aHost = aDesc.getParameter(
150 u"host"_ustr);
151 else
152 aHost = "localhost";
153 sal_uInt16 nPort = static_cast< sal_uInt16 >(
154 aDesc.getParameter(
155 u"port"_ustr).
156 toInt32());
157 bool bTcpNoDelay
158 = aDesc.getParameter(
159 u"tcpnodelay"_ustr).toInt32() != 0;
161 m_pSocket.reset(new io_acceptor::SocketAcceptor(
162 aHost, nPort, bTcpNoDelay, sConnectionDescription));
166 m_pSocket->init();
168 catch( ... )
171 std::unique_lock g( m_mutex );
172 m_pSocket.reset();
174 throw;
177 else
179 OUString delegatee = "com.sun.star.connection.Acceptor." + aDesc.getName();
180 _xAcceptor.set(_xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
182 if(!_xAcceptor.is())
183 throw ConnectionSetupException("Acceptor: unknown delegatee " + delegatee);
186 catch (const rtl::MalformedUriException & rEx)
188 throw IllegalArgumentException(
189 rEx.getMessage(),
190 Reference< XInterface > (),
191 0 );
193 m_sLastDescription = sConnectionDescription;
196 if( m_pPipe )
198 r = m_pPipe->accept();
200 else if( m_pSocket )
202 r = m_pSocket->accept();
204 else
206 r = _xAcceptor->accept(sConnectionDescription);
209 return r;
212 void SAL_CALL OAcceptor::stopAccepting( )
214 std::unique_lock guard( m_mutex );
216 if( m_pPipe )
218 m_pPipe->stopAccepting();
220 else if ( m_pSocket )
222 m_pSocket->stopAccepting();
224 else if( _xAcceptor.is() )
226 _xAcceptor->stopAccepting();
231 OUString OAcceptor::getImplementationName()
233 return u"com.sun.star.comp.io.Acceptor"_ustr;
236 sal_Bool OAcceptor::supportsService(const OUString& ServiceName)
238 return cppu::supportsService(this, ServiceName);
241 Sequence< OUString > OAcceptor::getSupportedServiceNames()
243 return { u"com.sun.star.connection.Acceptor"_ustr };
246 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
247 io_OAcceptor_get_implementation(
248 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
250 return cppu::acquire(new OAcceptor(context));
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */