22#include <unordered_set>
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>
34using namespace ::
cppu;
42 typedef std::unordered_set< css::uno::Reference< css::io::XStreamListener> >
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;
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();
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) :
143 sal::static_int_cast< sal_Int64 >(
144 reinterpret_cast< sal_IntPtr
>(&
m_socket)) );
150 ",peerPort=" + OUString::number(
m_socket.getPeerPort()) +
151 ",peerHost=" +
m_socket.getPeerHost( ) +
152 ",localPort=" + OUString::number(
m_socket.getLocalPort() ) +
153 ",localHost=" +
m_socket.getLocalHost();
162 if( aReadBytes.getLength() != nBytesToRead )
164 aReadBytes.realloc( nBytesToRead );
168 aReadBytes.getArray(), aReadBytes.getLength());
170 if(
i != nBytesToRead)
172 OUString message =
"acc_socket.cxx:SocketConnection::read: error - " +
175 IOException ioException(message,
static_cast<XConnection *
>(
this));
189 IOException ioException(
"acc_socket.cxx:SocketConnection::read: error - connection already closed",
static_cast<XConnection *
>(
this));
204 if(
m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
206 OUString message =
"acc_socket.cxx:SocketConnection::write: error - " +
209 IOException ioException(message,
static_cast<XConnection *
>(
this));
221 IOException ioException(
"acc_socket.cxx:SocketConnection::write: error - connection already closed",
static_cast<XConnection *
>(
this));
240 if( 1 == osl_atomic_increment( (&
m_nStatus) ) )
256 std::unique_lock guard(
_mutex);
263 std::unique_lock guard(
_mutex);
268 SocketAcceptor::SocketAcceptor( OUString sSocketName,
271 OUString sConnectionDescription) :
272 m_sSocketName(
std::move( sSocketName )),
273 m_sConnectionDescription(
std::move( sConnectionDescription )),
275 m_bTcpNoDelay( bTcpNoDelay ),
285 throw ConnectionSetupException(
286 "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " +
291 throw ConnectionSetupException(
292 "acc_socket.cxx:SocketAcceptor::init - error - invalid host " +
m_sSocketName );
294 m_socket.setOption( osl_Socket_OptionReuseAddr, 1);
298 throw ConnectionSetupException(
299 "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " +
305 throw ConnectionSetupException(
306 "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " +
315 if(
m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok )
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();
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 );
oslInterlockedCount m_nStatus
::osl::StreamSocket m_socket
XStreamListener_hash_set _listeners
::osl::AcceptorSocket m_socket
css::uno::Reference< css::connection::XConnection > accept()
OUString m_sConnectionDescription
void completeConnectionString()
virtual void SAL_CALL close() override
virtual OUString SAL_CALL getDescription() override
XStreamListener_hash_set _listeners
::osl::ConnectorSocket m_socket
oslInterlockedCount m_nStatus
virtual void SAL_CALL write(const css::uno::Sequence< sal_Int8 > &aData) override
virtual void SAL_CALL removeStreamListener(const css::uno::Reference< css::io::XStreamListener > &aListener) override
virtual void SAL_CALL flush() override
virtual void SAL_CALL addStreamListener(const css::uno::Reference< css::io::XStreamListener > &aListener) override
virtual sal_Int32 SAL_CALL read(css::uno::Sequence< sal_Int8 > &aReadBytes, sal_Int32 nBytesToRead) override
constexpr OUStringLiteral aData
static void callClosed(const Reference< XStreamListener > &xStreamListener)
static void callStarted(const Reference< XStreamListener > &xStreamListener)
std::unordered_set< css::uno::Reference< css::io::XStreamListener > > XStreamListener_hash_set
static void notifyListeners(SocketConnection *pCon, bool *notified, T t)
static void callClosed(const Reference< XStreamListener > &xStreamListener)
static void notifyListeners(SocketConnection *pCon, bool *notified, T t)
static void callStarted(const Reference< XStreamListener > &xStreamListener)