Skip to content

Instantly share code, notes, and snippets.

@cheatfate
Created May 27, 2016 12:40
proc poll*(timeout = 500) =
var
keys : array[64, ReadyKey]
let p = getGlobalDispatcher()
var i = 0
var count = p.selector.selectInto(p.adjustedTimeout(timeout), keys)
# if count != 0:
# echo(count)
while i < count:
var data = PData(keys[i].data)
let fd = keys[i].fd
if isReadable(keys[i].events):
if data.readCB != nil:
let cb = data.readCB
if cb(fd.AsyncFD):
if cb == data.readCB: # this is check if readCB was already replaced by callback
data.readCB = nil
if isWritable(keys[i].events):
if data.writeCB != nil:
let cb = data.writeCB
if cb(fd.AsyncFD):
if cb == data.writeCB: # this is check if writeCB was already replace by callback
data.writeCB = nil
var newEvents = 0
p.selector.withData(fd.SocketHandle, value) do:
if data.readCB != nil: newEvents = newEvents or EVENT_READ
if data.writeCB != nil: newEvents = newEvents or EVENT_WRITE
p.selector.updateHandle(fd.SocketHandle, newEvents)
inc(i)
# Timer processing.
processTimers(p)
# Callback queue processing
processPendingCallbacks(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment