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
get(nbytes)
Alias for: read
getline()
Alias for: readline
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
put(str)
Alias for: write
putline(str)
Alias for: writeline
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
Also aliased as: recv, get
readline() click to toggle source
# File lib/sock.rb, line 38
def readline
  msg = @sock.readline
  print msg.cyan if @debug
  msg
end
Also aliased as: recvline, getline
recv(nbytes)
Alias for: read
recvline()
Alias for: readline
send(str)
Alias for: write
sendline(str)
Alias for: writeline
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
Also aliased as: send, put
writeline(str) click to toggle source
# File lib/sock.rb, line 22
def writeline(str)
  send(str + "\n")
end
Also aliased as: sendline, putline

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