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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
// slang-json-rpc-connection.cpp
#include "slang-json-rpc-connection.h"
#include "../core/slang-process-util.h"
#include "../core/slang-short-list.h"
#include "../core/slang-string-util.h"
#include "slang-json-native.h"
#include "slang-json-rpc.h"
namespace Slang
{
/// Ctor
JSONRPCConnection::JSONRPCConnection()
: m_container(nullptr), m_typeMap(JSONNativeUtil::getTypeFuncsMap())
{
}
SlangResult JSONRPCConnection::init(
HTTPPacketConnection* connection,
CallStyle defaultCallStyle,
Process* process)
{
m_connection = connection;
m_process = process;
{
// If a call style isn't set, use the prefered style
const CallStyle preferedCallStyle = CallStyle::Array;
defaultCallStyle =
(defaultCallStyle == CallStyle::Default) ? preferedCallStyle : defaultCallStyle;
m_defaultCallStyle = defaultCallStyle;
}
m_sourceManager.initialize(nullptr, nullptr);
m_diagnosticSink.init(&m_sourceManager, &JSONLexer::calcLexemeLocation);
m_container.setSourceManager(&m_sourceManager);
return SLANG_OK;
}
SlangResult JSONRPCConnection::initWithStdStreams(CallStyle defaultCallStyle, Process* process)
{
RefPtr<Stream> stdinStream, stdoutStream;
Process::getStdStream(StdStreamType::In, stdinStream);
Process::getStdStream(StdStreamType::Out, stdoutStream);
RefPtr<BufferedReadStream> readStream(new BufferedReadStream(stdinStream));
RefPtr<HTTPPacketConnection> connection = new HTTPPacketConnection(readStream, stdoutStream);
return init(connection, defaultCallStyle, process);
}
void JSONRPCConnection::clearBuffers()
{
m_sourceManager.reset();
m_diagnosticSink.reset();
m_container.reset();
m_jsonRoot.reset();
}
bool JSONRPCConnection::isActive()
{
return m_connection->isActive() && (m_process == nullptr || !m_process->isTerminated());
}
JSONValue JSONRPCConnection::getCurrentMessageId()
{
SLANG_ASSERT(hasMessage());
return JSONRPCUtil::getId(&m_container, m_jsonRoot);
}
void JSONRPCConnection::disconnect()
{
if (m_process)
{
if (!m_process->isTerminated())
{
if (m_connection)
{
// Send. If succeeded, wait
if (SLANG_SUCCEEDED(sendCall(UnownedStringSlice::fromLiteral("quit"))))
{
// Wait for termination
m_process->waitForTermination(m_terminationTimeOutInMs);
}
}
if (!m_process->isTerminated())
{
// Okay, just try terminating
m_process->waitForTermination(m_terminationTimeOutInMs);
}
// Okay just kill it then
if (!m_process->isTerminated())
{
m_process->kill(-1);
}
}
m_process.setNull();
}
m_connection.setNull();
}
SlangResult JSONRPCConnection::sendRPC(const RttiInfo* rttiInfo, const void* data)
{
auto typeMap = JSONNativeUtil::getTypeFuncsMap();
// Convert to JSON
NativeToJSONConverter converter(&m_container, &typeMap, &m_diagnosticSink);
JSONValue value;
SLANG_RETURN_ON_FAIL(converter.convert(rttiInfo, data, value));
// Convert to text
JSONWriter writer(JSONWriter::IndentationStyle::Allman);
m_container.traverseRecursively(value, &writer);
const StringBuilder& builder = writer.getBuilder();
return m_connection->write(builder.getBuffer(), builder.getLength());
}
SlangResult JSONRPCConnection::sendError(JSONRPC::ErrorCode code, const JSONValue& id)
{
return sendError(code, m_diagnosticSink.outputBuffer.getUnownedSlice(), id);
}
SlangResult JSONRPCConnection::sendError(
JSONRPC::ErrorCode errorCode,
const UnownedStringSlice& msg,
const JSONValue& id)
{
JSONRPCErrorResponse errorResponse;
errorResponse.error.code = Int(errorCode);
errorResponse.error.message = msg;
errorResponse.id = id;
return sendRPC(&errorResponse);
}
SlangResult JSONRPCConnection::checkArrayObjectWrap(
const JSONValue& srcArgs,
const RttiInfo* dstArgsRttiInfo,
void* dstArgs,
const JSONValue& id)
{
if (dstArgsRttiInfo->m_kind == RttiInfo::Kind::Struct &&
srcArgs.getKind() == JSONValue::Kind::Array)
{
auto array = m_container.getArray(srcArgs);
if (array.getCount() == 1)
{
return toNativeOrSendError(array[0], dstArgsRttiInfo, dstArgs, id);
}
return SLANG_OK;
}
else
{
return toNativeOrSendError(srcArgs, dstArgsRttiInfo, dstArgs, id);
}
}
SlangResult JSONRPCConnection::toNativeArgsOrSendError(
const JSONValue& srcArgs,
const RttiInfo* dstArgsRttiInfo,
void* dstArgs,
const JSONValue& id)
{
if (dstArgsRttiInfo->m_kind == RttiInfo::Kind::Struct &&
srcArgs.getKind() == JSONValue::Kind::Array)
{
JSONToNativeConverter converter(&m_container, &m_typeMap, &m_diagnosticSink);
if (SLANG_FAILED(converter.convertArrayToStruct(srcArgs, dstArgsRttiInfo, dstArgs)))
{
return sendError(JSONRPC::ErrorCode::InvalidRequest, id);
}
return SLANG_OK;
}
else
{
return toNativeOrSendError(srcArgs, dstArgsRttiInfo, dstArgs, id);
}
}
SlangResult JSONRPCConnection::toNativeOrSendError(
const JSONValue& value,
const RttiInfo* info,
void* dst,
const JSONValue& id)
{
m_diagnosticSink.outputBuffer.clear();
JSONToNativeConverter converter(&m_container, &m_typeMap, &m_diagnosticSink);
if (SLANG_FAILED(converter.convert(value, info, dst)))
{
return sendError(JSONRPC::ErrorCode::InvalidRequest, id);
}
return SLANG_OK;
}
SlangResult JSONRPCConnection::sendCall(const UnownedStringSlice& method, const JSONValue& id)
{
JSONRPCCall call;
call.id = id;
call.method = method;
SLANG_RETURN_ON_FAIL(sendRPC(&call));
return SLANG_OK;
}
SlangResult JSONRPCConnection::sendResult(
const RttiInfo* rttiInfo,
const void* result,
const JSONValue& id)
{
JSONResultResponse response;
response.id = id;
NativeToJSONConverter converter(&m_container, &m_typeMap, &m_diagnosticSink);
SLANG_RETURN_ON_FAIL(converter.convert(rttiInfo, result, response.result));
// Send the RPC
SLANG_RETURN_ON_FAIL(sendRPC(&response));
return SLANG_OK;
}
SlangResult JSONRPCConnection::sendCall(
const UnownedStringSlice& method,
const RttiInfo* argsRttiInfo,
const void* args,
const JSONValue& id)
{
return sendCall(m_defaultCallStyle, method, argsRttiInfo, args, id);
}
SlangResult JSONRPCConnection::sendCall(
CallStyle callStyle,
const UnownedStringSlice& method,
const RttiInfo* argsRttiInfo,
const void* args,
const JSONValue& id)
{
JSONRPCCall call;
call.id = id;
call.method = method;
// Set up the converter to now convert the args.
NativeToJSONConverter converter(&m_container, &m_typeMap, &m_diagnosticSink);
// If we have a struct *and* call style is 'array', do special handling
if (argsRttiInfo->m_kind == RttiInfo::Kind::Struct &&
_getCallStyle(callStyle) == CallStyle::Array)
{
// Convert the args/params in the 'array' style
SLANG_RETURN_ON_FAIL(converter.convertStructToArray(argsRttiInfo, args, call.params));
}
else
{
// Convert the args/params in the 'object' sytle
SLANG_RETURN_ON_FAIL(converter.convert(argsRttiInfo, args, call.params));
}
// Send the RPC
SLANG_RETURN_ON_FAIL(sendRPC(&call));
return SLANG_OK;
}
SlangResult JSONRPCConnection::waitForResult(Int timeOutInMs)
{
// Invalidate m_jsonRoot before waitForResult, because when waitForResult fail,
// we don't want to use the result from the previous read.
m_jsonRoot.reset();
SLANG_RETURN_ON_FAIL(m_connection->waitForResult(timeOutInMs));
return tryReadMessage();
}
SlangResult JSONRPCConnection::tryReadMessage()
{
m_jsonRoot.reset();
SLANG_RETURN_ON_FAIL(m_connection->update());
if (!m_connection->hasContent())
{
return SLANG_OK;
}
auto content = m_connection->getContent();
UnownedStringSlice slice((const char*)content.begin(), content.getCount());
clearBuffers();
{
const SlangResult res =
JSONRPCUtil::parseJSON(slice, &m_container, &m_diagnosticSink, m_jsonRoot);
// Consume that content/packet
m_connection->consumeContent();
if (SLANG_FAILED(res))
{
// if we can't parse JSON, we return with id of 'null' as per the standard
return sendError(JSONRPC::ErrorCode::ParseError, JSONValue::makeNull());
}
}
return SLANG_OK;
}
JSONRPCMessageType JSONRPCConnection::getMessageType()
{
return JSONRPCUtil::getMessageType(&m_container, m_jsonRoot);
}
SlangResult JSONRPCConnection::getMessage(const RttiInfo* rttiInfo, void* out)
{
if (!hasMessage())
{
return SLANG_FAIL;
}
m_diagnosticSink.outputBuffer.clear();
JSONToNativeConverter converter(&m_container, &m_typeMap, &m_diagnosticSink);
// Get the RPC response
JSONResultResponse resultResponse;
SLANG_RETURN_ON_FAIL(converter.convert(m_jsonRoot, &resultResponse));
// Convert the result in the response
SLANG_RETURN_ON_FAIL(converter.convert(resultResponse.result, rttiInfo, out));
return SLANG_OK;
}
SlangResult JSONRPCConnection::getMessageOrSendError(const RttiInfo* rttiInfo, void* out)
{
if (!hasMessage())
{
return SLANG_FAIL;
}
const auto res = getMessage(rttiInfo, out);
if (SLANG_FAILED(res))
{
return sendError(JSONRPC::ErrorCode::ParseError, getCurrentMessageId());
}
return res;
}
SlangResult JSONRPCConnection::getRPC(const RttiInfo* rttiInfo, void* out)
{
if (!hasMessage())
{
return SLANG_FAIL;
}
m_diagnosticSink.outputBuffer.clear();
JSONToNativeConverter converter(&m_container, &m_typeMap, &m_diagnosticSink);
// Convert the result in the response
SLANG_RETURN_ON_FAIL(converter.convert(m_jsonRoot, rttiInfo, out));
return SLANG_OK;
}
SlangResult JSONRPCConnection::getRPCOrSendError(const RttiInfo* rttiInfo, void* out)
{
if (!hasMessage())
{
return SLANG_FAIL;
}
const auto res = getRPC(rttiInfo, out);
if (SLANG_FAILED(res))
{
return sendError(JSONRPC::ErrorCode::ParseError, getCurrentMessageId());
}
return res;
}
} // namespace Slang
|