| 1 |
|
| 2 |
#include "LibLsp/JsonRpc/WebSocketServer.h" |
| 3 |
#include "LibLsp/lsp/textDocument/signature_help.h" |
| 4 |
#include "LibLsp/lsp/general/initialize.h" |
| 5 |
#include "LibLsp/lsp/ProtocolJsonHandler.h" |
| 6 |
#include "LibLsp/lsp/textDocument/typeHierarchy.h" |
| 7 |
#include "LibLsp/lsp/AbsolutePath.h" |
| 8 |
#include "LibLsp/lsp/textDocument/resolveCompletionItem.h" |
| 9 |
#include <network/uri.hpp> |
| 10 |
|
| 11 |
|
| 12 |
#include "LibLsp/JsonRpc/Endpoint.h" |
| 13 |
#include "LibLsp/JsonRpc/stream.h" |
| 14 |
#include "LibLsp/JsonRpc/TcpServer.h" |
| 15 |
#include "LibLsp/lsp/textDocument/document_symbol.h" |
| 16 |
#include "LibLsp/lsp/workspace/execute_command.h" |
| 17 |
|
| 18 |
#include <boost/filesystem.hpp> |
| 19 |
#include <boost/asio.hpp> |
| 20 |
#include <iostream> |
| 21 |
#include <boost/beast/core.hpp> |
| 22 |
#include <boost/beast/websocket.hpp> |
| 23 |
#include <boost/asio/dispatch.hpp> |
| 24 |
#include <boost/asio/strand.hpp> |
| 25 |
#include <algorithm> |
| 26 |
#include <cstdlib> |
| 27 |
#include <functional> |
| 28 |
#include <iostream> |
| 29 |
#include <memory> |
| 30 |
#include <string> |
| 31 |
#include <thread> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include <boost/beast/version.hpp> |
| 35 |
#include <boost/beast/websocket.hpp> |
| 36 |
#include <boost/asio/dispatch.hpp> |
| 37 |
#include "LibLsp/JsonRpc/Endpoint.h" |
| 38 |
#include "LibLsp/JsonRpc/RemoteEndPoint.h" |
| 39 |
#include "LibLsp/JsonRpc/stream.h" |
| 40 |
#include "LibLsp/lsp/ProtocolJsonHandler.h" |
| 41 |
|
| 42 |
namespace beast = boost::beast; // from <boost/beast.hpp> |
| 43 |
namespace http = beast::http; // from <boost/beast/http.hpp> |
| 44 |
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp> |
| 45 |
namespace net = boost::asio; // from <boost/asio.hpp> |
| 46 |
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp> |
| 47 |
|
| 48 |
//------------------------------------------------------------------------------ |
| 49 |
|
| 50 |
std::string _address = "127.0.0.1"; |
| 51 |
std::string _port = "9333"; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
using namespace std; |
| 57 |
class DummyLog :public lsp::Log |
| 58 |
{ |
| 59 |
public: |
| 60 |
|
| 61 |
void log(Level level, std::wstring&& msg) |
| 62 |
{ |
| 63 |
std::wcout << msg << std::endl; |
| 64 |
}; |
| 65 |
void log(Level level, const std::wstring& msg) |
| 66 |
{ |
| 67 |
std::wcout << msg << std::endl; |
| 68 |
}; |
| 69 |
void log(Level level, std::string&& msg) |
| 70 |
{ |
| 71 |
std::cout << msg << std::endl; |
| 72 |
}; |
| 73 |
void log(Level level, const std::string& msg) |
| 74 |
{ |
| 75 |
std::cout << msg << std::endl; |
| 76 |
}; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
// Sends a WebSocket message and prints the response |
| 82 |
class Client : public std::enable_shared_from_this<Client> |
| 83 |
{ |
| 84 |
net::io_context ioc; |
| 85 |
tcp::resolver resolver_; |
| 86 |
websocket::stream<beast::tcp_stream> ws_; |
| 87 |
beast::flat_buffer buffer_; |
| 88 |
std::string host_; |
| 89 |
std::string user_agent_; |
| 90 |
std::shared_ptr < lsp::ProtocolJsonHandler > protocol_json_handler = std::make_shared< lsp::ProtocolJsonHandler>(); |
| 91 |
DummyLog _log; |
| 92 |
|
| 93 |
std::shared_ptr<GenericEndpoint> endpoint = std::make_shared<GenericEndpoint>(_log); |
| 94 |
|
| 95 |
std::shared_ptr<lsp::websocket_stream_wrapper> proxy_; |
| 96 |
public: |
| 97 |
RemoteEndPoint point; |
| 98 |
|
| 99 |
public: |
| 100 |
// Resolver and socket require an io_context |
| 101 |
explicit |
| 102 |
Client() |
| 103 |
: resolver_(net::make_strand(ioc)) |
| 104 |
, ws_(net::make_strand(ioc)),point(protocol_json_handler, endpoint, _log) |
| 105 |
{ |
| 106 |
proxy_ = std::make_shared<lsp::websocket_stream_wrapper>(ws_); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
// Start the asynchronous operation |
| 111 |
void |
| 112 |
run( |
| 113 |
char const* host, |
| 114 |
char const* port, char const* user_agent) |
| 115 |
{ |
| 116 |
// Save these for later |
| 117 |
host_ = host; |
| 118 |
user_agent_ = user_agent; |
| 119 |
// Look up the domain name |
| 120 |
resolver_.async_resolve( |
| 121 |
host, |
| 122 |
port, |
| 123 |
beast::bind_front_handler( |
| 124 |
&Client::on_resolve, |
| 125 |
shared_from_this())); |
| 126 |
std::thread([&] |
| 127 |
{ |
| 128 |
ioc.run(); |
| 129 |
}).detach(); |
| 130 |
while (!point.isWorking()) |
| 131 |
{ |
| 132 |
std::this_thread::sleep_for(std::chrono::milliseconds (50)); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
on_resolve( |
| 138 |
beast::error_code ec, |
| 139 |
tcp::resolver::results_type results) |
| 140 |
{ |
| 141 |
if (ec) |
| 142 |
return; |
| 143 |
|
| 144 |
// Set the timeout for the operation |
| 145 |
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30)); |
| 146 |
|
| 147 |
// Make the connection on the IP address we get from a lookup |
| 148 |
beast::get_lowest_layer(ws_).async_connect( |
| 149 |
results, |
| 150 |
beast::bind_front_handler( |
| 151 |
&Client::on_connect, |
| 152 |
shared_from_this())); |
| 153 |
} |
| 154 |
|
| 155 |
void |
| 156 |
on_connect(beast::error_code ec, tcp::resolver::results_type::endpoint_type) |
| 157 |
{ |
| 158 |
if (ec) |
| 159 |
return; |
| 160 |
|
| 161 |
// Turn off the timeout on the tcp_stream, because |
| 162 |
// the websocket stream has its own timeout system. |
| 163 |
beast::get_lowest_layer(ws_).expires_never(); |
| 164 |
|
| 165 |
// Set suggested timeout settings for the websocket |
| 166 |
ws_.set_option( |
| 167 |
websocket::stream_base::timeout::suggested( |
| 168 |
beast::role_type::client)); |
| 169 |
|
| 170 |
// Set a decorator to change the User-Agent of the handshake |
| 171 |
ws_.set_option(websocket::stream_base::decorator( |
| 172 |
[=](websocket::request_type& req) |
| 173 |
{ |
| 174 |
req.set(http::field::user_agent, |
| 175 |
user_agent_.c_str()); |
| 176 |
})); |
| 177 |
|
| 178 |
// Perform the websocket handshake |
| 179 |
ws_.async_handshake(host_, "/", |
| 180 |
beast::bind_front_handler( |
| 181 |
&Client::on_handshake, |
| 182 |
shared_from_this())); |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
on_handshake(beast::error_code ec) |
| 187 |
{ |
| 188 |
if (ec) |
| 189 |
return; |
| 190 |
|
| 191 |
// Send the message |
| 192 |
|
| 193 |
|
| 194 |
point.startProcessingMessages(proxy_, proxy_); |
| 195 |
// Read a message into our buffer |
| 196 |
ws_.async_read( |
| 197 |
buffer_, |
| 198 |
beast::bind_front_handler( |
| 199 |
&Client::on_read, |
| 200 |
shared_from_this())); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void |
| 205 |
on_read( |
| 206 |
beast::error_code ec, |
| 207 |
std::size_t bytes_transferred) |
| 208 |
{ |
| 209 |
boost::ignore_unused(bytes_transferred); |
| 210 |
|
| 211 |
if (ec) |
| 212 |
return; |
| 213 |
|
| 214 |
char* data = reinterpret_cast<char*>(buffer_.data().data()); |
| 215 |
std::vector<char> elements(data, data + bytes_transferred); |
| 216 |
buffer_.clear(); |
| 217 |
proxy_->on_request.EnqueueAll(std::move(elements), false); |
| 218 |
|
| 219 |
ws_.async_read( |
| 220 |
buffer_, |
| 221 |
beast::bind_front_handler( |
| 222 |
&Client::on_read, |
| 223 |
shared_from_this())); |
| 224 |
} |
| 225 |
|
| 226 |
void |
| 227 |
on_close(beast::error_code ec) |
| 228 |
{ |
| 229 |
if (ec) |
| 230 |
return; |
| 231 |
|
| 232 |
// If we get here then the connection is closed gracefully |
| 233 |
|
| 234 |
// The make_printable() function helps print a ConstBufferSequence |
| 235 |
std::cout << beast::make_printable(buffer_.data()) << std::endl; |
| 236 |
} |
| 237 |
}; |
| 238 |
|
| 239 |
class Server |
| 240 |
{ |
| 241 |
public: |
| 242 |
Server(const std::string& user_agent) : server(user_agent,_address, _port, protocol_json_handler, endpoint, _log) |
| 243 |
{ |
| 244 |
server.point.registerHandler( |
| 245 |
[&](const td_initialize::request& req) |
| 246 |
{ |
| 247 |
td_initialize::response rsp; |
| 248 |
CodeLensOptions code_lens_options; |
| 249 |
code_lens_options.resolveProvider = true; |
| 250 |
rsp.result.capabilities.codeLensProvider = code_lens_options; |
| 251 |
return rsp; |
| 252 |
}); |
| 253 |
std::thread([&]() |
| 254 |
{ |
| 255 |
server.run(); |
| 256 |
}).detach(); |
| 257 |
} |
| 258 |
~Server() |
| 259 |
{ |
| 260 |
server.stop(); |
| 261 |
} |
| 262 |
std::shared_ptr < lsp::ProtocolJsonHandler > protocol_json_handler = std::make_shared < lsp::ProtocolJsonHandler >(); |
| 263 |
DummyLog _log; |
| 264 |
|
| 265 |
std::shared_ptr < GenericEndpoint > endpoint = std::make_shared<GenericEndpoint>(_log); |
| 266 |
lsp::WebSocketServer server; |
| 267 |
|
| 268 |
}; |
| 269 |
|
| 270 |
int main() |
| 271 |
{ |
| 272 |
std::string user_agent = std::string(BOOST_BEAST_VERSION_STRING) +" websocket-server-async"; |
| 273 |
Server server(user_agent); |
| 274 |
|
| 275 |
auto client = std::make_shared<Client>(); |
| 276 |
user_agent = std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-async"; |
| 277 |
client->run(_address.c_str(), _port.c_str(), user_agent.c_str()); |
| 278 |
|
| 279 |
td_initialize::request req; |
| 280 |
|
| 281 |
auto rsp = client->point.waitResponse(req); |
| 282 |
if (rsp) |
| 283 |
{ |
| 284 |
std::cout << rsp->ToJson() << std::endl; |
| 285 |
} |
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|