1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QLOCALSOCKET_P_H
43 #define QLOCALSOCKET_P_H
44 
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists for the convenience
50 // of the QLocalSocket class.  This header file may change from
51 // version to version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #ifndef QT_NO_LOCALSOCKET
57 
58 #include "qlocalsocket.h"
59 #include "private/qiodevice_p.h"
60 
61 #include <qtimer.h>
62 
63 #if defined(QT_LOCALSOCKET_TCP)
64 #   include "qtcpsocket.h"
65 #elif defined(Q_OS_WIN)
66 #   include "private/qwindowspipewriter_p.h"
67 #   include "private/qringbuffer_p.h"
68 #   include <private/qwineventnotifier_p.h>
69 #else
70 #   include "private/qabstractsocketengine_p.h"
71 #   include <qtcpsocket.h>
72 #   include <qsocketnotifier.h>
73 #   include <errno.h>
74 #endif
75 
76 QT_BEGIN_NAMESPACE
77 
78 #if !defined(Q_OS_WIN) || defined(QT_LOCALSOCKET_TCP)
79 class QLocalUnixSocket : public QTcpSocket
80 {
81 
82 public:
QLocalUnixSocket()83     QLocalUnixSocket() : QTcpSocket()
84     {
85     };
86 
setSocketState(QAbstractSocket::SocketState state)87     inline void setSocketState(QAbstractSocket::SocketState state)
88     {
89         QTcpSocket::setSocketState(state);
90     };
91 
setErrorString(const QString & string)92     inline void setErrorString(const QString &string)
93     {
94         QTcpSocket::setErrorString(string);
95     }
96 
setSocketError(QAbstractSocket::SocketError error)97     inline void setSocketError(QAbstractSocket::SocketError error)
98     {
99         QTcpSocket::setSocketError(error);
100     }
101 
readData(char * data,qint64 maxSize)102     inline qint64 readData(char *data, qint64 maxSize)
103     {
104         return QTcpSocket::readData(data, maxSize);
105     }
106 
writeData(const char * data,qint64 maxSize)107     inline qint64 writeData(const char *data, qint64 maxSize)
108     {
109         return QTcpSocket::writeData(data, maxSize);
110     }
111 };
112 #endif //#if !defined(Q_OS_WIN) || defined(QT_LOCALSOCKET_TCP)
113 
114 class QLocalSocketPrivate : public QIODevicePrivate
115 {
116     Q_DECLARE_PUBLIC(QLocalSocket)
117 
118 public:
119     QLocalSocketPrivate();
120     void init();
121 
122 #if defined(QT_LOCALSOCKET_TCP)
123     QLocalUnixSocket* tcpSocket;
124     bool ownsTcpSocket;
125     void setSocket(QLocalUnixSocket*);
126     QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const;
127     void errorOccurred(QLocalSocket::LocalSocketError, const QString &function);
128     void _q_stateChanged(QAbstractSocket::SocketState newState);
129     void _q_error(QAbstractSocket::SocketError newError);
130 #elif defined(Q_OS_WIN)
131     ~QLocalSocketPrivate();
132     void destroyPipeHandles();
133     void setErrorString(const QString &function);
134     void _q_notified();
135     void _q_canWrite();
136     void _q_pipeClosed();
137     void _q_emitReadyRead();
138     DWORD checkPipeState();
139     void startAsyncRead();
140     bool completeAsyncRead();
141     void checkReadyRead();
142     HANDLE handle;
143     OVERLAPPED overlapped;
144     QWindowsPipeWriter *pipeWriter;
145     qint64 readBufferMaxSize;
146     QRingBuffer readBuffer;
147     int actualReadBufferSize;
148     QWinEventNotifier *dataReadNotifier;
149     QLocalSocket::LocalSocketError error;
150     bool readSequenceStarted;
151     bool pendingReadyRead;
152     bool pipeClosed;
153     static const qint64 initialReadBufferSize = 4096;
154 #else
155     QLocalUnixSocket unixSocket;
156     QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const;
157     void errorOccurred(QLocalSocket::LocalSocketError, const QString &function);
158     void _q_stateChanged(QAbstractSocket::SocketState newState);
159     void _q_error(QAbstractSocket::SocketError newError);
160     void _q_connectToSocket();
161     void _q_abortConnectionAttempt();
162     void cancelDelayedConnect();
163     QSocketNotifier *delayConnect;
164     QTimer *connectTimer;
165     int connectingSocket;
166     QString connectingName;
167     QIODevice::OpenMode connectingOpenMode;
168 #endif
169 
170     QString serverName;
171     QString fullServerName;
172     QLocalSocket::LocalSocketState state;
173 };
174 
175 QT_END_NAMESPACE
176 
177 #endif // QT_NO_LOCALSOCKET
178 
179 #endif // QLOCALSOCKET_P_H
180 
181