-
Notifications
You must be signed in to change notification settings - Fork 0
/
picoev.go
332 lines (290 loc) · 6.67 KB
/
picoev.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package picoev
/*
#include "picoev.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <fcntl.h>
#include <signal.h>
static inline void setup_sock(int fd) {
int on = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(int));
fcntl(fd, F_SETFL, O_NONBLOCK);
}
typedef struct {
int port;
int fd;
int reuse_addr_res;
int reuse_port_res;
int size;
int bind_res;
int listen_res;
int tcp_nodelay_res;
int nonblock_res;
} picoev_bind_acceptor_t;
void do_bind_acceptor(uintptr_t arg0, uintptr_t arg1) {
picoev_bind_acceptor_t* args = (picoev_bind_acceptor_t*)(void*)arg0;
int fd = socket(AF_INET, SOCK_STREAM, 0);
args->fd = fd;
if (fd == -1) {
return;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(args->port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
args->bind_res = bind(fd, (const struct sockaddr*)&addr, sizeof(addr));
if (args->bind_res != 0) {
return;
}
args->listen_res = listen(fd, SOMAXCONN);
if (args->listen_res != 0) {
return;
}
int on = 1;
args->tcp_nodelay_res = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(int));
if (args->tcp_nodelay_res != 0) {
return;
}
args->nonblock_res = fcntl(fd, F_SETFL, O_NONBLOCK);
if (args->nonblock_res != 0) {
return;
}
}
typedef struct {
int max_fd;
int result;
} picoev_init_t;
void do_picoev_init(size_t arg0, size_t arg1) {
picoev_init_t* args = (picoev_init_t*)(void*)arg0;
args->result = picoev_init(args->max_fd);
}
typedef struct {
int result;
} picoev_deinit_t;
void do_picoev_deinit(size_t arg0, size_t arg1) {
picoev_deinit_t* args = (picoev_deinit_t*)(void*)arg0;
args->result = picoev_deinit();
}
typedef struct {
size_t max_timeout;
int loop;
} picoev_create_loop_t;
void do_picoev_create_loop(size_t arg0, size_t arg1) {
picoev_create_loop_t* args = (picoev_create_loop_t*)(void*)arg0;
args->loop = (size_t)picoev_create_loop((int)args->max_timeout);
}
typedef struct {
size_t loop;
size_t result;
} picoev_destroy_loop_t;
void do_picoev_destroy_loop(size_t arg0, size_t arg1) {
picoev_destroy_loop_t* args = (picoev_destroy_loop_t*)(void*)arg0;
args->result = (size_t)picoev_destroy_loop((picoev_loop*)(void*)args->loop);
}
typedef struct {
size_t loop;
int fd;
int secs;
} picoev_set_timeout_t;
void do_picoev_set_timeout_loop(size_t arg0, size_t arg1) {
picoev_set_timeout_t* args = (picoev_set_timeout_t*)(void*)arg0;
picoev_set_timeout((picoev_loop*)(void*)args->loop, args->fd, args->secs);
}
void do_picoev_accept(picoev_loop* loop, int fd, int revents, void* cb_arg) {
}
typedef struct {
size_t loop;
int max_wait;
int result;
} picoev_loop_once_t;
void do_picoev_loop_once(size_t arg0, size_t arg1) {
picoev_loop_once_t* args = (picoev_loop_once_t*)(void*)arg0;
args->result = picoev_loop_once((picoev_loop*)(void*)args->loop, args->max_wait);
}
*/
import "C"
import (
"errors"
"github.com/moontrade/nogc"
"github.com/moontrade/nogc/unsafecgo"
"sync"
"unsafe"
)
type Loop C.picoev_loop
type Conn struct {
FD int32
_ int32
Read nogc.FatPointer
Write nogc.FatPointer
}
var (
initialized = false
initResult = 0
maxFD = 1024
mu sync.Mutex
)
func MaxFD() int {
return maxFD
}
func SetMaxFD(maxFd int) {
maxFD = maxFd
}
type initT struct {
maxFd int32
result int32
}
func Init(maxFd int) int {
mu.Lock()
defer mu.Unlock()
if initialized {
return initResult
}
args := initT{maxFd: int32(maxFd)}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_picoev_init), ptr, 0)
if args.result == 0 {
initialized = true
initResult = 0
} else {
initResult = int(args.result)
}
return int(args.result)
}
type deinitT struct {
result int32
}
func Deinit() int {
mu.Lock()
defer mu.Unlock()
if !initialized {
return 0
}
args := deinitT{}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_picoev_deinit), ptr, 0)
return int(args.result)
}
type createLoop struct {
maxTimeout uintptr
loop uintptr
}
func New(maxTimeout int) *Loop {
mu.Lock()
isInitialized := initialized
mu.Unlock()
if !isInitialized {
Init(maxFD)
}
args := createLoop{maxTimeout: uintptr(maxTimeout)}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_picoev_create_loop), ptr, 0)
return (*Loop)(unsafe.Pointer(args.loop))
}
/*
typedef struct {
int port;
int fd;
int reuse_addr_res;
int reuse_port_res;
int size;
int bind_res;
int listen_res;
int tcp_nodelay_res;
int nonblock_res;
} picoev_bind_acceptor_t;
*/
type bindAcceptorT struct {
port int32
fd int32
reuseAddrRes int32
reusePortRes int32
size int32
bindRes int32
listenRes int32
tcpNoDelayRes int32
nonblockRes int32
}
var (
ErrFD = errors.New("C.socket failed")
ErrReuseAddr = errors.New("reuse addr")
ErrReusePort = errors.New("reuse port")
ErrBind = errors.New("bind")
ErrListen = errors.New("listen")
ErrTCPNoDelay = errors.New("TCP_NODELAY")
ErrNONBLOCK = errors.New("NONBLOCK")
)
func (l *Loop) BindAcceptor(port int) error {
args := bindAcceptorT{port: int32(port)}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_bind_acceptor), ptr, 0)
if args.fd < 0 {
return ErrFD
}
if args.reuseAddrRes != 0 {
return ErrReuseAddr
}
if args.reusePortRes != 0 {
return ErrReusePort
}
if args.bindRes != 0 {
return ErrBind
}
if args.listenRes != 0 {
return ErrListen
}
if args.tcpNoDelayRes != 0 {
return ErrTCPNoDelay
}
if args.nonblockRes != 0 {
return ErrNONBLOCK
}
return nil
}
type destroyLoop struct {
loop uintptr
result int32
}
func (l *Loop) Destroy() int {
args := destroyLoop{loop: uintptr(unsafe.Pointer(l))}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_picoev_destroy_loop), ptr, 0)
return int(args.result)
}
type setTimeout struct {
loop uintptr
fd int32
secs int32
}
func (l *Loop) SetTimeout(fd, secs int32) {
args := setTimeout{
loop: uintptr(unsafe.Pointer(l)),
fd: fd,
secs: secs,
}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_picoev_set_timeout_loop), ptr, 0)
}
//func (l *Loop) Add(fd, events, timeoutInSecs)
type loopOnceT struct {
loop uintptr
maxWait int32
result int32
}
func (l *Loop) Once(maxWait int) int {
args := loopOnceT{loop: uintptr(unsafe.Pointer(l)), maxWait: int32(maxWait)}
ptr := uintptr(unsafe.Pointer(&args))
unsafecgo.NonBlocking((*byte)(C.do_picoev_loop_once), ptr, 0)
return int(args.result)
}
func (l *Loop) run() {
var (
args = destroyLoop{loop: uintptr(unsafe.Pointer(l))}
ptr = uintptr(unsafe.Pointer(&args))
)
for {
unsafecgo.NonBlocking((*byte)(C.do_picoev_loop_once), ptr, 0)
}
}