@@ -136,7 +136,7 @@ class InspectorIoDelegate: public node::inspector::SocketServerDelegate {
136
136
const std::string& script_name, bool wait);
137
137
// Calls PostIncomingMessage() with appropriate InspectorAction:
138
138
// kStartSession
139
- bool StartSession (int session_id, const std::string& target_id) override ;
139
+ void StartSession (int session_id, const std::string& target_id) override ;
140
140
// kSendMessage
141
141
void MessageReceived (int session_id, const std::string& message) override ;
142
142
// kEndSession
@@ -145,19 +145,22 @@ class InspectorIoDelegate: public node::inspector::SocketServerDelegate {
145
145
std::vector<std::string> GetTargetIds () override ;
146
146
std::string GetTargetTitle (const std::string& id) override ;
147
147
std::string GetTargetUrl (const std::string& id) override ;
148
- bool IsConnected () { return connected_; }
149
148
void ServerDone () override {
150
149
io_->ServerDone ();
151
150
}
152
151
152
+ void AssignTransport (InspectorSocketServer* server) {
153
+ server_ = server;
154
+ }
155
+
153
156
private:
154
157
InspectorIo* io_;
155
- bool connected_;
156
158
int session_id_;
157
159
const std::string script_name_;
158
160
const std::string script_path_;
159
161
const std::string target_id_;
160
162
bool waiting_;
163
+ InspectorSocketServer* server_;
161
164
};
162
165
163
166
void InterruptCallback (v8::Isolate*, void * agent) {
@@ -226,10 +229,6 @@ void InspectorIo::Stop() {
226
229
DispatchMessages ();
227
230
}
228
231
229
- bool InspectorIo::IsConnected () {
230
- return delegate_ != nullptr && delegate_->IsConnected ();
231
- }
232
-
233
232
bool InspectorIo::IsStarted () {
234
233
return platform_ != nullptr ;
235
234
}
@@ -264,6 +263,7 @@ void InspectorIo::IoThreadAsyncCb(uv_async_t* async) {
264
263
MessageQueue<TransportAction> outgoing_message_queue;
265
264
io->SwapBehindLock (&io->outgoing_message_queue_ , &outgoing_message_queue);
266
265
for (const auto & outgoing : outgoing_message_queue) {
266
+ int session_id = std::get<1 >(outgoing);
267
267
switch (std::get<0 >(outgoing)) {
268
268
case TransportAction::kKill :
269
269
transport->TerminateConnections ();
@@ -272,8 +272,14 @@ void InspectorIo::IoThreadAsyncCb(uv_async_t* async) {
272
272
transport->Stop (nullptr );
273
273
break ;
274
274
case TransportAction::kSendMessage :
275
- std::string message = StringViewToUtf8 (std::get<2 >(outgoing)->string ());
276
- transport->Send (std::get<1 >(outgoing), message);
275
+ transport->Send (session_id,
276
+ StringViewToUtf8 (std::get<2 >(outgoing)->string ()));
277
+ break ;
278
+ case TransportAction::kAcceptSession :
279
+ transport->AcceptSession (session_id);
280
+ break ;
281
+ case TransportAction::kDeclineSession :
282
+ transport->DeclineSession (session_id);
277
283
break ;
278
284
}
279
285
}
@@ -293,6 +299,7 @@ void InspectorIo::ThreadMain() {
293
299
wait_for_connect_);
294
300
delegate_ = &delegate;
295
301
Transport server (&delegate, &loop, options_.host_name (), options_.port ());
302
+ delegate.AssignTransport (&server);
296
303
TransportAndIo<Transport> queue_transport (&server, this );
297
304
thread_req_.data = &queue_transport;
298
305
if (!server.Start ()) {
@@ -308,6 +315,7 @@ void InspectorIo::ThreadMain() {
308
315
uv_run (&loop, UV_RUN_DEFAULT);
309
316
thread_req_.data = nullptr ;
310
317
CHECK_EQ (uv_loop_close (&loop), 0 );
318
+ delegate.AssignTransport (nullptr );
311
319
delegate_ = nullptr ;
312
320
}
313
321
@@ -358,6 +366,21 @@ void InspectorIo::NotifyMessageReceived() {
358
366
incoming_message_cond_.Broadcast (scoped_lock);
359
367
}
360
368
369
+ TransportAction InspectorIo::Attach (int session_id) {
370
+ Agent* agent = parent_env_->inspector_agent ();
371
+ if (agent->delegate () != nullptr )
372
+ return TransportAction::kDeclineSession ;
373
+
374
+ CHECK_EQ (session_delegate_, nullptr );
375
+ session_id_ = session_id;
376
+ state_ = State::kConnected ;
377
+ fprintf (stderr, " Debugger attached.\n " );
378
+ session_delegate_ = std::unique_ptr<InspectorSessionDelegate>(
379
+ new IoSessionDelegate (this ));
380
+ agent->Connect (session_delegate_.get ());
381
+ return TransportAction::kAcceptSession ;
382
+ }
383
+
361
384
void InspectorIo::DispatchMessages () {
362
385
// This function can be reentered if there was an incoming message while
363
386
// V8 was processing another inspector request (e.g. if the user is
@@ -375,16 +398,14 @@ void InspectorIo::DispatchMessages() {
375
398
MessageQueue<InspectorAction>::value_type task;
376
399
std::swap (dispatching_message_queue_.front (), task);
377
400
dispatching_message_queue_.pop_front ();
401
+ int id = std::get<1 >(task);
378
402
StringView message = std::get<2 >(task)->string ();
379
403
switch (std::get<0 >(task)) {
380
404
case InspectorAction::kStartSession :
381
- CHECK_EQ (session_delegate_, nullptr );
382
- session_id_ = std::get<1 >(task);
383
- state_ = State::kConnected ;
384
- fprintf (stderr, " Debugger attached.\n " );
385
- session_delegate_ = std::unique_ptr<InspectorSessionDelegate>(
386
- new IoSessionDelegate (this ));
387
- parent_env_->inspector_agent ()->Connect (session_delegate_.get ());
405
+ Write (Attach (id), id, StringView ());
406
+ break ;
407
+ case InspectorAction::kStartSessionUnconditionally :
408
+ Attach (id);
388
409
break ;
389
410
case InspectorAction::kEndSession :
390
411
CHECK_NE (session_delegate_, nullptr );
@@ -428,22 +449,23 @@ InspectorIoDelegate::InspectorIoDelegate(InspectorIo* io,
428
449
const std::string& script_name,
429
450
bool wait)
430
451
: io_(io),
431
- connected_ (false ),
432
452
session_id_ (0 ),
433
453
script_name_(script_name),
434
454
script_path_(script_path),
435
455
target_id_(GenerateID()),
436
- waiting_(wait) { }
456
+ waiting_(wait),
457
+ server_(nullptr ) { }
437
458
438
459
439
- bool InspectorIoDelegate::StartSession (int session_id,
460
+ void InspectorIoDelegate::StartSession (int session_id,
440
461
const std::string& target_id) {
441
- if (connected_)
442
- return false ;
443
- connected_ = true ;
444
- session_id_++;
445
- io_->PostIncomingMessage (InspectorAction::kStartSession , session_id, " " );
446
- return true ;
462
+ session_id_ = session_id;
463
+ InspectorAction action = InspectorAction::kStartSession ;
464
+ if (waiting_) {
465
+ action = InspectorAction::kStartSessionUnconditionally ;
466
+ server_->AcceptSession (session_id);
467
+ }
468
+ io_->PostIncomingMessage (action, session_id, " " );
447
469
}
448
470
449
471
void InspectorIoDelegate::MessageReceived (int session_id,
@@ -464,7 +486,6 @@ void InspectorIoDelegate::MessageReceived(int session_id,
464
486
}
465
487
466
488
void InspectorIoDelegate::EndSession (int session_id) {
467
- connected_ = false ;
468
489
io_->PostIncomingMessage (InspectorAction::kEndSession , session_id, " " );
469
490
}
470
491
0 commit comments