xref: /aoo41x/main/io/source/connector/ctr_socket.cxx (revision cdf0e10c)
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 
31*cdf0e10cSrcweir #include "connector.hxx"
32*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
33*cdf0e10cSrcweir #include <algorithm>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using namespace ::osl;
36*cdf0e10cSrcweir using namespace ::rtl;
37*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38*cdf0e10cSrcweir using namespace ::com::sun::star::io;
39*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace stoc_connector {
43*cdf0e10cSrcweir 	template<class T>
44*cdf0e10cSrcweir 	void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
45*cdf0e10cSrcweir 	{
46*cdf0e10cSrcweir   		XStreamListener_hash_set listeners;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 		{
49*cdf0e10cSrcweir 			::osl::MutexGuard guard(pCon->_mutex);
50*cdf0e10cSrcweir 			if(!*notified)
51*cdf0e10cSrcweir 			{
52*cdf0e10cSrcweir 				*notified = sal_True;
53*cdf0e10cSrcweir 				listeners = pCon->_listeners;
54*cdf0e10cSrcweir 			}
55*cdf0e10cSrcweir 		}
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 		::std::for_each(listeners.begin(), listeners.end(), t);
58*cdf0e10cSrcweir 	}
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	static void callStarted(Reference<XStreamListener> xStreamListener)
62*cdf0e10cSrcweir 	{
63*cdf0e10cSrcweir 		xStreamListener->started();
64*cdf0e10cSrcweir 	}
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	struct callError {
67*cdf0e10cSrcweir 		const Any & any;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 		callError(const Any & any);
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 		void operator () (Reference<XStreamListener> xStreamListener);
72*cdf0e10cSrcweir 	};
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 	callError::callError(const Any & aAny)
75*cdf0e10cSrcweir 		: any(aAny)
76*cdf0e10cSrcweir 	{
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	void callError::operator () (Reference<XStreamListener> xStreamListener)
80*cdf0e10cSrcweir 	{
81*cdf0e10cSrcweir 		xStreamListener->error(any);
82*cdf0e10cSrcweir 	}
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	static void callClosed(Reference<XStreamListener> xStreamListener)
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		xStreamListener->closed();
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	SocketConnection::SocketConnection( const OUString &sConnectionDescription ) :
91*cdf0e10cSrcweir 		m_nStatus( 0 ),
92*cdf0e10cSrcweir 		m_sDescription( sConnectionDescription ),
93*cdf0e10cSrcweir 		_started(sal_False),
94*cdf0e10cSrcweir 		_closed(sal_False),
95*cdf0e10cSrcweir 		_error(sal_False)
96*cdf0e10cSrcweir 	{
97*cdf0e10cSrcweir 		// make it unique
98*cdf0e10cSrcweir 		g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
99*cdf0e10cSrcweir 		m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) );
100*cdf0e10cSrcweir 		m_sDescription += OUString::valueOf(
101*cdf0e10cSrcweir             sal::static_int_cast< sal_Int64 >(
102*cdf0e10cSrcweir                 reinterpret_cast< sal_IntPtr >(&m_socket)),
103*cdf0e10cSrcweir             10 );
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	SocketConnection::~SocketConnection()
107*cdf0e10cSrcweir 	{
108*cdf0e10cSrcweir 		g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
109*cdf0e10cSrcweir 	}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	void SocketConnection::completeConnectionString()
112*cdf0e10cSrcweir 	{
113*cdf0e10cSrcweir 		sal_Int32 nPort;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 		nPort = m_socket.getPeerPort();
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 		OUStringBuffer buf( 256 );
118*cdf0e10cSrcweir 		buf.appendAscii( ",peerPort=" );
119*cdf0e10cSrcweir 		buf.append( (sal_Int32) nPort );
120*cdf0e10cSrcweir 		buf.appendAscii( ",peerHost=" );
121*cdf0e10cSrcweir 		buf.append( m_socket.getPeerHost() );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 		buf.appendAscii( ",localPort=" );
124*cdf0e10cSrcweir 		buf.append( (sal_Int32) nPort );
125*cdf0e10cSrcweir 		buf.appendAscii( ",localHost=" );
126*cdf0e10cSrcweir 		buf.append( m_socket.getLocalHost( ) );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 		m_sDescription += buf.makeStringAndClear();
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
132*cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
133*cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
134*cdf0e10cSrcweir 	{
135*cdf0e10cSrcweir 		if( ! m_nStatus )
136*cdf0e10cSrcweir 		{
137*cdf0e10cSrcweir 			notifyListeners(this, &_started, callStarted);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 			if( aReadBytes.getLength() != nBytesToRead )
140*cdf0e10cSrcweir 			{
141*cdf0e10cSrcweir 				aReadBytes.realloc( nBytesToRead );
142*cdf0e10cSrcweir 			}
143*cdf0e10cSrcweir 			sal_Int32 i = m_socket.read( aReadBytes.getArray()  , aReadBytes.getLength() );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None)
146*cdf0e10cSrcweir 			{
147*cdf0e10cSrcweir 				OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::read: error - "));
148*cdf0e10cSrcweir 				message += m_socket.getErrorAsString();
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 				IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 				Any any;
153*cdf0e10cSrcweir 				any <<= ioException;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir 				notifyListeners(this, &_error, callError(any));
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 				throw ioException;
158*cdf0e10cSrcweir 			}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 			return i;
161*cdf0e10cSrcweir 		}
162*cdf0e10cSrcweir 		else
163*cdf0e10cSrcweir 		{
164*cdf0e10cSrcweir 			OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::read: error - connection already closed"));
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 			IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 			Any any;
169*cdf0e10cSrcweir 			any <<= ioException;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 			notifyListeners(this, &_error, callError(any));
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 			throw ioException;
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 	}
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 	void SocketConnection::write( const Sequence < sal_Int8 > &seq )
178*cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
179*cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
180*cdf0e10cSrcweir 	{
181*cdf0e10cSrcweir 		if( ! m_nStatus )
182*cdf0e10cSrcweir 		{
183*cdf0e10cSrcweir 			if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
184*cdf0e10cSrcweir 			{
185*cdf0e10cSrcweir 				OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::write: error - "));
186*cdf0e10cSrcweir 				message += m_socket.getErrorAsString();
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 				IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 				Any any;
191*cdf0e10cSrcweir 				any <<= ioException;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 				notifyListeners(this, &_error, callError(any));
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 				throw ioException;
196*cdf0e10cSrcweir 			}
197*cdf0e10cSrcweir 		}
198*cdf0e10cSrcweir 		else
199*cdf0e10cSrcweir 		{
200*cdf0e10cSrcweir 			OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::write: error - connection already closed"));
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 			IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 			Any any;
205*cdf0e10cSrcweir 			any <<= ioException;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 			notifyListeners(this, &_error, callError(any));
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 			throw ioException;
210*cdf0e10cSrcweir 		}
211*cdf0e10cSrcweir 	}
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	void SocketConnection::flush( )
214*cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
215*cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
216*cdf0e10cSrcweir 	{
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	}
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	void SocketConnection::close()
221*cdf0e10cSrcweir 			throw(::com::sun::star::io::IOException,
222*cdf0e10cSrcweir 				  ::com::sun::star::uno::RuntimeException)
223*cdf0e10cSrcweir 	{
224*cdf0e10cSrcweir 			// ensure that close is called only once
225*cdf0e10cSrcweir 		if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
226*cdf0e10cSrcweir 		{
227*cdf0e10cSrcweir 			m_socket.shutdown();
228*cdf0e10cSrcweir 			notifyListeners(this, &_closed, callClosed);
229*cdf0e10cSrcweir 		}
230*cdf0e10cSrcweir 	}
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	OUString SocketConnection::getDescription()
233*cdf0e10cSrcweir 			throw( ::com::sun::star::uno::RuntimeException)
234*cdf0e10cSrcweir 	{
235*cdf0e10cSrcweir 		return m_sDescription;
236*cdf0e10cSrcweir 	}
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	// XConnectionBroadcaster
241*cdf0e10cSrcweir 	void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
242*cdf0e10cSrcweir 	{
243*cdf0e10cSrcweir 		MutexGuard guard(_mutex);
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir 		_listeners.insert(aListener);
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
249*cdf0e10cSrcweir 	{
250*cdf0e10cSrcweir 		MutexGuard guard(_mutex);
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 		_listeners.erase(aListener);
253*cdf0e10cSrcweir 	}
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256