Class: Solargraph::LanguageServer::Transport::DataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/language_server/transport/data_reader.rb

Instance Method Summary collapse

Constructor Details

#initializeDataReader

Returns a new instance of DataReader.



9
10
11
12
13
# File 'lib/solargraph/language_server/transport/data_reader.rb', line 9

def initialize
  @in_header = true
  @content_length = 0
  @buffer = String.new
end

Instance Method Details

#receive(data) ⇒ Object

Process raw data received from the client. The data will be parsed into messages based on the JSON-RPC protocol. Each message will be passed to the block declared via set_message_handler. Incomplete data will be buffered and subsequent data will be appended to the buffer.

Parameters:

  • data (String)


29
30
31
32
33
34
35
36
37
38
# File 'lib/solargraph/language_server/transport/data_reader.rb', line 29

def receive data
  data.each_char do |char|
    @buffer.concat char
    if @in_header
      prepare_to_parse_message if @buffer.end_with?("\r\n\r\n")
    else
      parse_message_from_buffer if @buffer.bytesize == @content_length
    end
  end
end

#set_message_handler {|The| ... } ⇒ Object

Declare a block to be executed for each message received from the client.

Yield Parameters:

  • The (Hash)

    message received from the client



19
20
21
# File 'lib/solargraph/language_server/transport/data_reader.rb', line 19

def set_message_handler &block
  @message_handler = block
end