-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathIpcChannel.cpp
193 lines (175 loc) · 6.2 KB
/
IpcChannel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2015, Christopher J. Foster and the other displaz contributors.
// Use of this code is governed by the BSD-style license found in LICENSE.txt
#include "IpcChannel.h"
#include <cstdint>
#include <QCoreApplication>
#include <QElapsedTimer>
#include "util.h"
#include "qtutil.h"
IpcChannel::IpcChannel(QLocalSocket* socket, QObject* parent)
: QObject(parent),
m_socket(socket),
m_messageSize(0)
{
m_socket->setParent(this);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(readReadyData()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(handleDisconnect()));
connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(handleError(QLocalSocket::LocalSocketError)));
}
std::unique_ptr<IpcChannel> IpcChannel::connectToServer(QString serverName, int timeoutMsecs)
{
std::unique_ptr<QLocalSocket> socket(new QLocalSocket());
QElapsedTimer timer;
timer.start();
qint64 currTimeout = timeoutMsecs;
while (true)
{
socket->connectToServer(serverName);
if (socket->waitForConnected(currTimeout))
return std::unique_ptr<IpcChannel>(new IpcChannel(socket.release()));
if (timeoutMsecs >= 0)
currTimeout = timeoutMsecs - timer.elapsed();
if (socket->error() == QLocalSocket::SocketTimeoutError)
{
return std::unique_ptr<IpcChannel>();
}
else if (socket->error() == QLocalSocket::UnknownSocketError)
{
// "Unknown" errors on linux may result in the socket getting into
// a bad state from which it never recovers. Make a new one.
socket.reset(new QLocalSocket());
}
// For maximum robustness, consider all other errors to be retryable.
//
// Some errors such as ServerNotFoundError may seem fatal, but a race
// condition may occur where the remote instance has only just started
// and hasn't created the socket yet. Prevent busy looping on these
// kinds of conditions by including a small sleep between retries.
milliSleep(1);
}
}
QByteArray IpcChannel::receiveMessage(int timeoutMsecs)
{
disconnect(m_socket, SIGNAL(readyRead()), 0, 0);
# ifdef _WIN32
// Win32 workaround - see waitForDisconnected()
QElapsedTimer timer;
timer.start();
while (m_socket->state() != QLocalSocket::UnconnectedState &&
(timeoutMsecs == -1 || timer.elapsed() < timeoutMsecs))
{
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
milliSleep(1);
if (appendCurrentMessage())
{
QByteArray msg = m_message;
clearCurrentMessage();
return msg;
}
}
# else
while (m_socket->waitForReadyRead(timeoutMsecs))
{
if (appendCurrentMessage())
{
QByteArray msg = m_message;
clearCurrentMessage();
return msg;
}
}
# endif
throw DisplazError("Could not read message from socket: %s",
m_socket->errorString());
}
void IpcChannel::sendMessage(const QByteArray& message)
{
QDataStream stream(m_socket);
stream << message.length();
stream.writeRawData(message.data(), message.length());
m_socket->flush();
}
bool IpcChannel::disconnectFromServer(int timeoutMsecs)
{
m_socket->disconnectFromServer();
return waitForDisconnected(timeoutMsecs);
}
bool IpcChannel::waitForDisconnected(int timeoutMsecs)
{
# ifdef _WIN32
// Aaaarghhh. QLocalSocket::waitForDisconnected() seems like it's meant to
// be a synchronous version for use without an event loop. However,
// qt-4.8.6 on windows (and it appears many other versions, including
// latest qt-5) have a bug in waitForDisconnected(): it uses a separate
// thread to do the actual write via an internal QWindowsPipeWriter.
// QLocalSocket is notified when the data has has been written via a
// connection, but this is a QueuedConnection due to being in a separate
// thread, so the signal never gets seen unless an event loop is used.
QElapsedTimer timer;
timer.start();
while (m_socket->state() != QLocalSocket::UnconnectedState &&
(timeoutMsecs == -1 || timer.elapsed() < timeoutMsecs))
{
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
milliSleep(1); // Crude, but whatever. This is a workaround.
}
return m_socket->state() == QLocalSocket::UnconnectedState;
# else
if (m_socket->state() == QLocalSocket::UnconnectedState)
return true;
return m_socket->waitForDisconnected(timeoutMsecs);
# endif
}
//--------------------------------------------------
// Private functions
/// Read and emit messages until data runs out
void IpcChannel::readReadyData()
{
while (appendCurrentMessage())
{
emit messageReceived(m_message);
clearCurrentMessage();
}
}
/// Log error messages
void IpcChannel::handleError(QLocalSocket::LocalSocketError errorCode)
{
if (errorCode == QLocalSocket::PeerClosedError)
return;
qWarning() << "Socket failure " << errorCode << ": " << m_socket->errorString();
}
void IpcChannel::handleDisconnect()
{
disconnect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(handleError(QLocalSocket::LocalSocketError)));
if (!m_message.isEmpty())
qWarning() << "Ignoring partial message: " << m_message;
emit disconnected();
}
/// Append bytes to current message from socket. Return true if
/// m_message is complete.
bool IpcChannel::appendCurrentMessage()
{
qint64 avail = m_socket->bytesAvailable();
if (m_messageSize == 0)
{
if (avail < (qint64)sizeof(quint32))
return false;
QDataStream stream(m_socket);
stream >> m_messageSize;
}
quint32 toRead = (quint32)std::min<qint64>(m_messageSize - m_message.length(), avail);
m_message.append(m_socket->read(toRead));
if (m_message.length() == (int)m_messageSize)
{
//qWarning() << "IPC:" << m_message;
return true;
}
return false;
}
/// Reset current message and make ready for next message from socket
void IpcChannel::clearCurrentMessage()
{
m_message.clear();
m_messageSize = 0;
}