Merge branch 'master' of github.com:Araq/Nimrod

This commit is contained in:
Araq 2013-01-22 00:30:12 +01:00
commit 0d8f45697c
2 changed files with 19 additions and 4 deletions

View file

@ -32,7 +32,7 @@
## **Warning:** The API of this module is unstable, and therefore is subject ## **Warning:** The API of this module is unstable, and therefore is subject
## to change. ## to change.
import sockets, strutils, parseutils, times, asyncio import sockets, strutils, parseutils, times, asyncio, os
type type
TIRC* = object of TObject TIRC* = object of TObject
@ -56,6 +56,7 @@ type
channelsToJoin: seq[string] channelsToJoin: seq[string]
msgLimit: bool msgLimit: bool
messageBuffer: seq[tuple[timeToSend: float, m: string]] messageBuffer: seq[tuple[timeToSend: float, m: string]]
lastReconnect: float
PIRC* = ref TIRC PIRC* = ref TIRC
@ -235,12 +236,19 @@ proc connect*(irc: PIRC) =
irc.send("NICK " & irc.nick, true) irc.send("NICK " & irc.nick, true)
irc.send("USER $1 * 0 :$2" % [irc.user, irc.realname], 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. ## 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. ## 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.sock = socket()
irc.connect() irc.connect()
irc.lastReconnect = epochTime()
proc irc*(address: string, port: TPort = 6667.TPort, proc irc*(address: string, port: TPort = 6667.TPort,
nick = "NimrodBot", nick = "NimrodBot",
@ -409,15 +417,22 @@ proc connect*(irc: PAsyncIRC) =
irc.asyncSock.connect(irc.address, irc.port) irc.asyncSock.connect(irc.address, irc.port)
proc reconnect*(irc: PAsyncIRC) = proc reconnect*(irc: PAsyncIRC, timeout = 5000) =
## Reconnects to an IRC server. ## 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. ## This should be used when an ``EvDisconnected`` event occurs.
## ##
## When successfully reconnected an ``EvConnected`` event will occur. ## When successfully reconnected an ``EvConnected`` event will occur.
let secSinceReconnect = int(epochTime() - irc.lastReconnect)
if secSinceReconnect < timeout:
sleep(timeout - secSinceReconnect)
irc.asyncSock = AsyncSocket() irc.asyncSock = AsyncSocket()
irc.myDispatcher.register(irc) irc.myDispatcher.register(irc)
irc.connect() irc.connect()
irc.lastReconnect = epochTime()
proc asyncIRC*(address: string, port: TPort = 6667.TPort, proc asyncIRC*(address: string, port: TPort = 6667.TPort,
nick = "NimrodBot", nick = "NimrodBot",

View file

@ -1449,7 +1449,7 @@ proc sleep*(milsecs: int) {.rtl, extern: "nos$1", tags: [FTime].} =
else: else:
var a, b: Ttimespec var a, b: Ttimespec
a.tv_sec = TTime(milsecs div 1000) a.tv_sec = TTime(milsecs div 1000)
a.tv_nsec = (milsecs mod 1000) * 1000 a.tv_nsec = (milsecs mod 1000) * 1000 * 1000
discard posix.nanosleep(a, b) discard posix.nanosleep(a, b)
proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1", proc getFileSize*(file: string): biggestInt {.rtl, extern: "nos$1",