Start a new debugger thread to listen for commands.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 4 Sep 2014 13:06:31 +0000 (21:06 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 4 Sep 2014 13:06:31 +0000 (21:06 +0800)
atom/browser/node_debugger.cc
atom/browser/node_debugger.h

index 0e7d58b..007180e 100644 (file)
@@ -7,24 +7,18 @@
 #include <string>
 
 #include "atom/common/atom_version.h"
+#include "base/bind.h"
 #include "base/command_line.h"
 #include "base/strings/string_number_conversions.h"
-#include "v8/include/v8.h"
+#include "net/socket/stream_socket.h"
+#include "net/socket/tcp_server_socket.h"
 #include "v8/include/v8-debug.h"
 
-#include "atom/common/node_includes.h"
-
 namespace atom {
 
-// static
-uv_async_t NodeDebugger::dispatch_debug_messages_async_;
-
-NodeDebugger::NodeDebugger() {
-  uv_async_init(uv_default_loop(),
-                &dispatch_debug_messages_async_,
-                DispatchDebugMessagesInMainThread);
-  uv_unref(reinterpret_cast<uv_handle_t*>(&dispatch_debug_messages_async_));
-
+NodeDebugger::NodeDebugger()
+    : thread_("NodeDebugger"),
+      weak_factory_(this) {
   bool use_debug_agent = false;
   int port = 5858;
   bool wait_for_connection = false;
@@ -44,26 +38,32 @@ NodeDebugger::NodeDebugger() {
   if (use_debug_agent) {
     if (!port_str.empty())
       base::StringToInt(port_str, &port);
-#if 0
-    v8::Debug::EnableAgent("atom-shell " ATOM_VERSION, port,
-                           wait_for_connection);
-    v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessagesInMsgThread,
-                                              false);
-#endif
+
+    if (!thread_.Start()) {
+      LOG(ERROR) << "Unable to start debugger thread";
+      return;
+    }
+
+    net::IPAddressNumber ip_number;
+    if (!net::ParseIPLiteralToNumber("127.0.0.1", &ip_number)) {
+      LOG(ERROR) << "Unable to convert ip address";
+      return;
+    }
+
+    server_socket_.reset(new net::TCPServerSocket(NULL, net::NetLog::Source()));
+    server_socket_->Listen(net::IPEndPoint(ip_number, port), 1);
+
+    thread_.message_loop()->PostTask(
+        FROM_HERE,
+        base::Bind(&NodeDebugger::DoAcceptLoop, weak_factory_.GetWeakPtr()));
   }
 }
 
 NodeDebugger::~NodeDebugger() {
+  thread_.Stop();
 }
 
-// static
-void NodeDebugger::DispatchDebugMessagesInMainThread(uv_async_t* handle) {
-  v8::Debug::ProcessDebugMessages();
-}
-
-// static
-void NodeDebugger::DispatchDebugMessagesInMsgThread() {
-  uv_async_send(&dispatch_debug_messages_async_);
+void NodeDebugger::DoAcceptLoop() {
 }
 
 }  // namespace atom
index 4dfdced..a0eb3a6 100644 (file)
@@ -5,8 +5,14 @@
 #ifndef ATOM_BROWSER_NODE_DEBUGGER_H_
 #define ATOM_BROWSER_NODE_DEBUGGER_H_
 
-#include "base/basictypes.h"
-#include "vendor/node/deps/uv/include/uv.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "base/threading/thread.h"
+
+namespace net {
+class StreamSocket;
+class TCPServerSocket;
+}
 
 namespace atom {
 
@@ -17,10 +23,13 @@ class NodeDebugger {
   virtual ~NodeDebugger();
 
  private:
-  static void DispatchDebugMessagesInMainThread(uv_async_t* handle);
-  static void DispatchDebugMessagesInMsgThread();
+  void DoAcceptLoop();
+
+  base::Thread thread_;
+  scoped_ptr<net::TCPServerSocket> server_socket_;
+  scoped_ptr<net::StreamSocket> accepted_socket_;
 
-  static uv_async_t dispatch_debug_messages_async_;
+  base::WeakPtrFactory<NodeDebugger> weak_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(NodeDebugger);
 };