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 "acceptor.hxx"
22 #include <unordered_set>
25 #include <rtl/ref.hxx>
26 #include <com/sun/star/connection/XConnection.hpp>
27 #include <com/sun/star/connection/XConnectionBroadcaster.hpp>
28 #include <com/sun/star/connection/ConnectionSetupException.hpp>
29 #include <com/sun/star/io/IOException.hpp>
30 #include <cppuhelper/implbase.hxx>
33 using namespace ::osl
;
34 using namespace ::cppu
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::io
;
37 using namespace ::com::sun::star::connection
;
40 namespace io_acceptor
{
42 typedef std::unordered_set
< css::uno::Reference
< css::io::XStreamListener
> >
43 XStreamListener_hash_set
;
47 class SocketConnection
: public ::cppu::WeakImplHelper
<
48 css::connection::XConnection
,
49 css::connection::XConnectionBroadcaster
>
53 explicit SocketConnection( OUString sConnectionDescription
);
55 virtual sal_Int32 SAL_CALL
read( css::uno::Sequence
< sal_Int8
>& aReadBytes
,
56 sal_Int32 nBytesToRead
) override
;
57 virtual void SAL_CALL
write( const css::uno::Sequence
< sal_Int8
>& aData
) override
;
58 virtual void SAL_CALL
flush( ) override
;
59 virtual void SAL_CALL
close( ) override
;
60 virtual OUString SAL_CALL
getDescription( ) override
;
62 // XConnectionBroadcaster
63 virtual void SAL_CALL
addStreamListener(const css::uno::Reference
< css::io::XStreamListener
>& aListener
) override
;
64 virtual void SAL_CALL
removeStreamListener(const css::uno::Reference
< css::io::XStreamListener
>& aListener
) override
;
67 void completeConnectionString();
69 ::osl::StreamSocket m_socket
;
70 oslInterlockedCount m_nStatus
;
71 OUString m_sDescription
;
77 XStreamListener_hash_set _listeners
;
83 static void notifyListeners(SocketConnection
* pCon
, bool * notified
, T t
)
85 XStreamListener_hash_set listeners
;
88 std::unique_lock
guard(pCon
->_mutex
);
92 listeners
= pCon
->_listeners
;
96 for(auto& listener
: listeners
)
100 static void callStarted(const Reference
<XStreamListener
>& xStreamListener
)
102 xStreamListener
->started();
110 explicit callError(const Any
& any
);
112 void operator () (const Reference
<XStreamListener
>& xStreamListener
);
117 callError::callError(const Any
& aAny
)
122 void callError::operator () (const Reference
<XStreamListener
>& xStreamListener
)
124 xStreamListener
->error(any
);
127 static void callClosed(const Reference
<XStreamListener
>& xStreamListener
)
129 xStreamListener
->closed();
133 SocketConnection::SocketConnection( OUString sConnectionDescription
) :
135 m_sDescription(std::move( sConnectionDescription
)),
141 m_sDescription
+= ",uniqueValue=" ;
142 m_sDescription
+= OUString::number(
143 sal::static_int_cast
< sal_Int64
>(
144 reinterpret_cast< sal_IntPtr
>(&m_socket
)) );
147 void SocketConnection::completeConnectionString()
150 ",peerPort=" + OUString::number(m_socket
.getPeerPort()) +
151 ",peerHost=" + m_socket
.getPeerHost( ) +
152 ",localPort=" + OUString::number( m_socket
.getLocalPort() ) +
153 ",localHost=" + m_socket
.getLocalHost();
156 sal_Int32
SocketConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
160 notifyListeners(this, &_started
, callStarted
);
162 if( aReadBytes
.getLength() != nBytesToRead
)
164 aReadBytes
.realloc( nBytesToRead
);
167 sal_Int32 i
= m_socket
.read(
168 aReadBytes
.getArray(), aReadBytes
.getLength());
170 if(i
!= nBytesToRead
)
172 OUString message
= "acc_socket.cxx:SocketConnection::read: error - " +
173 m_socket
.getErrorAsString();
175 IOException
ioException(message
, static_cast<XConnection
*>(this));
180 notifyListeners(this, &_error
, callError(any
));
189 IOException
ioException("acc_socket.cxx:SocketConnection::read: error - connection already closed", static_cast<XConnection
*>(this));
194 notifyListeners(this, &_error
, callError(any
));
200 void SocketConnection::write( const Sequence
< sal_Int8
> &seq
)
204 if( m_socket
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
206 OUString message
= "acc_socket.cxx:SocketConnection::write: error - " +
207 m_socket
.getErrorAsString();
209 IOException
ioException(message
, static_cast<XConnection
*>(this));
214 notifyListeners(this, &_error
, callError(any
));
221 IOException
ioException("acc_socket.cxx:SocketConnection::write: error - connection already closed", static_cast<XConnection
*>(this));
226 notifyListeners(this, &_error
, callError(any
));
232 void SocketConnection::flush( )
237 void SocketConnection::close()
239 // ensure close is called only once
240 if( 1 == osl_atomic_increment( (&m_nStatus
) ) )
243 notifyListeners(this, &_closed
, callClosed
);
247 OUString
SocketConnection::getDescription()
249 return m_sDescription
;
253 // XConnectionBroadcaster
254 void SAL_CALL
SocketConnection::addStreamListener(const Reference
<XStreamListener
> & aListener
)
256 std::unique_lock
guard(_mutex
);
258 _listeners
.insert(aListener
);
261 void SAL_CALL
SocketConnection::removeStreamListener(const Reference
<XStreamListener
> & aListener
)
263 std::unique_lock
guard(_mutex
);
265 _listeners
.erase(aListener
);
268 SocketAcceptor::SocketAcceptor( OUString sSocketName
,
271 OUString sConnectionDescription
) :
272 m_sSocketName(std::move( sSocketName
)),
273 m_sConnectionDescription(std::move( sConnectionDescription
)),
275 m_bTcpNoDelay( bTcpNoDelay
),
281 void SocketAcceptor::init()
283 if( ! m_addr
.setPort( m_nPort
) )
285 throw ConnectionSetupException(
286 "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " +
287 OUString::number( m_nPort
));
289 if( ! m_addr
.setHostname( m_sSocketName
.pData
) )
291 throw ConnectionSetupException(
292 "acc_socket.cxx:SocketAcceptor::init - error - invalid host " + m_sSocketName
);
294 m_socket
.setOption( osl_Socket_OptionReuseAddr
, 1);
296 if(! m_socket
.bind(m_addr
) )
298 throw ConnectionSetupException(
299 "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " +
300 m_sSocketName
+ ":" + OUString::number(m_nPort
));
303 if(! m_socket
.listen() )
305 throw ConnectionSetupException(
306 "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " +
307 m_sSocketName
+ ":" + OUString::number(m_nPort
) );
311 Reference
< XConnection
> SocketAcceptor::accept( )
313 rtl::Reference
<SocketConnection
> pConn(new SocketConnection( m_sConnectionDescription
));
315 if( m_socket
.acceptConnection( pConn
->m_socket
)!= osl_Socket_Ok
)
317 // stopAccepting was called
318 return Reference
< XConnection
> ();
322 return Reference
< XConnection
> ();
325 pConn
->completeConnectionString();
326 ::osl::SocketAddr remoteAddr
;
327 pConn
->m_socket
.getPeerAddr(remoteAddr
);
328 OUString remoteHostname
= remoteAddr
.getHostname();
329 // we enable tcpNoDelay for loopback connections because
330 // it can make a significant speed difference on linux boxes.
331 if( m_bTcpNoDelay
|| remoteHostname
== "localhost" ||
332 remoteHostname
.startsWith("127.0.0.") )
334 sal_Int32 nTcpNoDelay
= sal_Int32(true);
335 pConn
->m_socket
.setOption( osl_Socket_OptionTcpNoDelay
, &nTcpNoDelay
,
336 sizeof( nTcpNoDelay
) , osl_Socket_LevelTcp
);
342 void SocketAcceptor::stopAccepting()
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */