lib/byebug/dap/server.rb in byebug-dap-0.1.2 vs lib/byebug/dap/server.rb in byebug-dap-0.1.3
- old
+ new
@@ -1,32 +1,15 @@
module Byebug
module DAP
class Server
- class STDIO
- extend Forwardable
-
- def initialize
- @in = STDIN
- @out = STDOUT
- STDIN.sync = true
- STDOUT.sync = true
- end
-
- def close; @in.close; @out.close; end
- def flush; @in.flush; @out.flush; end
- def fsync; @in.fsync; @out.fsync; end
-
- def_delegators :@in, :close_read, :bytes, :chars, :codepoints, :each, :each_byte, :each_char, :each_codepoint, :each_line, :getbyte, :getc, :gets, :lines, :pread, :print, :printf, :read, :read_nonblock, :readbyte, :readchar, :readline, :readlines, :readpartial, :sysread, :ungetbyte, :ungetc
- def_delegators :@out, :<<, :close_write, :putc, :puts, :pwrite, :syswrite, :write, :write_nonblock
- public :<<, :bytes, :chars, :close_read, :close_write, :codepoints, :each, :each_byte, :each_char, :each_codepoint, :each_line, :getbyte, :getc, :gets, :lines, :pread, :print, :printf, :putc, :puts, :pwrite, :read, :read_nonblock, :readbyte, :readchar, :readline, :readlines, :readpartial, :sysread, :syswrite, :ungetbyte, :ungetc, :write, :write_nonblock
- end
-
- def initialize
+ def initialize(capture: true, forward: true)
@started = false
@mu = Mutex.new
@cond = ConditionVariable.new
@configured = false
+ @capture = capture
+ @forward = forward
end
def start(host, port = 0)
case host
when :stdio
@@ -40,25 +23,30 @@
def start_tcp(host, port)
return if @started
@started = true
- launch TCPServer.new(host, port)
+ @ios = CapturedIO.new(@forward, @forward) if @capture
+ launch_accept TCPServer.new(host, port)
end
def start_unix(socket)
return if @started
@started = true
- launch UNIXServer.new(socket)
+ @ios = CapturedIO.new(@forward, @forward) if @capture
+ launch_accept UNIXServer.new(socket)
end
def start_stdio
return if @started
@started = true
- launch STDIO.new
+ stream = STDIO.new
+ STDIN.close
+ @ios = CapturedIO.new(false, @forward) if @capture
+ launch stream
end
def wait_for_client
@mu.synchronize do
loop do
@@ -69,33 +57,61 @@
end
end
private
- def launch(server)
+ def log
+ if @ios
+ @ios.log
+ elsif defined?(LOG)
+ LOG
+ else
+ STDERR
+ end
+ end
+
+ def launch(stream)
DebugThread.new do
- if server.respond_to?(:accept)
- while session = server.accept
- debug session
- end
- else
- debug server
- end
+ debug stream
+
+ ensure
+ @ios&.restore
end
self
end
- def debug(session)
- Context.interface = Byebug::DAP::Interface.new(session)
- Context.processor = Byebug::DAP::CommandProcessor
+ def launch_accept(server)
+ DebugThread.new do
+ while socket = server.accept
+ debug socket
+ end
- Byebug::DAP::Controller.new(Context.interface) do
+ ensure
+ @ios&.restore
+ end
+
+ self
+ end
+
+ def debug(connection)
+ session = Byebug::DAP::Session.new(connection, @ios) do
@mu.synchronize do
@configured = true
@cond.broadcast
end
- end.run
+ end
+
+ session.execute
+
+ rescue IOError, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED
+ log.puts "Client disconnected"
+
+ rescue StandardError => e
+ log.puts "#{e.message} (#{e.class})", *e.backtrace
+
+ ensure
+ session.stop!
end
end
end
end