1
+ // Some temporary libuv hacks for servo
2
+
3
+ #[ cfg( target_os = "linux" ) ] ;
4
+ #[ cfg( target_os = "macos" ) ] ;
5
+ #[ cfg( target_os = "freebsd" ) ] ;
6
+
7
+
8
+ #[ nolink]
9
+ native mod rustrt {
10
+ fn rust_uvtmp_create_thread ( ) -> thread ;
11
+ fn rust_uvtmp_start_thread ( thread : thread ) ;
12
+ fn rust_uvtmp_join_thread ( thread : thread ) ;
13
+ fn rust_uvtmp_delete_thread ( thread : thread ) ;
14
+ fn rust_uvtmp_connect (
15
+ thread : thread ,
16
+ ip : str:: sbuf ,
17
+ chan : comm:: chan < iomsg > ) ;
18
+ fn rust_uvtmp_close_connection ( thread : thread , cd : connect_data ) ;
19
+ fn rust_uvtmp_write (
20
+ thread : thread ,
21
+ cd : connect_data ,
22
+ buf : * u8 ,
23
+ len : ctypes:: size_t ,
24
+ chan : comm:: chan < iomsg > ) ;
25
+ fn rust_uvtmp_read_start (
26
+ thread : thread ,
27
+ cd : connect_data ,
28
+ chan : comm:: chan < iomsg > ) ;
29
+ fn rust_uvtmp_delete_buf ( buf : * u8 ) ;
30
+ }
31
+
32
+ type thread = * ctypes:: void ;
33
+
34
+ type connect_data = * ctypes:: void ;
35
+
36
+ enum iomsg {
37
+ whatever,
38
+ connected( connect_data ) ,
39
+ wrote( connect_data ) ,
40
+ read( connect_data , * u8 , ctypes:: ssize_t )
41
+ }
42
+
43
+ fn create_thread ( ) -> thread {
44
+ rustrt:: rust_uvtmp_create_thread ( )
45
+ }
46
+
47
+ fn start_thread ( thread : thread ) {
48
+ rustrt:: rust_uvtmp_start_thread ( thread)
49
+ }
50
+
51
+ fn join_thread ( thread : thread ) {
52
+ rustrt:: rust_uvtmp_join_thread ( thread)
53
+ }
54
+
55
+ fn delete_thread ( thread : thread ) {
56
+ rustrt:: rust_uvtmp_delete_thread ( thread)
57
+ }
58
+
59
+ fn connect ( thread : thread , ip : str , ch : comm:: chan < iomsg > ) {
60
+ str:: as_buf ( ip) { |ipbuf|
61
+ rustrt:: rust_uvtmp_connect ( thread, ipbuf, ch)
62
+ }
63
+ }
64
+
65
+ fn close_connection ( thread : thread , cd : connect_data ) {
66
+ rustrt:: rust_uvtmp_close_connection ( thread , cd) ;
67
+ }
68
+
69
+ fn write ( thread : thread , cd : connect_data , bytes : [ u8 ] ,
70
+ chan : comm:: chan < iomsg > ) unsafe {
71
+ rustrt:: rust_uvtmp_write (
72
+ thread, cd, vec:: to_ptr ( bytes) , vec:: len ( bytes) , chan) ;
73
+ }
74
+
75
+ fn read_start ( thread : thread , cd : connect_data ,
76
+ chan : comm:: chan < iomsg > ) {
77
+ rustrt:: rust_uvtmp_read_start ( thread, cd, chan) ;
78
+ }
79
+
80
+ fn delete_buf ( buf : * u8 ) {
81
+ rustrt:: rust_uvtmp_delete_buf ( buf) ;
82
+ }
83
+
84
+ #[ test]
85
+ fn test_start_stop ( ) {
86
+ let thread = create_thread ( ) ;
87
+ start_thread ( thread) ;
88
+ join_thread ( thread) ;
89
+ delete_thread ( thread) ;
90
+ }
91
+
92
+ #[ test]
93
+ #[ ignore]
94
+ fn test_connect ( ) {
95
+ let thread = create_thread ( ) ;
96
+ start_thread ( thread) ;
97
+ let port = comm:: port ( ) ;
98
+ let chan = comm:: chan ( port) ;
99
+ connect ( thread, "74.125.224.146" , chan) ;
100
+ alt comm:: recv ( port) {
101
+ connected ( cd) {
102
+ close_connection ( thread, cd) ;
103
+ }
104
+ }
105
+ join_thread ( thread) ;
106
+ delete_thread ( thread) ;
107
+ }
108
+
109
+ #[ test]
110
+ #[ ignore]
111
+ fn test_http ( ) {
112
+ let thread = create_thread ( ) ;
113
+ start_thread ( thread) ;
114
+ let port = comm:: port ( ) ;
115
+ let chan = comm:: chan ( port) ;
116
+ connect ( thread, "74.125.224.146" , chan) ;
117
+ alt comm:: recv ( port) {
118
+ connected ( cd) {
119
+ write ( thread, cd, str:: bytes ( "GET / HTTP/1.0\n \n " ) , chan) ;
120
+ alt comm:: recv ( port) {
121
+ wrote ( cd) {
122
+ read_start ( thread, cd, chan) ;
123
+ let keep_going = true ;
124
+ while keep_going {
125
+ alt comm:: recv ( port) {
126
+ read ( _, buf, -1 ) {
127
+ keep_going = false ;
128
+ delete_buf ( buf) ;
129
+ }
130
+ read ( _, buf, len) {
131
+ unsafe {
132
+ log ( error, len) ;
133
+ let buf = vec:: unsafe:: from_buf ( buf, len as uint ) ;
134
+ let str = str:: unsafe_from_bytes ( buf) ;
135
+ #error ( "read something" ) ;
136
+ io:: println ( str) ;
137
+ }
138
+ delete_buf ( buf) ;
139
+ }
140
+ }
141
+ }
142
+ close_connection ( thread, cd) ;
143
+ }
144
+ }
145
+ }
146
+ }
147
+ join_thread ( thread) ;
148
+ delete_thread ( thread) ;
149
+ }
0 commit comments