Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
565c5d69
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
565c5d69
编写于
5月 01, 2012
作者:
J
Jeff Olson
提交者:
Brian Anderson
5月 22, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: impl for high-level tcp client/request workflow
上级
7e114b20
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
150 addition
and
25 deletion
+150
-25
src/libstd/net_tcp.rs
src/libstd/net_tcp.rs
+150
-25
未找到文件。
src/libstd/net_tcp.rs
浏览文件 @
565c5d69
...
...
@@ -4,7 +4,7 @@
import
ip
=
net_ip
;
export
tcp_connect_result
,
tcp_write_result
;
export
tcp_connect_result
,
tcp_write_result
,
tcp_read_start_result
;
export
connect
,
write
;
resource
tcp_socket
(
socket_data
:
@
tcp_socket_data
)
unsafe
{
...
...
@@ -36,6 +36,17 @@ enum tcp_write_result {
tcp_write_error
(
uv
::
ll
::
uv_err_data
)
}
enum
tcp_read_start_result
{
tcp_read_start_success
(
comm
::
port
<
tcp_read_result
>
),
tcp_read_start_error
(
uv
::
ll
::
uv_err_data
)
}
enum
tcp_read_result
{
tcp_read_data
([
u8
]),
tcp_read_done
,
tcp_read_err
(
uv
::
ll
::
uv_err_data
)
}
#[doc=
"
Initiate a client connection over TCP/IP
...
...
@@ -58,8 +69,10 @@ fn connect(input_ip: ip::ip_addr, port: uint) -> tcp_connect_result unsafe {
};
let
conn_data_ptr
=
ptr
::
addr_of
(
conn_data
);
let
hl_loop
=
uv
::
global_loop
::
get
();
let
reader_po
=
comm
::
port
::
<
tcp_read_result
>
();
let
socket_data
=
@
{
reader_port
:
comm
::
port
::
<
[
u8
]
>
(),
reader_po
:
reader_po
,
reader_ch
:
comm
::
chan
(
reader_po
),
stream_handle
:
uv
::
ll
::
tcp_t
(),
connect_req
:
uv
::
ll
::
connect_t
(),
write_req
:
uv
::
ll
::
write_t
(),
...
...
@@ -183,9 +196,99 @@ fn write(sock: tcp_socket, raw_write_data: [[u8]]) -> tcp_write_result
comm
::
recv
(
result_po
)
}
#[doc=
"
"
]
fn
read_start
(
sock
:
tcp_socket
)
->
tcp_read_start_result
unsafe
{
let
stream_handle_ptr
=
ptr
::
addr_of
((
**
sock
)
.stream_handle
);
let
start_po
=
comm
::
port
::
<
option
<
uv
::
ll
::
uv_err_data
>>
();
let
start_ch
=
comm
::
chan
(
start_po
);
uv
::
hl
::
interact
((
**
sock
)
.hl_loop
)
{|
loop_ptr
|
log
(
debug
,
#
fmt
(
"in tcp::read_start interact cb %?"
,
loop_ptr
));
alt
uv
::
ll
::
read_start
(
stream_handle_ptr
as
*
uv
::
ll
::
uv_stream_t
,
on_alloc_cb
,
on_tcp_read_cb
)
{
0i32
{
log
(
debug
,
"success doing uv_read_start"
);
comm
::
send
(
start_ch
,
none
);
}
_
{
log
(
debug
,
"error attempting uv_read_start"
);
let
err_data
=
uv
::
ll
::
get_last_err_data
(
loop_ptr
);
comm
::
send
(
start_ch
,
some
(
err_data
));
}
}
};
alt
comm
::
recv
(
start_po
)
{
some
(
err_data
)
{
tcp_read_start_error
(
err_data
)
}
none
{
tcp_read_start_success
((
**
sock
)
.reader_po
)
}
}
}
fn
read_stop
(
sock
:
tcp_socket
)
->
option
<
uv
::
ll
::
uv_err_data
>
unsafe
{
let
stream_handle_ptr
=
ptr
::
addr_of
((
**
sock
)
.stream_handle
);
let
stop_po
=
comm
::
port
::
<
option
<
uv
::
ll
::
uv_err_data
>>
();
let
stop_ch
=
comm
::
chan
(
stop_po
);
uv
::
hl
::
interact
((
**
sock
)
.hl_loop
)
{|
loop_ptr
|
log
(
debug
,
"in interact cb for tcp::read_stop"
);
alt
uv
::
ll
::
read_stop
(
stream_handle_ptr
as
*
uv
::
ll
::
uv_stream_t
)
{
0i32
{
log
(
debug
,
"successfully called uv_read_stop"
);
comm
::
send
(
stop_ch
,
none
);
}
_
{
log
(
debug
,
"failure in calling uv_read_stop"
);
let
err_data
=
uv
::
ll
::
get_last_err_data
(
loop_ptr
);
comm
::
send
(
stop_ch
,
some
(
err_data
));
}
}
};
comm
::
recv
(
stop_po
)
}
// INTERNAL API
crust
fn
on_tcp_read_cb
(
stream
:
*
uv
::
ll
::
uv_stream_t
,
nread
:
libc
::
ssize_t
,
++
buf
:
uv
::
ll
::
uv_buf_t
)
unsafe
{
let
loop_ptr
=
uv
::
ll
::
get_loop_for_uv_handle
(
stream
);
let
socket_data_ptr
=
uv
::
ll
::
get_data_for_uv_handle
(
stream
)
as
*
tcp_socket_data
;
let
reader_ch
=
(
*
socket_data_ptr
)
.reader_ch
;
alt
nread
{
// incoming err.. probably eof
-
1
{
let
err_data
=
uv
::
ll
::
get_last_err_data
(
loop_ptr
);
comm
::
send
(
reader_ch
,
tcp_read_err
(
err_data
));
}
// do nothing .. unneeded buf
0
{}
// have data
_
{
// we have data
log
(
debug
,
#
fmt
(
"tcp on_read_cb nread: %d"
,
nread
));
let
buf_base
=
uv
::
ll
::
get_base_from_buf
(
buf
);
let
buf_len
=
uv
::
ll
::
get_len_from_buf
(
buf
);
let
new_bytes
=
vec
::
unsafe
::
from_buf
(
buf_base
,
buf_len
);
comm
::
send
(
reader_ch
,
tcp_read_data
(
new_bytes
));
}
}
uv
::
ll
::
free_base_of_buf
(
buf
);
}
crust
fn
on_alloc_cb
(
handle
:
*
libc
::
c_void
,
++
suggested_size
:
libc
::
size_t
)
->
uv
::
ll
::
uv_buf_t
unsafe
{
log
(
debug
,
"tcp read on_alloc_cb!"
);
let
char_ptr
=
uv
::
ll
::
malloc_buf_base_of
(
suggested_size
);
log
(
debug
,
#
fmt
(
"tcp read on_alloc_cb h: %? char_ptr: %u sugsize: %u"
,
handle
,
char_ptr
as
uint
,
suggested_size
as
uint
));
uv
::
ll
::
buf_init
(
char_ptr
,
suggested_size
)
}
type
tcp_socket_close_data
=
{
closed_ch
:
comm
::
chan
<
()
>
...
...
@@ -272,9 +375,9 @@ enum conn_attempt {
conn_failure
(
uv
::
ll
::
uv_err_data
)
}
type
tcp_socket_data
=
{
reader_port
:
comm
::
port
<
[
u8
]
>
,
reader_po
:
comm
::
port
<
tcp_read_result
>
,
reader_ch
:
comm
::
chan
<
tcp_read_result
>
,
stream_handle
:
uv
::
ll
::
uv_tcp_t
,
connect_req
:
uv
::
ll
::
uv_connect_t
,
write_req
:
uv
::
ll
::
uv_write_t
,
...
...
@@ -310,30 +413,52 @@ fn test_gl_tcp_ipv4_request() {
alt
write
(
sock
,
write_data
)
{
tcp_write_success
{
log
(
debug
,
"tcp::write successful"
);
/*
let
mut
total_read_data
:
[
u8
]
=
[];
let reader_po = read_start(sock);
loop {
alt comm::recv(reader_po) {
new_read_data(data) {
total_read_data += data;
// theoretically, we could keep iterating, here, if
// we expect the server on the other end to keep
// streaming/chunking data to us, but..
read_stop(tcp_stream);
break;
}
done_reading {
break;
}
error {
fail "erroring occured during read attempt.."
+ "FIXME need info";
}
alt
read_start
(
sock
)
{
tcp_read_start_success
(
reader_po
)
{
loop
{
alt
comm
::
recv
(
reader_po
)
{
tcp_read_data
(
new_data
)
{
total_read_data
+=
new_data
;
// theoretically, we could keep iterating, if
// we expect the server on the other end to keep
// streaming/chunking data to us, but..
alt
read_stop
(
sock
)
{
some
(
err_data
)
{
log
(
debug
,
"error while calling read_stop"
);
log
(
debug
,
#
fmt
(
"read_stop error: %? %?"
,
err_data
.err_name
,
err_data
.err_msg
));
assert
false
;
}
none
{
// exiting the read loop
break
;
}
}
}
tcp_read_done
{
break
;
}
tcp_read_err
(
err_data
)
{
log
(
debug
,
"read error data recv'd"
);
log
(
debug
,
#
fmt
(
"read error: %? %?"
,
err_data
.err_name
,
err_data
.err_msg
));
assert
false
;
}
}
}
comm
::
send
(
data_ch
,
total_read_data
);
}
tcp_read_start_error
(
err_data
)
{
log
(
debug
,
"tcp_read_start_error received.."
);
log
(
debug
,
#
fmt
(
"tcp read_start error: %? %?"
,
err_data
.err_name
,
err_data
.err_msg
));
assert
false
;
}
}
comm::send(data_ch, total_read_data);
*/
}
tcp_write_error
(
err_data
)
{
log
(
debug
,
"tcp_write_error received.."
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录