7 #include <websocketpp/endpoint.hpp> 9 #include <nlohmann/json.hpp> 84 isolate(debug_context.get_isolate()),
86 debug_context(debug_context)
92 this->debug_server.get_alog().clear_channels(websocketpp::log::alevel::all);
93 this->debug_server.get_elog().clear_channels(websocketpp::log::elevel::all);
96 this->debug_server.set_validate_handler(
97 bind(&WebsocketChannel::websocket_validation_handler,
this, websocketpp::lib::placeholders::_1));
100 this->debug_server.set_open_handler(
bind(&WebsocketChannel::on_open,
this, websocketpp::lib::placeholders::_1));
103 this->debug_server.set_close_handler(
104 bind(&WebsocketChannel::on_close,
this, websocketpp::lib::placeholders::_1));
107 this->debug_server.set_message_handler(
108 bind(&WebsocketChannel::on_message,
this, websocketpp::lib::placeholders::_1,
109 websocketpp::lib::placeholders::_2));
110 this->debug_server.set_open_handler(
112 this->debug_server.set_http_handler(
115 this->debug_server.init_asio();
116 this->debug_server.set_reuse_addr(
true);
117 std::cerr <<
"Listening on port " << this->port << std::endl;
118 this->debug_server.listen(this->port);
120 this->debug_server.start_accept();
124 void WebsocketChannel::on_http(websocketpp::connection_hdl hdl) {
125 WebsocketChannel::DebugServerType::connection_ptr con = this->debug_server.get_con_from_hdl(hdl);
127 std::stringstream output;
128 output <<
"<!doctype html><html><body>You requested " 129 << con->get_resource()
133 con->set_status(websocketpp::http::status_code::ok);
135 con->set_body(output.str());
139 void WebsocketChannel::on_message(websocketpp::connection_hdl hdl, WebsocketChannel::DebugServerType::message_ptr msg) {
142 std::cerr <<
"Got debugger message: " << message_payload << std::endl;
144 v8_inspector::StringView message_view((uint8_t
const *)message_payload.c_str(), message_payload.length());
147 this->debug_context.
get_session().dispatchProtocolMessage(message_view);
148 this->message_received_time = std::chrono::high_resolution_clock::now();
152 void WebsocketChannel::on_open(websocketpp::connection_hdl hdl) {
153 std::cerr << fmt::format(
"on open") << std::endl;
154 assert(this->connections.size() == 0);
155 this->connections.insert(hdl);
160 void WebsocketChannel::on_close(websocketpp::connection_hdl hdl) {
161 assert(this->connections.size() == 1);
162 this->connections.erase(hdl);
163 assert(this->connections.size() == 0);
167 this->debug_context.
paused =
false;
171 void WebsocketChannel::Send(
const v8_inspector::StringView &
string) {
172 std::cerr << fmt::format(
"sending message of length: {}",
string.length()) << std::endl;
173 std::cerr <<
string.characters8() << std::endl;
175 if (
string.is8Bit()) {
176 this->
send_message((
void *)
string.characters8(),
string.length());
178 size_t length =
string.length();
179 auto source=
string.characters16();
180 auto buffer =
new char[length];
181 for(
int i = 0; i < length; i++) {
182 buffer[i] = source[i];
197 bool WebsocketChannel::websocket_validation_handler(websocketpp::connection_hdl hdl) {
199 std::cerr << fmt::format(
"validation handler") << std::endl;
200 return this->connections.size() == 0;
216 if (!this->connections.empty()) {
218 this->debug_server.send(*this->connections.begin(), data, length, websocketpp::frame::opcode::TEXT);
220 std::cerr << fmt::format(
"Not sending message because no connections: {}") << std::endl;
222 this->message_sent_time = std::chrono::high_resolution_clock::now();
227 this->debug_server.run_one();
231 this->debug_server.poll();
235 this->debug_server.poll_one();
248 while(this->connections.empty()) {
250 if (this->connections.empty()) {
252 std::this_thread::sleep_for(sleep_between_polls);
264 inspector(v8_inspector::V8Inspector::create(this->get_isolate(), this)),
265 session(inspector->connect(1, channel.get(), v8_inspector::StringView()))
273 inspector->contextCreated(v8_inspector::V8ContextInfo(*
this,
kContextGroupId, v8_inspector::StringView()));
275 v8::Debug::SetLiveEditEnabled(this->
isolate,
true);
280 void to_json(
nlohmann::json &j,
const ResponseMessage & response_message) {
282 {
"id", response_message.message_id},
283 {
"result", response_message.to_json()}
288 void to_json(
nlohmann::json &j,
const InformationalMessage & informational_message) {
289 j = informational_message.to_json();
295 std::chrono::duration<float> duration = std::chrono::high_resolution_clock::now() - this->message_received_time;
296 return duration.count();
300 std::chrono::duration<float> duration = std::chrono::high_resolution_clock::now() - this->message_sent_time;
301 return duration.count();
307 auto sent_time = this->seconds_since_message_sent();
308 auto received_time = this->seconds_since_message_received();
309 return sent_time < received_time ? sent_time : received_time;
v8::Isolate *const isolate
shortcut to the v8::isolate object instead of always going through the Isolate
v8::Global< v8::Context > const & get_global_context() const
v8_inspector::V8InspectorSession & get_session()
DebugContext(std::shared_ptr< v8toolkit::Isolate > isolate_helper, v8::Local< v8::Context > context, short port)
v8::Local< v8::Context > get_context() const
v8::Isolate * get_isolate() const
static const int kContextGroupId