Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "acceptor.hxx"
21 : :
22 : : #include <boost/unordered_set.hpp>
23 : : #include <algorithm>
24 : :
25 : : #include <rtl/ustrbuf.hxx>
26 : : #include <com/sun/star/connection/XConnectionBroadcaster.hpp>
27 : : #include <com/sun/star/connection/ConnectionSetupException.hpp>
28 : :
29 : : #include <cppuhelper/implbase2.hxx>
30 : :
31 : : using namespace ::osl;
32 : : using namespace ::rtl;
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;
37 : :
38 : :
39 : : namespace io_acceptor {
40 : : template<class T>
41 : : struct ReferenceHash
42 : : {
43 : 0 : size_t operator () (const ::com::sun::star::uno::Reference<T> & ref) const
44 : : {
45 : 0 : return (size_t)ref.get();
46 : : }
47 : : };
48 : :
49 : : template<class T>
50 : : struct ReferenceEqual
51 : : {
52 : 0 : sal_Bool operator () (const ::com::sun::star::uno::Reference<T> & op1,
53 : : const ::com::sun::star::uno::Reference<T> & op2) const
54 : : {
55 : 0 : return op1.get() == op2.get();
56 : : }
57 : : };
58 : :
59 : :
60 : : typedef ::boost::unordered_set< ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>,
61 : : ReferenceHash< ::com::sun::star::io::XStreamListener>,
62 : : ReferenceEqual< ::com::sun::star::io::XStreamListener> >
63 : : XStreamListener_hash_set;
64 : :
65 : :
66 : : class SocketConnection : public ::cppu::WeakImplHelper2<
67 : : ::com::sun::star::connection::XConnection,
68 : : ::com::sun::star::connection::XConnectionBroadcaster>
69 : :
70 : : {
71 : : public:
72 : : SocketConnection( const OUString & sConnectionDescription );
73 : : ~SocketConnection();
74 : :
75 : : virtual sal_Int32 SAL_CALL read( ::com::sun::star::uno::Sequence< sal_Int8 >& aReadBytes,
76 : : sal_Int32 nBytesToRead )
77 : : throw(::com::sun::star::io::IOException,
78 : : ::com::sun::star::uno::RuntimeException);
79 : : virtual void SAL_CALL write( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData )
80 : : throw(::com::sun::star::io::IOException,
81 : : ::com::sun::star::uno::RuntimeException);
82 : : virtual void SAL_CALL flush( ) throw(
83 : : ::com::sun::star::io::IOException,
84 : : ::com::sun::star::uno::RuntimeException);
85 : : virtual void SAL_CALL close( )
86 : : throw(::com::sun::star::io::IOException,
87 : : ::com::sun::star::uno::RuntimeException);
88 : : virtual ::rtl::OUString SAL_CALL getDescription( )
89 : : throw(::com::sun::star::uno::RuntimeException);
90 : :
91 : : // XConnectionBroadcaster
92 : : virtual void SAL_CALL addStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
93 : : throw(::com::sun::star::uno::RuntimeException);
94 : : virtual void SAL_CALL removeStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
95 : : throw(::com::sun::star::uno::RuntimeException);
96 : :
97 : : public:
98 : : void completeConnectionString();
99 : :
100 : : ::osl::StreamSocket m_socket;
101 : : ::osl::SocketAddr m_addr;
102 : : oslInterlockedCount m_nStatus;
103 : : ::rtl::OUString m_sDescription;
104 : :
105 : : ::osl::Mutex _mutex;
106 : : sal_Bool _started;
107 : : sal_Bool _closed;
108 : : sal_Bool _error;
109 : : XStreamListener_hash_set _listeners;
110 : : };
111 : :
112 : : template<class T>
113 : 0 : void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
114 : : {
115 [ # # ][ # # ]: 0 : XStreamListener_hash_set listeners;
116 : :
117 : : {
118 [ # # # # ]: 0 : ::osl::MutexGuard guard(pCon->_mutex);
119 [ # # ][ # # ]: 0 : if(!*notified)
120 : : {
121 : 0 : *notified = sal_True;
122 [ # # ][ # # ]: 0 : listeners = pCon->_listeners;
[ # # ][ # # ]
123 : : }
124 : : }
125 : :
126 [ # # ][ # # ]: 0 : ::std::for_each(listeners.begin(), listeners.end(), t);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
127 : 0 : }
128 : :
129 : 0 : static void callStarted(Reference<XStreamListener> xStreamListener)
130 : : {
131 : 0 : xStreamListener->started();
132 : 0 : }
133 : :
134 : : struct callError {
135 : : const Any & any;
136 : :
137 : : callError(const Any & any);
138 : :
139 : : void operator () (Reference<XStreamListener> xStreamListener);
140 : : };
141 : :
142 : 0 : callError::callError(const Any & aAny)
143 : 0 : : any(aAny)
144 : : {
145 : 0 : }
146 : :
147 : 0 : void callError::operator () (Reference<XStreamListener> xStreamListener)
148 : : {
149 : 0 : xStreamListener->error(any);
150 : 0 : }
151 : :
152 : 0 : static void callClosed(Reference<XStreamListener> xStreamListener)
153 : : {
154 : 0 : xStreamListener->closed();
155 : 0 : }
156 : :
157 : :
158 : 0 : SocketConnection::SocketConnection( const OUString &sConnectionDescription) :
159 : : m_nStatus( 0 ),
160 : : m_sDescription( sConnectionDescription ),
161 : : _started(sal_False),
162 : : _closed(sal_False),
163 [ # # ][ # # ]: 0 : _error(sal_False)
[ # # ][ # # ]
164 : : {
165 [ # # ]: 0 : g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
166 : : // make it unique
167 [ # # ]: 0 : m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) );
168 : : m_sDescription += OUString::valueOf(
169 : : sal::static_int_cast< sal_Int64 >(
170 : : reinterpret_cast< sal_IntPtr >(&m_socket)),
171 : 0 : 10 );
172 : 0 : }
173 : :
174 [ # # ][ # # ]: 0 : SocketConnection::~SocketConnection()
[ # # ][ # # ]
175 : : {
176 [ # # ]: 0 : g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
177 [ # # ]: 0 : }
178 : :
179 : 0 : void SocketConnection::completeConnectionString()
180 : : {
181 : 0 : OUStringBuffer buf( 256 );
182 [ # # ]: 0 : buf.appendAscii( ",peerPort=" );
183 [ # # ][ # # ]: 0 : buf.append( (sal_Int32) m_socket.getPeerPort() );
184 [ # # ]: 0 : buf.appendAscii( ",peerHost=" );
185 [ # # ][ # # ]: 0 : buf.append( m_socket.getPeerHost( ) );
186 : :
187 [ # # ]: 0 : buf.appendAscii( ",localPort=" );
188 [ # # ][ # # ]: 0 : buf.append( (sal_Int32) m_socket.getLocalPort() );
189 [ # # ]: 0 : buf.appendAscii( ",localHost=" );
190 [ # # ][ # # ]: 0 : buf.append( m_socket.getLocalHost() );
191 : :
192 [ # # ]: 0 : m_sDescription += buf.makeStringAndClear();
193 : 0 : }
194 : :
195 : 0 : sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
196 : : throw(::com::sun::star::io::IOException,
197 : : ::com::sun::star::uno::RuntimeException)
198 : : {
199 [ # # ]: 0 : if( ! m_nStatus )
200 : : {
201 : 0 : notifyListeners(this, &_started, callStarted);
202 : :
203 [ # # ]: 0 : if( aReadBytes.getLength() != nBytesToRead )
204 : : {
205 : 0 : aReadBytes.realloc( nBytesToRead );
206 : : }
207 : :
208 : 0 : sal_Int32 i = 0;
209 : 0 : i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() );
210 : :
211 [ # # ]: 0 : if(i != nBytesToRead)
212 : : {
213 [ # # ]: 0 : OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - "));
214 [ # # ]: 0 : message += m_socket.getErrorAsString();
215 : :
216 [ # # ][ # # ]: 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
217 : :
218 : 0 : Any any;
219 [ # # ]: 0 : any <<= ioException;
220 : :
221 [ # # ]: 0 : notifyListeners(this, &_error, callError(any));
222 : :
223 [ # # ]: 0 : throw ioException;
224 : : }
225 : :
226 : 0 : return i;
227 : : }
228 : : else
229 : : {
230 [ # # ]: 0 : OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - connection already closed"));
231 : :
232 [ # # ][ # # ]: 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
233 : :
234 : 0 : Any any;
235 [ # # ]: 0 : any <<= ioException;
236 : :
237 [ # # ]: 0 : notifyListeners(this, &_error, callError(any));
238 : :
239 [ # # ]: 0 : throw ioException;
240 : : }
241 : : }
242 : :
243 : 0 : void SocketConnection::write( const Sequence < sal_Int8 > &seq )
244 : : throw(::com::sun::star::io::IOException,
245 : : ::com::sun::star::uno::RuntimeException)
246 : : {
247 [ # # ]: 0 : if( ! m_nStatus )
248 : : {
249 [ # # ]: 0 : if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
250 : : {
251 [ # # ]: 0 : OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - "));
252 [ # # ]: 0 : message += m_socket.getErrorAsString();
253 : :
254 [ # # ][ # # ]: 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
255 : :
256 : 0 : Any any;
257 [ # # ]: 0 : any <<= ioException;
258 : :
259 [ # # ]: 0 : notifyListeners(this, &_error, callError(any));
260 : :
261 [ # # ]: 0 : throw ioException;
262 : : }
263 : : }
264 : : else
265 : : {
266 [ # # ]: 0 : OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - connection already closed"));
267 : :
268 [ # # ][ # # ]: 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
269 : :
270 : 0 : Any any;
271 [ # # ]: 0 : any <<= ioException;
272 : :
273 [ # # ]: 0 : notifyListeners(this, &_error, callError(any));
274 : :
275 [ # # ]: 0 : throw ioException;
276 : : }
277 : 0 : }
278 : :
279 : 0 : void SocketConnection::flush( )
280 : : throw(::com::sun::star::io::IOException,
281 : : ::com::sun::star::uno::RuntimeException)
282 : : {
283 : :
284 : 0 : }
285 : :
286 : 0 : void SocketConnection::close()
287 : : throw(::com::sun::star::io::IOException,
288 : : ::com::sun::star::uno::RuntimeException)
289 : : {
290 : : // enshure close is called only once
291 [ # # ]: 0 : if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
292 : : {
293 : 0 : m_socket.shutdown();
294 : 0 : notifyListeners(this, &_closed, callClosed);
295 : : }
296 : 0 : }
297 : :
298 : 0 : OUString SocketConnection::getDescription()
299 : : throw( ::com::sun::star::uno::RuntimeException)
300 : : {
301 : 0 : return m_sDescription;
302 : : }
303 : :
304 : :
305 : : // XConnectionBroadcaster
306 : 0 : void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
307 : : {
308 [ # # ]: 0 : MutexGuard guard(_mutex);
309 : :
310 [ # # ][ # # ]: 0 : _listeners.insert(aListener);
311 : 0 : }
312 : :
313 : 0 : void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
314 : : {
315 [ # # ]: 0 : MutexGuard guard(_mutex);
316 : :
317 [ # # ][ # # ]: 0 : _listeners.erase(aListener);
318 : 0 : }
319 : :
320 : 0 : SocketAcceptor::SocketAcceptor( const OUString &sSocketName,
321 : : sal_uInt16 nPort,
322 : : sal_Bool bTcpNoDelay,
323 : : const OUString &sConnectionDescription) :
324 : : m_sSocketName( sSocketName ),
325 : : m_sConnectionDescription( sConnectionDescription ),
326 : : m_nPort( nPort ),
327 : : m_bTcpNoDelay( bTcpNoDelay ),
328 [ # # ]: 0 : m_bClosed( sal_False )
329 : : {
330 : 0 : }
331 : :
332 : :
333 : 0 : void SocketAcceptor::init()
334 : : {
335 [ # # ]: 0 : if( ! m_addr.setPort( m_nPort ) )
336 : : {
337 : 0 : OUStringBuffer message( 128 );
338 [ # # ]: 0 : message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " );
339 [ # # ]: 0 : message.append( (sal_Int32) m_nPort );
340 : : throw ConnectionSetupException(
341 [ # # ][ # # ]: 0 : message.makeStringAndClear() , Reference< XInterface> () );
342 : : }
343 [ # # ][ # # ]: 0 : if( ! m_addr.setHostname( m_sSocketName.pData ) )
344 : : {
345 : 0 : OUStringBuffer message( 128 );
346 [ # # ]: 0 : message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid host " );
347 [ # # ]: 0 : message.append( m_sSocketName );
348 : : throw ConnectionSetupException(
349 [ # # ][ # # ]: 0 : message.makeStringAndClear(), Reference< XInterface > () );
350 : : }
351 : 0 : m_socket.setOption( osl_Socket_OptionReuseAddr, 1);
352 : :
353 [ # # ]: 0 : if(! m_socket.bind(m_addr) )
354 : : {
355 : 0 : OUStringBuffer message( 128 );
356 [ # # ]: 0 : message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " );
357 [ # # ][ # # ]: 0 : message.append( m_sSocketName ).appendAscii( ":" ).append((sal_Int32)m_nPort);
[ # # ]
358 : : throw ConnectionSetupException(
359 : : message.makeStringAndClear(),
360 [ # # ][ # # ]: 0 : Reference<XInterface>());
361 : : }
362 : :
363 [ # # ]: 0 : if(! m_socket.listen() )
364 : : {
365 : 0 : OUStringBuffer message( 128 );
366 [ # # ]: 0 : message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " );
367 [ # # ][ # # ]: 0 : message.append( m_sSocketName ).appendAscii( ":" ).append( (sal_Int32) m_nPort);
[ # # ]
368 [ # # ][ # # ]: 0 : throw ConnectionSetupException( message.makeStringAndClear(),Reference<XInterface>() );
369 : : }
370 : 0 : }
371 : :
372 : 0 : Reference< XConnection > SocketAcceptor::accept( )
373 : : {
374 [ # # ]: 0 : SocketConnection *pConn = new SocketConnection( m_sConnectionDescription );
375 : :
376 [ # # ]: 0 : if( m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok )
377 : : {
378 : : // stopAccepting was called
379 [ # # ]: 0 : delete pConn;
380 : 0 : return Reference < XConnection > ();
381 : : }
382 [ # # ]: 0 : if( m_bClosed )
383 : : {
384 [ # # ]: 0 : delete pConn;
385 : 0 : return Reference < XConnection > ();
386 : : }
387 : :
388 : 0 : pConn->completeConnectionString();
389 [ # # ]: 0 : if( m_bTcpNoDelay )
390 : : {
391 : 0 : sal_Int32 nTcpNoDelay = sal_True;
392 : : pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
393 [ # # ]: 0 : sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
394 : : }
395 : :
396 [ # # ]: 0 : return Reference < XConnection > ( (XConnection * ) pConn );
397 : : }
398 : :
399 : 0 : void SocketAcceptor::stopAccepting()
400 : : {
401 : 0 : m_bClosed = sal_True;
402 : 0 : m_socket.close();
403 : 0 : }
404 : : }
405 : :
406 : :
407 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|