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_tcp.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#include "qlocalsocket_p.h"
8
9#include <qhostaddress.h>
10#include <qsettings.h>
11#include <qdebug.h>
12
14
15using namespace Qt::StringLiterals;
16
18{
19 Q_Q(QLocalServer);
20 q->connect(&tcpServer, SIGNAL(newConnection()), SLOT(_q_onNewConnection()));
21}
22
23bool QLocalServerPrivate::listen(const QString &requestedServerName)
24{
25 tcpServer.setListenBacklogSize(listenBacklog);
26
27 if (!tcpServer.listen(QHostAddress::LocalHost))
28 return false;
29
30 const auto prefix = "QLocalServer/"_L1;
31 if (requestedServerName.startsWith(prefix))
32 fullServerName = requestedServerName;
33 else
34 fullServerName = prefix + requestedServerName;
35
36 QSettings settings("QtProject"_L1, "Qt"_L1);
38 qWarning("QLocalServer::listen: server name is already in use.");
39 tcpServer.close();
40 return false;
41 }
42
43 settings.setValue(fullServerName, tcpServer.serverPort());
44 return true;
45}
46
48{
49 return tcpServer.setSocketDescriptor(socketDescriptor);
50}
51
53{
54 QSettings settings("QtProject"_L1, "Qt"_L1);
55 if (fullServerName == "QLocalServer"_L1)
57 else
59 tcpServer.close();
60}
61
62void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
63{
65 tcpServer.waitForNewConnection(msec, timedOut);
66 else if (timedOut)
67 *timedOut = false;
68}
69
71{
72 Q_Q(QLocalServer);
73 QTcpSocket* tcpSocket = tcpServer.nextPendingConnection();
74 if (!tcpSocket) {
75 qWarning("QLocalServer: no pending connection");
76 return;
77 }
78
79 tcpSocket->setParent(q);
80 const quintptr socketDescriptor = tcpSocket->socketDescriptor();
81 q->incomingConnection(socketDescriptor);
82}
83
85{
86 const auto prefix = "QLocalServer/"_L1;
88 if (name.startsWith(prefix))
90 else
91 serverName = prefix + name;
92
93 QSettings settings("QtProject"_L1, "Qt"_L1);
96
97 return true;
98}
99
bool isEmpty() const noexcept
Definition qlist.h:401
void waitForNewConnection(int msec, bool *timedOut)
static bool removeServer(const QString &name)
QQueue< QLocalSocket * > pendingConnections
bool listen(const QString &name)
The QLocalServer class provides a local socket based server.
\inmodule QtCore
Definition qsettings.h:30
bool contains(QAnyStringView key) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(QAnyStringView key, const QVariant &value)
Sets the value of setting key to value.
void remove(QAnyStringView key)
Removes the setting key and any sub-settings of key.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTcpSocket class provides a TCP socket.
Definition qtcpsocket.h:18
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
#define qWarning
Definition qlogging.h:166
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint name
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
size_t quintptr
Definition qtypes.h:167
ptrdiff_t qintptr
Definition qtypes.h:166
QSettings settings("MySoft", "Star Runner")
[0]