Update ooo320-m1
[ooovba.git] / io / source / connector / ctr_socket.cxx
bloba1723ef8c4c81cf53f8aad8ea5a9cfd6a5e137c7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ctr_socket.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_io.hxx"
34 #include "connector.hxx"
35 #include <rtl/ustrbuf.hxx>
36 #include <algorithm>
38 using namespace ::osl;
39 using namespace ::rtl;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::io;
42 using namespace ::com::sun::star::connection;
45 namespace stoc_connector {
46 template<class T>
47 void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
49 XStreamListener_hash_set listeners;
52 ::osl::MutexGuard guard(pCon->_mutex);
53 if(!*notified)
55 *notified = sal_True;
56 listeners = pCon->_listeners;
60 ::std::for_each(listeners.begin(), listeners.end(), t);
64 static void callStarted(Reference<XStreamListener> xStreamListener)
66 xStreamListener->started();
69 struct callError {
70 const Any & any;
72 callError(const Any & any);
74 void operator () (Reference<XStreamListener> xStreamListener);
77 callError::callError(const Any & aAny)
78 : any(aAny)
82 void callError::operator () (Reference<XStreamListener> xStreamListener)
84 xStreamListener->error(any);
87 static void callClosed(Reference<XStreamListener> xStreamListener)
89 xStreamListener->closed();
93 SocketConnection::SocketConnection( const OUString &sConnectionDescription ) :
94 m_nStatus( 0 ),
95 m_sDescription( sConnectionDescription ),
96 _started(sal_False),
97 _closed(sal_False),
98 _error(sal_False)
100 // make it unique
101 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
102 m_sDescription += OUString( RTL_CONSTASCII_USTRINGPARAM( ",uniqueValue=" ) );
103 m_sDescription += OUString::valueOf(
104 sal::static_int_cast< sal_Int64 >(
105 reinterpret_cast< sal_IntPtr >(&m_socket)),
106 10 );
109 SocketConnection::~SocketConnection()
111 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
114 void SocketConnection::completeConnectionString()
116 sal_Int32 nPort;
118 nPort = m_socket.getPeerPort();
120 OUStringBuffer buf( 256 );
121 buf.appendAscii( ",peerPort=" );
122 buf.append( (sal_Int32) nPort );
123 buf.appendAscii( ",peerHost=" );
124 buf.append( m_socket.getPeerHost() );
126 buf.appendAscii( ",localPort=" );
127 buf.append( (sal_Int32) nPort );
128 buf.appendAscii( ",localHost=" );
129 buf.append( m_socket.getLocalHost( ) );
131 m_sDescription += buf.makeStringAndClear();
134 sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
135 throw(::com::sun::star::io::IOException,
136 ::com::sun::star::uno::RuntimeException)
138 if( ! m_nStatus )
140 notifyListeners(this, &_started, callStarted);
142 if( aReadBytes.getLength() != nBytesToRead )
144 aReadBytes.realloc( nBytesToRead );
146 sal_Int32 i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() );
148 if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None)
150 OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::read: error - "));
151 message += m_socket.getErrorAsString();
153 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
155 Any any;
156 any <<= ioException;
158 notifyListeners(this, &_error, callError(any));
160 throw ioException;
163 return i;
165 else
167 OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::read: error - connection already closed"));
169 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
171 Any any;
172 any <<= ioException;
174 notifyListeners(this, &_error, callError(any));
176 throw ioException;
180 void SocketConnection::write( const Sequence < sal_Int8 > &seq )
181 throw(::com::sun::star::io::IOException,
182 ::com::sun::star::uno::RuntimeException)
184 if( ! m_nStatus )
186 if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
188 OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::write: error - "));
189 message += m_socket.getErrorAsString();
191 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
193 Any any;
194 any <<= ioException;
196 notifyListeners(this, &_error, callError(any));
198 throw ioException;
201 else
203 OUString message(RTL_CONSTASCII_USTRINGPARAM("ctr_socket.cxx:SocketConnection::write: error - connection already closed"));
205 IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
207 Any any;
208 any <<= ioException;
210 notifyListeners(this, &_error, callError(any));
212 throw ioException;
216 void SocketConnection::flush( )
217 throw(::com::sun::star::io::IOException,
218 ::com::sun::star::uno::RuntimeException)
223 void SocketConnection::close()
224 throw(::com::sun::star::io::IOException,
225 ::com::sun::star::uno::RuntimeException)
227 // ensure that close is called only once
228 if( 1 == osl_incrementInterlockedCount( (&m_nStatus) ) )
230 m_socket.shutdown();
231 notifyListeners(this, &_closed, callClosed);
235 OUString SocketConnection::getDescription()
236 throw( ::com::sun::star::uno::RuntimeException)
238 return m_sDescription;
243 // XConnectionBroadcaster
244 void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
246 MutexGuard guard(_mutex);
248 _listeners.insert(aListener);
251 void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
253 MutexGuard guard(_mutex);
255 _listeners.erase(aListener);