Class: ThemeCheck::LanguageServer::IOMessenger
- Defined in:
- lib/theme_check/language_server/io_messenger.rb
Instance Method Summary collapse
- #close_input ⇒ Object
- #close_output ⇒ Object
-
#initialize(in_stream: STDIN, out_stream: STDOUT, err_stream: STDERR) ⇒ IOMessenger
constructor
A new instance of IOMessenger.
- #log(message) ⇒ Object
- #read_message ⇒ Object
- #send_message(message_body) ⇒ Object
Constructor Details
#initialize(in_stream: STDIN, out_stream: STDOUT, err_stream: STDERR) ⇒ IOMessenger
Returns a new instance of IOMessenger.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/theme_check/language_server/io_messenger.rb', line 6 def initialize( in_stream: STDIN, out_stream: STDOUT, err_stream: STDERR ) validate!([in_stream, out_stream, err_stream]) @in = in_stream @out = out_stream @err = err_stream # Because programming is fun, # # Ruby on Windows turns \n into \r\n. Which means that \r\n # gets turned into \r\r\n. Which means that the protocol # breaks on windows unless we turn STDOUT into binary mode. # # Hours wasted: 9. @out.binmode @out.sync = true # do not buffer @err.sync = true # do not buffer # Lock for writing, otherwise messages might be interspersed. @writer = Mutex.new end |
Instance Method Details
#close_input ⇒ Object
59 60 61 |
# File 'lib/theme_check/language_server/io_messenger.rb', line 59 def close_input @in.close unless @in.closed? end |
#close_output ⇒ Object
63 64 65 66 |
# File 'lib/theme_check/language_server/io_messenger.rb', line 63 def close_output @err.close @out.close end |
#log(message) ⇒ Object
54 55 56 57 |
# File 'lib/theme_check/language_server/io_messenger.rb', line 54 def log() @err.puts() @err.flush end |
#read_message ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/theme_check/language_server/io_messenger.rb', line 33 def length = initial_line.match(/Content-Length: (\d+)/)[1].to_i content = '' length_to_read = 2 + length # 2 is the empty line length (\r\n) while content.length < length_to_read chunk = @in.read(length_to_read - content.length) raise DoneStreaming if chunk.nil? content += chunk end content.lstrip! end |
#send_message(message_body) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/theme_check/language_server/io_messenger.rb', line 45 def () @writer.synchronize do @out.write("Content-Length: #{.bytesize}\r\n") @out.write("\r\n") @out.write() @out.flush end end |