Modified behaviour of checkBuffer to match new select behaviour. Refs #822.

This commit is contained in:
Dominik Picheta 2014-04-05 23:10:51 +01:00
commit 51c203bab0
2 changed files with 14 additions and 15 deletions

View file

@ -346,6 +346,7 @@ proc poll*(irc: PIRC, ev: var TIRCEvent,
var line = TaintedString"" var line = TaintedString""
var socks = @[irc.sock] var socks = @[irc.sock]
var ret = socks.select(timeout) var ret = socks.select(timeout)
if ret == -1: osError(osLastError())
if socks.len() != 0 and ret != 0: if socks.len() != 0 and ret != 0:
irc.sock.readLine(line) irc.sock.readLine(line)
ev = irc.processLine(line.string) ev = irc.processLine(line.string)

View file

@ -926,15 +926,12 @@ proc createFdSet(fd: var TFdSet, s: seq[TSocket], m: var int) =
FD_SET(i.fd, fd) FD_SET(i.fd, fd)
proc pruneSocketSet(s: var seq[TSocket], fd: var TFdSet) = proc pruneSocketSet(s: var seq[TSocket], fd: var TFdSet) =
var i = 0 var newSet: seq[TSocket] = @[]
var L = s.len for sock in s:
while i < L: if FD_ISSET(sock.fd, fd) == 1'i32:
if FD_ISSET(s[i].fd, fd) == 0'i32: ## Is set.
s[i] = s[L-1] newSet.add(sock)
dec(L) s = newSet
else:
inc(i)
setLen(s, L)
proc hasDataBuffered*(s: TSocket): bool = proc hasDataBuffered*(s: TSocket): bool =
## Determines whether a socket has data buffered. ## Determines whether a socket has data buffered.
@ -954,9 +951,9 @@ proc checkBuffer(readfds: var seq[TSocket]): int =
for s in readfds: for s in readfds:
if hasDataBuffered(s): if hasDataBuffered(s):
inc(result) inc(result)
else:
res.add(s) res.add(s)
readfds = res if result > 0:
readfds = res
proc select*(readfds, writefds, exceptfds: var seq[TSocket], proc select*(readfds, writefds, exceptfds: var seq[TSocket],
timeout = 500): int {.tags: [FReadIO].} = timeout = 500): int {.tags: [FReadIO].} =
@ -965,8 +962,9 @@ proc select*(readfds, writefds, exceptfds: var seq[TSocket],
## If there are none; 0 is returned. ## If there are none; 0 is returned.
## ``Timeout`` is in miliseconds and -1 can be specified for no timeout. ## ``Timeout`` is in miliseconds and -1 can be specified for no timeout.
## ##
## A socket is removed from the specific ``seq`` when it has data waiting to ## Sockets which are **not** ready for reading, writing or which don't have
## be read/written to or has errors (``exceptfds``). ## errors waiting on them are removed from the ``readfds``, ``writefds``,
## ``exceptfds`` sequences respectively.
let buffersFilled = checkBuffer(readfds) let buffersFilled = checkBuffer(readfds)
if buffersFilled > 0: if buffersFilled > 0:
return buffersFilled return buffersFilled
@ -1013,7 +1011,7 @@ proc selectWrite*(writefds: var seq[TSocket],
timeout = 500): int {.tags: [FReadIO].} = timeout = 500): int {.tags: [FReadIO].} =
## When a socket in ``writefds`` is ready to be written to then a non-zero ## When a socket in ``writefds`` is ready to be written to then a non-zero
## value will be returned specifying the count of the sockets which can be ## value will be returned specifying the count of the sockets which can be
## written to. The sockets which can be written to will also be removed ## written to. The sockets which **cannot** be written to will also be removed
## from ``writefds``. ## from ``writefds``.
## ##
## ``timeout`` is specified in miliseconds and ``-1`` can be specified for ## ``timeout`` is specified in miliseconds and ``-1`` can be specified for