Class: DebugAdapterProtocolTunnel::DAPConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/debuggers.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, plugins) ⇒ DAPConnection

Returns a new instance of DAPConnection.



180
181
182
183
184
185
186
187
# File 'lib/debuggers.rb', line 180

def initialize(connection, plugins)
  @connection = connection
  @plugins = plugins
  @running = false
  @next_seq = 1
  @write_mutex = Mutex.new
  @_dump_messages = (ENV['HAPLO_DAP_DEBUG'] == '1')
end

Instance Method Details

#_write(message) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/debuggers.rb', line 250

def _write(message)
  @write_mutex.synchronize do
    m = {'seq' => @next_seq}
    @next_seq += 1
    m.merge!(message)
    puts "DAP WRITE: #{JSON.pretty_generate(m)}\n" if @_dump_messages
    msg_json = JSON.generate(m)
    @connection.write("Content-Length: #{msg_json.bytesize}\r\n\r\n")
    @connection.write(msg_json)
  end
end

#closeObject



189
190
191
192
193
194
195
# File 'lib/debuggers.rb', line 189

def close
  begin
    @connection.close
  rescue => e
    # ignore any errors
  end
end

#runObject



197
198
199
200
201
202
203
204
# File 'lib/debuggers.rb', line 197

def run
  begin
    run2
  rescue => e
    puts "DEBUGGER: Local connection closed"
  end
  self.close
end

#run2Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/debuggers.rb', line 206

def run2
  @running = true
  have_initialized = false
  while @running
    header = @connection.readline
    blank = @connection.readline
    if header =~ /\Acontent-length:\s+(\d+)\r\n\z/i && blank == "\r\n"
      body = @connection.read($1.to_i)
      message = JSON.parse(body)
      puts "DAP READ: #{JSON.pretty_generate(message)}\n" if @_dump_messages
      unless have_initialized
        if message['type'] == 'request' && message['command'] == 'initialize'
          puts "DEBUGGER: Local connection from #{message['arguments']['clientName']} (#{message['arguments']['clientID']})\nDEBUGGER: Starting remote debugger..."
          plugin_locations = {}
          @plugins.each { |p| plugin_locations[p.name] = p.plugin_dir }
          start_response = PluginTool.post("/api/development-plugin-loader/debugger-dap-start", {
            :plugin_locations => JSON.generate(plugin_locations)
          })
          unless start_response =~ /\ATOKEN: (.+)\z/
            raise "Remote debugger failed to start"
          end
          @token = $1
          puts "DEBUGGER: Remote debugger started."
          have_initialized = true
        else
          raise "Expected initialize message after DAP connection"
        end
      end
      # TODO: Send messages in another thread, so that they can be batched together
      message_response = PluginTool.post_with_json_response("/api/development-plugin-loader/debugger-dap-messages", {
        :token => @token,
        :messages => JSON.generate([message])
      })
      if message_response['error']
        puts "DEBUGGER: Server responded with error: #{message_response['error']}"
      else
        message_response['messages'].each do |response|
          _write(response) if response
        end
      end
    end
  end
end