Debian Codegraph @

LspCpp/include/LibLsp/JsonRpc/RemoteEndPoint.h

raw
1 #pragma once
2 #include "LibLsp/lsp/lsp_diagnostic.h"
3 #include "LibLsp/JsonRpc/Cancellation.h"
4 #include "LibLsp/JsonRpc/lsResponseMessage.h"
5 #include "LibLsp/JsonRpc/RequestInMessage.h"
6 #include "LibLsp/JsonRpc/NotificationInMessage.h"
7 #include "traits.h"
8 #include <future>
9 #include <string>
10 #include "threaded_queue.h"
11 #include <unordered_map>
12 #include "MessageIssue.h"
13 #include "LibLsp/JsonRpc/MessageJsonHandler.h"
14 #include "Endpoint.h"
15 #include "future.h"
16 #include "MessageProducer.h"
17
18 class MessageJsonHandler;
19 class Endpoint;
20 struct LspMessage;
21 class RemoteEndPoint;
22 namespace lsp
23 {
24 class ostream;
25 class istream;
26
27 ////////////////////////////////////////////////////////////////////////////////
28 // ResponseOrError<T>
29 ////////////////////////////////////////////////////////////////////////////////
30
31 // ResponseOrError holds either the response to a request or an error
32 // message.
33 template<typename T>
34 struct ResponseOrError
35 {
36 using Request = T;
37 ResponseOrError();
38 ResponseOrError(T const& response);
39 ResponseOrError(T&& response);
40 ResponseOrError(Rsp_Error const& error);
41 ResponseOrError(Rsp_Error&& error);
42 ResponseOrError(ResponseOrError const& other);
43 ResponseOrError(ResponseOrError&& other) noexcept;
44
45 ResponseOrError& operator=(ResponseOrError const& other);
46 ResponseOrError& operator=(ResponseOrError&& other) noexcept;
47 bool IsError() const
48 {
49 return is_error;
50 }
51 std::string ToJson()
52 {
53 if (is_error)
54 {
55 return error.ToJson();
56 }
57 return response.ToJson();
58 }
59 T response;
60 Rsp_Error error; // empty represents success.
61 bool is_error;
62 };
63
64 template<typename T>
65 ResponseOrError<T>::ResponseOrError() : is_error(false)
66 {
67 }
68
69 template<typename T>
70 ResponseOrError<T>::ResponseOrError(T const& resp) : response(resp), is_error(false)
71 {
72 }
73 template<typename T>
74 ResponseOrError<T>::ResponseOrError(T&& resp) : response(std::move(resp)), is_error(false)
75 {
76 }
77 template<typename T>
78 ResponseOrError<T>::ResponseOrError(Rsp_Error const& err) : error(err), is_error(true)
79 {
80 }
81 template<typename T>
82 ResponseOrError<T>::ResponseOrError(Rsp_Error&& err) : error(std::move(err)), is_error(true)
83 {
84 }
85 template<typename T>
86 ResponseOrError<T>::ResponseOrError(ResponseOrError const& other)
87 : response(other.response), error(other.error), is_error(other.is_error)
88 {
89 }
90 template<typename T>
91 ResponseOrError<T>::ResponseOrError(ResponseOrError&& other) noexcept
92 : response(std::move(other.response)), error(std::move(other.error)), is_error(other.is_error)
93 {
94 }
95 template<typename T>
96 ResponseOrError<T>& ResponseOrError<T>::operator=(ResponseOrError const& other)
97 {
98 response = other.response;
99 error = other.error;
100 is_error = other.is_error;
101 return *this;
102 }
103 template<typename T>
104 ResponseOrError<T>& ResponseOrError<T>::operator=(ResponseOrError&& other) noexcept
105 {
106 response = std::move(other.response);
107 error = std::move(other.error);
108 is_error = other.is_error;
109 return *this;
110 }
111
112 } // namespace lsp
113
114 class RemoteEndPoint : MessageIssueHandler
115 {
116
117 template<typename F, int N>
118 using ParamType = lsp::traits::ParameterType<F, N>;
119
120 template<typename T>
121 using IsRequest = lsp::traits::EnableIfIsType<RequestInMessage, T>;
122
123 template<typename T>
124 using IsResponse = lsp::traits::EnableIfIsType<ResponseInMessage, T>;
125
126 template<typename T>
127 using IsNotify = lsp::traits::EnableIfIsType<NotificationInMessage, T>;
128
129 template<typename F, typename ReturnType>
130 using IsRequestHandler = lsp::traits::EnableIf<
131 lsp::traits::CompatibleWith<F, std::function<ReturnType(RequestInMessage const&)>>::value>;
132
133 template<typename F, typename ReturnType>
134 using IsRequestHandlerWithMonitor = lsp::traits::EnableIf<lsp::traits::CompatibleWith<
135 F, std::function<ReturnType(RequestInMessage const&, CancelMonitor const&)>>::value>;
136
137 public:
138 RemoteEndPoint(
139 std::shared_ptr<MessageJsonHandler> const& json_handler, std::shared_ptr<Endpoint> const& localEndPoint,
140 lsp::Log& _log, lsp::JSONStreamStyle style = lsp::JSONStreamStyle::Standard, uint8_t max_workers = 2
141 );
142
143 ~RemoteEndPoint() override;
144 template<typename F, typename RequestType = ParamType<F, 0>, typename ResponseType = typename RequestType::Response>
145 IsRequestHandler<F, lsp::ResponseOrError<ResponseType>> registerHandler(F&& handler)
146 {
147 processRequestJsonHandler(handler);
148 local_endpoint->registerRequestHandler(
149 RequestType::kMethodInfo,
150 [=](std::unique_ptr<LspMessage> msg)
151 {
152 auto req = reinterpret_cast<RequestType const*>(msg.get());
153 lsp::ResponseOrError<ResponseType> res(handler(*req));
154 if (res.is_error)
155 {
156 res.error.id = req->id;
157 send(res.error);
158 }
159 else
160 {
161 res.response.id = req->id;
162 send(res.response);
163 }
164 return true;
165 }
166 );
167 }
168 template<typename F, typename RequestType = ParamType<F, 0>, typename ResponseType = typename RequestType::Response>
169 IsRequestHandlerWithMonitor<F, lsp::ResponseOrError<ResponseType>> registerHandler(F&& handler)
170 {
171 processRequestJsonHandler(handler);
172 local_endpoint->registerRequestHandler(
173 RequestType::kMethodInfo,
174 [=](std::unique_ptr<LspMessage> msg)
175 {
176 auto req = static_cast<RequestType const*>(msg.get());
177 lsp::ResponseOrError<ResponseType> res(handler(*req, getCancelMonitor(req->id)));
178 if (res.is_error)
179 {
180 res.error.id = req->id;
181 send(res.error);
182 }
183 else
184 {
185 res.response.id = req->id;
186 send(res.response);
187 }
188 return true;
189 }
190 );
191 }
192 using RequestErrorCallback = std::function<void(Rsp_Error const&)>;
193
194 template<typename T, typename F, typename ResponseType = ParamType<F, 0>>
195 void send(T& request, F&& handler, RequestErrorCallback onError)
196 {
197 processRequestJsonHandler(handler);
198 auto cb = [=](std::unique_ptr<LspMessage> msg)
199 {
200 if (!msg)
201 {
202 return true;
203 }
204 auto const result = msg.get();
205
206 if (static_cast<ResponseInMessage*>(result)->IsErrorType())
207 {
208 auto const rsp_error = static_cast<Rsp_Error const*>(result);
209 onError(*rsp_error);
210 }
211 else
212 {
213 handler(*static_cast<ResponseType*>(result));
214 }
215
216 return true;
217 };
218 internalSendRequest(request, cb);
219 }
220
221 template<typename F, typename NotifyType = ParamType<F, 0>>
222 IsNotify<NotifyType> registerHandler(F&& handler)
223 {
224 {
225 std::lock_guard<std::mutex> lock(m_sendMutex);
226 if (!jsonHandler->GetNotificationJsonHandler(NotifyType::kMethodInfo))
227 {
228 jsonHandler->SetNotificationJsonHandler(
229 NotifyType::kMethodInfo, [](Reader& visitor) { return NotifyType::ReflectReader(visitor); }
230 );
231 }
232 }
233 local_endpoint->registerNotifyHandler(
234 NotifyType::kMethodInfo,
235 [=](std::unique_ptr<LspMessage> msg)
236 {
237 handler(*static_cast<NotifyType*>(msg.get()));
238 return true;
239 }
240 );
241 }
242
243 template<typename T, typename = IsRequest<T>>
244 lsp::future<lsp::ResponseOrError<typename T::Response>> send(T& request)
245 {
246
247 processResponseJsonHandler(request);
248 using Response = typename T::Response;
249 auto promise = std::make_shared<lsp::promise<lsp::ResponseOrError<Response>>>();
250 auto cb = [=](std::unique_ptr<LspMessage> msg)
251 {
252 if (!msg)
253 {
254 return true;
255 }
256 auto result = msg.get();
257
258 if (reinterpret_cast<ResponseInMessage*>(result)->IsErrorType())
259 {
260 Rsp_Error* rsp_error = static_cast<Rsp_Error*>(result);
261 Rsp_Error temp;
262 std::swap(temp, *rsp_error);
263 promise->set_value(std::move(lsp::ResponseOrError<Response>(std::move(temp))));
264 }
265 else
266 {
267 Response temp;
268 std::swap(temp, *static_cast<Response*>(result));
269 promise->set_value(std::move(lsp::ResponseOrError<Response>(std::move(temp))));
270 }
271 return true;
272 };
273 internalSendRequest(request, cb);
274 return promise->get_future();
275 }
276
277 template<typename T, typename = IsRequest<T>>
278 std::unique_ptr<lsp::ResponseOrError<typename T::Response>> waitResponse(T& request, unsigned const time_out = 0)
279 {
280 auto future_rsp = send(request);
281 if (time_out == 0)
282 {
283 future_rsp.wait();
284 }
285 else
286 {
287 auto state = future_rsp.wait_for(std::chrono::milliseconds(time_out));
288 if (lsp::future_status::timeout == state)
289 {
290 return {};
291 }
292 }
293
294 using Response = typename T::Response;
295 return std::make_unique<lsp::ResponseOrError<Response>>(std::move(future_rsp.get()));
296 }
297
298 void send(NotificationInMessage& msg)
299 {
300 sendMsg(msg);
301 }
302
303 void send(ResponseInMessage& msg)
304 {
305 sendMsg(msg);
306 }
307
308 void sendNotification(NotificationInMessage& msg)
309 {
310 send(msg);
311 }
312 void sendResponse(ResponseInMessage& msg)
313 {
314 send(msg);
315 }
316 template<typename T>
317 T createRequest()
318 {
319 auto req = T();
320 req.id.set(getNextRequestId());
321 return req;
322 }
323
324 int getNextRequestId();
325
326 bool cancelRequest(lsRequestId const&);
327
328 void startProcessingMessages(std::shared_ptr<lsp::istream> r, std::shared_ptr<lsp::ostream> w);
329
330 bool isWorking() const;
331 void stop();
332
333 std::unique_ptr<LspMessage> internalWaitResponse(RequestInMessage&, unsigned time_out = 0);
334
335 bool internalSendRequest(RequestInMessage& info, GenericResponseHandler handler);
336
337 void handle(std::vector<MessageIssue>&&) override;
338 void handle(MessageIssue&&) override;
339
340 private:
341 CancelMonitor getCancelMonitor(lsRequestId const&);
342 void sendMsg(LspMessage& msg);
343 void mainLoop(std::unique_ptr<LspMessage>);
344 bool dispatch(std::string const&);
345 template<typename F, typename RequestType = ParamType<F, 0>>
346 IsRequest<RequestType> processRequestJsonHandler(F const& handler)
347 {
348 std::lock_guard<std::mutex> lock(m_sendMutex);
349 if (!jsonHandler->GetRequestJsonHandler(RequestType::kMethodInfo))
350 {
351 jsonHandler->SetRequestJsonHandler(
352 RequestType::kMethodInfo, [](Reader& visitor) { return RequestType::ReflectReader(visitor); }
353 );
354 }
355 }
356 template<typename T, typename = IsRequest<T>>
357 void processResponseJsonHandler(T& request)
358 {
359 using Response = typename T::Response;
360 std::lock_guard<std::mutex> lock(m_sendMutex);
361 if (!jsonHandler->GetResponseJsonHandler(T::kMethodInfo))
362 {
363 jsonHandler->SetResponseJsonHandler(
364 T::kMethodInfo,
365 [](Reader& visitor)
366 {
367 if (visitor.HasMember("error"))
368 {
369 return Rsp_Error::ReflectReader(visitor);
370 }
371 return Response::ReflectReader(visitor);
372 }
373 );
374 }
375 }
376
377 struct Data;
378
379 Data* d_ptr;
380
381 std::shared_ptr<MessageJsonHandler> jsonHandler;
382 std::mutex m_sendMutex;
383
384 std::shared_ptr<Endpoint> local_endpoint;
385
386 public:
387 std::shared_ptr<std::thread> message_producer_thread_;
388 };