LibreOffice Module io (master) 1
acc_socket.cxx
Go to the documentation of this file.
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#include "acceptor.hxx"
21
22#include <unordered_set>
23
24#include <mutex>
25#include <rtl/ref.hxx>
26#include <com/sun/star/connection/XConnection.hpp>
27#include <com/sun/star/connection/XConnectionBroadcaster.hpp>
28#include <com/sun/star/connection/ConnectionSetupException.hpp>
29#include <com/sun/star/io/IOException.hpp>
31#include <utility>
32
33using namespace ::osl;
34using namespace ::cppu;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::io;
37using namespace ::com::sun::star::connection;
38
39
40namespace io_acceptor {
41
42 typedef std::unordered_set< css::uno::Reference< css::io::XStreamListener> >
44
45 namespace {
46
47 class SocketConnection : public ::cppu::WeakImplHelper<
48 css::connection::XConnection,
49 css::connection::XConnectionBroadcaster>
50
51 {
52 public:
53 explicit SocketConnection( OUString sConnectionDescription );
54
55 virtual sal_Int32 SAL_CALL read( css::uno::Sequence< sal_Int8 >& aReadBytes,
56 sal_Int32 nBytesToRead ) override;
57 virtual void SAL_CALL write( const css::uno::Sequence< sal_Int8 >& aData ) override;
58 virtual void SAL_CALL flush( ) override;
59 virtual void SAL_CALL close( ) override;
60 virtual OUString SAL_CALL getDescription( ) override;
61
62 // XConnectionBroadcaster
63 virtual void SAL_CALL addStreamListener(const css::uno::Reference< css::io::XStreamListener>& aListener) override;
64 virtual void SAL_CALL removeStreamListener(const css::uno::Reference< css::io::XStreamListener>& aListener) override;
65
66 public:
67 void completeConnectionString();
68
69 ::osl::StreamSocket m_socket;
70 oslInterlockedCount m_nStatus;
72
73 std::mutex _mutex;
75 bool _closed;
76 bool _error;
78 };
79
80 }
81
82 template<class T>
83 static void notifyListeners(SocketConnection * pCon, bool * notified, T t)
84 {
86
87 {
88 std::unique_lock guard(pCon->_mutex);
89 if(!*notified)
90 {
91 *notified = true;
92 listeners = pCon->_listeners;
93 }
94 }
95
96 for(auto& listener : listeners)
97 t(listener);
98 }
99
100 static void callStarted(const Reference<XStreamListener>& xStreamListener)
101 {
102 xStreamListener->started();
103 }
104
105 namespace {
106
107 struct callError {
108 const Any & any;
109
110 explicit callError(const Any & any);
111
112 void operator () (const Reference<XStreamListener>& xStreamListener);
113 };
114
115 }
116
117 callError::callError(const Any & aAny)
118 : any(aAny)
119 {
120 }
121
122 void callError::operator () (const Reference<XStreamListener>& xStreamListener)
123 {
124 xStreamListener->error(any);
125 }
126
127 static void callClosed(const Reference<XStreamListener>& xStreamListener)
128 {
129 xStreamListener->closed();
130 }
131
132
133 SocketConnection::SocketConnection( OUString sConnectionDescription) :
134 m_nStatus( 0 ),
135 m_sDescription(std::move( sConnectionDescription )),
136 _started(false),
137 _closed(false),
138 _error(false)
139 {
140 // make it unique
141 m_sDescription += ",uniqueValue=" ;
142 m_sDescription += OUString::number(
143 sal::static_int_cast< sal_Int64 >(
144 reinterpret_cast< sal_IntPtr >(&m_socket)) );
145 }
146
148 {
150 ",peerPort=" + OUString::number(m_socket.getPeerPort()) +
151 ",peerHost=" + m_socket.getPeerHost( ) +
152 ",localPort=" + OUString::number( m_socket.getLocalPort() ) +
153 ",localHost=" + m_socket.getLocalHost();
154 }
155
156 sal_Int32 SocketConnection::read( Sequence < sal_Int8 > & aReadBytes , sal_Int32 nBytesToRead )
157 {
158 if( ! m_nStatus )
159 {
161
162 if( aReadBytes.getLength() != nBytesToRead )
163 {
164 aReadBytes.realloc( nBytesToRead );
165 }
166
167 sal_Int32 i = m_socket.read(
168 aReadBytes.getArray(), aReadBytes.getLength());
169
170 if(i != nBytesToRead)
171 {
172 OUString message = "acc_socket.cxx:SocketConnection::read: error - " +
173 m_socket.getErrorAsString();
174
175 IOException ioException(message, static_cast<XConnection *>(this));
176
177 Any any;
178 any <<= ioException;
179
180 notifyListeners(this, &_error, callError(any));
181
182 throw ioException;
183 }
184
185 return i;
186 }
187 else
188 {
189 IOException ioException("acc_socket.cxx:SocketConnection::read: error - connection already closed", static_cast<XConnection *>(this));
190
191 Any any;
192 any <<= ioException;
193
194 notifyListeners(this, &_error, callError(any));
195
196 throw ioException;
197 }
198 }
199
200 void SocketConnection::write( const Sequence < sal_Int8 > &seq )
201 {
202 if( ! m_nStatus )
203 {
204 if( m_socket.write( seq.getConstArray() , seq.getLength() ) != seq.getLength() )
205 {
206 OUString message = "acc_socket.cxx:SocketConnection::write: error - " +
207 m_socket.getErrorAsString();
208
209 IOException ioException(message, static_cast<XConnection *>(this));
210
211 Any any;
212 any <<= ioException;
213
214 notifyListeners(this, &_error, callError(any));
215
216 throw ioException;
217 }
218 }
219 else
220 {
221 IOException ioException("acc_socket.cxx:SocketConnection::write: error - connection already closed", static_cast<XConnection *>(this));
222
223 Any any;
224 any <<= ioException;
225
226 notifyListeners(this, &_error, callError(any));
227
228 throw ioException;
229 }
230 }
231
233 {
234
235 }
236
238 {
239 // ensure close is called only once
240 if( 1 == osl_atomic_increment( (&m_nStatus) ) )
241 {
242 m_socket.shutdown();
244 }
245 }
246
248 {
249 return m_sDescription;
250 }
251
252
253 // XConnectionBroadcaster
254 void SAL_CALL SocketConnection::addStreamListener(const Reference<XStreamListener> & aListener)
255 {
256 std::unique_lock guard(_mutex);
257
258 _listeners.insert(aListener);
259 }
260
261 void SAL_CALL SocketConnection::removeStreamListener(const Reference<XStreamListener> & aListener)
262 {
263 std::unique_lock guard(_mutex);
264
265 _listeners.erase(aListener);
266 }
267
268 SocketAcceptor::SocketAcceptor( OUString sSocketName,
269 sal_uInt16 nPort,
270 bool bTcpNoDelay,
271 OUString sConnectionDescription) :
272 m_sSocketName(std::move( sSocketName )),
273 m_sConnectionDescription(std::move( sConnectionDescription )),
274 m_nPort( nPort ),
275 m_bTcpNoDelay( bTcpNoDelay ),
276 m_bClosed( false )
277 {
278 }
279
280
282 {
283 if( ! m_addr.setPort( m_nPort ) )
284 {
285 throw ConnectionSetupException(
286 "acc_socket.cxx:SocketAcceptor::init - error - invalid tcp/ip port " +
287 OUString::number( m_nPort ));
288 }
289 if( ! m_addr.setHostname( m_sSocketName.pData ) )
290 {
291 throw ConnectionSetupException(
292 "acc_socket.cxx:SocketAcceptor::init - error - invalid host " + m_sSocketName );
293 }
294 m_socket.setOption( osl_Socket_OptionReuseAddr, 1);
295
296 if(! m_socket.bind(m_addr) )
297 {
298 throw ConnectionSetupException(
299 "acc_socket.cxx:SocketAcceptor::init - error - couldn't bind on " +
300 m_sSocketName + ":" + OUString::number(m_nPort));
301 }
302
303 if(! m_socket.listen() )
304 {
305 throw ConnectionSetupException(
306 "acc_socket.cxx:SocketAcceptor::init - error - can't listen on " +
307 m_sSocketName + ":" + OUString::number(m_nPort) );
308 }
309 }
310
311 Reference< XConnection > SocketAcceptor::accept( )
312 {
313 rtl::Reference<SocketConnection> pConn(new SocketConnection( m_sConnectionDescription ));
314
315 if( m_socket.acceptConnection( pConn->m_socket )!= osl_Socket_Ok )
316 {
317 // stopAccepting was called
318 return Reference < XConnection > ();
319 }
320 if( m_bClosed )
321 {
322 return Reference < XConnection > ();
323 }
324
325 pConn->completeConnectionString();
326 ::osl::SocketAddr remoteAddr;
327 pConn->m_socket.getPeerAddr(remoteAddr);
328 OUString remoteHostname = remoteAddr.getHostname();
329 // we enable tcpNoDelay for loopback connections because
330 // it can make a significant speed difference on linux boxes.
331 if( m_bTcpNoDelay || remoteHostname == "localhost" ||
332 remoteHostname.startsWith("127.0.0.") )
333 {
334 sal_Int32 nTcpNoDelay = sal_Int32(true);
335 pConn->m_socket.setOption( osl_Socket_OptionTcpNoDelay , &nTcpNoDelay,
336 sizeof( nTcpNoDelay ) , osl_Socket_LevelTcp );
337 }
338
339 return pConn;
340 }
341
343 {
344 m_bClosed = true;
345 m_socket.close();
346 }
347}
348
349
350/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
OUString m_sDescription
Definition: acc_socket.cxx:71
const Any & any
Definition: acc_socket.cxx:108
std::mutex _mutex
Definition: acc_socket.cxx:73
bool _started
Definition: acc_socket.cxx:74
oslInterlockedCount m_nStatus
Definition: acc_socket.cxx:70
::osl::StreamSocket m_socket
Definition: acc_socket.cxx:69
XStreamListener_hash_set _listeners
Definition: acc_socket.cxx:77
bool _error
Definition: acc_socket.cxx:76
bool _closed
Definition: acc_socket.cxx:75
::osl::AcceptorSocket m_socket
Definition: acceptor.hxx:65
css::uno::Reference< css::connection::XConnection > accept()
Definition: acc_socket.cxx:311
::osl::SocketAddr m_addr
Definition: acceptor.hxx:64
virtual void SAL_CALL close() override
Definition: acc_socket.cxx:237
virtual OUString SAL_CALL getDescription() override
Definition: acc_socket.cxx:247
XStreamListener_hash_set _listeners
Definition: connector.hxx:89
::osl::ConnectorSocket m_socket
Definition: connector.hxx:80
oslInterlockedCount m_nStatus
Definition: connector.hxx:81
virtual void SAL_CALL write(const css::uno::Sequence< sal_Int8 > &aData) override
Definition: acc_socket.cxx:200
virtual void SAL_CALL removeStreamListener(const css::uno::Reference< css::io::XStreamListener > &aListener) override
Definition: acc_socket.cxx:261
virtual void SAL_CALL flush() override
Definition: acc_socket.cxx:232
virtual void SAL_CALL addStreamListener(const css::uno::Reference< css::io::XStreamListener > &aListener) override
Definition: acc_socket.cxx:254
virtual sal_Int32 SAL_CALL read(css::uno::Sequence< sal_Int8 > &aReadBytes, sal_Int32 nBytesToRead) override
Definition: acc_socket.cxx:156
bool close
constexpr OUStringLiteral aData
int i
static void callClosed(const Reference< XStreamListener > &xStreamListener)
Definition: acc_socket.cxx:127
static void callStarted(const Reference< XStreamListener > &xStreamListener)
Definition: acc_socket.cxx:100
std::unordered_set< css::uno::Reference< css::io::XStreamListener > > XStreamListener_hash_set
Definition: acc_socket.cxx:43
static void notifyListeners(SocketConnection *pCon, bool *notified, T t)
Definition: acc_socket.cxx:83
static void callClosed(const Reference< XStreamListener > &xStreamListener)
Definition: ctr_socket.cxx:78
static void notifyListeners(SocketConnection *pCon, bool *notified, T t)
Definition: ctr_socket.cxx:33
static void callStarted(const Reference< XStreamListener > &xStreamListener)
Definition: ctr_socket.cxx:51