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>
24 #include <osl/mutex.hxx>
25 #include <rtl/ustrbuf.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>
32 using namespace ::osl
;
33 using namespace ::cppu
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::io
;
36 using namespace ::com::sun::star::connection
;
39 namespace io_acceptor
{
41 typedef std::unordered_set
< css::uno::Reference
< css::io::XStreamListener
> >
42 XStreamListener_hash_set
;
45 class SocketConnection
: public ::cppu::WeakImplHelper
<
46 css::connection::XConnection
,
47 css::connection::XConnectionBroadcaster
>
51 explicit SocketConnection( const OUString
& sConnectionDescription
);
53 virtual sal_Int32 SAL_CALL
read( css::uno::Sequence
< sal_Int8
>& aReadBytes
,
54 sal_Int32 nBytesToRead
) override
;
55 virtual void SAL_CALL
write( const css::uno::Sequence
< sal_Int8
>& aData
) override
;
56 virtual void SAL_CALL
flush( ) override
;
57 virtual void SAL_CALL
close( ) override
;
58 virtual OUString SAL_CALL
getDescription( ) override
;
60 // XConnectionBroadcaster
61 virtual void SAL_CALL
addStreamListener(const css::uno::Reference
< css::io::XStreamListener
>& aListener
) override
;
62 virtual void SAL_CALL
removeStreamListener(const css::uno::Reference
< css::io::XStreamListener
>& aListener
) override
;
65 void completeConnectionString();
67 ::osl::StreamSocket m_socket
;
68 oslInterlockedCount m_nStatus
;
69 OUString m_sDescription
;
75 XStreamListener_hash_set _listeners
;
79 static void notifyListeners(SocketConnection
* pCon
, bool * notified
, T t
)
81 XStreamListener_hash_set listeners
;
84 ::osl::MutexGuard
guard(pCon
->_mutex
);
88 listeners
= pCon
->_listeners
;
92 for(auto& listener
: listeners
)
96 static void callStarted(const Reference
<XStreamListener
>& xStreamListener
)
98 xStreamListener
->started();
104 explicit callError(const Any
& any
);
106 void operator () (const Reference
<XStreamListener
>& xStreamListener
);
109 callError::callError(const Any
& aAny
)
114 void callError::operator () (const Reference
<XStreamListener
>& xStreamListener
)
116 xStreamListener
->error(any
);
119 static void callClosed(const Reference
<XStreamListener
>& xStreamListener
)
121 xStreamListener
->closed();
125 SocketConnection::SocketConnection( const OUString
&sConnectionDescription
) :
127 m_sDescription( sConnectionDescription
),
133 m_sDescription
+= ",uniqueValue=" ;
134 m_sDescription
+= OUString::number(
135 sal::static_int_cast
< sal_Int64
>(
136 reinterpret_cast< sal_IntPtr
>(&m_socket
)) );
139 void SocketConnection::completeConnectionString()
141 OUStringBuffer
buf( 256 );
142 buf
.append( ",peerPort=" );
143 buf
.append( m_socket
.getPeerPort() );
144 buf
.append( ",peerHost=" );
145 buf
.append( m_socket
.getPeerHost( ) );
147 buf
.append( ",localPort=" );
148 buf
.append( m_socket
.getLocalPort() );
149 buf
.append( ",localHost=" );
150 buf
.append( m_socket
.getLocalHost() );
152 m_sDescription
+= buf
;
155 sal_Int32
SocketConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
159 notifyListeners(this, &_started
, callStarted
);
161 if( aReadBytes
.getLength() != nBytesToRead
)
163 aReadBytes
.realloc( nBytesToRead
);
166 sal_Int32 i
= m_socket
.read(
167 aReadBytes
.getArray(), aReadBytes
.getLength());
169 if(i
!= nBytesToRead
)
171 OUString message
= "acc_socket.cxx:SocketConnection::read: error - " +
172 m_socket
.getErrorAsString();
174 IOException
ioException(message
, static_cast<XConnection
*>(this));
179 notifyListeners(this, &_error
, callError(any
));
188 IOException
ioException("acc_socket.cxx:SocketConnection::read: error - connection already closed", static_cast<XConnection
*>(this));
193 notifyListeners(this, &_error
, callError(any
));
199 void SocketConnection::write( const Sequence
< sal_Int8
> &seq
)
203 if( m_socket
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
205 OUString message
= "acc_socket.cxx:SocketConnection::write: error - " +
206 m_socket
.getErrorAsString();
208 IOException
ioException(message
, static_cast<XConnection
*>(this));
213 notifyListeners(this, &_error
, callError(any
));
220 IOException
ioException("acc_socket.cxx:SocketConnection::write: error - connection already closed", static_cast<XConnection
*>(this));
225 notifyListeners(this, &_error
, callError(any
));
231 void SocketConnection::flush( )
236 void SocketConnection::close()
238 // ensure close is called only once
239 if( 1 == osl_atomic_increment( (&m_nStatus
) ) )
242 notifyListeners(this, &_closed
, callClosed
);
246 OUString
SocketConnection::getDescription()
248 return m_sDescription
;
252 // XConnectionBroadcaster
253 void SAL_CALL
SocketConnection::addStreamListener(const Reference
<XStreamListener
> & aListener
)
255 MutexGuard
guard(_mutex
);
257 _listeners
.insert(aListener
);
260 void SAL_CALL
SocketConnection::removeStreamListener(const Reference
<XStreamListener
> & aListener
)
262 MutexGuard
guard(_mutex
);
264 _listeners
.erase(aListener
);
267 SocketAcceptor::SocketAcceptor( const OUString
&sSocketName
,
270 const OUString
&sConnectionDescription
) :
271 m_sSocketName( sSocketName
),
272 m_sConnectionDescription( sConnectionDescription
),
274 m_bTcpNoDelay( bTcpNoDelay
),
280 void SocketAcceptor::init()
282 if( ! m_addr
.setPort( m_nPort
) )
284 throw ConnectionSetupException(
285 "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " +
286 OUString::number( m_nPort
));
288 if( ! m_addr
.setHostname( m_sSocketName
.pData
) )
290 throw ConnectionSetupException(
291 "acc_socket.cxx:SocketAcceptor::init - error - invalid host " + m_sSocketName
);
293 m_socket
.setOption( osl_Socket_OptionReuseAddr
, 1);
295 if(! m_socket
.bind(m_addr
) )
297 throw ConnectionSetupException(
298 "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " +
299 m_sSocketName
+ ":" + OUString::number(m_nPort
));
302 if(! m_socket
.listen() )
304 throw ConnectionSetupException(
305 "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " +
306 m_sSocketName
+ ":" + OUString::number(m_nPort
) );
310 Reference
< XConnection
> SocketAcceptor::accept( )
312 std::unique_ptr
<SocketConnection
> pConn(new SocketConnection( m_sConnectionDescription
));
314 if( m_socket
.acceptConnection( pConn
->m_socket
)!= osl_Socket_Ok
)
316 // stopAccepting was called
317 return Reference
< XConnection
> ();
321 return Reference
< XConnection
> ();
324 pConn
->completeConnectionString();
325 ::osl::SocketAddr remoteAddr
;
326 pConn
->m_socket
.getPeerAddr(remoteAddr
);
327 OUString remoteHostname
= remoteAddr
.getHostname();
328 // we enable tcpNoDelay for loopback connections because
329 // it can make a significant speed difference on linux boxes.
330 if( m_bTcpNoDelay
|| remoteHostname
== "localhost" ||
331 remoteHostname
.startsWith("127.0.0.") )
333 sal_Int32 nTcpNoDelay
= sal_Int32(true);
334 pConn
->m_socket
.setOption( osl_Socket_OptionTcpNoDelay
, &nTcpNoDelay
,
335 sizeof( nTcpNoDelay
) , osl_Socket_LevelTcp
);
338 return Reference
< XConnection
> ( static_cast<XConnection
*>(pConn
.release()) );
341 void SocketAcceptor::stopAccepting()
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */