Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qlocalserver.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qlocalserver.h"
5#include "qlocalserver_p.h"
6#include "qlocalsocket.h"
7
8#if defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)
9#include <QtCore/qt_windows.h>
10#endif
11
13
14using namespace Qt::StringLiterals;
15
88 : QObject(*new QLocalServerPrivate, parent)
89{
90 Q_D(QLocalServer);
91 d->init();
92}
93
104{
105 if (isListening())
106 close();
107}
108
146void QLocalServer::setSocketOptions(SocketOptions options)
147{
148 Q_D(QLocalServer);
149
150 d->socketOptions = options;
151}
152
159QLocalServer::SocketOptions QLocalServer::socketOptions() const
160{
161 Q_D(const QLocalServer);
162 return d->socketOptions;
163}
164
165QBindable<QLocalServer::SocketOptions> QLocalServer::bindableSocketOptions()
166{
167 Q_D(QLocalServer);
168 return &d->socketOptions;
169}
170
192{
193 Q_D(const QLocalServer);
194 if (!isListening())
195 return -1;
196#if defined(QT_LOCALSOCKET_TCP)
197 return d->tcpServer.socketDescriptor();
198#elif defined(Q_OS_WIN)
199 const auto handle = d->connectionEventNotifier->handle();
200 return handle != INVALID_HANDLE_VALUE ? qintptr(handle) : -1;
201#else
202 return d->socketNotifier->socket();
203#endif
204}
205
213{
214 Q_D(QLocalServer);
215 if (!isListening())
216 return;
217 qDeleteAll(d->pendingConnections);
218 d->pendingConnections.clear();
219 d->closeServer();
220 d->serverName.clear();
221 d->fullServerName.clear();
222 d->errorString.clear();
224}
225
234{
235 Q_D(const QLocalServer);
236 return d->errorString;
237}
238
246{
247 Q_D(const QLocalServer);
248 return !(d->pendingConnections.isEmpty());
249}
250
272
287{
288 Q_D(QLocalServer);
289 d->pendingConnections.enqueue(socket);
291}
292
300{
301 Q_D(const QLocalServer);
302 return !(d->serverName.isEmpty());
303}
304
326{
327 Q_D(QLocalServer);
328 if (isListening()) {
329 qWarning("QLocalServer::listen() called when already listening");
330 return false;
331 }
332
333 if (name.isEmpty()) {
335 QString function = "QLocalServer::listen"_L1;
336 d->errorString = tr("%1: Name error").arg(function);
337 return false;
338 }
339
340 if (!d->listen(name)) {
341 d->serverName.clear();
342 d->fullServerName.clear();
343 return false;
344 }
345
346 d->serverName = name;
347 return true;
348}
349
368bool QLocalServer::listen(qintptr socketDescriptor)
369{
370 Q_D(QLocalServer);
371 if (isListening()) {
372 qWarning("QLocalServer::listen() called when already listening");
373 return false;
374 }
375
376 d->serverName.clear();
377 d->fullServerName.clear();
378
379 if (!d->listen(socketDescriptor)) {
380 return false;
381 }
382
383 return true;
384}
385
393{
394 Q_D(const QLocalServer);
395 return d->maxPendingConnections;
396}
397
420{
421 Q_D(QLocalServer);
422 if (d->pendingConnections.isEmpty())
423 return nullptr;
424 QLocalSocket *nextSocket = d->pendingConnections.dequeue();
425#ifndef QT_LOCALSOCKET_TCP
426 if (d->pendingConnections.size() <= d->maxPendingConnections)
427#ifndef Q_OS_WIN
428 d->socketNotifier->setEnabled(true);
429#else
430 d->connectionEventNotifier->setEnabled(true);
431#endif
432#endif
433 return nextSocket;
434}
435
453
461{
462 Q_D(const QLocalServer);
463 return d->serverName;
464}
465
474{
475 Q_D(const QLocalServer);
476 return d->fullServerName;
477}
478
485{
486 Q_D(const QLocalServer);
487 return d->error;
488}
489
504{
505 Q_D(QLocalServer);
506 d->maxPendingConnections = numConnections;
507}
508
526bool QLocalServer::waitForNewConnection(int msec, bool *timedOut)
527{
528 Q_D(QLocalServer);
529 if (timedOut)
530 *timedOut = false;
531
532 if (!isListening())
533 return false;
534
535 d->waitForNewConnection(msec, timedOut);
536
537 return !d->pendingConnections.isEmpty();
538}
539
552{
553 Q_D(QLocalServer);
554 d->listenBacklog = size;
555}
556
565{
566 Q_D(const QLocalServer);
567 return d->listenBacklog;
568}
569
571
572#include "moc_qlocalserver.cpp"
573
SocketError
This enum describes the socket errors that can occur.
virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QAbstractSocket with the native socket descriptor socketDescriptor.
static bool removeServer(const QString &name)
The QLocalServer class provides a local socket based server.
QString fullServerName() const
Returns the full path that the server is listening on.
bool waitForNewConnection(int msec=0, bool *timedOut=nullptr)
Waits for at most msec milliseconds or until an incoming connection is available.
QString errorString() const
Returns the human-readable message appropriate to the current error reported by serverError().
virtual void incomingConnection(quintptr socketDescriptor)
This virtual function is called by QLocalServer when a new connection is available.
void newConnection()
This signal is emitted every time a new connection is available.
QLocalServer(QObject *parent=nullptr)
Create a new local socket server with the given parent.
SocketOptions socketOptions
the socket options that control how the socket operates.
QAbstractSocket::SocketError serverError() const
Returns the type of error that occurred last or NoError.
void setSocketOptions(SocketOptions options)
void setListenBacklogSize(int size)
Sets the backlog queue size of to be accepted connections to size.
QBindable< SocketOptions > bindableSocketOptions()
int maxPendingConnections() const
Returns the maximum number of pending accepted connections.
virtual bool hasPendingConnections() const
Returns true if the server has a pending connection; otherwise returns false.
QString serverName() const
Returns the server name if the server is listening for connections; otherwise returns QString()
int listenBacklogSize() const
Returns the backlog queue size of to be accepted connections.
static bool removeServer(const QString &name)
void addPendingConnection(QLocalSocket *socket)
This function is called by QLocalServer::incomingConnection() to add the socket to the list of pendin...
~QLocalServer()
Destroys the QLocalServer object.
bool listen(const QString &name)
Tells the server to listen for incoming connections on name.
virtual QLocalSocket * nextPendingConnection()
Returns the next pending connection as a connected QLocalSocket object.
void close()
Stop listening for incoming connections.
qintptr socketDescriptor() const
bool isListening() const
Returns true if the server is listening for incoming connections otherwise false.
void setMaxPendingConnections(int numConnections)
Sets the maximum number of pending accepted connections to numConnections.
The QLocalSocket class provides a local socket.
\inmodule QtCore
Definition qobject.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
GLuint64 GLenum void * handle
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint name
#define tr(X)
#define emit
size_t quintptr
Definition qtypes.h:167
ptrdiff_t qintptr
Definition qtypes.h:166
QTcpSocket * socket
[1]