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 <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"
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
;
45 class OAcceptor
: public WeakImplHelper
< XAcceptor
, XServiceInfo
>
48 explicit OAcceptor(const Reference
< XComponentContext
> & xCtx
);
49 virtual ~OAcceptor() override
;
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
;
61 std::unique_ptr
<io_acceptor::PipeAcceptor
> m_pPipe
;
62 std::unique_ptr
<io_acceptor::SocketAcceptor
> m_pSocket
;
64 OUString m_sLastDescription
;
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() )
80 OAcceptor::~OAcceptor()
88 /// @throws AlreadyAcceptingException
89 BeingInAccept( bool *pFlag
,std::u16string_view sConnectionDescription
)
93 throw AlreadyAcceptingException( OUString::Concat("AlreadyAcceptingException :") + sConnectionDescription
);
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" )
129 m_pPipe
.reset(new io_acceptor::PipeAcceptor(aName
, sConnectionDescription
));
138 std::unique_lock
g( m_mutex
);
144 else if ( aDesc
.getName() == "socket" )
147 if (aDesc
.hasParameter(
149 aHost
= aDesc
.getParameter(
153 sal_uInt16 nPort
= static_cast< sal_uInt16
>(
158 = aDesc
.getParameter(
159 u
"tcpnodelay"_ustr
).toInt32() != 0;
161 m_pSocket
.reset(new io_acceptor::SocketAcceptor(
162 aHost
, nPort
, bTcpNoDelay
, sConnectionDescription
));
171 std::unique_lock
g( m_mutex
);
179 OUString delegatee
= "com.sun.star.connection.Acceptor." + aDesc
.getName();
180 _xAcceptor
.set(_xSMgr
->createInstanceWithContext(delegatee
, _xCtx
), UNO_QUERY
);
183 throw ConnectionSetupException("Acceptor: unknown delegatee " + delegatee
);
186 catch (const rtl::MalformedUriException
& rEx
)
188 throw IllegalArgumentException(
190 Reference
< XInterface
> (),
193 m_sLastDescription
= sConnectionDescription
;
198 r
= m_pPipe
->accept();
202 r
= m_pSocket
->accept();
206 r
= _xAcceptor
->accept(sConnectionDescription
);
212 void SAL_CALL
OAcceptor::stopAccepting( )
214 std::unique_lock
guard( m_mutex
);
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: */