This module allows high-level and efficient I/O multiplexing.
Supported OS primitives: epoll, kqueue, poll and Windows select.
To use threadsafe version of this module, it needs to be compiled with both -d:threadsafe and --threads:on options.
Supported features: files, sockets, pipes, timers, processes, signals and user events.
Fully supported OS: MacOSX, FreeBSD, OpenBSD, NetBSD, Linux (except for Android).
Partially supported OS: Windows (only sockets and user events), Solaris (files, sockets, handles and user events). Android (files, sockets, handles and user events).
TODO: /dev/poll, event ports and filesystem events.
Event {.pure.} = enum
Read, ## Descriptor is available for read
Write, ## Descriptor is available for write
Timer, ## Timer descriptor is completed
Signal, ## Signal is raised
Process, ## Process is finished
Vnode, ## BSD specific file change
User, ## User event is raised
Error, ## Error occurred while waiting for descriptor
VnodeWrite, ## NOTE_WRITE (BSD specific, write to file occurred)
VnodeDelete, ## NOTE_DELETE (BSD specific, unlink of file occurred)
VnodeExtend, ## NOTE_EXTEND (BSD specific, file extended)
VnodeAttrib, ## NOTE_ATTRIB (BSD specific, file attributes changed)
VnodeLink, ## NOTE_LINK (BSD specific, file link count changed)
VnodeRename, ## NOTE_RENAME (BSD specific, file renamed)
VnodeRevoke ## NOTE_REVOKE (BSD specific, file revoke occurred)IOSelectorsException = object of CatchableError
proc registerProcess[T](s: Selector[T]; pid: int; data: T): int {.discardable.}Registers a process id (pid) notification (when process has exited) in selector s.
The data is application-defined data, which will be passed when process with pid has exited.
Returns the file descriptor for the registered signal.
Source Editproc registerSignal[T](s: Selector[T]; signal: int; data: T): int {.discardable.}Registers Unix signal notification with signal to selector s.
The data is application-defined data, which will be passed when signal raises.
Returns the file descriptor for the registered signal.
Note: This function is not supported on Windows.
proc registerTimer[T](s: Selector[T]; timeout: int; oneshot: bool; data: T): int {.
discardable.}Registers timer notification with timeout (in milliseconds) to selector s.
If oneshot is true, timer will be notified only once.
Set oneshot to false if you want periodic notifications.
The data is application-defined data, which will be passed, when the timer is triggered.
Returns the file descriptor for the registered timer.
Source Editproc registerVnode[T](s: Selector[T]; fd: cint; events: set[Event]; data: T)
Registers selector BSD/MacOSX specific vnode events for file descriptor fd and events events. data application-defined data, which to be passed, when vnode event happens.
Note: This function is supported only by BSD and MacOSX.
Source Editproc select[T](s: Selector[T]; timeout: int): seq[ReadyKey]
Waits for events registered in selector s.
The timeout argument specifies the maximum number of milliseconds the function will be blocked for if no events are ready. Specifying a timeout of -1 causes the function to block indefinitely.
Returns a list of triggered events.
Source Editproc selectInto[T](s: Selector[T]; timeout: int;
results: var openArray[ReadyKey]): intWaits for events registered in selector s.
The timeout argument specifies the maximum number of milliseconds the function will be blocked for if no events are ready. Specifying a timeout of -1 causes the function to block indefinitely. All available events will be stored in results array.
Returns number of triggered events.
Source Edittemplate withData[T](s: Selector[T]; fd: SocketHandle | int;
value, body1, body2: untyped)Retrieves the application-data assigned with descriptor fd to value. This value can be modified in the scope of the withData call.
s.withData(fd, value) do: # block is executed only if `fd` registered in selector `s`. value.uid = 1000 do: # block is executed if `fd` not registered in selector `s`. raiseSource Edit
template withData[T](s: Selector[T]; fd: SocketHandle | int;
value, body: untyped)Retrieves the application-data assigned with descriptor fd to value. This value can be modified in the scope of the withData call.
s.withData(fd, value) do: # block is executed only if `fd` registered in selector `s` value.uid = 1000Source Edit
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/selectors.html