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


5
6
7
8
9
# File 'lib/solargraph/language_server/transport/data_reader.rb', line 5

def initialize
  @in_header = true
  @content_length = 0
  @buffer = ''
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.


25
26
27
28
29
30
31
32
33
34
# File 'lib/solargraph/language_server/transport/data_reader.rb', line 25

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


15
16
17
# File 'lib/solargraph/language_server/transport/data_reader.rb', line 15

def set_message_handler &block
  @message_handler = block
end