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 | #include "qlocalserver.h"
|
---|
43 | #include "qlocalserver_p.h"
|
---|
44 | #include "qlocalsocket.h"
|
---|
45 |
|
---|
46 | #include <qdebug.h>
|
---|
47 | #include <qdatetime.h>
|
---|
48 | #include <qcoreapplication.h>
|
---|
49 | #include <QMetaType>
|
---|
50 |
|
---|
51 | // The buffer size need to be 0 otherwise data could be
|
---|
52 | // lost if the socket that has written data closes the connection
|
---|
53 | // before it is read. Pipewriter is used for write buffering.
|
---|
54 | #define BUFSIZE 0
|
---|
55 |
|
---|
56 | QT_BEGIN_NAMESPACE
|
---|
57 |
|
---|
58 | QLocalServerThread::QLocalServerThread(QObject *parent) : QThread(parent),
|
---|
59 | maxPendingConnections(1)
|
---|
60 | {
|
---|
61 | stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
62 | gotConnectionEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
63 | }
|
---|
64 |
|
---|
65 | QLocalServerThread::~QLocalServerThread()
|
---|
66 | {
|
---|
67 | stop();
|
---|
68 | closeServer();
|
---|
69 | CloseHandle(stopEvent);
|
---|
70 | CloseHandle(gotConnectionEvent);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void QLocalServerThread::stop()
|
---|
74 | {
|
---|
75 | if (isRunning()) {
|
---|
76 | SetEvent(stopEvent);
|
---|
77 | wait();
|
---|
78 | ResetEvent(stopEvent);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | void QLocalServerThread::closeServer()
|
---|
83 | {
|
---|
84 | while (!pendingHandles.isEmpty())
|
---|
85 | CloseHandle(pendingHandles.dequeue());
|
---|
86 | }
|
---|
87 |
|
---|
88 | QString QLocalServerThread::setName(const QString &name)
|
---|
89 | {
|
---|
90 | QString pipePath = QLatin1String("\\\\.\\pipe\\");
|
---|
91 | if (name.startsWith(pipePath))
|
---|
92 | fullServerName = name;
|
---|
93 | else
|
---|
94 | fullServerName = pipePath + name;
|
---|
95 | for (int i = pendingHandles.count(); i < maxPendingConnections; ++i)
|
---|
96 | if (!makeHandle())
|
---|
97 | break;
|
---|
98 | return fullServerName;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool QLocalServerThread::makeHandle()
|
---|
102 | {
|
---|
103 | if (pendingHandles.count() >= maxPendingConnections)
|
---|
104 | return false;
|
---|
105 |
|
---|
106 | HANDLE handle = INVALID_HANDLE_VALUE;
|
---|
107 | QT_WA({
|
---|
108 | handle = CreateNamedPipeW(
|
---|
109 | (TCHAR*)fullServerName.utf16(), // pipe name
|
---|
110 | PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, // read/write access
|
---|
111 | PIPE_TYPE_MESSAGE | // message type pipe
|
---|
112 | PIPE_READMODE_MESSAGE | // message-read mode
|
---|
113 | PIPE_WAIT, // blocking mode
|
---|
114 | PIPE_UNLIMITED_INSTANCES, // max. instances
|
---|
115 | BUFSIZE, // output buffer size
|
---|
116 | BUFSIZE, // input buffer size
|
---|
117 | 3000, // client time-out
|
---|
118 | NULL);
|
---|
119 | }, {
|
---|
120 | handle = CreateNamedPipeA(
|
---|
121 | fullServerName.toLocal8Bit().constData(), // pipe name
|
---|
122 | PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, // read/write access
|
---|
123 | PIPE_TYPE_MESSAGE | // message type pipe
|
---|
124 | PIPE_READMODE_MESSAGE | // message-read mode
|
---|
125 | PIPE_WAIT, // blocking mode
|
---|
126 | PIPE_UNLIMITED_INSTANCES, // max. instances
|
---|
127 | BUFSIZE, // output buffer size
|
---|
128 | BUFSIZE, // input buffer size
|
---|
129 | 3000, // client time-out
|
---|
130 | NULL);
|
---|
131 | });
|
---|
132 |
|
---|
133 | if (INVALID_HANDLE_VALUE == handle) {
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | pendingHandles.enqueue(handle);
|
---|
137 | return true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void QLocalServerThread::run()
|
---|
141 | {
|
---|
142 | OVERLAPPED op;
|
---|
143 | HANDLE handleArray[2];
|
---|
144 | memset(&op, 0, sizeof(op));
|
---|
145 | handleArray[0] = op.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
---|
146 | handleArray[1] = stopEvent;
|
---|
147 | HANDLE handle = INVALID_HANDLE_VALUE;
|
---|
148 |
|
---|
149 | forever {
|
---|
150 | if (INVALID_HANDLE_VALUE == handle) {
|
---|
151 | makeHandle();
|
---|
152 | if (!pendingHandles.isEmpty())
|
---|
153 | handle = pendingHandles.dequeue();
|
---|
154 | }
|
---|
155 | if (INVALID_HANDLE_VALUE == handle) {
|
---|
156 | int windowsError = GetLastError();
|
---|
157 | QString function = QLatin1String("QLocalServer::run");
|
---|
158 | QString errorString = QLocalServer::tr("%1: Unknown error %2").arg(function).arg(windowsError);
|
---|
159 | emit error(QAbstractSocket::UnknownSocketError, errorString);
|
---|
160 | CloseHandle(handleArray[0]);
|
---|
161 | SetEvent(gotConnectionEvent);
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | BOOL isConnected = ConnectNamedPipe(handle, &op) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
|
---|
166 | if (!isConnected) {
|
---|
167 | switch (WaitForMultipleObjects(2, handleArray, FALSE, INFINITE))
|
---|
168 | {
|
---|
169 | case WAIT_OBJECT_0 + 1:
|
---|
170 | CloseHandle(handle);
|
---|
171 | CloseHandle(handleArray[0]);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | emit connected(handle);
|
---|
176 | handle = INVALID_HANDLE_VALUE;
|
---|
177 | ResetEvent(handleArray[0]);
|
---|
178 | SetEvent(gotConnectionEvent);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | void QLocalServerPrivate::init()
|
---|
183 | {
|
---|
184 | Q_Q(QLocalServer);
|
---|
185 | qRegisterMetaType<HANDLE>("HANDLE");
|
---|
186 | q->connect(&waitForConnection, SIGNAL(connected(HANDLE)),
|
---|
187 | q, SLOT(_q_openSocket(HANDLE)), Qt::QueuedConnection);
|
---|
188 | q->connect(&waitForConnection, SIGNAL(finished()),
|
---|
189 | q, SLOT(_q_stoppedListening()), Qt::QueuedConnection);
|
---|
190 | q->connect(&waitForConnection, SIGNAL(terminated()),
|
---|
191 | q, SLOT(_q_stoppedListening()), Qt::QueuedConnection);
|
---|
192 | q->connect(&waitForConnection, SIGNAL(error(QAbstractSocket::SocketError, const QString &)),
|
---|
193 | q, SLOT(_q_setError(QAbstractSocket::SocketError, const QString &)));
|
---|
194 | }
|
---|
195 |
|
---|
196 | bool QLocalServerPrivate::removeServer(const QString &name)
|
---|
197 | {
|
---|
198 | Q_UNUSED(name);
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | bool QLocalServerPrivate::listen(const QString &name)
|
---|
203 | {
|
---|
204 | fullServerName = waitForConnection.setName(name);
|
---|
205 | serverName = name;
|
---|
206 | waitForConnection.start();
|
---|
207 | return true;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void QLocalServerPrivate::_q_setError(QAbstractSocket::SocketError e, const QString &eString)
|
---|
211 | {
|
---|
212 | error = e;
|
---|
213 | errorString = eString;
|
---|
214 | }
|
---|
215 |
|
---|
216 | void QLocalServerPrivate::_q_stoppedListening()
|
---|
217 | {
|
---|
218 | Q_Q(QLocalServer);
|
---|
219 | if (!inWaitingFunction)
|
---|
220 | q->close();
|
---|
221 | }
|
---|
222 |
|
---|
223 | void QLocalServerPrivate::_q_openSocket(HANDLE handle)
|
---|
224 | {
|
---|
225 | Q_Q(QLocalServer);
|
---|
226 | q->incomingConnection((int)handle);
|
---|
227 | }
|
---|
228 |
|
---|
229 | void QLocalServerPrivate::closeServer()
|
---|
230 | {
|
---|
231 | waitForConnection.stop();
|
---|
232 | waitForConnection.closeServer();
|
---|
233 | }
|
---|
234 |
|
---|
235 | void QLocalServerPrivate::waitForNewConnection(int msecs, bool *timedOut)
|
---|
236 | {
|
---|
237 | Q_Q(QLocalServer);
|
---|
238 | if (!pendingConnections.isEmpty() || !q->isListening())
|
---|
239 | return;
|
---|
240 |
|
---|
241 | DWORD result = WaitForSingleObject(waitForConnection.gotConnectionEvent,
|
---|
242 | (msecs == -1) ? INFINITE : msecs);
|
---|
243 | if (result == WAIT_TIMEOUT) {
|
---|
244 | if (timedOut)
|
---|
245 | *timedOut = true;
|
---|
246 | } else {
|
---|
247 | ResetEvent(waitForConnection.gotConnectionEvent);
|
---|
248 | QCoreApplication::instance()->processEvents();
|
---|
249 | }
|
---|
250 | }
|
---|
|
---|