Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "connector.hxx"
22 : #include <rtl/ustrbuf.hxx>
23 : #include <algorithm>
24 :
25 : using namespace ::osl;
26 : using namespace ::rtl;
27 : using namespace ::com::sun::star::uno;
28 : using namespace ::com::sun::star::io;
29 : using namespace ::com::sun::star::connection;
30 :
31 :
32 : namespace stoc_connector {
33 : template<class T>
34 0 : void notifyListeners(SocketConnection * pCon, sal_Bool * notified, T t)
35 : {
36 0 : XStreamListener_hash_set listeners;
37 :
38 : {
39 0 : ::osl::MutexGuard guard(pCon->_mutex);
40 0 : if(!*notified)
41 : {
42 0 : *notified = sal_True;
43 0 : listeners = pCon->_listeners;
44 : }
45 : }
46 :
47 0 : ::std::for_each(listeners.begin(), listeners.end(), t);
48 0 : }
49 :
50 :
51 0 : static void callStarted(Reference<XStreamListener> xStreamListener)
52 : {
53 0 : xStreamListener->started();
54 0 : }
55 :
56 : struct callError {
57 : const Any & any;
58 :
59 : callError(const Any & any);
60 :
61 : void operator () (Reference<XStreamListener> xStreamListener);
62 : };
63 :
64 0 : callError::callError(const Any & aAny)
65 0 : : any(aAny)
66 : {
67 0 : }
68 :
69 0 : void callError::operator () (Reference<XStreamListener> xStreamListener)
70 : {
71 0 : xStreamListener->error(any);
72 0 : }
73 :
74 0 : static void callClosed(Reference<XStreamListener> xStreamListener)
75 : {
76 0 : xStreamListener->closed();
77 0 : }
78 :
79 :
80 0 : SocketConnection::SocketConnection( const OUString &sConnectionDescription ) :
81 : m_nStatus( 0 ),
82 : m_sDescription( sConnectionDescription ),
83 : _started(sal_False),
84 : _closed(sal_False),
85 0 : _error(sal_False)
86 : {
87 : // make it unique
88 0 : g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
89 0 : m_sDescription += ",uniqueValue=";
90 : m_sDescription += OUString::valueOf(
91 : sal::static_int_cast< sal_Int64 >(
92 : reinterpret_cast< sal_IntPtr >(&m_socket)),
93 0 : 10 );
94 0 : }
95 :
96 0 : SocketConnection::~SocketConnection()
97 : {
98 0 : g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
99 0 : }
100 :
101 0 : void SocketConnection::completeConnectionString()
102 : {
103 : sal_Int32 nPort;
104 :
105 0 : nPort = m_socket.getPeerPort();
106 :
107 0 : OUStringBuffer buf( 256 );
108 0 : buf.appendAscii( ",peerPort=" );
109 0 : buf.append( (sal_Int32) nPort );
110 0 : buf.appendAscii( ",peerHost=" );
111 0 : buf.append( m_socket.getPeerHost() );
112 :
113 0 : buf.appendAscii( ",localPort=" );
114 0 : buf.append( (sal_Int32) nPort );
115 0 : buf.appendAscii( ",localHost=" );
116 0 : buf.append( m_socket.getLocalHost( ) );
117 :
118 0 : m_sDescription += buf.makeStringAndClear();
119 0 : }
120 :
121 0 : sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
122 : throw(::com::sun::star::io::IOException,
123 : ::com::sun::star::uno::RuntimeException)
124 : {
125 0 : if( ! m_nStatus )
126 : {
127 0 : notifyListeners(this, &_started, callStarted);
128 :
129 0 : if( aReadBytes.getLength() != nBytesToRead )
130 : {
131 0 : aReadBytes.realloc( nBytesToRead );
132 : }
133 0 : sal_Int32 i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() );
134 :
135 0 : if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None)
136 : {
137 0 : OUString message("ctr_socket.cxx:SocketConnection::read: error - ");
138 0 : message += m_socket.getErrorAsString();
139 :
140 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
141 :
142 0 : Any any;
143 0 : any <<= ioException;
144 :
145 0 : notifyListeners(this, &_error, callError(any));
146 :
147 0 : throw ioException;
148 : }
149 :
150 0 : return i;
151 : }
152 : else
153 : {
154 0 : OUString message("ctr_socket.cxx:SocketConnection::read: error - connection already closed");
155 :
156 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
157 :
158 0 : Any any;
159 0 : any <<= ioException;
160 :
161 0 : notifyListeners(this, &_error, callError(any));
162 :
163 0 : throw ioException;
164 : }
165 : }
166 :
167 0 : void SocketConnection::write( const Sequence < sal_Int8 > &seq )
168 : throw(::com::sun::star::io::IOException,
169 : ::com::sun::star::uno::RuntimeException)
170 : {
171 0 : if( ! m_nStatus )
172 : {
173 0 : if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
174 : {
175 0 : OUString message("ctr_socket.cxx:SocketConnection::write: error - ");
176 0 : message += m_socket.getErrorAsString();
177 :
178 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
179 :
180 0 : Any any;
181 0 : any <<= ioException;
182 :
183 0 : notifyListeners(this, &_error, callError(any));
184 :
185 0 : throw ioException;
186 : }
187 : }
188 : else
189 : {
190 0 : OUString message("ctr_socket.cxx:SocketConnection::write: error - connection already closed");
191 :
192 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
193 :
194 0 : Any any;
195 0 : any <<= ioException;
196 :
197 0 : notifyListeners(this, &_error, callError(any));
198 :
199 0 : throw ioException;
200 : }
201 0 : }
202 :
203 0 : void SocketConnection::flush( )
204 : throw(::com::sun::star::io::IOException,
205 : ::com::sun::star::uno::RuntimeException)
206 : {
207 :
208 0 : }
209 :
210 0 : void SocketConnection::close()
211 : throw(::com::sun::star::io::IOException,
212 : ::com::sun::star::uno::RuntimeException)
213 : {
214 : // ensure that close is called only once
215 0 : if( 1 == osl_atomic_increment( (&m_nStatus) ) )
216 : {
217 0 : m_socket.shutdown();
218 0 : notifyListeners(this, &_closed, callClosed);
219 : }
220 0 : }
221 :
222 0 : OUString SocketConnection::getDescription()
223 : throw( ::com::sun::star::uno::RuntimeException)
224 : {
225 0 : return m_sDescription;
226 : }
227 :
228 :
229 :
230 : // XConnectionBroadcaster
231 0 : void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
232 : {
233 0 : MutexGuard guard(_mutex);
234 :
235 0 : _listeners.insert(aListener);
236 0 : }
237 :
238 0 : void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException)
239 : {
240 0 : MutexGuard guard(_mutex);
241 :
242 0 : _listeners.erase(aListener);
243 0 : }
244 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|