Qt 4.8
qlocalserver.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qlocalserver.h"
43 #include "qlocalserver_p.h"
44 #include "qlocalsocket.h"
45 
47 
48 #ifndef QT_NO_LOCALSERVER
49 
93  : QObject(*new QLocalServerPrivate, parent)
94 {
96  d->init();
97 }
98 
109 {
110  if (isListening())
111  close();
112 }
113 
121 {
122  Q_D(QLocalServer);
123  if (!isListening())
124  return;
125  qDeleteAll(d->pendingConnections);
126  d->pendingConnections.clear();
127  d->closeServer();
128  d->serverName.clear();
129  d->fullServerName.clear();
130  d->errorString.clear();
132 }
133 
142 {
143  Q_D(const QLocalServer);
144  return d->errorString;
145 }
146 
154 {
155  Q_D(const QLocalServer);
156  return !(d->pendingConnections.isEmpty());
157 }
158 
175 {
176  Q_D(QLocalServer);
177  QLocalSocket *socket = new QLocalSocket(this);
178  socket->setSocketDescriptor(socketDescriptor);
179  d->pendingConnections.enqueue(socket);
181 }
182 
190 {
191  Q_D(const QLocalServer);
192  return !(d->serverName.isEmpty());
193 }
194 
217 {
218  Q_D(QLocalServer);
219  if (isListening()) {
220  qWarning("QLocalServer::listen() called when already listening");
221  return false;
222  }
223 
224  if (name.isEmpty()) {
226  QString function = QLatin1String("QLocalServer::listen");
227  d->errorString = tr("%1: Name error").arg(function);
228  return false;
229  }
230 
231  if (!d->listen(name)) {
232  d->serverName.clear();
233  d->fullServerName.clear();
234  return false;
235  }
236 
237  d->serverName = name;
238  return true;
239 }
240 
248 {
249  Q_D(const QLocalServer);
250  return d->maxPendingConnections;
251 }
252 
278 {
279  Q_D(QLocalServer);
280  if (d->pendingConnections.isEmpty())
281  return 0;
282  QLocalSocket *nextSocket = d->pendingConnections.dequeue();
283 #ifndef QT_LOCALSOCKET_TCP
284 #ifdef Q_OS_SYMBIAN
285  if(!d->socketNotifier)
286  return nextSocket;
287 #endif
288  if (d->pendingConnections.size() <= d->maxPendingConnections)
289 #ifndef Q_OS_WIN
290  d->socketNotifier->setEnabled(true);
291 #else
292  d->connectionEventNotifier->setEnabled(true);
293 #endif
294 #endif
295  return nextSocket;
296 }
297 
315 {
317 }
318 
326 {
327  Q_D(const QLocalServer);
328  return d->serverName;
329 }
330 
339 {
340  Q_D(const QLocalServer);
341  return d->fullServerName;
342 }
343 
350 {
351  Q_D(const QLocalServer);
352  return d->error;
353 }
354 
369 {
370  Q_D(QLocalServer);
371  d->maxPendingConnections = numConnections;
372 }
373 
391 bool QLocalServer::waitForNewConnection(int msec, bool *timedOut)
392 {
393  Q_D(QLocalServer);
394  if (timedOut)
395  *timedOut = false;
396 
397  if (!isListening())
398  return false;
399 
400  d->waitForNewConnection(msec, timedOut);
401 
402  return !d->pendingConnections.isEmpty();
403 }
404 
405 #endif
406 
408 
409 #include "moc_qlocalserver.cpp"
410 
double d
Definition: qnumeric_p.h:62
virtual bool hasPendingConnections() const
Returns true if the server has a pending connection; otherwise returns false.
QIntegerForSizeof< void * >::Unsigned quintptr
Definition: qglobal.h:986
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString serverName() const
Returns the server name if the server is listening for connections; otherwise returns QString() ...
int maxPendingConnections() const
Returns the maximum number of pending accepted connections.
void close()
Stop listening for incoming connections.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
void newConnection()
This signal is emitted every time a new connection is available.
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
void setMaxPendingConnections(int numConnections)
Sets the maximum number of pending accepted connections to numConnections.
The QLocalSocket class provides a local socket.
Definition: qlocalsocket.h:58
~QLocalServer()
Destroys the QLocalServer object.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString fullServerName() const
Returns the full path that the server is listening on.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
bool waitForNewConnection(int msec=0, bool *timedOut=0)
Waits for at most msec milliseconds or until an incoming connection is available. ...
static bool removeServer(const QString &name)
Removes any server instance that might cause a call to listen() to fail and returns true if successfu...
const char * name
#define emit
Definition: qobjectdefs.h:76
SocketError
This enum describes the socket errors that can occur.
Q_CORE_EXPORT void qWarning(const char *,...)
The QLocalServer class provides a local socket based server.
Definition: qlocalserver.h:58
bool listen(const QString &name)
Tells the server to listen for incoming connections on name.
QLocalServer(QObject *parent=0)
Create a new local socket server with the given parent.
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
QString errorString() const
Returns the human-readable message appropriate to the current error reported by serverError().
#define Q_OS_WIN
Definition: qglobal.h:270
bool isListening() const
Returns true if the server is listening for incoming connections otherwise false. ...
virtual void incomingConnection(quintptr socketDescriptor)
This virtual function is called by QLocalServer when a new connection is available.
QAbstractSocket::SocketError serverError() const
Returns the type of error that occurred last or NoError.
virtual QLocalSocket * nextPendingConnection()
Returns the next pending connection as a connected QLocalSocket object.
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
bool setSocketDescriptor(quintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
static bool removeServer(const QString &name)