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 0 : }
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 : m_sDescription += ",uniqueValue=";
89 0 : m_sDescription += OUString::number(
90 : sal::static_int_cast< sal_Int64 >(
91 : reinterpret_cast< sal_IntPtr >(&m_socket)),
92 0 : 10 );
93 0 : }
94 :
95 0 : SocketConnection::~SocketConnection()
96 : {
97 0 : }
98 :
99 0 : void SocketConnection::completeConnectionString()
100 : {
101 : sal_Int32 nPort;
102 :
103 0 : nPort = m_socket.getPeerPort();
104 :
105 0 : OUStringBuffer buf( 256 );
106 0 : buf.appendAscii( ",peerPort=" );
107 0 : buf.append( (sal_Int32) nPort );
108 0 : buf.appendAscii( ",peerHost=" );
109 0 : buf.append( m_socket.getPeerHost() );
110 :
111 0 : buf.appendAscii( ",localPort=" );
112 0 : buf.append( (sal_Int32) nPort );
113 0 : buf.appendAscii( ",localHost=" );
114 0 : buf.append( m_socket.getLocalHost( ) );
115 :
116 0 : m_sDescription += buf.makeStringAndClear();
117 0 : }
118 :
119 0 : sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
120 : throw(::com::sun::star::io::IOException,
121 : ::com::sun::star::uno::RuntimeException, std::exception)
122 : {
123 0 : if( ! m_nStatus )
124 : {
125 0 : notifyListeners(this, &_started, callStarted);
126 :
127 0 : if( aReadBytes.getLength() != nBytesToRead )
128 : {
129 0 : aReadBytes.realloc( nBytesToRead );
130 : }
131 0 : sal_Int32 i = m_socket.read( aReadBytes.getArray() , aReadBytes.getLength() );
132 :
133 0 : if(i != nBytesToRead && m_socket.getError() != osl_Socket_E_None)
134 : {
135 0 : OUString message("ctr_socket.cxx:SocketConnection::read: error - ");
136 0 : message += m_socket.getErrorAsString();
137 :
138 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
139 :
140 0 : Any any;
141 0 : any <<= ioException;
142 :
143 0 : notifyListeners(this, &_error, callError(any));
144 :
145 0 : throw ioException;
146 : }
147 :
148 0 : return i;
149 : }
150 : else
151 : {
152 0 : OUString message("ctr_socket.cxx:SocketConnection::read: error - connection already closed");
153 :
154 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
155 :
156 0 : Any any;
157 0 : any <<= ioException;
158 :
159 0 : notifyListeners(this, &_error, callError(any));
160 :
161 0 : throw ioException;
162 : }
163 : }
164 :
165 0 : void SocketConnection::write( const Sequence < sal_Int8 > &seq )
166 : throw(::com::sun::star::io::IOException,
167 : ::com::sun::star::uno::RuntimeException, std::exception)
168 : {
169 0 : if( ! m_nStatus )
170 : {
171 0 : if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
172 : {
173 0 : OUString message("ctr_socket.cxx:SocketConnection::write: error - ");
174 0 : message += m_socket.getErrorAsString();
175 :
176 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
177 :
178 0 : Any any;
179 0 : any <<= ioException;
180 :
181 0 : notifyListeners(this, &_error, callError(any));
182 :
183 0 : throw ioException;
184 : }
185 : }
186 : else
187 : {
188 0 : OUString message("ctr_socket.cxx:SocketConnection::write: error - connection already closed");
189 :
190 0 : IOException ioException(message, Reference<XInterface>(static_cast<XConnection *>(this)));
191 :
192 0 : Any any;
193 0 : any <<= ioException;
194 :
195 0 : notifyListeners(this, &_error, callError(any));
196 :
197 0 : throw ioException;
198 : }
199 0 : }
200 :
201 0 : void SocketConnection::flush( )
202 : throw(::com::sun::star::io::IOException,
203 : ::com::sun::star::uno::RuntimeException, std::exception)
204 : {
205 :
206 0 : }
207 :
208 0 : void SocketConnection::close()
209 : throw(::com::sun::star::io::IOException,
210 : ::com::sun::star::uno::RuntimeException, std::exception)
211 : {
212 : // ensure that close is called only once
213 0 : if( 1 == osl_atomic_increment( (&m_nStatus) ) )
214 : {
215 0 : m_socket.shutdown();
216 0 : notifyListeners(this, &_closed, callClosed);
217 : }
218 0 : }
219 :
220 0 : OUString SocketConnection::getDescription()
221 : throw( ::com::sun::star::uno::RuntimeException, std::exception)
222 : {
223 0 : return m_sDescription;
224 : }
225 :
226 :
227 :
228 : // XConnectionBroadcaster
229 0 : void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException, std::exception)
230 : {
231 0 : MutexGuard guard(_mutex);
232 :
233 0 : _listeners.insert(aListener);
234 0 : }
235 :
236 0 : void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener) throw(RuntimeException, std::exception)
237 : {
238 0 : MutexGuard guard(_mutex);
239 :
240 0 : _listeners.erase(aListener);
241 0 : }
242 : }
243 :
244 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|