about summary refs log tree commit diff
path: root/src/rt/rust_uv.cpp
diff options
context:
space:
mode:
authorJeff Olson <olson.jeffery@gmail.com>2012-03-15 21:42:07 -0700
committerBrian Anderson <banderson@mozilla.com>2012-04-06 15:35:48 -0700
commit3817ba7578a5044a5572f35de367b6ca31a35bc0 (patch)
tree95ef3041fd93b73c1930daed9407454b0275bec6 /src/rt/rust_uv.cpp
parent9d274ec5f2eb4b572aca99e98eb9a042882f999e (diff)
downloadrust-3817ba7578a5044a5572f35de367b6ca31a35bc0.tar.gz
rust-3817ba7578a5044a5572f35de367b6ca31a35bc0.zip
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!
Diffstat (limited to 'src/rt/rust_uv.cpp')
-rw-r--r--src/rt/rust_uv.cpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp
index e6d6c2e52ba..2a575bd00e3 100644
--- a/src/rt/rust_uv.cpp
+++ b/src/rt/rust_uv.cpp
@@ -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;
+}