-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonkey_patch.rb
More file actions
26 lines (21 loc) · 665 Bytes
/
Copy pathmonkey_patch.rb
File metadata and controls
26 lines (21 loc) · 665 Bytes
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
require 'lightio'
# apply monkey patch as early as possible
# after monkey patch, it just normal ruby code
LightIO::Monkey.patch_all!
TCPServer.open('localhost', 3000) do |server|
while (socket = server.accept)
_, port, host = socket.peeraddr
puts "accept connection from #{host}:#{port}"
# Don't worry, Thread.new create green threads, it cost very light
Thread.new(socket) do |socket|
data = nil
begin
socket.write(data) while (data = socket.readpartial(4096))
rescue EOFError
_, port, host = socket.peeraddr
puts "*** #{host}:#{port} disconnected"
socket.close
end
end
end
end