Qt 6.x
The Qt SDK
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
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
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
267{
268 Q_D(QLocalServer);
269 QLocalSocket *socket = new QLocalSocket(this);
271 d->pendingConnections.enqueue(socket);
273}
274
282{
283 Q_D(const QLocalServer);
284 return !(d->serverName.isEmpty());
285}
286
308{
309 Q_D(QLocalServer);
310 if (isListening()) {
311 qWarning("QLocalServer::listen() called when already listening");
312 return false;
313 }
314
315 if (name.isEmpty()) {
317 QString function = "QLocalServer::listen"_L1;
318 d->errorString = tr("%1: Name error").arg(function);
319 return false;
320 }
321
322 if (!d->listen(name)) {
323 d->serverName.clear();
324 d->fullServerName.clear();
325 return false;
326 }
327
328 d->serverName = name;
329 return true;
330}
331
350bool QLocalServer::listen(qintptr socketDescriptor)
351{
352 Q_D(QLocalServer);
353 if (isListening()) {
354 qWarning("QLocalServer::listen() called when already listening");
355 return false;
356 }
357
358 d->serverName.clear();
359 d->fullServerName.clear();
360
361 if (!d->listen(socketDescriptor)) {
362 return false;
363 }
364
365 return true;
366}
367
375{
376 Q_D(const QLocalServer);
377 return d->maxPendingConnections;
378}
379
402{
403 Q_D(QLocalServer);
404 if (d->pendingConnections.isEmpty())
405 return nullptr;
406 QLocalSocket *nextSocket = d->pendingConnections.dequeue();
407#ifndef QT_LOCALSOCKET_TCP
408 if (d->pendingConnections.size() <= d->maxPendingConnections)
409#ifndef Q_OS_WIN
410 d->socketNotifier->setEnabled(true);
411#else
412 d->connectionEventNotifier->setEnabled(true);
413#endif
414#endif
415 return nextSocket;
416}
417
432{
434}
435
443{
444 Q_D(const QLocalServer);
445 return d->serverName;
446}
447
456{
457 Q_D(const QLocalServer);
458 return d->fullServerName;
459}
460
467{
468 Q_D(const QLocalServer);
469 return d->error;
470}
471
486{
487 Q_D(QLocalServer);
488 d->maxPendingConnections = numConnections;
489}
490
508bool QLocalServer::waitForNewConnection(int msec, bool *timedOut)
509{
510 Q_D(QLocalServer);
511 if (timedOut)
512 *timedOut = false;
513
514 if (!isListening())
515 return false;
516
517 d->waitForNewConnection(msec, timedOut);
518
519 return !d->pendingConnections.isEmpty();
520}
521
534{
535 Q_D(QLocalServer);
536 d->listenBacklog = size;
537}
538
547{
548 Q_D(const QLocalServer);
549 return d->listenBacklog;
550}
551
553
554#include "moc_qlocalserver.cpp"
555
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.
\inmodule QtCore
Definition qproperty.h:809
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)
~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:90
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
#define qWarning
Definition qlogging.h:162
GLuint64 GLenum void * handle
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint name
#define tr(X)
#define emit
size_t quintptr
Definition qtypes.h:72
ptrdiff_t qintptr
Definition qtypes.h:71
QTcpSocket * socket
[1]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent