core: Simplify uvll bindings and strip out currently-unused bits

No more mapping uv structs to Rust structs
This commit is contained in:
Brian Anderson 2013-03-12 20:04:25 -07:00
parent 723d2247c1
commit 54bb7226e1
5 changed files with 379 additions and 1795 deletions

View File

@ -44,21 +44,28 @@ use cast::{transmute, transmute_mut_region};
use ptr::null;
use sys::size_of;
use super::uvll;
use super::uvll::*;
use super::io::{IpAddr, Ipv4, Ipv6};
use unstable::finally::Finally;
#[cfg(test)] use unstable::run_in_bare_thread;
#[cfg(test)] use super::thread::Thread;
#[cfg(test)] use cell::Cell;
fn ip4_to_uv_ip4(addr: IpAddr) -> uvll::sockaddr_in {
fn ip4_as_uv_ip4(addr: IpAddr, f: &fn(*sockaddr_in)) {
match addr {
Ipv4(a, b, c, d, p) => {
unsafe {
uvll::ip4_addr(fmt!("%u.%u.%u.%u",
a as uint,
b as uint,
c as uint,
d as uint), p as int)
let addr = malloc_ip4_addr(fmt!("%u.%u.%u.%u",
a as uint,
b as uint,
c as uint,
d as uint), p as int);
do (|| {
f(addr);
}).finally {
free_ip4_addr(addr);
}
}
}
Ipv6 => fail!()
@ -301,7 +308,7 @@ pub impl StreamWatcher {
data.close_cb.swap_unwrap()();
}
drop_watcher_data(&mut stream_watcher);
unsafe { free(handle as *c_void) }
unsafe { free_handle(handle as *c_void) }
}
}
}
@ -330,8 +337,7 @@ impl Callback for ConnectionCallback { }
pub impl TcpWatcher {
static fn new(loop_: &mut Loop) -> TcpWatcher {
unsafe {
let size = size_of::<uvll::uv_tcp_t>() as size_t;
let handle = malloc(size) as *uvll::uv_tcp_t;
let handle = malloc_handle(UV_TCP);
fail_unless!(handle.is_not_null());
fail_unless!(0 == uvll::tcp_init(loop_.native_handle(), handle));
let mut watcher = NativeHandle::from_native_handle(handle);
@ -343,12 +349,13 @@ pub impl TcpWatcher {
fn bind(&mut self, address: IpAddr) {
match address {
Ipv4(*) => {
let addr = ip4_to_uv_ip4(address);
let result = unsafe {
uvll::tcp_bind(self.native_handle(), &addr)
};
// XXX: bind is likely to fail. need real error handling
fail_unless!(result == 0);
do ip4_as_uv_ip4(address) |addr| {
let result = unsafe {
uvll::tcp_bind(self.native_handle(), addr)
};
// XXX: bind is likely to fail. need real error handling
fail_unless!(result == 0);
}
}
_ => fail!()
}
@ -363,11 +370,12 @@ pub impl TcpWatcher {
let connect_handle = connect_watcher.native_handle();
match address {
Ipv4(*) => {
let addr = ip4_to_uv_ip4(address);
rtdebug!("connect_t: %x", connect_handle as uint);
fail_unless!(0 == uvll::tcp_connect(connect_handle,
self.native_handle(),
&addr, connect_cb));
do ip4_as_uv_ip4(address) |addr| {
rtdebug!("connect_t: %x", connect_handle as uint);
fail_unless!(0 == uvll::tcp_connect(connect_handle,
self.native_handle(),
addr, connect_cb));
}
}
_ => fail!()
}
@ -443,7 +451,7 @@ impl ConnectRequest {
static fn new() -> ConnectRequest {
let connect_handle = unsafe {
malloc(size_of::<uvll::uv_connect_t>() as size_t)
malloc_req(UV_CONNECT)
};
fail_unless!(connect_handle.is_not_null());
let connect_handle = connect_handle as *uvll::uv_connect_t;
@ -460,7 +468,7 @@ impl ConnectRequest {
}
fn delete(self) {
unsafe { free(self.native_handle() as *c_void) }
unsafe { free_req(self.native_handle() as *c_void) }
}
}
@ -482,7 +490,7 @@ impl WriteRequest {
static fn new() -> WriteRequest {
let write_handle = unsafe {
malloc(size_of::<uvll::uv_write_t>() as size_t)
malloc_req(UV_WRITE)
};
fail_unless!(write_handle.is_not_null());
let write_handle = write_handle as *uvll::uv_write_t;
@ -498,7 +506,7 @@ impl WriteRequest {
}
fn delete(self) {
unsafe { free(self.native_handle() as *c_void) }
unsafe { free_req(self.native_handle() as *c_void) }
}
}

File diff suppressed because it is too large Load Diff

View File

@ -930,8 +930,6 @@ pub unsafe fn tcp_connect(connect_ptr: *uv_connect_t,
addr_ptr: *sockaddr_in,
after_connect_cb: *u8)
-> libc::c_int {
log(debug, fmt!("b4 foreign tcp_connect--addr port: %u cb: %u",
(*addr_ptr).sin_port as uint, after_connect_cb as uint));
return rustrt::rust_uv_tcp_connect(connect_ptr, tcp_handle_ptr,
after_connect_cb, addr_ptr);
}
@ -1021,22 +1019,8 @@ pub unsafe fn async_send(async_handle: *uv_async_t) {
pub unsafe fn buf_init(input: *u8, len: uint) -> uv_buf_t {
let out_buf = uv_buf_t { base: ptr::null(), len: 0 as libc::size_t };
let out_buf_ptr = ptr::addr_of(&out_buf);
log(debug, fmt!("buf_init - input %u len %u out_buf: %u",
input as uint,
len as uint,
out_buf_ptr as uint));
// yuck :/
rustrt::rust_uv_buf_init(out_buf_ptr, input, len as size_t);
//let result = rustrt::rust_uv_buf_init_2(input, len as size_t);
log(debug, ~"after rust_uv_buf_init");
let res_base = get_base_from_buf(out_buf);
let res_len = get_len_from_buf(out_buf);
//let res_base = get_base_from_buf(result);
log(debug, fmt!("buf_init - result %u len %u",
res_base as uint,
res_len as uint));
return out_buf;
//return result;
}
pub unsafe fn ip4_addr(ip: &str, port: int)
-> sockaddr_in {
@ -1078,8 +1062,6 @@ pub unsafe fn ip6_name(src: &sockaddr_in6) -> ~str {
0u8,0u8,0u8,0u8,0u8,0u8];
do vec::as_imm_buf(dst) |dst_buf, size| {
let src_unsafe_ptr = to_unsafe_ptr(src);
log(debug, fmt!("val of src *sockaddr_in6: %? sockaddr_in6: %?",
src_unsafe_ptr, src));
let result = rustrt::rust_uv_ip6_name(src_unsafe_ptr,
dst_buf, size as libc::size_t);
match result {

View File

@ -479,6 +479,34 @@ extern "C" struct sockaddr_in6
rust_uv_ip6_addr(const char* ip, int port) {
return uv_ip6_addr(ip, port);
}
extern "C" struct sockaddr_in*
rust_uv_ip4_addrp(const char* ip, int port) {
struct sockaddr_in addr = uv_ip4_addr(ip, port);
struct sockaddr_in *addrp = (sockaddr_in*)malloc(sizeof(struct sockaddr_in));
assert(addrp);
memcpy(addrp, &addr, sizeof(struct sockaddr_in));
return addrp;
}
extern "C" struct sockaddr_in6*
rust_uv_ip6_addrp(const char* ip, int port) {
struct sockaddr_in6 addr = uv_ip6_addr(ip, port);
struct sockaddr_in6 *addrp = (sockaddr_in6*)malloc(sizeof(struct sockaddr_in6));
assert(addrp);
memcpy(addrp, &addr, sizeof(struct sockaddr_in6));
return addrp;
}
extern "C" void
rust_uv_free_ip4_addr(sockaddr_in *addrp) {
free(addrp);
}
extern "C" void
rust_uv_free_ip6_addr(sockaddr_in6 *addrp) {
free(addrp);
}
extern "C" int
rust_uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size) {
return uv_ip4_name(src, dst, size);
@ -563,3 +591,23 @@ extern "C" int
rust_uv_idle_stop(uv_idle_t* idle) {
return uv_idle_stop(idle);
}
extern "C" size_t
rust_uv_handle_size(uintptr_t type) {
return uv_handle_size((uv_handle_type)type);
}
extern "C" size_t
rust_uv_req_size(uintptr_t type) {
return uv_req_size((uv_req_type)type);
}
extern "C" uintptr_t
rust_uv_handle_type_max() {
return UV_HANDLE_TYPE_MAX;
}
extern "C" uintptr_t
rust_uv_req_type_max() {
return UV_REQ_TYPE_MAX;
}

View File

@ -202,3 +202,12 @@ rust_dbg_extern_identity_TwoU64s
rust_dbg_extern_identity_double
rust_dbg_extern_identity_u8
rust_get_rt_env
rust_uv_handle_size
rust_uv_req_size
rust_uv_handle_type_max
rust_uv_req_type_max
rust_uv_ip4_addrp
rust_uv_ip6_addrp
rust_uv_free_ip4_addr
rust_uv_free_ip6_addr