@@ -4,34 +4,6 @@ import std::io;
4
4
import std::net;
5
5
import coroutine;
6
6
7
- fn void client (void * arg ) {
8
- io::printfn (" [%d] Client connected! " , coroutine::id ());
9
-
10
- TcpSocket * client_sock = (TcpSocket * )arg ;
11
- char [] buf = mem::new_array (char , 1024 );
12
- defer {
13
- client_sock .close ()!! ;
14
- free (client_sock );
15
- free (buf .ptr );
16
- }
17
-
18
- while OUTER : (true ) {
19
- coroutine::sleep_read (client_sock .sock );
20
- usz n = client_sock .read (buf )!! ;
21
- if (n == 0 ) break OUTER ;
22
- char [] chunk = buf [0 : n ];
23
- io::printfn (" [%d] Client sent %d bytes " , coroutine::id (), chunk .len );
24
-
25
- while (chunk .len > 0 ) {
26
- coroutine::sleep_write (client_sock .sock );
27
- usz m = client_sock .write (chunk )!! ;
28
- if (m == 0 ) break OUTER ;
29
- chunk = chunk [m .. ];
30
- }
31
- }
32
- io::printfn (" [%d] Client disconnected " , coroutine::id ());
33
- }
34
-
35
7
fn void main () {
36
8
coroutine::init ();
37
9
@@ -45,6 +17,32 @@ fn void main() {
45
17
coroutine::sleep_read (server_sock .sock );
46
18
TcpSocket client_sock = tcp::accept (& server_sock )!! ;
47
19
client_sock .sock .set_non_blocking (true )!! ;
48
- coroutine::go (& client , @clone (client_sock ));
20
+ coroutine::go (fn void (void * arg ) {
21
+ io::printfn (" [%d] Client connected! " , coroutine::id ());
22
+
23
+ TcpSocket * client_sock = (TcpSocket * )arg ;
24
+ char [] buf = mem::new_array (char , 1024 );
25
+ defer {
26
+ client_sock .close ()!! ;
27
+ free (client_sock );
28
+ free (buf .ptr );
29
+ }
30
+
31
+ while OUTER : (true ) {
32
+ coroutine::sleep_read (client_sock .sock );
33
+ usz n = client_sock .read (buf )!! ;
34
+ if (n == 0 ) break OUTER ;
35
+ char [] chunk = buf [0 : n ];
36
+ io::printfn (" [%d] Client sent %d bytes " , coroutine::id (), chunk .len );
37
+
38
+ while (chunk .len > 0 ) {
39
+ coroutine::sleep_write (client_sock .sock );
40
+ usz m = client_sock .write (chunk )!! ;
41
+ if (m == 0 ) break OUTER ;
42
+ chunk = chunk [m .. ];
43
+ }
44
+ }
45
+ io::printfn (" [%d] Client disconnected " , coroutine::id ());
46
+ }, @clone (client_sock ));
49
47
}
50
48
}
0 commit comments