-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSelector.swift
55 lines (47 loc) · 2.02 KB
/
Selector.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Selector.swift
// Embassy
//
// Created by Fang-Pen Lin on 5/20/16.
// Copyright © 2016 Fang-Pen Lin. All rights reserved.
//
import Foundation
/// Event of IO
public enum IOEvent {
case read
case write
}
/// Represent a subscription for a file descriptor in Selector
public struct SelectorKey {
/// File descriptor
let fileDescriptor: Int32
/// Events to monitor
let events: Set<IOEvent>
/// User custom data to be returned when we see an IO event
let data: Any?
}
/// Selector provides a way to poll lots of file descriptors for IO events in an efficient way.
/// The basic interface design follows https://docs.python.org/3/library/selectors.html
public protocol Selector {
/// Register a file descriptor for given IO events to watch
/// - Parameter fileDescriptor: the file descriptor to watch
/// - Parameter events: IO events to watch
/// - Parameter data: user custom data to be returned when we see an IO event
/// - Returns: added SelectorKey
@discardableResult
func register(_ fileDescriptor: Int32, events: Set<IOEvent>, data: Any?) throws -> SelectorKey
/// Unregister a file descriptor from selector
@discardableResult
func unregister(_ fileDescriptor: Int32) throws -> SelectorKey
/// Close the selector to release underlaying resource
func close()
/// Select to see if the registered file descriptors have IO events, wait until
/// we see a file descriptor ready or timeout
/// - Parameter timeout: how long time to wait until return empty list,
/// if timeout <= 0, it won't block but returns current file descriptor status immediately,
/// if timeout == nil, it will block until there is a file descriptor ready
/// - Returns: an array of (key, events) for ready file descriptors
func select(timeout: TimeInterval?) throws -> [(SelectorKey, Set<IOEvent>)]
/// Return the SelectorKey for given file descriptor
subscript(fileDescriptor: Int32) -> SelectorKey? { get }
}