1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 | #else
|
---|
69 | # include "private/qnativesocketengine_p.h"
|
---|
70 | # include <qtcpsocket.h>
|
---|
71 | # include <qsocketnotifier.h>
|
---|
72 | # include <errno.h>
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | QT_BEGIN_NAMESPACE
|
---|
76 |
|
---|
77 | #if !defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)
|
---|
78 | static inline int qSocket(int af, int socketype, int proto)
|
---|
79 | {
|
---|
80 | int ret;
|
---|
81 | while((ret = qt_socket_socket(af, socketype, proto)) == -1 && errno == EINTR){}
|
---|
82 | return ret;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static inline int qBind(int fd, const sockaddr *sa, int len)
|
---|
86 | {
|
---|
87 | int ret;
|
---|
88 | while((ret = QT_SOCKET_BIND(fd, (sockaddr*)sa, len)) == -1 && errno == EINTR){}
|
---|
89 | return ret;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static inline int qConnect(int fd, const sockaddr *sa, int len)
|
---|
93 | {
|
---|
94 | int ret;
|
---|
95 | while((ret = QT_SOCKET_CONNECT(fd, (sockaddr*)sa, len)) == -1 && errno == EINTR){}
|
---|
96 | return ret;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static inline int qListen(int fd, int backlog)
|
---|
100 | {
|
---|
101 | int ret;
|
---|
102 | while((ret = qt_socket_listen(fd, backlog)) == -1 && errno == EINTR){}
|
---|
103 | return ret;
|
---|
104 | }
|
---|
105 |
|
---|
106 | static inline int qAccept(int fd, struct sockaddr *addr, QT_SOCKLEN_T *addrlen)
|
---|
107 | {
|
---|
108 | int ret;
|
---|
109 | while((ret = qt_socket_accept(fd, addr, addrlen)) == -1 && errno == EINTR){}
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 | #endif //#if !defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)
|
---|
113 |
|
---|
114 | #if !defined(Q_OS_WIN) || defined(QT_LOCALSOCKET_TCP)
|
---|
115 | class QLocalUnixSocket : public QTcpSocket
|
---|
116 | {
|
---|
117 |
|
---|
118 | public:
|
---|
119 | QLocalUnixSocket() : QTcpSocket()
|
---|
120 | {
|
---|
121 | };
|
---|
122 |
|
---|
123 | inline void setSocketState(QAbstractSocket::SocketState state)
|
---|
124 | {
|
---|
125 | QTcpSocket::setSocketState(state);
|
---|
126 | };
|
---|
127 |
|
---|
128 | inline void setErrorString(const QString &string)
|
---|
129 | {
|
---|
130 | QTcpSocket::setErrorString(string);
|
---|
131 | }
|
---|
132 |
|
---|
133 | inline void setSocketError(QAbstractSocket::SocketError error)
|
---|
134 | {
|
---|
135 | QTcpSocket::setSocketError(error);
|
---|
136 | }
|
---|
137 |
|
---|
138 | inline qint64 readData(char *data, qint64 maxSize)
|
---|
139 | {
|
---|
140 | return QTcpSocket::readData(data, maxSize);
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline qint64 writeData(const char *data, qint64 maxSize)
|
---|
144 | {
|
---|
145 | return QTcpSocket::writeData(data, maxSize);
|
---|
146 | }
|
---|
147 | };
|
---|
148 | #endif //#if !defined(Q_OS_WIN) || defined(QT_LOCALSOCKET_TCP)
|
---|
149 |
|
---|
150 | class QLocalSocketPrivate : public QIODevicePrivate
|
---|
151 | {
|
---|
152 | Q_DECLARE_PUBLIC(QLocalSocket)
|
---|
153 |
|
---|
154 | public:
|
---|
155 | QLocalSocketPrivate();
|
---|
156 | void init();
|
---|
157 |
|
---|
158 | #if defined(QT_LOCALSOCKET_TCP)
|
---|
159 | QLocalUnixSocket* tcpSocket;
|
---|
160 | bool ownsTcpSocket;
|
---|
161 | void setSocket(QLocalUnixSocket*);
|
---|
162 | QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const;
|
---|
163 | void errorOccurred(QLocalSocket::LocalSocketError, const QString &function);
|
---|
164 | void _q_stateChanged(QAbstractSocket::SocketState newState);
|
---|
165 | void _q_error(QAbstractSocket::SocketError newError);
|
---|
166 | #elif defined(Q_OS_WIN)
|
---|
167 | ~QLocalSocketPrivate() {
|
---|
168 | CloseHandle(overlapped.hEvent);
|
---|
169 | }
|
---|
170 |
|
---|
171 | void setErrorString(const QString &function);
|
---|
172 | void _q_notified();
|
---|
173 | void _q_canWrite();
|
---|
174 | void _q_pipeClosed();
|
---|
175 | qint64 readData(char *data, qint64 maxSize);
|
---|
176 | qint64 bytesAvailable();
|
---|
177 | bool readFromSocket();
|
---|
178 | HANDLE handle;
|
---|
179 | OVERLAPPED overlapped;
|
---|
180 | QWindowsPipeWriter *pipeWriter;
|
---|
181 | qint64 readBufferMaxSize;
|
---|
182 | QRingBuffer readBuffer;
|
---|
183 | QTimer dataNotifier;
|
---|
184 | QLocalSocket::LocalSocketError error;
|
---|
185 | bool readyReadEmitted;
|
---|
186 | bool pipeClosed;
|
---|
187 | #else
|
---|
188 | QLocalUnixSocket unixSocket;
|
---|
189 | QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const;
|
---|
190 | void errorOccurred(QLocalSocket::LocalSocketError, const QString &function);
|
---|
191 | void _q_stateChanged(QAbstractSocket::SocketState newState);
|
---|
192 | void _q_error(QAbstractSocket::SocketError newError);
|
---|
193 | void _q_connectToSocket();
|
---|
194 | void _q_abortConnectionAttempt();
|
---|
195 | QSocketNotifier *delayConnect;
|
---|
196 | QTimer *connectTimer;
|
---|
197 | int connectingSocket;
|
---|
198 | QString connectingName;
|
---|
199 | QIODevice::OpenMode connectingOpenMode;
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | QString serverName;
|
---|
203 | QString fullServerName;
|
---|
204 | QLocalSocket::LocalSocketState state;
|
---|
205 | };
|
---|
206 |
|
---|
207 | QT_END_NAMESPACE
|
---|
208 |
|
---|
209 | #endif // QT_NO_LOCALSOCKET
|
---|
210 |
|
---|
211 | #endif // QLOCALSOCKET_P_H
|
---|
212 |
|
---|