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 .
21 #include "connector.hxx"
22 #include <rtl/ustrbuf.hxx>
25 using namespace ::osl
;
26 using namespace ::rtl
;
27 using namespace ::com::sun::star::uno
;
28 using namespace ::com::sun::star::io
;
29 using namespace ::com::sun::star::connection
;
32 namespace stoc_connector
{
34 void notifyListeners(SocketConnection
* pCon
, sal_Bool
* notified
, T t
)
36 XStreamListener_hash_set listeners
;
39 ::osl::MutexGuard
guard(pCon
->_mutex
);
43 listeners
= pCon
->_listeners
;
47 ::std::for_each(listeners
.begin(), listeners
.end(), t
);
51 static void callStarted(Reference
<XStreamListener
> xStreamListener
)
53 xStreamListener
->started();
59 callError(const Any
& any
);
61 void operator () (Reference
<XStreamListener
> xStreamListener
);
64 callError::callError(const Any
& aAny
)
69 void callError::operator () (Reference
<XStreamListener
> xStreamListener
)
71 xStreamListener
->error(any
);
74 static void callClosed(Reference
<XStreamListener
> xStreamListener
)
76 xStreamListener
->closed();
80 SocketConnection::SocketConnection( const OUString
&sConnectionDescription
) :
82 m_sDescription( sConnectionDescription
),
88 m_sDescription
+= ",uniqueValue=";
89 m_sDescription
+= OUString::valueOf(
90 sal::static_int_cast
< sal_Int64
>(
91 reinterpret_cast< sal_IntPtr
>(&m_socket
)),
95 SocketConnection::~SocketConnection()
99 void SocketConnection::completeConnectionString()
103 nPort
= m_socket
.getPeerPort();
105 OUStringBuffer
buf( 256 );
106 buf
.appendAscii( ",peerPort=" );
107 buf
.append( (sal_Int32
) nPort
);
108 buf
.appendAscii( ",peerHost=" );
109 buf
.append( m_socket
.getPeerHost() );
111 buf
.appendAscii( ",localPort=" );
112 buf
.append( (sal_Int32
) nPort
);
113 buf
.appendAscii( ",localHost=" );
114 buf
.append( m_socket
.getLocalHost( ) );
116 m_sDescription
+= buf
.makeStringAndClear();
119 sal_Int32
SocketConnection::read( Sequence
< sal_Int8
> & aReadBytes
, sal_Int32 nBytesToRead
)
120 throw(::com::sun::star::io::IOException
,
121 ::com::sun::star::uno::RuntimeException
)
125 notifyListeners(this, &_started
, callStarted
);
127 if( aReadBytes
.getLength() != nBytesToRead
)
129 aReadBytes
.realloc( nBytesToRead
);
131 sal_Int32 i
= m_socket
.read( aReadBytes
.getArray() , aReadBytes
.getLength() );
133 if(i
!= nBytesToRead
&& m_socket
.getError() != osl_Socket_E_None
)
135 OUString
message("ctr_socket.cxx:SocketConnection::read: error - ");
136 message
+= m_socket
.getErrorAsString();
138 IOException
ioException(message
, Reference
<XInterface
>(static_cast<XConnection
*>(this)));
143 notifyListeners(this, &_error
, callError(any
));
152 OUString
message("ctr_socket.cxx:SocketConnection::read: error - connection already closed");
154 IOException
ioException(message
, Reference
<XInterface
>(static_cast<XConnection
*>(this)));
159 notifyListeners(this, &_error
, callError(any
));
165 void SocketConnection::write( const Sequence
< sal_Int8
> &seq
)
166 throw(::com::sun::star::io::IOException
,
167 ::com::sun::star::uno::RuntimeException
)
171 if( m_socket
.write( seq
.getConstArray() , seq
.getLength() ) != seq
.getLength() )
173 OUString
message("ctr_socket.cxx:SocketConnection::write: error - ");
174 message
+= m_socket
.getErrorAsString();
176 IOException
ioException(message
, Reference
<XInterface
>(static_cast<XConnection
*>(this)));
181 notifyListeners(this, &_error
, callError(any
));
188 OUString
message("ctr_socket.cxx:SocketConnection::write: error - connection already closed");
190 IOException
ioException(message
, Reference
<XInterface
>(static_cast<XConnection
*>(this)));
195 notifyListeners(this, &_error
, callError(any
));
201 void SocketConnection::flush( )
202 throw(::com::sun::star::io::IOException
,
203 ::com::sun::star::uno::RuntimeException
)
208 void SocketConnection::close()
209 throw(::com::sun::star::io::IOException
,
210 ::com::sun::star::uno::RuntimeException
)
212 // ensure that close is called only once
213 if( 1 == osl_atomic_increment( (&m_nStatus
) ) )
216 notifyListeners(this, &_closed
, callClosed
);
220 OUString
SocketConnection::getDescription()
221 throw( ::com::sun::star::uno::RuntimeException
)
223 return m_sDescription
;
228 // XConnectionBroadcaster
229 void SAL_CALL
SocketConnection::addStreamListener(const Reference
<XStreamListener
> & aListener
) throw(RuntimeException
)
231 MutexGuard
guard(_mutex
);
233 _listeners
.insert(aListener
);
236 void SAL_CALL
SocketConnection::removeStreamListener(const Reference
<XStreamListener
> & aListener
) throw(RuntimeException
)
238 MutexGuard
guard(_mutex
);
240 _listeners
.erase(aListener
);
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */