class SockIO

The SockIO class implements the low level interface for TCP sockets.

Public Class Methods

new(sock, bufsize=8192) click to toggle source

Creates a new SockIO object

sock

The socket which will be used

bufsize

The size of the buffer to use (default is 8192)

# File lib/network/sockio.rb, line 25
def initialize(sock, bufsize=8192)
  @sock,@bufsize=sock,bufsize
  @inbuffer = ""
  @outbuffer = ""
end

Public Instance Methods

read() click to toggle source

read will receive a data from the socket.

return

The data read

IOError

A sockets error occurred.

EOFError

The connection has closed normally.

# File lib/network/sockio.rb, line 36
def read
  @sock.recv(@bufsize)
end
read_flush() click to toggle source

#read_flush will kill the input buffer

# File lib/network/sockio.rb, line 63
def read_flush
  @inbuffer = ""
end
read_urgent() click to toggle source

#read_urgent will receive urgent data from the socket.

return

The data read

IOError

A sockets error occurred.

EOFError

The connection has closed normally.

# File lib/network/sockio.rb, line 72
def read_urgent
  @sock.recv(@bufsize, Socket::MSG_OOB)
end
write(msg) click to toggle source

write will transmit a message to the socket

msg

The message string to be sent.

return

false if more data to be written, true if all data written

IOError

A sockets error occurred.

EOFError

The connection has closed normally.

# File lib/network/sockio.rb, line 46
def write(msg)
  @outbuffer << msg
  n = @sock.send(@outbuffer, 0)
  # save unsent data for next call
  @outbuffer.slice!(0...n)
  @outbuffer.size == 0
rescue Exception
  @outbuffer = ""  # Does it really matter?
  raise
end
write_flush() click to toggle source

#write_flush will kill the output buffer

# File lib/network/sockio.rb, line 58
def write_flush
  @outbuffer = ""
end
write_urgent(msg) click to toggle source

#write_urgent will write urgent data to the socket.

msg

The message string to be sent.

IOError

A sockets error occurred.

EOFError

The connection has closed normally.

# File lib/network/sockio.rb, line 81
def write_urgent(msg)
  @sock.send(msg, Socket::MSG_OOB)
end