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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "languageclientinterface.h"
#include "languageclienttr.h"
#include <QLocalSocket>
#include <QLoggingCategory>
#include <memory>
using namespace LanguageServerProtocol;
using namespace Utils;
static Q_LOGGING_CATEGORY(LOGLSPCLIENTV, "qtc.languageclient.messages", QtWarningMsg);
namespace LanguageClient {
BaseClientInterface::BaseClientInterface()
{
m_buffer.open(QIODevice::ReadWrite | QIODevice::Append);
}
BaseClientInterface::~BaseClientInterface()
{
m_buffer.close();
}
void BaseClientInterface::sendMessage(const JsonRpcMessage message)
{
const BaseMessage baseMessage = message.toBaseMessage();
sendData(baseMessage.header());
sendData(baseMessage.content);
}
void BaseClientInterface::resetBuffer()
{
m_buffer.close();
m_buffer.setData(nullptr);
m_buffer.open(QIODevice::ReadWrite | QIODevice::Append);
}
void BaseClientInterface::parseData(const QByteArray &data)
{
const qint64 preWritePosition = m_buffer.pos();
qCDebug(parseLog) << "parse buffer pos: " << preWritePosition;
qCDebug(parseLog) << " data: " << data;
if (!m_buffer.atEnd())
m_buffer.seek(preWritePosition + m_buffer.bytesAvailable());
m_buffer.write(data);
m_buffer.seek(preWritePosition);
while (!m_buffer.atEnd()) {
QString parseError;
BaseMessage::parse(&m_buffer, parseError, m_currentMessage);
qCDebug(parseLog) << " complete: " << m_currentMessage.isComplete();
qCDebug(parseLog) << " length: " << m_currentMessage.contentLength;
qCDebug(parseLog) << " content: " << m_currentMessage.content;
if (!parseError.isEmpty())
emit error(parseError);
if (!m_currentMessage.isComplete())
break;
parseCurrentMessage();
}
if (m_buffer.atEnd()) {
m_buffer.close();
m_buffer.setData(nullptr);
m_buffer.open(QIODevice::ReadWrite | QIODevice::Append);
}
}
void BaseClientInterface::parseCurrentMessage()
{
if (m_currentMessage.mimeType == JsonRpcMessage::jsonRpcMimeType()) {
emit messageReceived(JsonRpcMessage(m_currentMessage));
} else {
emit error(Tr::tr("Cannot handle MIME type \"%1\" of message.")
.arg(QString::fromUtf8(m_currentMessage.mimeType)));
}
m_currentMessage = BaseMessage();
}
StdIOClientInterface::StdIOClientInterface()
: m_logFile("lspclient.XXXXXX.log")
{
m_logFile.setAutoRemove(false);
QTC_CHECK(m_logFile.open());
}
StdIOClientInterface::~StdIOClientInterface()
{
delete m_process;
}
void StdIOClientInterface::startImpl()
{
if (m_process) {
QTC_CHECK(!m_process->isRunning());
delete m_process;
}
m_process = new Process;
m_process->setProcessMode(ProcessMode::Writer);
connect(m_process, &Process::readyReadStandardError,
this, &StdIOClientInterface::readError);
connect(m_process, &Process::readyReadStandardOutput,
this, &StdIOClientInterface::readOutput);
connect(m_process, &Process::started, this, &StdIOClientInterface::started);
connect(m_process, &Process::done, this, [this] {
m_logFile.flush();
if (m_process->result() != ProcessResult::FinishedWithSuccess)
emit error(QString("%1 (see logs in \"%2\")")
.arg(m_process->exitMessage())
.arg(m_logFile.fileName()));
emit finished();
});
m_logFile.write(QString("Starting server: %1\nOutput:\n\n").arg(m_cmd.toUserOutput()).toUtf8());
m_process->setCommand(m_cmd);
m_process->setWorkingDirectory(m_workingDirectory);
if (m_env)
m_process->setEnvironment(*m_env);
else
m_process->setEnvironment(m_cmd.executable().deviceEnvironment());
m_process->setAllowCoreDumps(m_allowCoreDumps);
m_process->start();
}
void StdIOClientInterface::setCommandLine(const CommandLine &cmd)
{
m_cmd = cmd;
}
void StdIOClientInterface::setWorkingDirectory(const FilePath &workingDirectory)
{
m_workingDirectory = workingDirectory;
}
void StdIOClientInterface::setEnvironment(const Environment &environment)
{
m_env = environment;
}
void StdIOClientInterface::setAllowCoreDumps(bool enable)
{
m_allowCoreDumps = enable;
}
FilePath StdIOClientInterface::serverDeviceTemplate() const
{
return m_cmd.executable();
}
void StdIOClientInterface::sendData(const QByteArray &data)
{
if (!m_process || m_process->state() != QProcess::Running) {
emit error(Tr::tr("Cannot send data to unstarted server %1")
.arg(m_cmd.toUserOutput()));
return;
}
qCDebug(LOGLSPCLIENTV) << "StdIOClient send data:";
qCDebug(LOGLSPCLIENTV).noquote() << data;
m_process->writeRaw(data);
}
void StdIOClientInterface::readError()
{
QTC_ASSERT(m_process, return);
const QByteArray stdErr = m_process->readAllRawStandardError();
m_logFile.write(stdErr);
qCDebug(LOGLSPCLIENTV) << "StdIOClient std err:\n";
qCDebug(LOGLSPCLIENTV).noquote() << stdErr;
}
void StdIOClientInterface::readOutput()
{
QTC_ASSERT(m_process, return);
const QByteArray &out = m_process->readAllRawStandardOutput();
qCDebug(LOGLSPCLIENTV) << "StdIOClient std out:\n";
qCDebug(LOGLSPCLIENTV).noquote() << out;
parseData(out);
}
class LocalSocketClientInterface::Private
{
public:
Private(LocalSocketClientInterface *q, const QString &serverName)
: q(q), serverName(serverName) {}
void discardSocket();
LocalSocketClientInterface * const q;
const QString serverName;
std::unique_ptr<QLocalSocket> socket;
};
LocalSocketClientInterface::LocalSocketClientInterface(const QString &serverName)
: d(new Private(this, serverName))
{
}
LocalSocketClientInterface::~LocalSocketClientInterface()
{
d->discardSocket();
delete d;
}
void LocalSocketClientInterface::startImpl()
{
d->discardSocket();
d->socket.reset(new QLocalSocket);
d->socket->setServerName(d->serverName); // TODO: Map path?
connect(d->socket.get(), &QLocalSocket::errorOccurred, this,
[this] { emit error(d->socket->errorString()); });
connect(d->socket.get(), &QLocalSocket::readyRead, this,
[this] { parseData(d->socket->readAll()); });
connect(d->socket.get(), &QLocalSocket::connected, this, &LocalSocketClientInterface::started);
connect(d->socket.get(), &QLocalSocket::disconnected, this, &LocalSocketClientInterface::finished);
d->socket->connectToServer();
}
void LocalSocketClientInterface::sendData(const QByteArray &data)
{
d->socket->write(data);
}
void LocalSocketClientInterface::Private::discardSocket()
{
if (socket) {
socket->disconnect(q);
socket->disconnectFromServer();
}
}
} // namespace LanguageClient
|