Source Edit

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.

Imports

nativesockets, oserrors

Types

  1. Event {.pure.} = enum
  2. Read, ## Descriptor is available for read
  3. Write, ## Descriptor is available for write
  4. Timer, ## Timer descriptor is completed
  5. Signal, ## Signal is raised
  6. Process, ## Process is finished
  7. Vnode, ## BSD specific file change
  8. User, ## User event is raised
  9. Error, ## Error occurred while waiting for descriptor
  10. VnodeWrite, ## NOTE_WRITE (BSD specific, write to file occurred)
  11. VnodeDelete, ## NOTE_DELETE (BSD specific, unlink of file occurred)
  12. VnodeExtend, ## NOTE_EXTEND (BSD specific, file extended)
  13. VnodeAttrib, ## NOTE_ATTRIB (BSD specific, file attributes changed)
  14. VnodeLink, ## NOTE_LINK (BSD specific, file link count changed)
  15. VnodeRename, ## NOTE_RENAME (BSD specific, file renamed)
  16. VnodeRevoke ## NOTE_REVOKE (BSD specific, file revoke occurred)

An enum which hold event types Source Edit

  1. IOSelectorsException = object of CatchableError

Exception that is raised if an IOSelectors error occurs. Source Edit

  1. ReadyKey = object
  2. fd*: int ## file/socket descriptor
  3. events*: set[Event] ## set of events
  4. errorCode*: OSErrorCode ## additional error code information for
  5. ## Error events

An object which holds result for descriptor Source Edit

  1. SelectEvent = object

An object which holds user defined event Source Edit

  1. Selector[T] = ref object

An object which holds descriptors to be checked for read/write status Source Edit

Consts

  1. ioselSupportedPlatform = false

This constant is used to determine whether the destination platform is fully supported by ioselectors module. Source Edit

Procs

  1. proc close(ev: SelectEvent) {....raises: [], tags: [], forbids: [].}

Closes user-defined event ev. Source Edit

  1. proc close[T](s: Selector[T])

Closes the selector. Source Edit

  1. proc contains[T](s: Selector[T]; fd: SocketHandle | int): bool {.inline.}

Determines whether selector contains a file descriptor. Source Edit

  1. proc getData[T](s: Selector[T]; fd: SocketHandle | int): var T

Retrieves application-defined data associated with descriptor fd. If specified descriptor fd is not registered, empty/default value will be returned. Source Edit

  1. proc getFd[T](s: Selector[T]): int

Retrieves the underlying selector’s file descriptor.

For poll and select selectors -1 is returned.

Source Edit

  1. proc newSelectEvent(): SelectEvent {....raises: [], tags: [], forbids: [].}

Creates a new user-defined event. Source Edit

  1. proc newSelector[T](): Selector[T]

Creates a new selector Source Edit

  1. proc registerEvent[T](s: Selector[T]; ev: SelectEvent; data: T)

Registers selector event ev in selector s.

The data is application-defined data, which will be passed when ev happens.

Source Edit

  1. proc registerHandle[T](s: Selector[T]; fd: int | SocketHandle;
  2. events: set[Event]; data: T)

Registers file/socket descriptor fd to selector s with events set in events. The data is application-defined data, which will be passed when an event is triggered. Source Edit

  1. 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 Edit

  1. proc 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.

Source Edit

  1. proc registerTimer[T](s: Selector[T]; timeout: int; oneshot: bool; data: T): int {.
  2. 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 Edit

  1. proc 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 Edit

  1. proc 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 Edit

  1. proc selectInto[T](s: Selector[T]; timeout: int;
  2. results: var openArray[ReadyKey]): int

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. All available events will be stored in results array.

Returns number of triggered events.

Source Edit

  1. proc setData[T](s: Selector[T]; fd: SocketHandle | int; data: var T): bool

Associate application-defined data with descriptor fd.

Returns true, if data was successfully updated, false otherwise.

Source Edit

  1. proc trigger(ev: SelectEvent) {....raises: [], tags: [], forbids: [].}

Trigger event ev. Source Edit

  1. proc unregister[T](s: Selector[T]; ev: SelectEvent)

Unregisters user-defined event ev from selector s. Source Edit

  1. proc unregister[T](s: Selector[T]; fd: int | SocketHandle | cint)

Unregisters file/socket descriptor fd from selector s. Source Edit

  1. proc updateHandle[T](s: Selector[T]; fd: int | SocketHandle; events: set[Event])

Update file/socket descriptor fd, registered in selector s with new events set event. Source Edit

Templates

  1. template isEmpty[T](s: Selector[T]): bool

Returns true, if there are no registered events or descriptors in selector. Source Edit

  1. template withData[T](s: Selector[T]; fd: SocketHandle | int;
  2. 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.

  1. s.withData(fd, value) do:
  2. # block is executed only if `fd` registered in selector `s`.
  3. value.uid = 1000
  4. do:
  5. # block is executed if `fd` not registered in selector `s`.
  6. raise

Source Edit

  1. template withData[T](s: Selector[T]; fd: SocketHandle | int;
  2. 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.

  1. s.withData(fd, value) do:
  2. # block is executed only if `fd` registered in selector `s`
  3. value.uid = 1000

Source Edit