forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibEvent.swift
163 lines (127 loc) · 4.17 KB
/
LibEvent.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// LibEvent.swift
// PerfectLib
//
// Created by Kyle Jessup on 7/5/15.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version, as supplemented by the
// Perfect Additional Terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License, as supplemented by the
// Perfect Additional Terms, for more details.
//
// You should have received a copy of the GNU Affero General Public License
// and the Perfect Additional Terms that immediately follow the terms and
// conditions of the GNU Affero General Public License along with this
// program. If not, see <http://www.perfect.org/AGPL_3_0_With_Perfect_Additional_Terms.txt>.
//
import Foundation
import LibEvent
// I'm unsure why these aren't coming through
/** Indicates that a timeout has occurred. It's not necessary to pass
this flag to event_for new()/event_assign() to get a timeout. */
let EV_TIMEOUT: Int32 = 0x01
/** Wait for a socket or FD to become readable */
let EV_READ: Int32 = 0x02
/** Wait for a socket or FD to become writeable */
let EV_WRITE: Int32 = 0x04
typealias EventCallBack = (Int32, Int16, AnyObject?) -> ()
private typealias PrimEventCallBack = @convention(c) (Int32, Int16, UnsafeMutablePointer<Void>) -> ()
class LibEvent {
internal static let eventBase: LibEventBase = LibEventBase()
private var event: COpaquePointer? = nil
private var userData: AnyObject?
private var cb: EventCallBack?
private var base: LibEventBase?
private static var eventCallBack: PrimEventCallBack {
let c: PrimEventCallBack = {
(a,b,c) in
let evt = Unmanaged<LibEvent>.fromOpaque(COpaquePointer(c)).takeRetainedValue()
let queue = evt.base!.eventDispatchQueue
let userData: AnyObject? = evt.userData
let callBack: EventCallBack = evt.cb!
evt.base = nil
evt.cb = nil
evt.userData = nil
evt.del()
dispatch_async(queue) {
callBack(a, b, userData)
}
}
return c
}
init(base: LibEventBase, fd: Int32, what: Int32, userData: AnyObject?, callBack: EventCallBack) {
self.userData = userData
self.cb = callBack
self.base = base
event = event_new(base.eventBase, fd, Int16(what), LibEvent.eventCallBack, UnsafeMutablePointer(Unmanaged.passRetained(self).toOpaque()))
}
deinit {
del()
}
func add(inout tv: timeval) {
event_add(event!, &tv)
}
func add(timeout: Double) {
if timeout == -1 {
event_add(event!, nil)
} else {
var tv: timeval = timeval()
let i = floor(timeout)
let f = timeout - i
tv.tv_sec = __darwin_time_t(i)
tv.tv_usec = __darwin_suseconds_t(f * 100000)
event_add(event!, &tv)
}
}
func add() {
event_add(event!, nil)
}
func del() {
if let e = event {
event_free(e)
self.event = nil
}
}
}
let EVLOOP_NO_EXIT_ON_EMPTY = Int32(0/*0x04*/) // not supported until libevent 2.1
class LibEventBase {
var eventBase: COpaquePointer
private var baseDispatchQueue: dispatch_queue_t
var eventDispatchQueue: dispatch_queue_t
init() {
evthread_use_pthreads()
baseDispatchQueue = dispatch_queue_create("LibEvent Base", DISPATCH_QUEUE_SERIAL)
eventDispatchQueue = dispatch_queue_create("LibEvent Event", DISPATCH_QUEUE_CONCURRENT)
eventBase = event_base_new()
addDummyEvent()
triggerEventBaseLoop()
}
private func addDummyEvent() {
let event = LibEvent(base: self, fd: -1, what: EV_TIMEOUT, userData: nil) {
(fd:Int32, w:Int16, ud:AnyObject?) -> () in
self.addDummyEvent()
}
event.add(1_000_000)
}
private func triggerEventBaseLoop() {
dispatch_async(baseDispatchQueue) {
self.eventBaseLoop()
}
}
private func eventBaseLoop() {
let r = event_base_dispatch(self.eventBase) //event_base_loop(self.eventBase, EVLOOP_NO_EXIT_ON_EMPTY)
if r == 1 {
triggerEventBaseLoop()
} else if r == -1 {
print("eventBaseLoop exited because of error")
}
}
}