-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.rb
executable file
·38 lines (35 loc) · 908 Bytes
/
script.rb
1
2
3
4
5
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
32
33
34
35
36
37
38
#!/usr/bin/env ruby
require 'pp'
require 'socket'
socket = Socket.new(:INET, :STREAM)
socket.setsockopt(:SOCKET, :REUSEADDR, true)
sockaddr = Socket.pack_sockaddr_in(2200, '127.0.0.1')
socket.bind(sockaddr)
socket.listen(_backlog = 3)
run = true
clients = []
to_read, to_write = IO.pipe
Signal.trap('TERM') { to_write.puts 'TERM' }
while run do
ready_to_read, _ready_to_write, _errors = select([to_read, socket] + clients, [], [])
if ready_to_read.nil?
puts 'beat'
next
end
ready_to_read.each do |io|
if io == to_read
signal = to_read.gets.chomp
run = false if signal == 'TERM'
elsif io == socket
client_socket, _client_addrinfo = socket.accept
clients << client_socket
else
begin
input = io.read_nonblock(4096)
io.puts "#{input.chomp.reverse}"
rescue Errno::ECONNRESET
clients.delete(io)
end
end
end
end