4 ms·
Rust, let me share your awesomeness with C
- db48x 7y agoThat's a nice problem. As far as I know, the Rust standard library doesn't have a way to create a TcpStream that wraps an existing raw file descriptor. You can only get a raw file descriptor via FFI anyway, so that's fine for most programs. A TcpStream is just a struct containing the file descriptor (https://github.com/rust-lang/rust/blob/master/src/libstd/sys_common/net.rs#L207-L209 https://github.com/rust-lang/rust/blob/master/src/libstd/sys...) and nothing else, so this would be fine: let stream: std::net::TcpStream = unsafe { std::mem::transmute(socket) };
- ttouch 7y agothat's a good idea, but keep in mind that the underlying file descriptor could be of a UDP stream, or a TCP stream to another host. I finally solved the problem, check out https://github.com/dzervas/mage/blob/master/src/api_ffi.rs https://github.com/dzervas/mage/blob/master/src/api_ffi.rs. When you bind or connect, I return a "file descriptor" that you will pass back to me and I use it as an index to a vector of connections :) It works fairly good - although I have other problems...
- db48x 7y agoTrue enough! I thought you were just going to see existing connections or something. What you're doing is probably what's going on under the hood anyway.