I’m try to write a wrapper around the rust analyzer to work over TCP while working “like normal”, I’m using GNU nc
to connect to this wrapper.
What am I doing wrong here:
#!/usr/bin/env ruby
require 'socket'
listener_port = 4444
listener = TCPServer.new(listener_port)
def handle_client(client_socket)
recv_cnt = ''
loop do
begin
data = client_socket.read_nonblock(4096)
recv_cnt += data
rescue IO::WaitReadable
# No more data available for reading, exit the loop
break
rescue IO::EAGAINWaitReadable
# No more data available for reading, exit the loop (for some platforms)
break
rescue EOFError
# Client closed the connection, exit the loop
break
end
end
puts "Received content: #{recv_cnt}"
exec_ra = 'rust-analyzer parse'
parsed_cnt = `echo '#{recv_cnt}' | #{exec_ra}`
puts "Parsed content: #{parsed_cnt}"
client_socket.puts(parsed_cnt)
end
loop do
client_socket = listener.accept
Thread.new(client_socket) do |socket|
client_address = socket.peeraddr(true)[3]
puts "New connection from: #{client_address}"
begin
handle_client(socket)
rescue StandardError => e
puts "An error occurred for connection from #{client_address}: #{e.message}"
ensure
socket.close
puts "Connection from #{client_address} handled successfully."
end
end
end
?