adding uv::direct and beginning to work out tcp request case

lots of changes, here.. should've commited sooner.
- added uv::direct module that contains rust fns that map, neatly, to
the libuv c library as much as possible. they operate on ptrs to libuv
structs mapped in rust, as much as possible (there are some notable
exceptions). these uv::direct fns should only take inputs from rust and,
as neccesary, translate them into C-friendly types and then pass to the
C functions. We want to them to return ints, as the libuv functions do,
so we can start tracking status.
- the notable exceptions for structs above is due to ref gh-1402, which
prevents us from passing structs, by value, across the Rust<->C barrier
(they turn to garbage, pretty much). So in the cases where we get back
by-val structs from C (uv_buf_init(), uv_ip4_addr(), uv_err_t in callbacks)
, we're going to use *ctypes::void (or just errnum ints for uv_err_t) until
gh-1402 is resolved.
- using crust functions, in these uv::direct fns, for callbacks from libuv,
will eschew uv_err_t, if possible, in favor a struct int.. if at all
possible (probably isn't.. hm.. i know libuv wants to eventually move to
replace uv_err_t with an int, as well.. so hm).
- started flushing out a big, gnarly test case to exercise the tcp request
side of the uv::direct functions. I'm at the point where, after the
connection is established, we write to the stream... when the writing is
done, we will read from it, then tear the whole thing down.

overall, it turns out that doing "close to the metal" interaction with
c libraries is painful (and more chatty) when orchestrated from rust. My
understanding is that not much, at all, is written in this fashion in the
existant core/std codebase.. malloc'ing in C has been preferred, from what
I've gathered. So we're treading new ground, here!
This commit is contained in:
Jeff Olson 2012-03-15 21:42:07 -07:00 committed by Brian Anderson
parent 9d274ec5f2
commit 3817ba7578
2 changed files with 585 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import map::hashmap;
export loop_new, loop_delete, run, close, run_in_bg;
export async_init, async_send;
export timer_init, timer_start, timer_stop;
export uv_ip4_addr, uv_ip6_addr;
// these are processed solely in the
// process_operation() crust fn below
@ -16,7 +17,7 @@ enum uv_operation {
enum uv_handle {
uv_async([u8], uv_loop),
uv_timer([u8], uv_loop)
uv_timer([u8], uv_loop),
}
enum uv_msg {
@ -50,6 +51,191 @@ enum uv_loop {
uv_loop_new(comm::chan<uv_msg>, *libc::c_void)
}
// libuv struct mappings
type uv_ip4_addr = {
ip: [u8],
port: int
};
type uv_ip6_addr = uv_ip4_addr;
enum uv_handle_type {
UNKNOWN_HANDLE = 0,
UV_TCP,
UV_UDP,
UV_NAMED_PIPE,
UV_TTY,
UV_FILE,
UV_TIMER,
UV_PREPARE,
UV_CHECK,
UV_IDLE,
UV_ASYNC,
UV_ARES_TASK,
UV_ARES_EVENT,
UV_PROCESS,
UV_FS_EVENT
}
type handle_type = ctypes::enum;
type uv_handle_fields = {
loop_handle: *ctypes::void,
type_: handle_type,
close_cb: *u8,
mutable data: *ctypes::void,
};
// unix size: 8
type uv_err_t = {
code: ctypes::c_int,
sys_errno_: ctypes::c_int
};
// don't create one of these directly. instead,
// count on it appearing in libuv callbacks or embedded
// in other types as a pointer to be used in other
// operations (so mostly treat it as opaque, once you
// have it in this form..)
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
type uv_stream_t = {
fields: uv_handle_fields
};
// unix size: 272
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
type uv_tcp_t = {
fields: uv_handle_fields,
a00: *u8, a01: *u8, a02: *u8, a03: *u8,
a04: *u8, a05: *u8, a06: *u8, a07: *u8,
a08: *u8, a09: *u8, a10: *u8, a11: *u8,
a12: *u8, a13: *u8, a14: *u8, a15: *u8,
a16: *u8, a17: *u8, a18: *u8, a19: *u8,
a20: *u8, a21: *u8, a22: *u8, a23: *u8,
a24: *u8, a25: *u8, a26: *u8, a27: *u8,
a28: *u8, a29: *u8
};
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn gen_stub_uv_tcp_t() -> uv_tcp_t {
ret { fields: { loop_handle: ptr::null(), type_: 0u32,
close_cb: ptr::null(),
mutable data: ptr::null() },
a00: 0 as *u8, a01: 0 as *u8, a02: 0 as *u8, a03: 0 as *u8,
a04: 0 as *u8, a05: 0 as *u8, a06: 0 as *u8, a07: 0 as *u8,
a08: 0 as *u8, a09: 0 as *u8, a10: 0 as *u8, a11: 0 as *u8,
a12: 0 as *u8, a13: 0 as *u8, a14: 0 as *u8, a15: 0 as *u8,
a16: 0 as *u8, a17: 0 as *u8, a18: 0 as *u8, a19: 0 as *u8,
a20: 0 as *u8, a21: 0 as *u8, a22: 0 as *u8, a23: 0 as *u8,
a24: 0 as *u8, a25: 0 as *u8, a26: 0 as *u8, a27: 0 as *u8,
a28: 0 as *u8, a29: 0 as *u8
};
}
#[cfg(target_os = "win32")]
type uv_tcp_t = {
loop_handle: *ctypes::void
};
#[cfg(target_os = "win32")]
fn gen_stub_uv_tcp_t() -> uv_tcp_t {
ret { loop_handle: ptr::null() };
}
// unix size: 48
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
type uv_connect_t = {
a00: *u8, a01: *u8, a02: *u8, a03: *u8,
a04: *u8, a05: *u8
};
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn gen_stub_uv_connect_t() -> uv_connect_t {
ret {
a00: 0 as *u8, a01: 0 as *u8, a02: 0 as *u8, a03: 0 as *u8,
a04: 0 as *u8, a05: 0 as *u8
};
}
// ref #1402 .. don't use this, like sockaddr_in
// unix size: 16
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
type uv_buf_t = {
base: *u8,
len: ctypes::size_t
};
// no gen stub method.. should create
// it via uv::direct::buf_init()
#[cfg(target_os = "win32")]
type uv_connect_t = {
loop_handle: *ctypes::void
};
#[cfg(target_os = "win32")]
fn gen_stub_uv_connect_t() -> uv_connect_t {
ret { loop_handle: ptr::null() };
}
// unix size: 144
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
type uv_write_t = {
fields: uv_handle_fields,
a00: *u8, a01: *u8, a02: *u8, a03: *u8,
a04: *u8, a05: *u8, a06: *u8, a07: *u8,
a08: *u8, a09: *u8, a10: *u8, a11: *u8,
a12: *u8, a13: *u8
};
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn gen_stub_uv_write_t() -> uv_write_t {
ret { fields: { loop_handle: ptr::null(), type_: 0u32,
close_cb: ptr::null(),
mutable data: ptr::null() },
a00: 0 as *u8, a01: 0 as *u8, a02: 0 as *u8, a03: 0 as *u8,
a04: 0 as *u8, a05: 0 as *u8, a06: 0 as *u8, a07: 0 as *u8,
a08: 0 as *u8, a09: 0 as *u8, a10: 0 as *u8, a11: 0 as *u8,
a12: 0 as *u8, a13: 0 as *u8
};
}
#[cfg(target_os = "win32")]
type uv_write_t = {
loop_handle: *ctypes::void
};
#[cfg(target_os = "win32")]
fn gen_stub_uv_write_t() -> uv_write_t {
ret { loop_handle: ptr::null() };
}
// not going to use this type, for now, because of
// github issue #1402
// unix size: 16
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
type sockaddr_in = {
sin_family: u16,
sin_port: u16,
sin_addr: u32, // in_addr: this is an opaque, per-platform struct
sin_zero: *u8
};
// unix size: 28 .. make due w/ 32
type sockaddr_in6 = {
a0: *u8, a1: *u8,
a2: *u8, a3: *u8
};
#[nolink]
native mod rustrt {
fn rust_uv_loop_new() -> *libc::c_void;
@ -78,6 +264,130 @@ native mod rustrt {
timeout: libc::c_uint,
repeat: libc::c_uint);
fn rust_uv_timer_stop(handle: *libc::c_void);
////////////
// NOT IN rustrt.def.in
////////////
fn rust_uv_free(ptr: *ctypes::void);
fn rust_uv_tcp_init(
loop_handle: *ctypes::void,
handle_ptr: *uv_tcp_t) -> ctypes::c_int;
fn rust_uv_buf_init(base: *u8, len: ctypes::size_t)
-> uv_buf_t;
fn rust_uv_last_error(loop_handle: *ctypes::void) -> uv_err_t;
fn rust_uv_ip4_addr(ip: *u8, port: ctypes::c_int)
-> *ctypes::void;
fn rust_uv_tcp_connect(connect_ptr: *uv_connect_t,
tcp_handle_ptr: *uv_tcp_t,
addr: *ctypes::void,
after_cb: *u8) -> ctypes::c_int;
// sizeof testing helpers
fn rust_uv_helper_uv_tcp_t_size() -> ctypes::c_uint;
fn rust_uv_helper_uv_connect_t_size() -> ctypes::c_uint;
fn rust_uv_helper_uv_buf_t_size() -> ctypes::c_uint;
fn rust_uv_helper_uv_write_t_size() -> ctypes::c_uint;
fn rust_uv_helper_uv_err_t_size() -> ctypes::c_uint;
fn rust_uv_helper_sockaddr_in_size() -> ctypes::c_uint;
// data accessors for rust-mapped uv structs
fn rust_uv_get_stream_handle_for_connect(connect: *uv_connect_t)
-> *uv_stream_t;
fn rust_uv_get_loop_for_uv_handle(handle: *ctypes::void)
-> *ctypes::void;
fn rust_uv_get_data_for_uv_handle(handle: *ctypes::void)
-> *ctypes::void;
fn rust_uv_set_data_for_uv_handle(handle: *ctypes::void,
data: *ctypes::void);
fn rust_uv_get_data_for_req(req: *ctypes::void) -> *ctypes::void;
fn rust_uv_set_data_for_req(req: *ctypes::void,
data: *ctypes::void);
}
// this module is structured around functions that directly
// expose libuv functionality and data structures. for use
// in higher level mappings
mod direct {
unsafe fn loop_new() -> *ctypes::void {
ret rustrt::rust_uv_loop_new();
}
unsafe fn loop_delete(loop_handle: *ctypes::void) {
rustrt::rust_uv_loop_delete(loop_handle);
}
unsafe fn run(loop_handle: *ctypes::void) {
rustrt::rust_uv_run(loop_handle);
}
unsafe fn tcp_init(loop_handle: *ctypes::void, handle: *uv_tcp_t)
-> ctypes::c_int {
ret rustrt::rust_uv_tcp_init(loop_handle, handle);
}
unsafe fn tcp_connect(connect_ptr: *uv_connect_t,
tcp_handle_ptr: *uv_tcp_t,
address: *ctypes::void,
after_connect_cb: *u8)
-> ctypes::c_int {
ret rustrt::rust_uv_tcp_connect(connect_ptr, tcp_handle_ptr,
address, after_connect_cb);
}
unsafe fn write(req: *ctypes::void, stream: *ctypes::void,
buf: *[uv_buf_t], cb: *u8) -> ctypes::c_int {
ret rustrt::rust_uv_write(
}
unsafe fn uv_last_error(loop_handle: *ctypes::void) -> uv_err_t {
ret rustrt::rust_uv_last_error(loop_handle);
}
// libuv struct initializers
unsafe fn tcp_t() -> uv_tcp_t {
ret gen_stub_uv_tcp_t();
}
unsafe fn connect_t() -> uv_connect_t {
ret gen_stub_uv_connect_t();
}
unsafe fn write_t() -> uv_write_t {
ret gen_stub_uv_write_t();
}
// FIXME: see github issue #1402
unsafe fn buf_init(input: *u8, len: uint) -> *ctypes::void {
ret rustrt::rust_uv_buf_init(input, len);
}
unsafe fn get_loop_for_uv_handle(handle: *ctypes::void)
-> *ctypes::void {
ret rustrt::rust_uv_get_loop_for_uv_handle(handle);
}
unsafe fn get_stream_handle_for_connect(connect: *uv_connect_t)
-> *uv_stream_t {
ret rustrt::rust_uv_get_stream_handle_for_connect(connect);
}
unsafe fn get_data_for_req(req: *ctypes::void) -> *ctypes::void {
ret rustrt::rust_uv_get_data_for_req(req);
}
unsafe fn set_data_for_req(req: *ctypes::void,
data: *ctypes::void) {
rustrt::rust_uv_set_data_for_req(req, data);
}
// FIXME: see github issue #1402
unsafe fn ip4_addr(ip: str, port: ctypes::c_int)
-> *ctypes::void {
let addr_vec = str::bytes(ip);
addr_vec += [0u8]; // add null terminator
let addr_vec_ptr = vec::unsafe::to_ptr(addr_vec);
let ip_back = str::from_bytes(addr_vec);
io::println(#fmt("vec val: '%s' length: %u",ip_back, vec::len(addr_vec)));
ret rustrt::rust_uv_ip4_addr(addr_vec_ptr, port);
}
// this is lame.
// FIXME: see github issue #1402
unsafe fn ip4_addr_free(ptr: *ctypes::void) {
rustrt::rust_uv_free(ptr);
}
}
// public functions
@ -626,3 +936,177 @@ fn test_uv_timer() {
assert comm::recv(exit_port);
uv::loop_delete(test_loop);
}
// BEGIN TCP REQUEST TEST SUITE
type request_wrapper = {
write_req: *uv_write_t,
req_buf: *[uv_buf_t]
};
crust fn on_alloc(handle: *ctypes::void,
suggested_size: ctypes::size_t) -> uv_buf_t
unsafe {
io::println("beginning on_alloc...");
io::println("ending on_alloc...");
let new_vec: @[u8] = @[];
let ptr = vec::unsafe::to_ptr(*new_vec);
ret direct::buf_init(ptr, vec::len(*new_vec));
}
crust fn on_write_complete_cb(write_handle: *uv_write_t,
status: ctypes::c_int) unsafe {
io::println("beginning on_write_complete_cb");
io::println("ending on_write_complete_cb");
}
crust fn on_connect_cb(connect_handle_ptr: *uv_connect_t,
status: ctypes::c_int) unsafe {
io::println(#fmt("beginning on_connect_cb .. status: %d",
status as int));
let stream = direct::get_stream_handle_for_connect(connect_handle_ptr);
if (status == 0i32) {
io::println("on_connect_cb: in status=0 if..");
let data = direct::get_data_for_req(
connect_handle_ptr as *ctypes::void)
as *request_wrapper;
let write_handle = (*data).write_req as *ctypes::void;
io::println(#fmt("on_connect_cb: tcp stream: %d write_handle addr %d",
stream as int, write_handle as int));
direct::write(write_handle,
stream as *ctypes::void,
(*data).req_buf,
on_write_complete_cb);
io::println("on_connect_cb: after direct::write()");
}
else {
let loop_handle = direct::get_loop_for_uv_handle(
stream as *ctypes::void);
let err = direct::uv_last_error(loop_handle);
}
io::println("finishing on_connect_cb");
}
fn impl_uv_tcp_request() unsafe {
let test_loop = direct::loop_new();
let tcp_handle = direct::tcp_t();
let tcp_handle_ptr = ptr::addr_of(tcp_handle);
let connect_handle = direct::connect_t();
let connect_handle_ptr = ptr::addr_of(connect_handle);
// this is the persistent payload of data that we
// need to pass around to get this example to work.
// In C, this would be a malloc'd or stack-allocated
// struct that we'd cast to a void* and store as the
// data field in our uv_connect_t struct
let req_str = str::bytes("GET / HTTP/1.1\r\nHost: google.com"
+ "\r\n\r\n\r\n");
let req_msg_ptr: *u8 = vec::unsafe::to_ptr(req_str);
let req_msg = [
direct::buf_init(req_msg_ptr, vec::len(req_str))
];
// this is the enclosing record, we'll pass a ptr to
// this to C..
let write_handle = direct::write_t();
let write_handle_ptr = ptr::addr_of(write_handle);
io::println(#fmt("tcp req setup: tcp stream: %d write_handle addr %d",
tcp_handle_ptr as int, write_handle_ptr as int));
let req = { writer_handle: write_handle_ptr,
req_buf: ptr::addr_of(req_msg) };
io::println("building addr...");
let addr = direct::ip4_addr("173.194.33.40", 80i32);
let tcp_init_result = direct::tcp_init(
test_loop as *ctypes::void, tcp_handle_ptr);
if (tcp_init_result == 0i32) {
io::println("sucessful tcp_init_result");
// this should set up the connection request..
let tcp_connect_result = direct::tcp_connect(
connect_handle_ptr, tcp_handle_ptr,
addr, on_connect_cb);
if (tcp_connect_result == 0i32) {
// not set the data on the connect_req until its initialized
direct::set_data_for_req(
connect_handle_ptr as *ctypes::void,
ptr::addr_of(req) as *ctypes::void);
io::println("before run tcp req loop");
direct::run(test_loop);
io::println("after run tcp req loop");
// FIXME: see github issue #1402
direct::ip4_addr_free(addr);
}
else {
io::println("direct::tcp_connect() failure");
assert false;
}
}
else {
io::println("direct::tcp_init() failure");
assert false;
}
}
// START HERE AND WORK YOUR WAY UP VIA CALLBACKS
#[test]
#[ignore(cfg(target_os = "freebsd"))]
fn test_uv_tcp_request() unsafe {
impl_uv_tcp_request();
}
// END TCP REQUEST TEST SUITE
// struct size tests
#[test]
#[ignore(cfg(target_os = "freebsd"))]
fn test_uv_struct_size_uv_tcp_t() {
let native_handle_size = rustrt::rust_uv_helper_uv_tcp_t_size();
let rust_handle_size = sys::size_of::<uv_tcp_t>();
let output = #fmt("uv_tcp_t -- native: %u rust: %u",
native_handle_size as uint, rust_handle_size);
io::println(output);
assert native_handle_size as uint == rust_handle_size;
}
#[test]
#[ignore(cfg(target_os = "freebsd"))]
fn test_uv_struct_size_uv_connect_t() {
let native_handle_size =
rustrt::rust_uv_helper_uv_connect_t_size();
let rust_handle_size = sys::size_of::<uv_connect_t>();
let output = #fmt("uv_connect_t -- native: %u rust: %u",
native_handle_size as uint, rust_handle_size);
io::println(output);
assert native_handle_size as uint == rust_handle_size;
}
#[test]
#[ignore(cfg(target_os = "freebsd"))]
fn test_uv_struct_size_uv_buf_t() {
let native_handle_size =
rustrt::rust_uv_helper_uv_buf_t_size();
let rust_handle_size = sys::size_of::<uv_buf_t>();
let output = #fmt("uv_buf_t -- native: %u rust: %u",
native_handle_size as uint, rust_handle_size);
io::println(output);
assert native_handle_size as uint == rust_handle_size;
}
#[test]
#[ignore(cfg(target_os = "freebsd"))]
fn test_uv_struct_size_uv_write_t() {
let native_handle_size =
rustrt::rust_uv_helper_uv_write_t_size();
let rust_handle_size = sys::size_of::<uv_write_t>();
let output = #fmt("uv_write_t -- native: %u rust: %u",
native_handle_size as uint, rust_handle_size);
io::println(output);
assert native_handle_size as uint == rust_handle_size;
}
#[test]
#[ignore(cfg(target_os = "freebsd"))]
fn test_uv_struct_size_sockaddr_in() {
let native_handle_size =
rustrt::rust_uv_helper_sockaddr_in_size();
let rust_handle_size = sys::size_of::<sockaddr_in>();
let output = #fmt("sockaddr_in -- native: %u rust: %u",
native_handle_size as uint, rust_handle_size);
io::println(output);
assert native_handle_size as uint == rust_handle_size;
}

View File

@ -80,6 +80,10 @@ native_close_op_cb(uv_handle_t* op_handle) {
}
// native fns bound in rust
extern "C" void
rust_uv_free(void* ptr) {
current_kernel_free(ptr);
}
extern "C" void*
rust_uv_loop_new() {
return (void*)uv_loop_new();
@ -195,3 +199,99 @@ rust_uv_timer_stop(uv_timer_t* the_timer) {
uv_timer_stop(the_timer);
}
extern "C" int
rust_uv_tcp_init(uv_loop_t* loop, uv_tcp_t* handle) {
return uv_tcp_init(loop, handle);
}
extern "C" size_t
rust_uv_helper_uv_tcp_t_size() {
return sizeof(uv_tcp_t);
}
extern "C" size_t
rust_uv_helper_uv_connect_t_size() {
return sizeof(uv_connect_t);
}
extern "C" size_t
rust_uv_helper_uv_buf_t_size() {
return sizeof(uv_buf_t);
}
extern "C" size_t
rust_uv_helper_uv_write_t_size() {
return sizeof(uv_write_t);
}
extern "C" size_t
rust_uv_helper_uv_err_t_size() {
return sizeof(uv_err_t);
}
extern "C" size_t
rust_uv_helper_sockaddr_in_size() {
return sizeof(sockaddr_in);
}
extern "C" uv_stream_t*
rust_uv_get_stream_handle_for_connect(uv_connect_t* connect) {
return connect->handle;
}
extern "C" uv_buf_t
rust_uv_buf_init(char* base, size_t len) {
return uv_buf_init(base, len);
}
extern "C" uv_loop_t*
rust_uv_get_loop_for_uv_handle(uv_handle_t* handle) {
return handle->loop;
}
extern "C" void*
rust_uv_get_data_for_uv_handle(uv_handle_t* handle) {
return handle->data;
}
extern "C" void
rust_uv_set_data_for_uv_handle(uv_handle_t* handle,
void* data) {
handle->data = data;
}
extern "C" void*
rust_uv_get_data_for_req(uv_req_t* req) {
return req->data;
}
extern "C" void
rust_uv_set_data_for_req(uv_req_t* req, void* data) {
req->data = data;
}
extern "C" uv_err_t
rust_uv_last_error(uv_loop_t* loop) {
return uv_last_error(loop);
}
extern "C" int
rust_uv_tcp_connect(uv_connect_t* connect_ptr,
uv_tcp_t* tcp_ptr,
void* addr_ptr,
uv_connect_cb cb) {
//return uv_tcp_connect(connect_ptr, tcp_ptr, addr, cb);
printf("inside rust_uv_tcp_connect\n");
sockaddr_in addr_tmp = *((sockaddr_in*)addr_ptr);
sockaddr_in addr = addr_tmp;
printf("before tcp_connect .. port: %d\n", addr.sin_port);
int result = uv_tcp_connect(connect_ptr, tcp_ptr, addr, cb);
printf ("leaving rust_uv_tcp_connect.. and result: %d\n", result);
return result;
}
extern "C" void*
rust_uv_ip4_addr(const char* ip, int port) {
sockaddr_in* addr_ptr = (sockaddr_in*)current_kernel_malloc(
sizeof(sockaddr_in),
"sockaddr_in");
printf("before creating addr_ptr.. ip %s port %d\n", ip, port);
*addr_ptr = uv_ip4_addr("173.194.33.40", 80);
printf("after creating .. port: %d\n", addr_ptr->sin_port);
return (void*)addr_ptr;
}