class MClient

Assure connection to server, extend socket connection by SocketReactive module.

MClient.run_one_shot("localhost",2200)       do |socket| .. end.join

MClient.run_continous("localhost",2200,6000) do |socket| .. end.join

Public Class Methods

run_continious(host,port,timer_interconnection_ms,&b) click to toggle source

maintain a conntection to a TCP serveur, sleep timer_interconnection_ms millisecondes beetwen each reconnections

# File lib/minitcp.rb, line 218
def self.run_continious(host,port,timer_interconnection_ms,&b)
  Thread.new do
    loop { run_one_shot(host,port,&b).join ; sleep timer_interconnection_ms/1000.0 }
  end
end
run_continous(host,port,timer_interconnection,&b) click to toggle source
# File lib/minitcp.rb, line 224
def self.run_continous(host,port,timer_interconnection,&b)
  self.run_continious(host,port,timer_interconnection,&b)
end
run_one_shot(host="localhost",port=80) { |socket| ... } click to toggle source

Connecte to a TCP server, call block with client socket if connection sucess. enssure close connection after end of block

# File lib/minitcp.rb, line 230
def self.run_one_shot(host="localhost",port=80)
  begin
    sleep(0.03) # give some time for server ready (for test...)
    socket = TCPSocket.new(host,port)
  rescue
    puts "not connected to #{host}:#{port}: " + $!.to_s
    return (Thread.new {})
  end
  SocketReactive::make_socket_reactive(socket)
  Thread.new do
    begin
      yield(socket)
    rescue Exception => e
      puts "#{e}\n  #{e.backtrace.join("\n  ")}"
    ensure
      socket.close() rescue nil
    end
  end
end