class Sock
Sock
is a helper class for using tcp sockets in an easily debuggable way.
Public Class Methods
connect(host, port, debug = false) { |s| ... }
click to toggle source
Static
# File lib/sock.rb, line 85 def self.connect(host, port, debug = false) s = Sock.new(host, port, debug) yield s s.close end
new(host, port, debug = false)
click to toggle source
Constructor
# File lib/sock.rb, line 10 def initialize(host, port, debug = false) @sock = TCPSocket.new host, port @debug = debug end
Public Instance Methods
close()
click to toggle source
# File lib/sock.rb, line 79 def close @sock.close end
go_interactive()
click to toggle source
# File lib/sock.rb, line 50 def go_interactive loop do return unless write_out return unless read_in IO.select([@sock, STDIN], [], [@sock, STDIN]) end end
read(nbytes)
click to toggle source
# File lib/sock.rb, line 32 def read(nbytes) msg = @sock.recv nbytes print msg.cyan if @debug msg end
readline()
click to toggle source
# File lib/sock.rb, line 38 def readline msg = @sock.readline print msg.cyan if @debug msg end
write(str)
click to toggle source
Methods
# File lib/sock.rb, line 17 def write(str) print str.magenta if @debug @sock.write(str) end
Private Instance Methods
read_in(data = nil)
click to toggle source
# File lib/sock.rb, line 69 def read_in(data = nil) @sock.write(data) while (data = STDIN.read_nonblock(100)) != '' return true rescue Errno::EAGAIN return true rescue EOFError return false end
write_out(data = nil)
click to toggle source
# File lib/sock.rb, line 58 def write_out(data = nil) STDOUT.write(data) while (data = @sock.read_nonblock(100)) != '' return false rescue Errno::EAGAIN return true rescue EOFError @sock.close return false end