xref: /trunk/main/io/source/acceptor/acc_socket.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_io.hxx"
30*cdf0e10cSrcweir #include "acceptor.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <hash_set>
33*cdf0e10cSrcweir #include <algorithm>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36*cdf0e10cSrcweir #include <com/sun/star/connection/XConnectionBroadcaster.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/connection/ConnectionSetupException.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::osl;
42*cdf0e10cSrcweir using namespace ::rtl;
43*cdf0e10cSrcweir using namespace ::cppu;
44*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
45*cdf0e10cSrcweir using namespace ::com::sun::star::io;
46*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace io_acceptor {
50*cdf0e10cSrcweir     template<class T>
51*cdf0e10cSrcweir     struct ReferenceHash
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         size_t operator () (const ::com::sun::star::uno::Reference<T> & ref) const
54*cdf0e10cSrcweir         {
55*cdf0e10cSrcweir             return (size_t)ref.get();
56*cdf0e10cSrcweir         }
57*cdf0e10cSrcweir     };
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     template<class T>
60*cdf0e10cSrcweir     struct ReferenceEqual
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         sal_Bool operator () (const ::com::sun::star::uno::Reference<T> & op1,
63*cdf0e10cSrcweir                               const ::com::sun::star::uno::Reference<T> & op2) const
64*cdf0e10cSrcweir         {
65*cdf0e10cSrcweir             return op1.get() == op2.get();
66*cdf0e10cSrcweir         }
67*cdf0e10cSrcweir     };
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     typedef ::std::hash_set< ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>,
71*cdf0e10cSrcweir                              ReferenceHash< ::com::sun::star::io::XStreamListener>,
72*cdf0e10cSrcweir                              ReferenceEqual< ::com::sun::star::io::XStreamListener> >
73*cdf0e10cSrcweir             XStreamListener_hash_set;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     class SocketConnection : public ::cppu::WeakImplHelper2<
77*cdf0e10cSrcweir         ::com::sun::star::connection::XConnection,
78*cdf0e10cSrcweir         ::com::sun::star::connection::XConnectionBroadcaster>
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir     public:
82*cdf0e10cSrcweir         SocketConnection( const OUString & sConnectionDescription );
83*cdf0e10cSrcweir         ~SocketConnection();
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         virtual sal_Int32 SAL_CALL read( ::com::sun::star::uno::Sequence< sal_Int8 >& aReadBytes,
86*cdf0e10cSrcweir                                          sal_Int32 nBytesToRead )
87*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
88*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException);
89*cdf0e10cSrcweir         virtual void SAL_CALL write( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData )
90*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
91*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException);
92*cdf0e10cSrcweir         virtual void SAL_CALL flush(  ) throw(
93*cdf0e10cSrcweir             ::com::sun::star::io::IOException,
94*cdf0e10cSrcweir             ::com::sun::star::uno::RuntimeException);
95*cdf0e10cSrcweir         virtual void SAL_CALL close(  )
96*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
97*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException);
98*cdf0e10cSrcweir         virtual ::rtl::OUString SAL_CALL getDescription(  )
99*cdf0e10cSrcweir             throw(::com::sun::star::uno::RuntimeException);
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         // XConnectionBroadcaster
102*cdf0e10cSrcweir         virtual void SAL_CALL addStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
103*cdf0e10cSrcweir             throw(::com::sun::star::uno::RuntimeException);
104*cdf0e10cSrcweir         virtual void SAL_CALL removeStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
105*cdf0e10cSrcweir             throw(::com::sun::star::uno::RuntimeException);
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     public:
108*cdf0e10cSrcweir         void completeConnectionString();
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         ::osl::StreamSocket m_socket;
111*cdf0e10cSrcweir         ::osl::SocketAddr m_addr;
112*cdf0e10cSrcweir         oslInterlockedCount m_nStatus;
113*cdf0e10cSrcweir         ::rtl::OUString m_sDescription;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         ::osl::Mutex _mutex;
116*cdf0e10cSrcweir         sal_Bool     _started;
117*cdf0e10cSrcweir         sal_Bool     _closed;
118*cdf0e10cSrcweir         sal_Bool     _error;
119*cdf0e10cSrcweir         XStreamListener_hash_set _listeners;
120*cdf0e10cSrcweir     };
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     template<class T>
123*cdf0e10cSrcweir     void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         XStreamListener_hash_set listeners;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         {
128*cdf0e10cSrcweir             ::osl::MutexGuard guard(pCon->_mutex);
129*cdf0e10cSrcweir             if(!*notified)
130*cdf0e10cSrcweir             {
131*cdf0e10cSrcweir                 *notified = sal_True;
132*cdf0e10cSrcweir                 listeners = pCon->_listeners;
133*cdf0e10cSrcweir             }
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         ::std::for_each(listeners.begin(), listeners.end(), t);
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     static void callStarted(Reference<XStreamListener> xStreamListener)
140*cdf0e10cSrcweir     {
141*cdf0e10cSrcweir         xStreamListener->started();
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     struct callError {
145*cdf0e10cSrcweir         const Any & any;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         callError(const Any & any);
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         void operator () (Reference<XStreamListener> xStreamListener);
150*cdf0e10cSrcweir     };
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     callError::callError(const Any & aAny)
153*cdf0e10cSrcweir         : any(aAny)
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     void callError::operator () (Reference<XStreamListener> xStreamListener)
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir         xStreamListener->error(any);
160*cdf0e10cSrcweir     }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir     static void callClosed(Reference<XStreamListener> xStreamListener)
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir         xStreamListener->closed();
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     SocketConnection::SocketConnection( const OUString &sConnectionDescription) :
169*cdf0e10cSrcweir         m_nStatus( 0 ),
170*cdf0e10cSrcweir         m_sDescription( sConnectionDescription ),
171*cdf0e10cSrcweir         _started(sal_False),
172*cdf0e10cSrcweir         _closed(sal_False),
173*cdf0e10cSrcweir         _error(sal_False)
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
176*cdf0e10cSrcweir         // make it unique
177*cdf0e10cSrcweir         m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) );
178*cdf0e10cSrcweir         m_sDescription += OUString::valueOf(
179*cdf0e10cSrcweir             sal::static_int_cast< sal_Int64 >(
180*cdf0e10cSrcweir                 reinterpret_cast< sal_IntPtr >(&m_socket)),
181*cdf0e10cSrcweir             10 );
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     SocketConnection::~SocketConnection()
185*cdf0e10cSrcweir     {
186*cdf0e10cSrcweir         g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
187*cdf0e10cSrcweir     }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir     void SocketConnection::completeConnectionString()
190*cdf0e10cSrcweir     {
191*cdf0e10cSrcweir         OUStringBuffer buf( 256 );
192*cdf0e10cSrcweir         buf.appendAscii( ",peerPort=" );
193*cdf0e10cSrcweir         buf.append( (sal_Int32) m_socket.getPeerPort() );
194*cdf0e10cSrcweir         buf.appendAscii( ",peerHost=" );
195*cdf0e10cSrcweir         buf.append( m_socket.getPeerHost( ) );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         buf.appendAscii( ",localPort=" );
198*cdf0e10cSrcweir         buf.append( (sal_Int32) m_socket.getLocalPort() );
199*cdf0e10cSrcweir         buf.appendAscii( ",localHost=" );
200*cdf0e10cSrcweir         buf.append( m_socket.getLocalHost() );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         m_sDescription += buf.makeStringAndClear();
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
206*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
207*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         if( ! m_nStatus )
210*cdf0e10cSrcweir         {
211*cdf0e10cSrcweir             notifyListeners(this, &_started, callStarted);
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir             if( aReadBytes.getLength() != nBytesToRead )
214*cdf0e10cSrcweir             {
215*cdf0e10cSrcweir                 aReadBytes.realloc( nBytesToRead );
216*cdf0e10cSrcweir             }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir             sal_Int32 i = 0;
219*cdf0e10cSrcweir             i = m_socket.read( aReadBytes.getArray()  , aReadBytes.getLength() );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir             if(i != nBytesToRead)
222*cdf0e10cSrcweir             {
223*cdf0e10cSrcweir                 OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - "));
224*cdf0e10cSrcweir                 message +=  m_socket.getErrorAsString();
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir                 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir                 Any any;
229*cdf0e10cSrcweir                 any <<= ioException;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir                 notifyListeners(this, &_error, callError(any));
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir                 throw ioException;
234*cdf0e10cSrcweir             }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir             return i;
237*cdf0e10cSrcweir         }
238*cdf0e10cSrcweir         else
239*cdf0e10cSrcweir         {
240*cdf0e10cSrcweir             OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - connection already closed"));
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir             IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir             Any any;
245*cdf0e10cSrcweir             any <<= ioException;
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir             notifyListeners(this, &_error, callError(any));
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir             throw ioException;
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir     }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir     void SocketConnection::write( const Sequence < sal_Int8 > &seq )
254*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
255*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
256*cdf0e10cSrcweir     {
257*cdf0e10cSrcweir         if( ! m_nStatus )
258*cdf0e10cSrcweir         {
259*cdf0e10cSrcweir             if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - "));
262*cdf0e10cSrcweir                 message += m_socket.getErrorAsString();
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir                 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir                 Any any;
267*cdf0e10cSrcweir                 any <<= ioException;
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir                 notifyListeners(this, &_error, callError(any));
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir                 throw ioException;
272*cdf0e10cSrcweir             }
273*cdf0e10cSrcweir         }
274*cdf0e10cSrcweir         else
275*cdf0e10cSrcweir         {
276*cdf0e10cSrcweir             OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - connection already closed"));
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir             IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir             Any any;
281*cdf0e10cSrcweir             any <<= ioException;
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir             notifyListeners(this, &_error, callError(any));
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir             throw ioException;
286*cdf0e10cSrcweir         }
287*cdf0e10cSrcweir     }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     void SocketConnection::flush( )
290*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
291*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
292*cdf0e10cSrcweir     {
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     void SocketConnection::close()
297*cdf0e10cSrcweir             throw(::com::sun::star::io::IOException,
298*cdf0e10cSrcweir                   ::com::sun::star::uno::RuntimeException)
299*cdf0e10cSrcweir     {
300*cdf0e10cSrcweir         // enshure close is called only once
301*cdf0e10cSrcweir         if(  1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
302*cdf0e10cSrcweir         {
303*cdf0e10cSrcweir             m_socket.shutdown();
304*cdf0e10cSrcweir             notifyListeners(this, &_closed, callClosed);
305*cdf0e10cSrcweir         }
306*cdf0e10cSrcweir     }
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir     OUString SocketConnection::getDescription()
309*cdf0e10cSrcweir             throw( ::com::sun::star::uno::RuntimeException)
310*cdf0e10cSrcweir     {
311*cdf0e10cSrcweir         return m_sDescription;
312*cdf0e10cSrcweir     }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir     // XConnectionBroadcaster
316*cdf0e10cSrcweir     void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
317*cdf0e10cSrcweir     {
318*cdf0e10cSrcweir         MutexGuard guard(_mutex);
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir         _listeners.insert(aListener);
321*cdf0e10cSrcweir     }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir     void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
324*cdf0e10cSrcweir     {
325*cdf0e10cSrcweir         MutexGuard guard(_mutex);
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir         _listeners.erase(aListener);
328*cdf0e10cSrcweir     }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir     SocketAcceptor::SocketAcceptor( const OUString &sSocketName,
331*cdf0e10cSrcweir                                     sal_uInt16 nPort,
332*cdf0e10cSrcweir                                     sal_Bool bTcpNoDelay,
333*cdf0e10cSrcweir                                     const OUString &sConnectionDescription) :
334*cdf0e10cSrcweir         m_sSocketName( sSocketName ),
335*cdf0e10cSrcweir         m_sConnectionDescription( sConnectionDescription ),
336*cdf0e10cSrcweir         m_nPort( nPort ),
337*cdf0e10cSrcweir         m_bTcpNoDelay( bTcpNoDelay ),
338*cdf0e10cSrcweir         m_bClosed( sal_False )
339*cdf0e10cSrcweir     {
340*cdf0e10cSrcweir     }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir     void SocketAcceptor::init()
344*cdf0e10cSrcweir     {
345*cdf0e10cSrcweir         if( ! m_addr.setPort( m_nPort ) )
346*cdf0e10cSrcweir         {
347*cdf0e10cSrcweir             OUStringBuffer message( 128 );
348*cdf0e10cSrcweir             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " );
349*cdf0e10cSrcweir             message.append( (sal_Int32) m_nPort );
350*cdf0e10cSrcweir             throw ConnectionSetupException(
351*cdf0e10cSrcweir                 message.makeStringAndClear() , Reference< XInterface> () );
352*cdf0e10cSrcweir         }
353*cdf0e10cSrcweir         if( ! m_addr.setHostname( m_sSocketName.pData ) )
354*cdf0e10cSrcweir         {
355*cdf0e10cSrcweir             OUStringBuffer message( 128 );
356*cdf0e10cSrcweir             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid host " );
357*cdf0e10cSrcweir             message.append( m_sSocketName );
358*cdf0e10cSrcweir             throw ConnectionSetupException(
359*cdf0e10cSrcweir                 message.makeStringAndClear(), Reference< XInterface > () );
360*cdf0e10cSrcweir         }
361*cdf0e10cSrcweir         m_socket.setOption( osl_Socket_OptionReuseAddr, 1);
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir         if(! m_socket.bind(m_addr) )
364*cdf0e10cSrcweir         {
365*cdf0e10cSrcweir             OUStringBuffer message( 128 );
366*cdf0e10cSrcweir             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " );
367*cdf0e10cSrcweir             message.append( m_sSocketName ).appendAscii( ":" ).append((sal_Int32)m_nPort);
368*cdf0e10cSrcweir             throw ConnectionSetupException(
369*cdf0e10cSrcweir                 message.makeStringAndClear(),
370*cdf0e10cSrcweir                 Reference<XInterface>());
371*cdf0e10cSrcweir         }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir         if(! m_socket.listen() )
374*cdf0e10cSrcweir         {
375*cdf0e10cSrcweir             OUStringBuffer message( 128 );
376*cdf0e10cSrcweir             message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " );
377*cdf0e10cSrcweir             message.append( m_sSocketName ).appendAscii( ":" ).append( (sal_Int32) m_nPort);
378*cdf0e10cSrcweir             throw ConnectionSetupException( message.makeStringAndClear(),Reference<XInterface>() );
379*cdf0e10cSrcweir         }
380*cdf0e10cSrcweir     }
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir     Reference< XConnection > SocketAcceptor::accept( )
383*cdf0e10cSrcweir     {
384*cdf0e10cSrcweir         SocketConnection *pConn = new SocketConnection( m_sConnectionDescription );
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir         if( m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok )
387*cdf0e10cSrcweir         {
388*cdf0e10cSrcweir             // stopAccepting was called
389*cdf0e10cSrcweir             delete pConn;
390*cdf0e10cSrcweir             return Reference < XConnection > ();
391*cdf0e10cSrcweir         }
392*cdf0e10cSrcweir         if( m_bClosed )
393*cdf0e10cSrcweir         {
394*cdf0e10cSrcweir             delete pConn;
395*cdf0e10cSrcweir             return Reference < XConnection > ();
396*cdf0e10cSrcweir         }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir         pConn->completeConnectionString();
399*cdf0e10cSrcweir         if( m_bTcpNoDelay )
400*cdf0e10cSrcweir         {
401*cdf0e10cSrcweir             sal_Int32 nTcpNoDelay = sal_True;
402*cdf0e10cSrcweir             pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
403*cdf0e10cSrcweir                                        sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
404*cdf0e10cSrcweir         }
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir         return Reference < XConnection > ( (XConnection * ) pConn );
407*cdf0e10cSrcweir     }
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir     void SocketAcceptor::stopAccepting()
410*cdf0e10cSrcweir     {
411*cdf0e10cSrcweir         m_bClosed = sal_True;
412*cdf0e10cSrcweir         m_socket.close();
413*cdf0e10cSrcweir     }
414*cdf0e10cSrcweir }
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir 
417