class Flare::Net::Connection

Description

Attributes

host[R]
port[R]
received_size[R]
sent_size[R]

Public Class Methods

new(host, port, uplink_limit = nil, downlink_limit = nil) click to toggle source
# File lib/flare/net/connection.rb, line 22
def initialize(host, port, uplink_limit = nil, downlink_limit = nil)
  @host = host
  @port = port
  @socket = TCPSocket.open(host, port)
  @last_sent = ""
  @sent_size = 0
  @received_size = 0
  @uplink_limit = Flare::Util::Bwlimit.new(uplink_limit)
  @downlink_limit = Flare::Util::Bwlimit.new(downlink_limit)
end

Public Instance Methods

close() click to toggle source
# File lib/flare/net/connection.rb, line 36
def close
  @socket.close unless @socket.closed?
end
closed?() click to toggle source
# File lib/flare/net/connection.rb, line 40
def closed?
  @socket.closed?
end
getline() click to toggle source
# File lib/flare/net/connection.rb, line 68
def getline
  ret = @socket.gets
  return nil if ret.nil?
  if $DEBUG
    puts "<-- #{ret.chomp}"
  end
  size = ret.size
  @received_size += size
  @downlink_limit.inc size
  @downlink_limit.wait
  ret
end
last_sent() click to toggle source
# File lib/flare/net/connection.rb, line 64
def last_sent
  @last_sent
end
read(length = nil) click to toggle source
# File lib/flare/net/connection.rb, line 81
def read(length = nil)
  ret = @socket.read(length)
  return nil if ret.nil?
  size = ret.size
  @received_size += size
  @downlink_limit.inc size
  @downlink_limit.wait
  ret
end
reconnect() click to toggle source
# File lib/flare/net/connection.rb, line 44
def reconnect
  if @socket.closed?
    @socket = nil
    @socket = TCPSocket.open(@host, @port)
  end
  @socket
end
send(cmd) click to toggle source
# File lib/flare/net/connection.rb, line 52
def send(cmd)
  if $DEBUG
    puts "--> server=[#{self}] cmd=[#{cmd.chomp}]"
  end
  size = cmd.size
  @sent_size += size
  @socket.write cmd
  @last_sent = cmd
  @uplink_limit.inc size
  @uplink_limit.wait
end
to_s() click to toggle source
# File lib/flare/net/connection.rb, line 91
def to_s
  "#{@host}:#{@port}"
end