Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 101 lines (84 sloc) 3.146 kB
5f4610b initial commit
aycan.irican authored
1 // thnx to:
2 // http://therning.org/magnus/archives/238
3
4 #include <ev.h>
5 #include <stdio.h> // puts
6 #include <stdlib.h> // malloc
7 #include <string.h> // bzero
8
2eb655d using C socket calls via FFI
aycan.irican authored
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <stdio.h>
12 #include <netdb.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include <netinet/in_systm.h>
18 #include <netinet/ip.h>
19 #include <netinet/tcp.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <errno.h>
24
25
5f4610b initial commit
aycan.irican authored
26 /* evloop */
27 struct ev_loop * wev_default_loop (int f) { ev_default_loop(f); }
28 struct ev_loop * wev_loop_new (unsigned int f) { ev_loop_new(f); }
29 void wev_loop (struct ev_loop *l, int f) { ev_loop(l,f);}
30 void wev_unloop(struct ev_loop *l, int h) { ev_unloop(l,h); }
31 void wev_loop_destroy(struct ev_loop *l) { ev_loop_destroy(l); }
32
33 /* ev_io */
34 void wev_io_init (ev_io *w, void *cb, int fd, int events) { ev_io_init(w,cb,fd,events); }
35 void wev_io_start (struct ev_loop *l, ev_io *w) { ev_io_start(l, w); }
2482b0f finally test is working.
aycan.irican authored
36 void wev_io_stop (struct ev_loop *l, ev_io *w) { ev_io_stop(l, w); }
37 /* void *wmkevio () { size_t s = (sizeof (struct ev_io)); void *ptr = malloc (s); bzero(ptr,s); return ptr;} */
38 /* void wfreeevio (struct ev_io *evio) { free(evio); } */
5f4610b initial commit
aycan.irican authored
39
40 /* ev_timer */
41 void wev_timer_init (ev_timer *w, void *cb, ev_tstamp after, ev_tstamp repeat)
42 { ev_timer_init(w,cb,after,repeat); }
43 void wev_timer_start (struct ev_loop *l, ev_timer *w) { ev_timer_start(l, w); }
2482b0f finally test is working.
aycan.irican authored
44 void wev_timer_stop (struct ev_loop *l, ev_timer *w) { ev_timer_stop(l, w); }
5f4610b initial commit
aycan.irican authored
45
47b4203 @gregorycollins Add support for ev_async watchers
authored
46 /* ev_async */
47 void wev_async_init (ev_async *w, void *cb) {
48 ev_async_init(w,cb);
49 }
50 void wev_async_send (struct ev_loop *l, ev_async *w) { ev_async_send(l,w); }
51 void wev_async_start (struct ev_loop *l, ev_async *w) { ev_async_start(l,w); }
52 void wev_async_stop (struct ev_loop *l, ev_async *w) { ev_async_stop(l,w); }
53
2eb655d using C socket calls via FFI
aycan.irican authored
54 void set_nonblocking(int fd)
55 {
56 int flags;
57 /* If they have O_NONBLOCK, use the Posix way to do it */
58 if (-1 == (flags = fcntl(fd, F_GETFL, 0))) flags = 0;
59 int res = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
60 assert(res==0);
61 }
62
63 int c_accept(int server_fd) {
64 int fd = accept(server_fd, NULL, NULL);
65 if (fd == -1 && ( errno == EAGAIN || errno == EWOULDBLOCK) ) return -2;
66 if (fd > 0) {
67 set_nonblocking(fd);
68 return fd;
69 } else {
70 return -1;
71 }
72 }
73
5f4610b initial commit
aycan.irican authored
74 /* test: gcc -g -lev wrapper.c -o wrapper */
75
76 /* ev_io stdin_watcher; */
77 /* ev_timer timeout_watcher; */
78
79 /* static void stdin_cb (EV_P_ struct ev_io *w, int revents) { */
80 /* puts ("stdin ready"); */
81 /* wev_io_stop (EV_A_ w); */
82 /* wev_unloop (EV_A_ EVUNLOOP_ALL); */
83 /* } */
84
85 /* static void timeout_cb (EV_P_ struct ev_timer *w, int revents) { */
86 /* puts ("timeout"); */
87 /* wev_unloop (EV_A_ EVUNLOOP_ONE); */
88 /* } */
89
90 /* int main (int argc, char** argv) { */
91 /* struct ev_loop *loop = wev_default_loop(0); */
92 /* wev_io_init (&stdin_watcher, stdin_cb, /\*STDIN_FILENO*\/ 0, EV_READ); */
93 /* wev_io_start (loop, &stdin_watcher); */
94
95 /* wev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); */
96 /* wev_timer_start (loop, &timeout_watcher); */
97
98 /* wev_loop (loop, 0); */
99 /* return 0; */
100 /* } */
Something went wrong with that request. Please try again.