xref: /trunk/main/io/source/acceptor/acc_socket.cxx (revision 01ad09a0)
13716f815SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
33716f815SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
43716f815SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
53716f815SAndrew Rist  * distributed with this work for additional information
63716f815SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
73716f815SAndrew Rist  * to you under the Apache License, Version 2.0 (the
83716f815SAndrew Rist  * "License"); you may not use this file except in compliance
93716f815SAndrew Rist  * with the License.  You may obtain a copy of the License at
103716f815SAndrew Rist  *
113716f815SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
123716f815SAndrew Rist  *
133716f815SAndrew Rist  * Unless required by applicable law or agreed to in writing,
143716f815SAndrew Rist  * software distributed under the License is distributed on an
153716f815SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
163716f815SAndrew Rist  * KIND, either express or implied.  See the License for the
173716f815SAndrew Rist  * specific language governing permissions and limitations
183716f815SAndrew Rist  * under the License.
193716f815SAndrew Rist  *
203716f815SAndrew Rist  *************************************************************/
213716f815SAndrew Rist 
223716f815SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_io.hxx"
26cdf0e10cSrcweir #include "acceptor.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <hash_set>
29cdf0e10cSrcweir #include <algorithm>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32cdf0e10cSrcweir #include <com/sun/star/connection/XConnectionBroadcaster.hpp>
33cdf0e10cSrcweir #include <com/sun/star/connection/ConnectionSetupException.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::osl;
38cdf0e10cSrcweir using namespace ::rtl;
39cdf0e10cSrcweir using namespace ::cppu;
40cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41cdf0e10cSrcweir using namespace ::com::sun::star::io;
42cdf0e10cSrcweir using namespace ::com::sun::star::connection;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace io_acceptor {
46cdf0e10cSrcweir 	template<class T>
47cdf0e10cSrcweir 	struct ReferenceHash
48cdf0e10cSrcweir 	{
operator ()io_acceptor::ReferenceHash49cdf0e10cSrcweir 		size_t operator () (const ::com::sun::star::uno::Reference<T> & ref) const
50cdf0e10cSrcweir         {
51cdf0e10cSrcweir 			return (size_t)ref.get();
52cdf0e10cSrcweir 		}
53cdf0e10cSrcweir 	};
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	template<class T>
56cdf0e10cSrcweir 	struct ReferenceEqual
57cdf0e10cSrcweir 	{
operator ()io_acceptor::ReferenceEqual58cdf0e10cSrcweir 		sal_Bool operator () (const ::com::sun::star::uno::Reference<T> & op1,
59cdf0e10cSrcweir 							  const ::com::sun::star::uno::Reference<T> & op2) const
60cdf0e10cSrcweir         {
61cdf0e10cSrcweir 			return op1.get() == op2.get();
62cdf0e10cSrcweir 		}
63cdf0e10cSrcweir 	};
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	typedef ::std::hash_set< ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>,
67cdf0e10cSrcweir                              ReferenceHash< ::com::sun::star::io::XStreamListener>,
68cdf0e10cSrcweir                              ReferenceEqual< ::com::sun::star::io::XStreamListener> >
69cdf0e10cSrcweir 	        XStreamListener_hash_set;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	class SocketConnection : public ::cppu::WeakImplHelper2<
73cdf0e10cSrcweir         ::com::sun::star::connection::XConnection,
74cdf0e10cSrcweir 		::com::sun::star::connection::XConnectionBroadcaster>
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	{
77cdf0e10cSrcweir 	public:
78cdf0e10cSrcweir 		SocketConnection( const OUString & sConnectionDescription );
79cdf0e10cSrcweir 		~SocketConnection();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 		virtual sal_Int32 SAL_CALL read( ::com::sun::star::uno::Sequence< sal_Int8 >& aReadBytes,
82cdf0e10cSrcweir 										 sal_Int32 nBytesToRead )
83cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
84cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException);
85cdf0e10cSrcweir 		virtual void SAL_CALL write( const ::com::sun::star::uno::Sequence< sal_Int8 >& aData )
86cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
87cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException);
88cdf0e10cSrcweir 		virtual void SAL_CALL flush(  ) throw(
89cdf0e10cSrcweir 			::com::sun::star::io::IOException,
90cdf0e10cSrcweir 			::com::sun::star::uno::RuntimeException);
91cdf0e10cSrcweir 		virtual void SAL_CALL close(  )
92cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
93cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException);
94cdf0e10cSrcweir 		virtual ::rtl::OUString SAL_CALL getDescription(  )
95cdf0e10cSrcweir 			throw(::com::sun::star::uno::RuntimeException);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 		// XConnectionBroadcaster
98cdf0e10cSrcweir 		virtual void SAL_CALL addStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
99cdf0e10cSrcweir 			throw(::com::sun::star::uno::RuntimeException);
100cdf0e10cSrcweir 		virtual void SAL_CALL removeStreamListener(const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStreamListener>& aListener)
101cdf0e10cSrcweir 			throw(::com::sun::star::uno::RuntimeException);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	public:
104cdf0e10cSrcweir 		void completeConnectionString();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 		::osl::StreamSocket m_socket;
107cdf0e10cSrcweir 		::osl::SocketAddr m_addr;
108cdf0e10cSrcweir 		oslInterlockedCount m_nStatus;
109cdf0e10cSrcweir 		::rtl::OUString m_sDescription;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 		::osl::Mutex _mutex;
112cdf0e10cSrcweir 		sal_Bool     _started;
113cdf0e10cSrcweir 		sal_Bool     _closed;
114cdf0e10cSrcweir 		sal_Bool     _error;
115cdf0e10cSrcweir 		XStreamListener_hash_set _listeners;
116cdf0e10cSrcweir 	};
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	template<class T>
notifyListeners(SocketConnection * pCon,sal_Bool * notified,T t)119cdf0e10cSrcweir 	void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir   		XStreamListener_hash_set listeners;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			::osl::MutexGuard guard(pCon->_mutex);
125cdf0e10cSrcweir 			if(!*notified)
126cdf0e10cSrcweir 			{
127cdf0e10cSrcweir 				*notified = sal_True;
128cdf0e10cSrcweir 				listeners = pCon->_listeners;
129cdf0e10cSrcweir 			}
130cdf0e10cSrcweir 		}
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 		::std::for_each(listeners.begin(), listeners.end(), t);
133cdf0e10cSrcweir 	}
134cdf0e10cSrcweir 
callStarted(Reference<XStreamListener> xStreamListener)135cdf0e10cSrcweir 	static void callStarted(Reference<XStreamListener> xStreamListener)
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		xStreamListener->started();
138cdf0e10cSrcweir 	}
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	struct callError {
141cdf0e10cSrcweir 		const Any & any;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 		callError(const Any & any);
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 		void operator () (Reference<XStreamListener> xStreamListener);
146cdf0e10cSrcweir 	};
147cdf0e10cSrcweir 
callError(const Any & aAny)148cdf0e10cSrcweir 	callError::callError(const Any & aAny)
149cdf0e10cSrcweir 		: any(aAny)
150cdf0e10cSrcweir 	{
151cdf0e10cSrcweir 	}
152cdf0e10cSrcweir 
operator ()(Reference<XStreamListener> xStreamListener)153cdf0e10cSrcweir 	void callError::operator () (Reference<XStreamListener> xStreamListener)
154cdf0e10cSrcweir 	{
155cdf0e10cSrcweir 		xStreamListener->error(any);
156cdf0e10cSrcweir 	}
157cdf0e10cSrcweir 
callClosed(Reference<XStreamListener> xStreamListener)158cdf0e10cSrcweir 	static void callClosed(Reference<XStreamListener> xStreamListener)
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		xStreamListener->closed();
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
SocketConnection(const OUString & sConnectionDescription)164cdf0e10cSrcweir 	SocketConnection::SocketConnection( const OUString &sConnectionDescription) :
165cdf0e10cSrcweir 		m_nStatus( 0 ),
166cdf0e10cSrcweir 		m_sDescription( sConnectionDescription ),
167cdf0e10cSrcweir 		_started(sal_False),
168cdf0e10cSrcweir 		_closed(sal_False),
169cdf0e10cSrcweir 		_error(sal_False)
170cdf0e10cSrcweir 	{
171cdf0e10cSrcweir 		g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
172cdf0e10cSrcweir 		// make it unique
173cdf0e10cSrcweir 		m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) );
174cdf0e10cSrcweir 		m_sDescription += OUString::valueOf(
175cdf0e10cSrcweir             sal::static_int_cast< sal_Int64 >(
176cdf0e10cSrcweir                 reinterpret_cast< sal_IntPtr >(&m_socket)),
177cdf0e10cSrcweir             10 );
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 
~SocketConnection()180cdf0e10cSrcweir 	SocketConnection::~SocketConnection()
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
183cdf0e10cSrcweir 	}
184cdf0e10cSrcweir 
completeConnectionString()185cdf0e10cSrcweir 	void SocketConnection::completeConnectionString()
186cdf0e10cSrcweir 	{
187cdf0e10cSrcweir 		OUStringBuffer buf( 256 );
188cdf0e10cSrcweir 		buf.appendAscii( ",peerPort=" );
189cdf0e10cSrcweir 		buf.append( (sal_Int32) m_socket.getPeerPort() );
190cdf0e10cSrcweir 		buf.appendAscii( ",peerHost=" );
191cdf0e10cSrcweir 		buf.append( m_socket.getPeerHost( ) );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 		buf.appendAscii( ",localPort=" );
194cdf0e10cSrcweir 		buf.append( (sal_Int32) m_socket.getLocalPort() );
195cdf0e10cSrcweir 		buf.appendAscii( ",localHost=" );
196cdf0e10cSrcweir 		buf.append( m_socket.getLocalHost() );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 		m_sDescription += buf.makeStringAndClear();
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 
read(Sequence<sal_Int8> & aReadBytes,sal_Int32 nBytesToRead)201cdf0e10cSrcweir 	sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
202cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
203cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
204cdf0e10cSrcweir 	{
205cdf0e10cSrcweir 		if( ! m_nStatus )
206cdf0e10cSrcweir 		{
207cdf0e10cSrcweir 			notifyListeners(this, &_started, callStarted);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 			if( aReadBytes.getLength() != nBytesToRead )
210cdf0e10cSrcweir 			{
211cdf0e10cSrcweir 				aReadBytes.realloc( nBytesToRead );
212cdf0e10cSrcweir 			}
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 			sal_Int32 i = 0;
215cdf0e10cSrcweir 			i = m_socket.read( aReadBytes.getArray()  , aReadBytes.getLength() );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 			if(i != nBytesToRead)
218cdf0e10cSrcweir 			{
219cdf0e10cSrcweir 				OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - "));
220cdf0e10cSrcweir 				message +=	m_socket.getErrorAsString();
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 				IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 				Any any;
225cdf0e10cSrcweir 				any <<= ioException;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 				notifyListeners(this, &_error, callError(any));
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 				throw ioException;
230cdf0e10cSrcweir 			}
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 			return i;
233cdf0e10cSrcweir 		}
234cdf0e10cSrcweir 		else
235cdf0e10cSrcweir 		{
236cdf0e10cSrcweir 			OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::read: error - connection already closed"));
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 			IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 			Any any;
241cdf0e10cSrcweir 			any <<= ioException;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 			notifyListeners(this, &_error, callError(any));
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 			throw ioException;
246cdf0e10cSrcweir 		}
247cdf0e10cSrcweir 	}
248cdf0e10cSrcweir 
write(const Sequence<sal_Int8> & seq)249cdf0e10cSrcweir 	void SocketConnection::write( const Sequence < sal_Int8 > &seq )
250cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
251cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
252cdf0e10cSrcweir 	{
253cdf0e10cSrcweir 		if( ! m_nStatus )
254cdf0e10cSrcweir 		{
255cdf0e10cSrcweir 			if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
256cdf0e10cSrcweir 			{
257cdf0e10cSrcweir 				OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - "));
258cdf0e10cSrcweir 				message += m_socket.getErrorAsString();
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 				IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 				Any any;
263cdf0e10cSrcweir 				any <<= ioException;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 				notifyListeners(this, &_error, callError(any));
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 				throw ioException;
268cdf0e10cSrcweir 			}
269cdf0e10cSrcweir 		}
270cdf0e10cSrcweir 		else
271cdf0e10cSrcweir 		{
272cdf0e10cSrcweir 			OUString message(RTL_CONSTASCII_USTRINGPARAM("acc_socket.cxx:SocketConnection::write: error - connection already closed"));
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 			IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 			Any any;
277cdf0e10cSrcweir 			any <<= ioException;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 			notifyListeners(this, &_error, callError(any));
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 			throw ioException;
282cdf0e10cSrcweir 		}
283cdf0e10cSrcweir 	}
284cdf0e10cSrcweir 
flush()285cdf0e10cSrcweir 	void SocketConnection::flush( )
286cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
287cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
288cdf0e10cSrcweir 	{
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 	}
291cdf0e10cSrcweir 
close()292cdf0e10cSrcweir 	void SocketConnection::close()
293cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
294cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
295cdf0e10cSrcweir 	{
296*01ad09a0Smseidel 		// ensure close is called only once
297cdf0e10cSrcweir 		if(  1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
298cdf0e10cSrcweir 		{
299cdf0e10cSrcweir 			m_socket.shutdown();
300cdf0e10cSrcweir 			notifyListeners(this, &_closed, callClosed);
301cdf0e10cSrcweir 		}
302cdf0e10cSrcweir 	}
303cdf0e10cSrcweir 
getDescription()304cdf0e10cSrcweir 	OUString SocketConnection::getDescription()
305cdf0e10cSrcweir 			throw( ::com::sun::star::uno::RuntimeException)
306cdf0e10cSrcweir 	{
307cdf0e10cSrcweir 		return m_sDescription;
308cdf0e10cSrcweir 	}
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 	// XConnectionBroadcaster
addStreamListener(const Reference<XStreamListener> & aListener)312cdf0e10cSrcweir 	void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
313cdf0e10cSrcweir 	{
314cdf0e10cSrcweir 		MutexGuard guard(_mutex);
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 		_listeners.insert(aListener);
317cdf0e10cSrcweir 	}
318cdf0e10cSrcweir 
removeStreamListener(const Reference<XStreamListener> & aListener)319cdf0e10cSrcweir 	void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
320cdf0e10cSrcweir 	{
321cdf0e10cSrcweir 		MutexGuard guard(_mutex);
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		_listeners.erase(aListener);
324cdf0e10cSrcweir 	}
325cdf0e10cSrcweir 
SocketAcceptor(const OUString & sSocketName,sal_uInt16 nPort,sal_Bool bTcpNoDelay,const OUString & sConnectionDescription)326cdf0e10cSrcweir 	SocketAcceptor::SocketAcceptor( const OUString &sSocketName,
327cdf0e10cSrcweir 									sal_uInt16 nPort,
328cdf0e10cSrcweir 									sal_Bool bTcpNoDelay,
329cdf0e10cSrcweir 									const OUString &sConnectionDescription) :
330cdf0e10cSrcweir 		m_sSocketName( sSocketName ),
331cdf0e10cSrcweir 		m_sConnectionDescription( sConnectionDescription ),
332cdf0e10cSrcweir 		m_nPort( nPort ),
333cdf0e10cSrcweir 		m_bTcpNoDelay( bTcpNoDelay ),
334cdf0e10cSrcweir 		m_bClosed( sal_False )
335cdf0e10cSrcweir 	{
336cdf0e10cSrcweir 	}
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 
init()339cdf0e10cSrcweir 	void SocketAcceptor::init()
340cdf0e10cSrcweir 	{
341cdf0e10cSrcweir 		if( ! m_addr.setPort( m_nPort ) )
342cdf0e10cSrcweir 		{
343cdf0e10cSrcweir 			OUStringBuffer message( 128 );
344cdf0e10cSrcweir 			message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " );
345cdf0e10cSrcweir 			message.append( (sal_Int32) m_nPort );
346cdf0e10cSrcweir 			throw ConnectionSetupException(
347cdf0e10cSrcweir 				message.makeStringAndClear() , Reference< XInterface> () );
348cdf0e10cSrcweir 		}
349cdf0e10cSrcweir 		if( ! m_addr.setHostname( m_sSocketName.pData ) )
350cdf0e10cSrcweir 		{
351cdf0e10cSrcweir 			OUStringBuffer message( 128 );
352cdf0e10cSrcweir 			message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - invalid host " );
353cdf0e10cSrcweir 			message.append( m_sSocketName );
354cdf0e10cSrcweir 			throw ConnectionSetupException(
355cdf0e10cSrcweir 				message.makeStringAndClear(), Reference< XInterface > () );
356cdf0e10cSrcweir 		}
357cdf0e10cSrcweir 		m_socket.setOption( osl_Socket_OptionReuseAddr, 1);
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 		if(! m_socket.bind(m_addr) )
360cdf0e10cSrcweir 		{
361cdf0e10cSrcweir 			OUStringBuffer message( 128 );
362cdf0e10cSrcweir 			message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " );
363cdf0e10cSrcweir 			message.append( m_sSocketName ).appendAscii( ":" ).append((sal_Int32)m_nPort);
364cdf0e10cSrcweir 			throw ConnectionSetupException(
365cdf0e10cSrcweir 				message.makeStringAndClear(),
366cdf0e10cSrcweir 				Reference<XInterface>());
367cdf0e10cSrcweir 		}
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 		if(! m_socket.listen() )
370cdf0e10cSrcweir 		{
371cdf0e10cSrcweir 			OUStringBuffer message( 128 );
372cdf0e10cSrcweir 			message.appendAscii( "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " );
373cdf0e10cSrcweir 			message.append( m_sSocketName ).appendAscii( ":" ).append( (sal_Int32) m_nPort);
374cdf0e10cSrcweir 			throw ConnectionSetupException(	message.makeStringAndClear(),Reference<XInterface>() );
375cdf0e10cSrcweir 		}
376cdf0e10cSrcweir 	}
377cdf0e10cSrcweir 
accept()378cdf0e10cSrcweir 	Reference< XConnection > SocketAcceptor::accept( )
379cdf0e10cSrcweir 	{
380cdf0e10cSrcweir 		SocketConnection *pConn = new SocketConnection( m_sConnectionDescription );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 		if( m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok )
383cdf0e10cSrcweir 		{
384cdf0e10cSrcweir 			// stopAccepting was called
385cdf0e10cSrcweir 			delete pConn;
386cdf0e10cSrcweir 			return Reference < XConnection > ();
387cdf0e10cSrcweir 		}
388cdf0e10cSrcweir 		if( m_bClosed )
389cdf0e10cSrcweir 		{
390cdf0e10cSrcweir 			delete pConn;
391cdf0e10cSrcweir 			return Reference < XConnection > ();
392cdf0e10cSrcweir 		}
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 		pConn->completeConnectionString();
395cdf0e10cSrcweir 		if( m_bTcpNoDelay )
396cdf0e10cSrcweir 		{
397cdf0e10cSrcweir 			sal_Int32 nTcpNoDelay = sal_True;
398cdf0e10cSrcweir 			pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
399cdf0e10cSrcweir 									   sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
400cdf0e10cSrcweir 		}
401cdf0e10cSrcweir 
402cdf0e10cSrcweir 		return Reference < XConnection > ( (XConnection * ) pConn );
403cdf0e10cSrcweir 	}
404cdf0e10cSrcweir 
stopAccepting()405cdf0e10cSrcweir 	void SocketAcceptor::stopAccepting()
406cdf0e10cSrcweir 	{
407cdf0e10cSrcweir 		m_bClosed = sal_True;
408cdf0e10cSrcweir 		m_socket.close();
409cdf0e10cSrcweir 	}
410cdf0e10cSrcweir }
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 
413