Created
January 25, 2016 00:07
Revisions
-
dom96 renamed this gist
Jan 25, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dom96 created this gist
Jan 25, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ import selectors, net type Data = ref object of RootRef someData: string var selector = newSelector() var sock = newSocket() # The {} means "Don't notify me about any events for this sock", the sock is still registered though selector.register(sock, {}, Data(someData: "Blah blah")) # The sock remains registered until it is closed. # Calling `selector.select()` will always return @[] because # none of the sockets that have been registered want to # receive any event notifications. # Let's update the events that 'sock' wants to hear about. # The following will ask selectors to give us notifications when `sock` becomes readable or writable. selector.update(sock, {EvRead, EvWrite}) # Now, selector.select() will return @[] as long as `sock` is unreadable. Once it is # readable it will return @[(SelectorKey(fd: sock.fd, events: {EvRead, EvWrite}, Data(someData: "Blah blah")), # {EvRead})] # Because sockets are almost always writeable, you will likely also receive the same thing as # above but with {EvWrite} in the ReadyInfo.events field (ReadyInfo is a tuple). # Because receiving these notifications is only necessary when we actually *want* to # send some data through the socket, we can ... selector.update(sock, {EvRead}) # ... to get rid of this notification.