-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtimer.go
65 lines (55 loc) · 1.19 KB
/
timer.go
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
package uv
/*
#include <uv.h>
*/
import "C"
import "unsafe"
type Timer struct {
t *C.uv_timer_t
l *C.uv_loop_t
Handle
}
func TimerInit(loop *Loop) (timer *Timer, err error) {
var t C.uv_timer_t
if loop == nil {
loop = DefaultLoop()
}
r := C.uv_timer_init(loop.l, &t)
if r != 0 {
return nil, &Error{int(r)}
}
t.data = unsafe.Pointer(&callback_info{})
return &Timer{&t, loop.l, Handle{(*C.uv_handle_t)(unsafe.Pointer(&t)), t.data}}, nil
}
func (timer *Timer) GetLoop() *Loop {
return &Loop{timer.l}
}
func (timer *Timer) Start(cb func(*Handle, int), timeout int64, repeat int64) (err error) {
cbi := (*callback_info)(timer.t.data)
cbi.timer_cb = cb
r := uv_timer_start(timer.t, timeout, repeat)
if r != 0 {
return &Error{r}
}
return nil
}
func (timer *Timer) Stop() (err error) {
r := C.uv_timer_stop(timer.t)
if r != 0 {
return &Error{int(r)}
}
return nil
}
func (timer *Timer) Again() (err error) {
r := C.uv_timer_again(timer.t)
if r != 0 {
return &Error{int(r)}
}
return nil
}
func (timer *Timer) SetRepeat(repeat uint64) {
C.uv_timer_set_repeat(timer.t, C.uint64_t(repeat))
}
func (timer *Timer) GetRepeat() int64 {
return int64(C.uv_timer_get_repeat(timer.t))
}