diff options
author | Dominik Picheta <dominikpicheta@googlemail.com> | 2013-01-21 14:44:23 +0000 |
---|---|---|
committer | Dominik Picheta <dominikpicheta@googlemail.com> | 2013-01-21 14:44:23 +0000 |
commit | ddc8913ebbaba375010d415a5ff89fd1ee7fba96 (patch) | |
tree | 656898edde86ba78a6552210367d27a560fb288f /lib | |
parent | 9aecfcd8c8bf96a3aaa77c1bb7c1c242aabd960f (diff) | |
download | Nim-ddc8913ebbaba375010d415a5ff89fd1ee7fba96.tar.gz |
Added a timeout parameter to irc.reconnect.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/irc.nim | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/pure/irc.nim b/lib/pure/irc.nim index 8916681f7..aa4e2d557 100644 --- a/lib/pure/irc.nim +++ b/lib/pure/irc.nim @@ -32,7 +32,7 @@ ## **Warning:** The API of this module is unstable, and therefore is subject ## to change. -import sockets, strutils, parseutils, times, asyncio +import sockets, strutils, parseutils, times, asyncio, os type TIRC* = object of TObject @@ -56,6 +56,7 @@ type channelsToJoin: seq[string] msgLimit: bool messageBuffer: seq[tuple[timeToSend: float, m: string]] + lastReconnect: float PIRC* = ref TIRC @@ -235,12 +236,19 @@ proc connect*(irc: PIRC) = irc.send("NICK " & irc.nick, true) irc.send("USER $1 * 0 :$2" % [irc.user, irc.realname], true) -proc reconnect*(irc: PIRC) = +proc reconnect*(irc: PIRC, timeout = 5000) = ## Reconnects to an IRC server. ## + ## ``Timeout`` specifies the time to wait in miliseconds between multiple + ## consecutive reconnections. + ## ## This should be used when an ``EvDisconnected`` event occurs. + let secSinceReconnect = int(epochTime() - irc.lastReconnect) + if secSinceReconnect < timeout: + sleep(timeout - secSinceReconnect) irc.sock = socket() irc.connect() + irc.lastReconnect = epochTime() proc irc*(address: string, port: TPort = 6667.TPort, nick = "NimrodBot", @@ -409,15 +417,22 @@ proc connect*(irc: PAsyncIRC) = irc.asyncSock.connect(irc.address, irc.port) -proc reconnect*(irc: PAsyncIRC) = +proc reconnect*(irc: PAsyncIRC, timeout = 5000) = ## Reconnects to an IRC server. ## + ## ``Timeout`` specifies the time to wait in miliseconds between multiple + ## consecutive reconnections. + ## ## This should be used when an ``EvDisconnected`` event occurs. ## ## When successfully reconnected an ``EvConnected`` event will occur. + let secSinceReconnect = int(epochTime() - irc.lastReconnect) + if secSinceReconnect < timeout: + sleep(timeout - secSinceReconnect) irc.asyncSock = AsyncSocket() irc.myDispatcher.register(irc) irc.connect() + irc.lastReconnect = epochTime() proc asyncIRC*(address: string, port: TPort = 6667.TPort, nick = "NimrodBot", |