IO select. Reconnect procedure

Hey, I have a minor problem with my IRC bot. It disconnects every while
and then, often with a few days in between. My problem lies in the
reconnecting procedure. The bot does not notice when it has been
disconnected. I have tried Socket.closed?, time-out timer for the select
IO. Nothing seems to work.

Any idea why?

//Walle

[/code]
def loop()
  while !@connection.closed?
    ready = select([@connection, $stdin], nil, nil, 10)
    next if !ready
    ready[0].each do |message|
    if(message == $stdin)
      return if $stdin.eof
      message = $stdin.gets
      send(message)
    elsif(message == @connection)
      return if @connection.eof
      message = @connection.gets
      data_response(message.strip)
    end
    end
  end
    raise Errno::EPIPE
  end
[/code]

···

--
Posted via http://www.ruby-forum.com/.

Maybe this will help.

class TCPSocket
  def off?
    begin
      eof
    rescue
      Kernel.puts "#{__FILE__}:#{__LINE__} #{$!.inspect}"
      true
    end
  end
end

def loop
  until connection.off?
    ...
  end
end

···

--
Posted via http://www.ruby-forum.com/.

Walle Wallen wrote:

Hey, I have a minor problem with my IRC bot. It disconnects every while
and then, often with a few days in between. My problem lies in the
reconnecting procedure. The bot does not notice when it has been
disconnected. I have tried Socket.closed?, time-out timer for the select
IO. Nothing seems to work.

Any idea why?

Maybe you have to detect when it sends you an empty string? (that
usually means the socket has closed). If it's a bug report it though.
-rp

···

--
Posted via http://www.ruby-forum.com/\.

It still happens, but very rarely... The bot kicks in again if I write
anything in the window($stdin).
Does anyone have any thoughts on this?

//Walle

···

--
Posted via http://www.ruby-forum.com/.

Walle Wallen wrote:

It still happens, but very rarely... The bot kicks in again if I write
anything in the window($stdin).
Does anyone have any thoughts on this?

Yeah I think it's a generic "problem" of TCP. If the other side has
closed the socket (or died, or been rebooted), and you never write
anything to the socket, you will never learn it is closed until the next
time you write something.

http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Socket#keepalive

···

--
Posted via http://www.ruby-forum.com/\.

Roger Pack wrote:

Walle Wallen wrote:

It still happens, but very rarely... The bot kicks in again if I write
anything in the window($stdin).
Does anyone have any thoughts on this?

Yeah I think it's a generic "problem" of TCP. If the other side has
closed the socket (or died, or been rebooted), and you never write
anything to the socket, you will never learn it is closed until the next
time you write something.

Ruby Programming/Reference/Objects/Socket - Wikibooks, open books for an open world

Oh, okey.
Thank you for the insight.

//Walle

···

--
Posted via http://www.ruby-forum.com/\.