1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ctr_socket.cxx,v $
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>
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
{
47 void notifyListeners(SocketConnection
* pCon
, sal_Bool
* notified
, T t
)
49 XStreamListener_hash_set listeners
;
52 ::osl::MutexGuard
guard(pCon
->_mutex
);
56 listeners
= pCon
->_listeners
;
60 ::std::for_each(listeners
.begin(), listeners
.end(), t
);
64 static void callStarted(Reference
<XStreamListener
> xStreamListener
)
66 xStreamListener
->started();
72 callError(const Any
& any
);
74 void operator () (Reference
<XStreamListener
> xStreamListener
);
77 callError::callError(const 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
) :
95 m_sDescription( sConnectionDescription
),
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
)),
109 SocketConnection::~SocketConnection()
111 g_moduleCount
.modCnt
.release( &g_moduleCount
.modCnt
);
114 void SocketConnection::completeConnectionString()
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
)
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)));
158 notifyListeners(this, &_error
, callError(any
));
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)));
174 notifyListeners(this, &_error
, callError(any
));
180 void SocketConnection::write( const Sequence
< sal_Int8
> &seq
)
181 throw(::com::sun::star::io::IOException
,
182 ::com::sun::star::uno::RuntimeException
)
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)));
196 notifyListeners(this, &_error
, callError(any
));
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)));
210 notifyListeners(this, &_error
, callError(any
));
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
) ) )
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
);