source: trunk/src/network/socket/qlocalserver.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 11.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtNetwork module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qlocalserver.h"
43#include "qlocalserver_p.h"
44#include "qlocalsocket.h"
45
46QT_BEGIN_NAMESPACE
47
48#ifndef QT_NO_LOCALSERVER
49
50/*!
51 \class QLocalServer
52 \since 4.4
53
54 \brief The QLocalServer class provides a local socket based server.
55
56 This class makes it possible to accept incoming local socket
57 connections.
58
59 Call listen() to have the server start listening
60 for incoming connections on a specified key. The
61 newConnection() signal is then emitted each time a client
62 connects to the server.
63
64 Call nextPendingConnection() to accept the pending connection
65 as a connected QLocalSocket. The function returns a pointer to a
66 QLocalSocket that can be used for communicating with the client.
67
68 If an error occurs, serverError() returns the type of error, and
69 errorString() can be called to get a human readable description
70 of what happened.
71
72 When listening for connections, the name which the server is
73 listening on is available through serverName().
74
75 Calling close() makes QLocalServer stop listening for incoming connections.
76
77 Although QLocalServer is designed for use with an event loop, it's possible
78 to use it without one. In that case, you must use waitForNewConnection(),
79 which blocks until either a connection is available or a timeout expires.
80
81 \sa QLocalSocket, QTcpServer
82*/
83
84/*!
85 Create a new local socket server with the given \a parent.
86
87 \sa listen()
88 */
89QLocalServer::QLocalServer(QObject *parent)
90 : QObject(*new QLocalServerPrivate, parent)
91{
92 Q_D(QLocalServer);
93 d->init();
94}
95
96/*!
97 Destroys the QLocalServer object. If the server is listening for
98 connections, it is automatically closed.
99
100 Any client QLocalSockets that are still connected must either
101 disconnect or be reparented before the server is deleted.
102
103 \sa close()
104 */
105QLocalServer::~QLocalServer()
106{
107 if (isListening())
108 close();
109}
110
111/*!
112 Stop listening for incoming connections. Existing connections are not
113 effected, but any new connections will be refused.
114
115 \sa isListening(), listen()
116 */
117void QLocalServer::close()
118{
119 Q_D(QLocalServer);
120 if (!isListening())
121 return;
122 qDeleteAll(d->pendingConnections);
123 d->pendingConnections.clear();
124 d->closeServer();
125 d->serverName.clear();
126 d->fullServerName.clear();
127 d->errorString.clear();
128 d->error = QAbstractSocket::UnknownSocketError;
129}
130
131/*!
132 Returns the human-readable message appropriate to the current error
133 reported by serverError(). If no suitable string is available, an empty
134 string is returned.
135
136 \sa serverError()
137 */
138QString QLocalServer::errorString() const
139{
140 Q_D(const QLocalServer);
141 return d->errorString;
142}
143
144/*!
145 Returns true if the server has a pending connection; otherwise
146 returns false.
147
148 \sa nextPendingConnection(), setMaxPendingConnections()
149 */
150bool QLocalServer::hasPendingConnections() const
151{
152 Q_D(const QLocalServer);
153 return !(d->pendingConnections.isEmpty());
154}
155
156/*!
157 This virtual function is called by QLocalServer when a new connection
158 is available. \a socketDescriptor is the native socket descriptor for
159 the accepted connection.
160
161 The base implementation creates a QLocalSocket, sets the socket descriptor
162 and then stores the QLocalSocket in an internal list of pending
163 connections. Finally newConnection() is emitted.
164
165 Reimplement this function to alter the server's behavior
166 when a connection is available.
167
168 \sa newConnection(), nextPendingConnection(),
169 QLocalSocket::setSocketDescriptor()
170 */
171void QLocalServer::incomingConnection(quintptr socketDescriptor)
172{
173 Q_D(QLocalServer);
174 QLocalSocket *socket = new QLocalSocket(this);
175 socket->setSocketDescriptor(socketDescriptor);
176 d->pendingConnections.enqueue(socket);
177 emit newConnection();
178}
179
180/*!
181 Returns true if the server is listening for incoming connections
182 otherwise false.
183
184 \sa listen(), close()
185 */
186bool QLocalServer::isListening() const
187{
188 Q_D(const QLocalServer);
189 return !(d->serverName.isEmpty());
190}
191
192/*!
193 Tells the server to listen for incoming connections on \a name.
194 If the server is currently listening then it will return false.
195 Return true on success otherwise false.
196
197 \a name can be a single name and QLocalServer will determine
198 the correct platform specific path. serverName() will return
199 the name that is passed into listen.
200
201 Usually you would just pass in a name like "foo", but on Unix this
202 could also be a path such as "/tmp/foo" and on Windows this could
203 be a pipe path such as "\\\\.\\pipe\\foo". On OS/2, "\\socket\\" is
204 automatically prepended to the name if it does not already sttart with
205 this prefix.
206
207 Note:
208 On Unix if the server crashes without closing listen will fail
209 with AddressInUseError. To create a new server the file should be removed.
210 On Windows two local servers can listen to the same pipe at the same
211 time, but any connections will go to one of the server.
212
213 \sa serverName(), isListening(), close()
214 */
215bool QLocalServer::listen(const QString &name)
216{
217 Q_D(QLocalServer);
218 if (isListening()) {
219 qWarning("QLocalServer::listen() called when already listening");
220 return false;
221 }
222
223 if (name.isEmpty()) {
224 d->error = QAbstractSocket::HostNotFoundError;
225 QString function = QLatin1String("QLocalServer::listen");
226 d->errorString = tr("%1: Name error").arg(function);
227 return false;
228 }
229
230 if (!d->listen(name)) {
231 d->serverName.clear();
232 d->fullServerName.clear();
233 return false;
234 }
235
236 d->serverName = name;
237 return true;
238}
239
240/*!
241 Returns the maximum number of pending accepted connections.
242 The default is 30.
243
244 \sa setMaxPendingConnections(), hasPendingConnections()
245 */
246int QLocalServer::maxPendingConnections() const
247{
248 Q_D(const QLocalServer);
249 return d->maxPendingConnections;
250}
251
252/*!
253 \fn void QLocalServer::newConnection()
254
255 This signal is emitted every time a new connection is available.
256
257 \sa hasPendingConnections(), nextPendingConnection()
258*/
259
260/*!
261 Returns the next pending connection as a connected QLocalSocket object.
262
263 The socket is created as a child of the server, which means that it is
264 automatically deleted when the QLocalServer object is destroyed. It is
265 still a good idea to delete the object explicitly when you are done with
266 it, to avoid wasting memory.
267
268 0 is returned if this function is called when there are no pending
269 connections.
270
271 \sa hasPendingConnections(), newConnection(), incomingConnection()
272 */
273QLocalSocket *QLocalServer::nextPendingConnection()
274{
275 Q_D(QLocalServer);
276 if (d->pendingConnections.isEmpty())
277 return 0;
278 QLocalSocket *nextSocket = d->pendingConnections.dequeue();
279#ifdef Q_OS_SYMBIAN
280 if(!d->socketNotifier)
281 return nextSocket;
282#endif
283#ifndef QT_LOCALSOCKET_TCP
284 if (d->pendingConnections.size() <= d->maxPendingConnections)
285#ifndef Q_OS_WIN
286 d->socketNotifier->setEnabled(true);
287#else
288 d->connectionEventNotifier->setEnabled(true);
289#endif
290#endif
291 return nextSocket;
292}
293
294/*!
295 \since 4.5
296
297 Removes any server instance that might cause a call to listen() to fail
298 and returns true if successful; otherwise returns false.
299 This function is meant to recover from a crash, when the previous server
300 instance has not been cleaned up.
301
302 On Windows and OS/2, this function does nothing; on Unix, it removes the
303 socket file given by \a name.
304
305 \warning Be careful to avoid removing sockets of running instances.
306*/
307bool QLocalServer::removeServer(const QString &name)
308{
309 return QLocalServerPrivate::removeServer(name);
310}
311
312/*!
313 Returns the server name if the server is listening for connections;
314 otherwise returns QString()
315
316 \sa listen(), fullServerName()
317 */
318QString QLocalServer::serverName() const
319{
320 Q_D(const QLocalServer);
321 return d->serverName;
322}
323
324/*!
325 Returns the full path that the server is listening on.
326
327 Note: This is platform specific
328
329 \sa listen(), serverName()
330 */
331QString QLocalServer::fullServerName() const
332{
333 Q_D(const QLocalServer);
334 return d->fullServerName;
335}
336
337/*!
338 Returns the type of error that occurred last or NoError.
339
340 \sa errorString()
341 */
342QAbstractSocket::SocketError QLocalServer::serverError() const
343{
344 Q_D(const QLocalServer);
345 return d->error;
346}
347
348/*!
349 Sets the maximum number of pending accepted connections to
350 \a numConnections. QLocalServer will accept no more than
351 \a numConnections incoming connections before nextPendingConnection()
352 is called.
353
354 Note: Even though QLocalServer will stop accepting new connections
355 after it has reached its maximum number of pending connections,
356 the operating system may still keep them in queue which will result
357 in clients signaling that it is connected.
358
359 \sa maxPendingConnections(), hasPendingConnections()
360 */
361void QLocalServer::setMaxPendingConnections(int numConnections)
362{
363 Q_D(QLocalServer);
364 d->maxPendingConnections = numConnections;
365}
366
367/*!
368 Waits for at most \a msec milliseconds or until an incoming connection
369 is available. Returns true if a connection is available; otherwise
370 returns false. If the operation timed out and \a timedOut is not 0,
371 *timedOut will be set to true.
372
373 This is a blocking function call. Its use is ill-advised in a
374 single-threaded GUI application, since the whole application will stop
375 responding until the function returns. waitForNewConnection() is mostly
376 useful when there is no event loop available.
377
378 The non-blocking alternative is to connect to the newConnection() signal.
379
380 If msec is -1, this function will not time out.
381
382 \sa hasPendingConnections(), nextPendingConnection()
383 */
384bool QLocalServer::waitForNewConnection(int msec, bool *timedOut)
385{
386 Q_D(QLocalServer);
387 if (timedOut)
388 *timedOut = false;
389
390 if (!isListening())
391 return false;
392
393 d->waitForNewConnection(msec, timedOut);
394
395 return !d->pendingConnections.isEmpty();
396}
397
398#endif
399
400QT_END_NAMESPACE
401
402#include "moc_qlocalserver.cpp"
403
Note: See TracBrowser for help on using the repository browser.