Skip to content

Commit 9e39219

Browse files
fzzzybrson
authored andcommitted
Shuffle around to work with rust-spidermonkey
1 parent 6d360d2 commit 9e39219

File tree

3 files changed

+80
-51
lines changed

3 files changed

+80
-51
lines changed

src/libstd/uvtmp.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ native mod rustrt {
1313
fn rust_uvtmp_delete_thread(thread: thread);
1414
fn rust_uvtmp_connect(
1515
thread: thread,
16+
req_id: u32,
1617
ip: str::sbuf,
17-
chan: comm::chan<iomsg>);
18-
fn rust_uvtmp_close_connection(thread: thread, cd: connect_data);
18+
chan: comm::chan<iomsg>) -> connect_data;
19+
fn rust_uvtmp_close_connection(thread: thread, req_id: u32);
1920
fn rust_uvtmp_write(
2021
thread: thread,
21-
cd: connect_data,
22+
req_id: u32,
2223
buf: *u8,
2324
len: ctypes::size_t,
2425
chan: comm::chan<iomsg>);
2526
fn rust_uvtmp_read_start(
2627
thread: thread,
27-
cd: connect_data,
28+
req_id: u32,
2829
chan: comm::chan<iomsg>);
2930
fn rust_uvtmp_delete_buf(buf: *u8);
31+
fn rust_uvtmp_get_req_id(cd: connect_data) -> u32;
3032
}
3133

3234
type thread = *ctypes::void;
@@ -56,31 +58,36 @@ fn delete_thread(thread: thread) {
5658
rustrt::rust_uvtmp_delete_thread(thread)
5759
}
5860

59-
fn connect(thread: thread, ip: str, ch: comm::chan<iomsg>) {
61+
fn connect(thread: thread, req_id: u32,
62+
ip: str, ch: comm::chan<iomsg>) -> connect_data {
6063
str::as_buf(ip) {|ipbuf|
61-
rustrt::rust_uvtmp_connect(thread, ipbuf, ch)
64+
rustrt::rust_uvtmp_connect(thread, req_id, ipbuf, ch)
6265
}
6366
}
6467

65-
fn close_connection(thread: thread, cd: connect_data) {
66-
rustrt::rust_uvtmp_close_connection(thread ,cd);
68+
fn close_connection(thread: thread, req_id: u32) {
69+
rustrt::rust_uvtmp_close_connection(thread, req_id);
6770
}
6871

69-
fn write(thread: thread, cd: connect_data,bytes: [u8],
72+
fn write(thread: thread, req_id: u32, bytes: [u8],
7073
chan: comm::chan<iomsg>) unsafe {
7174
rustrt::rust_uvtmp_write(
72-
thread, cd, vec::to_ptr(bytes), vec::len(bytes), chan);
75+
thread, req_id, vec::to_ptr(bytes), vec::len(bytes), chan);
7376
}
7477

75-
fn read_start(thread: thread, cd: connect_data,
78+
fn read_start(thread: thread, req_id: u32,
7679
chan: comm::chan<iomsg>) {
77-
rustrt::rust_uvtmp_read_start(thread, cd, chan);
80+
rustrt::rust_uvtmp_read_start(thread, req_id, chan);
7881
}
7982

8083
fn delete_buf(buf: *u8) {
8184
rustrt::rust_uvtmp_delete_buf(buf);
8285
}
8386

87+
fn get_req_id(cd: connect_data) -> u32 {
88+
ret rustrt::rust_uvtmp_get_req_id(cd);
89+
}
90+
8491
#[test]
8592
fn test_start_stop() {
8693
let thread = create_thread();
@@ -96,7 +103,7 @@ fn test_connect() {
96103
start_thread(thread);
97104
let port = comm::port();
98105
let chan = comm::chan(port);
99-
connect(thread, "74.125.224.146", chan);
106+
connect(thread, 0u32, "74.125.224.146", chan);
100107
alt comm::recv(port) {
101108
connected(cd) {
102109
close_connection(thread, cd);
@@ -113,7 +120,7 @@ fn test_http() {
113120
start_thread(thread);
114121
let port = comm::port();
115122
let chan = comm::chan(port);
116-
connect(thread, "74.125.224.146", chan);
123+
connect(thread, 0u32, "74.125.224.146", chan);
117124
alt comm::recv(port) {
118125
connected(cd) {
119126
write(thread, cd, str::bytes("GET / HTTP/1.0\n\n"), chan);

src/rt/rust_uvtmp.cpp

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
class rust_uvtmp_thread;
88

99
struct connect_data {
10+
uint32_t req_id;
1011
rust_uvtmp_thread *thread;
12+
char * ip_addr;
1113
uv_connect_t connect;
1214
uv_tcp_t tcp;
1315
chan_handle chan;
@@ -60,12 +62,13 @@ send(rust_task *task, chan_handle chan, void *data) {
6062
class rust_uvtmp_thread : public rust_thread {
6163

6264
private:
65+
std::map<int, connect_data *> req_map;
6366
rust_task *task;
6467
uv_loop_t *loop;
6568
uv_idle_t idle;
6669
lock_and_signal lock;
6770
bool stop_flag;
68-
std::queue<std::pair<std::string, chan_handle> > connect_queue;
71+
std::queue<std::pair<connect_data *, chan_handle> > connect_queue;
6972
std::queue<connect_data*> close_connection_queue;
7073
std::queue<write_data*> write_queue;
7174
std::queue<read_start_data*> read_start_queue;
@@ -90,40 +93,50 @@ class rust_uvtmp_thread : public rust_thread {
9093
stop_flag = true;
9194
}
9295

93-
void connect(char *ip, chan_handle chan) {
96+
connect_data *connect(uint32_t req_id, char *ip, chan_handle chan) {
9497
scoped_lock with(lock);
95-
connect_queue.push(std::pair<std::string, chan_handle>
96-
(std::string(ip), chan));
98+
if (req_map.count(req_id)) return NULL;
99+
connect_data *cd = new connect_data();
100+
req_map[req_id] = cd;
101+
cd->req_id = req_id;
102+
cd->ip_addr = ip;
103+
connect_queue.push(
104+
std::pair<connect_data *, chan_handle>(cd, chan));
105+
return cd;
97106
}
98107

99108
void
100-
close_connection(connect_data *cd) {
101-
scoped_lock with(lock);
102-
close_connection_queue.push(cd);
109+
close_connection(uint32_t req_id) {
110+
scoped_lock with(lock);
111+
connect_data *cd = req_map[req_id];
112+
close_connection_queue.push(cd);
113+
req_map.erase(req_id);
103114
}
104115

105116
void
106-
write(connect_data *cd, uint8_t *buf, size_t len, chan_handle chan) {
107-
scoped_lock with(lock);
108-
write_data *wd = new write_data();
109-
wd->cd = cd;
110-
wd->buf = new uint8_t[len];
111-
wd->len = len;
112-
wd->chan = chan;
113-
114-
memcpy(wd->buf, buf, len);
115-
116-
write_queue.push(wd);
117+
write(uint32_t req_id, uint8_t *buf, size_t len, chan_handle chan) {
118+
scoped_lock with(lock);
119+
connect_data *cd = req_map[req_id];
120+
write_data *wd = new write_data();
121+
wd->cd = cd;
122+
wd->buf = new uint8_t[len];
123+
wd->len = len;
124+
wd->chan = chan;
125+
126+
memcpy(wd->buf, buf, len);
127+
128+
write_queue.push(wd);
117129
}
118130

119131
void
120-
read_start(connect_data *cd, chan_handle chan) {
121-
scoped_lock with(lock);
122-
read_start_data *rd = new read_start_data();
123-
rd->cd = cd;
124-
rd->chan = chan;
125-
126-
read_start_queue.push(rd);
132+
read_start(uint32_t req_id, chan_handle chan) {
133+
scoped_lock with(lock);
134+
connect_data *cd = req_map[req_id];
135+
read_start_data *rd = new read_start_data();
136+
rd->cd = cd;
137+
rd->chan = chan;
138+
139+
read_start_queue.push(rd);
127140
}
128141

129142
private:
@@ -153,12 +166,12 @@ class rust_uvtmp_thread : public rust_thread {
153166
make_new_connections() {
154167
assert(lock.lock_held_by_current_thread());
155168
while (!connect_queue.empty()) {
156-
std::pair<std::string, chan_handle> pair = connect_queue.front();
169+
std::pair<connect_data *, chan_handle> pair = connect_queue.front();
157170
connect_queue.pop();
171+
connect_data *cd = pair.first;
158172
struct sockaddr_in client_addr = uv_ip4_addr("0.0.0.0", 0);
159-
struct sockaddr_in server_addr = uv_ip4_addr(pair.first.c_str(), 80);
173+
struct sockaddr_in server_addr = uv_ip4_addr(cd->ip_addr, 80);
160174

161-
connect_data *cd = new connect_data();
162175
cd->thread = this;
163176
cd->chan = pair.second;
164177
cd->connect.data = cd;
@@ -318,29 +331,36 @@ rust_uvtmp_delete_thread(rust_uvtmp_thread *thread) {
318331
delete thread;
319332
}
320333

321-
extern "C" void
322-
rust_uvtmp_connect(rust_uvtmp_thread *thread, char *ip, chan_handle *chan) {
323-
thread->connect(ip, *chan);
334+
extern "C" connect_data *
335+
rust_uvtmp_connect(rust_uvtmp_thread *thread, uint32_t req_id, char *ip, chan_handle *chan) {
336+
return thread->connect(req_id, ip, *chan);
324337
}
325338

326339
extern "C" void
327-
rust_uvtmp_close_connection(rust_uvtmp_thread *thread, connect_data *cd) {
328-
thread->close_connection(cd);
340+
rust_uvtmp_close_connection(rust_uvtmp_thread *thread, uint32_t req_id) {
341+
thread->close_connection(req_id);
329342
}
330343

331344
extern "C" void
332-
rust_uvtmp_write(rust_uvtmp_thread *thread, connect_data *cd,
345+
rust_uvtmp_write(rust_uvtmp_thread *thread, uint32_t req_id,
333346
uint8_t *buf, size_t len, chan_handle *chan) {
334-
thread->write(cd, buf, len, *chan);
347+
thread->write(req_id, buf, len, *chan);
335348
}
336349

337350
extern "C" void
338-
rust_uvtmp_read_start(rust_uvtmp_thread *thread, connect_data *cd,
351+
rust_uvtmp_read_start(rust_uvtmp_thread *thread, uint32_t req_id,
339352
chan_handle *chan) {
340-
thread->read_start(cd, *chan);
353+
thread->read_start(req_id, *chan);
341354
}
342355

343356
extern "C" void
344357
rust_uvtmp_delete_buf(uint8_t *buf) {
345358
delete [] buf;
346359
}
360+
361+
extern "C" uint32_t
362+
rust_uvtmp_get_req_id(connect_data *cd) {
363+
return cd->req_id;
364+
}
365+
366+

src/rt/rustrt.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@ rust_uvtmp_close_connection
9797
rust_uvtmp_write
9898
rust_uvtmp_read_start
9999
rust_uvtmp_delete_buf
100+
rust_uvtmp_get_req_id
101+

0 commit comments

Comments
 (0)