September 18, 2015 [Code Example for Beginners (like me)] Sockets with Fibers | ||||
---|---|---|---|---|
| ||||
hello! yesterday i got curious about how fibers work, and if the can be used as a replacement in network programming. so i started hacking a small example together, which is hopefully useful to other D beginners too :) http://pastebin.com/Xg4GJbKE |
September 18, 2015 Re: [Code Example for Beginners (like me)] Sockets with Fibers | ||||
---|---|---|---|---|
| ||||
Posted in reply to ddos | On Friday, 18 September 2015 at 07:58:10 UTC, ddos wrote: > hello! > > yesterday i got curious about how fibers work, and if the can be used as a replacement in network programming. so i started hacking a small example together, which is hopefully useful to other D beginners too :) > > http://pastebin.com/Xg4GJbKE Two things: In the line `string s = (cast(immutable(char)*)buf)[0..len];`, the cast to immutable is unsafe, as `buf` is modified on the next iteration and so the slice into it is not actually immutable. `writeln` can take a regular `char[]` fine, so you can just drop the immutable; otherwise you can do `cast(string)((buf[0..len]).idup)`. Also, using select/poll/epoll would be better than looping and sleeping. std.socket includes a SocketSet class for doing this (though the API for it is kinda bad). |
Copyright © 1999-2021 by the D Language Foundation