@@ -57,13 +57,10 @@ struct StreamReadEV {
5757var GLOBAL_REACTOR = new_uv_reactor();
5858
5959
60- use std::io;
6160
6261fn timer_cb(timer:*libuv::uv_timer_t) void {
6362 let ev = libuv::get_data_for_handle<libuv::uv_timer_t|TimerEV>(timer);
64- io::print_s("timer_cb\n");
6563 ev.wake();
66- io::print_s("timer_cb end\n");
6764 let cb = unsafe_cast<()>(&close_cb);
6865 let handle = unsafe_cast<()>(timer);
6966 libuv::uv_close(handle,cb);
@@ -120,62 +117,66 @@ use std::io;
120117fn async_cb(async_t:*libuv::uv_async_t) void {
121118 let reactor = libuv::get_data_for_handle<libuv::uv_async_t|UVReactor>(async_t);
122119 let ch = reactor.ch;
123- let ev = ch.recv();
124- match ev {
125- TimerEV(ev) => {
126- let timer = libuv::new_uv_timer_t();
127- gc::keep_alive_pinned(timer);
128- let cb = unsafe_cast<()>(&timer_cb);
129- gc::keep_alive_pinned(cb);
130- libuv::uv_timer_init(libuv::uv_default_loop(), timer);
131- libuv::set_data_for_handle(timer, &ev);
132- libuv::uv_timer_start(timer, cb, ev.timeout_ms, ev.repeat_ms);
133-
134- }
135- TCPListenEV(ev) => {
136- gc::keep_alive_pinned(ev.handle);
137- libuv::uv_tcp_init(reactor.loop, ev.handle);
138- libuv::uv_tcp_bind(ev.handle, &ev.addr, 0 as u32);
139- libuv::set_data_for_handle(ev.handle, &ev.on_connect);
140- let cb = unsafe_cast<()>(&conn_cb);
141-
142- libuv::uv_listen(unsafe_cast<libuv::uv_stream_t>(ev.handle), 128 as i32, cb);
143-
144-
145- }
146- StreamReadEV(ev) => {
147- gc::keep_alive_pinned(ev.handle);
148- let cb = unsafe_cast<()>(&read_cb);
149- let alloc_cb = unsafe_cast<()>(&alloc_cb);
150- libuv::uv_read_start(ev.handle, alloc_cb, cb);
151- libuv::set_data_for_handle(ev.handle, &ev);
152-
153- }
154- ReadStopEV(ev) => {
155- libuv::uv_read_stop(ev.handle);
156- }
157- WriteEV(ev) => {
158- let req = libuv::new_uv_write_t();
159- gc::keep_alive_pinned(req);
160-
161- let raw_slice_p = &ev.buf[0];
162- gc::pin(raw_slice_p);
163- let buf = libuv::uv_buf_init(&ev.buf[0], ev.buf.len() as u32);
164- let buf_p = &buf;
165- gc::pin(buf_p);
166- libuv::set_data_for_handle(req, &ev);
167-
168- let cb = unsafe_cast<()>(&write_cb);
169- libuv::uv_write(req, ev.handle, &buf, 1 as u32, cb);
170- libuv::set_data_for_handle(req, &ev);
171- }
172- StopEV(ev) => {
173- libuv::uv_stop(reactor.loop);
174- libuv::uv_loop_close(GLOBAL_REACTOR.loop);
175- }
176- _ => {
120+ let evs = ch.recv_all();
121+ for let i = 0; i < arr_len(evs); i = i + 1 {
122+ let ev = evs[i];
123+ match ev {
124+ TimerEV(ev) => {
125+ let timer = libuv::new_uv_timer_t();
126+ gc::keep_alive_pinned(timer);
127+ let cb = unsafe_cast<()>(&timer_cb);
128+ gc::keep_alive_pinned(cb);
129+ libuv::uv_timer_init(libuv::uv_default_loop(), timer);
130+ libuv::set_data_for_handle(timer, &ev);
131+ libuv::uv_timer_start(timer, cb, ev.timeout_ms, ev.repeat_ms);
132+
133+ }
134+ TCPListenEV(ev) => {
135+ gc::keep_alive_pinned(ev.handle);
136+ libuv::uv_tcp_init(reactor.loop, ev.handle);
137+ libuv::uv_tcp_bind(ev.handle, &ev.addr, 0 as u32);
138+ libuv::set_data_for_handle(ev.handle, &ev.on_connect);
139+ let cb = unsafe_cast<()>(&conn_cb);
140+
141+ libuv::uv_listen(unsafe_cast<libuv::uv_stream_t>(ev.handle), 128 as i32, cb);
142+
143+
144+ }
145+ StreamReadEV(ev) => {
146+ gc::keep_alive_pinned(ev.handle);
147+ let cb = unsafe_cast<()>(&read_cb);
148+ let alloc_cb = unsafe_cast<()>(&alloc_cb);
149+ libuv::uv_read_start(ev.handle, alloc_cb, cb);
150+ libuv::set_data_for_handle(ev.handle, &ev);
151+
152+ }
153+ ReadStopEV(ev) => {
154+ libuv::uv_read_stop(ev.handle);
155+ }
156+ WriteEV(ev) => {
157+ let req = libuv::new_uv_write_t();
158+ gc::keep_alive_pinned(req);
159+
160+ let raw_slice_p = &ev.buf[0];
161+ gc::pin(raw_slice_p);
162+ let buf = libuv::uv_buf_init(&ev.buf[0], ev.buf.len() as u32);
163+ let buf_p = &buf;
164+ gc::pin(buf_p);
165+ libuv::set_data_for_handle(req, &ev);
166+
167+ let cb = unsafe_cast<()>(&write_cb);
168+ libuv::uv_write(req, ev.handle, &buf, 1 as u32, cb);
169+ libuv::set_data_for_handle(req, &ev);
170+ }
171+ StopEV(ev) => {
172+ libuv::uv_stop(reactor.loop);
173+ libuv::uv_loop_close(GLOBAL_REACTOR.loop);
174+ }
175+ _ => {
176+ }
177177 }
178178 }
179+
179180 return;
180181}
181182
0 commit comments