1// Copyright (C) 2022 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#ifndef QABSTRACTSOCKET_P_H
5#define QABSTRACTSOCKET_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the QAbstractSocket class. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19#include "QtNetwork/qabstractsocket.h"
20#include "QtCore/qbytearray.h"
21#include "QtCore/qlist.h"
22#include "QtCore/qtimer.h"
23#include "private/qiodevice_p.h"
24#include "private/qabstractsocketengine_p.h"
25#include "qnetworkproxy.h"
26
27QT_BEGIN_NAMESPACE
28
29class QHostInfo;
30
31class QAbstractSocketPrivate : public QIODevicePrivate, public QAbstractSocketEngineReceiver
32{
33 Q_DECLARE_PUBLIC(QAbstractSocket)
34public:
35 QAbstractSocketPrivate();
36 virtual ~QAbstractSocketPrivate();
37
38 // from QAbstractSocketEngineReceiver
39 inline void readNotification() override { canReadNotification(); }
40 inline void writeNotification() override { canWriteNotification(); }
41 inline void exceptionNotification() override {}
42 inline void closeNotification() override { canCloseNotification(); }
43 void connectionNotification() override;
44#ifndef QT_NO_NETWORKPROXY
45 inline void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) override {
46 Q_Q(QAbstractSocket);
47 emit q->proxyAuthenticationRequired(proxy, authenticator);
48 }
49#endif
50
51 virtual bool bind(const QHostAddress &address, quint16 port, QAbstractSocket::BindMode mode);
52
53 virtual bool canReadNotification();
54 bool canWriteNotification();
55 void canCloseNotification();
56
57 // slots
58 void _q_connectToNextAddress();
59 void _q_startConnecting(const QHostInfo &hostInfo);
60 void _q_testConnection();
61 void _q_abortConnectionAttempt();
62
63 bool emittedReadyRead = false;
64 bool emittedBytesWritten = false;
65
66 bool abortCalled = false;
67 bool pendingClose = false;
68
69 QAbstractSocket::PauseModes pauseMode = QAbstractSocket::PauseNever;
70
71 QString hostName;
72 quint16 port = 0;
73 QHostAddress host;
74 QList<QHostAddress> addresses;
75
76 quint16 localPort = 0;
77 quint16 peerPort = 0;
78 QHostAddress localAddress;
79 QHostAddress peerAddress;
80 QString peerName;
81
82 QAbstractSocketEngine *socketEngine = nullptr;
83 qintptr cachedSocketDescriptor = -1;
84
85#ifndef QT_NO_NETWORKPROXY
86 QNetworkProxy proxy;
87 QNetworkProxy proxyInUse;
88 QString protocolTag;
89 void resolveProxy(const QString &hostName, quint16 port);
90#else
91 inline void resolveProxy(const QString &, quint16) { }
92#endif
93 inline void resolveProxy(quint16 port) { resolveProxy(hostName: QString(), port); }
94
95 void resetSocketLayer();
96 virtual bool flush();
97
98 bool initSocketLayer(QAbstractSocket::NetworkLayerProtocol protocol);
99 virtual void configureCreatedSocket();
100 void startConnectingByName(const QString &host);
101 void fetchConnectionParameters();
102 bool readFromSocket();
103 virtual bool writeToSocket();
104 void emitReadyRead(int channel = 0);
105 void emitBytesWritten(qint64 bytes, int channel = 0);
106
107 void setError(QAbstractSocket::SocketError errorCode, const QString &errorString);
108 void setErrorAndEmit(QAbstractSocket::SocketError errorCode, const QString &errorString);
109
110 qint64 readBufferMaxSize = 0;
111 bool isBuffered = false;
112 bool hasPendingData = false;
113
114 QTimer *connectTimer = nullptr;
115
116 int hostLookupId = -1;
117
118 QAbstractSocket::SocketType socketType = QAbstractSocket::UnknownSocketType;
119 QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState;
120
121 // Must be kept in sync with QIODevicePrivate::errorString.
122 QAbstractSocket::SocketError socketError = QAbstractSocket::UnknownSocketError;
123
124 QAbstractSocket::NetworkLayerProtocol preferredNetworkLayerProtocol =
125 QAbstractSocket::UnknownNetworkLayerProtocol;
126
127 bool prePauseReadSocketNotifierState = false;
128 bool prePauseWriteSocketNotifierState = false;
129 bool prePauseExceptionSocketNotifierState = false;
130 static void pauseSocketNotifiers(QAbstractSocket*);
131 static void resumeSocketNotifiers(QAbstractSocket*);
132 static QAbstractSocketEngine* getSocketEngine(QAbstractSocket*);
133};
134
135QT_END_NAMESPACE
136
137#endif // QABSTRACTSOCKET_P_H
138

source code of qtbase/src/network/socket/qabstractsocket_p.h