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 | #ifndef QLOCALSOCKET_P_H |
5 | #define QLOCALSOCKET_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 QLocalSocket 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 | |
20 | #include "qlocalsocket.h" |
21 | #include "private/qiodevice_p.h" |
22 | |
23 | #include <qtimer.h> |
24 | |
25 | QT_REQUIRE_CONFIG(localserver); |
26 | |
27 | #if defined(QT_LOCALSOCKET_TCP) |
28 | # include "qtcpsocket.h" |
29 | #elif defined(Q_OS_WIN) |
30 | # include "private/qwindowspipereader_p.h" |
31 | # include "private/qwindowspipewriter_p.h" |
32 | # include <qwineventnotifier.h> |
33 | #else |
34 | # include "private/qabstractsocketengine_p.h" |
35 | # include <qtcpsocket.h> |
36 | # include <qsocketnotifier.h> |
37 | # include <errno.h> |
38 | #endif |
39 | |
40 | struct sockaddr_un; |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | #if !defined(Q_OS_WIN) || defined(QT_LOCALSOCKET_TCP) |
45 | |
46 | class QLocalUnixSocket : public QTcpSocket |
47 | { |
48 | |
49 | public: |
50 | QLocalUnixSocket() : QTcpSocket() |
51 | { |
52 | }; |
53 | |
54 | inline void setSocketState(QAbstractSocket::SocketState state) |
55 | { |
56 | QTcpSocket::setSocketState(state); |
57 | }; |
58 | |
59 | inline void setErrorString(const QString &string) |
60 | { |
61 | QTcpSocket::setErrorString(string); |
62 | } |
63 | |
64 | inline void setSocketError(QAbstractSocket::SocketError error) |
65 | { |
66 | QTcpSocket::setSocketError(error); |
67 | } |
68 | |
69 | inline qint64 readData(char *data, qint64 maxSize) override |
70 | { |
71 | return QTcpSocket::readData(data, maxlen: maxSize); |
72 | } |
73 | |
74 | inline qint64 writeData(const char *data, qint64 maxSize) override |
75 | { |
76 | return QTcpSocket::writeData(data, len: maxSize); |
77 | } |
78 | }; |
79 | #endif //#if !defined(Q_OS_WIN) || defined(QT_LOCALSOCKET_TCP) |
80 | |
81 | class QLocalSocketPrivate : public QIODevicePrivate |
82 | { |
83 | public: |
84 | Q_DECLARE_PUBLIC(QLocalSocket) |
85 | |
86 | QLocalSocketPrivate(); |
87 | void init(); |
88 | |
89 | #if defined(QT_LOCALSOCKET_TCP) |
90 | QLocalUnixSocket* tcpSocket; |
91 | bool ownsTcpSocket; |
92 | void setSocket(QLocalUnixSocket*); |
93 | QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const; |
94 | void setErrorAndEmit(QLocalSocket::LocalSocketError, const QString &function); |
95 | void _q_stateChanged(QAbstractSocket::SocketState newState); |
96 | void _q_errorOccurred(QAbstractSocket::SocketError newError); |
97 | #elif defined(Q_OS_WIN) |
98 | ~QLocalSocketPrivate(); |
99 | qint64 pipeWriterBytesToWrite() const; |
100 | void _q_canRead(); |
101 | void _q_bytesWritten(qint64 bytes); |
102 | void _q_pipeClosed(); |
103 | void _q_winError(ulong windowsError, const QString &function); |
104 | void _q_writeFailed(); |
105 | HANDLE handle; |
106 | QWindowsPipeWriter *pipeWriter; |
107 | QWindowsPipeReader *pipeReader; |
108 | QLocalSocket::LocalSocketError error; |
109 | #else |
110 | QLocalUnixSocket unixSocket; |
111 | QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const; |
112 | void setErrorAndEmit(QLocalSocket::LocalSocketError, const QString &function); |
113 | void _q_stateChanged(QAbstractSocket::SocketState newState); |
114 | void _q_errorOccurred(QAbstractSocket::SocketError newError); |
115 | void _q_connectToSocket(); |
116 | void _q_abortConnectionAttempt(); |
117 | void cancelDelayedConnect(); |
118 | void describeSocket(qintptr socketDescriptor); |
119 | static bool parseSockaddr(const sockaddr_un &addr, uint len, |
120 | QString &fullServerName, QString &serverName, bool &abstractNamespace); |
121 | QSocketNotifier *delayConnect; |
122 | QTimer *connectTimer; |
123 | QString connectingName; |
124 | int connectingSocket; |
125 | QIODevice::OpenMode connectingOpenMode; |
126 | #endif |
127 | QLocalSocket::LocalSocketState state; |
128 | QString serverName; |
129 | QString fullServerName; |
130 | #if defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP) |
131 | bool emittedReadyRead; |
132 | bool emittedBytesWritten; |
133 | #endif |
134 | |
135 | Q_OBJECT_BINDABLE_PROPERTY(QLocalSocketPrivate, QLocalSocket::SocketOptions, socketOptions) |
136 | }; |
137 | |
138 | QT_END_NAMESPACE |
139 | |
140 | #endif // QLOCALSOCKET_P_H |
141 | |
142 | |