Simple create for data network exchange

I am looking for simple library to transfer serialize-able enum
over TCP/IP.

I mean I have for example enum Boo

#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
struct Foo {}

#[derive(Debug, Clone, RustcDecodable, RustcEncodable)]
enum Boo {
 FooVariant(Foo),
 Str(String),
 Num(i32),
}

and I want to functions like:

fn send(s: &mut TcpStream, boo: &Boo) -> Result<(), Error>;
fn recv(s: &mut TcpStream) -> Result<Boo, Error>;

so somebody else deal with problems like sending 10 bytes, receiving 2 bytes + 8 bytes,
encoding/decoding of objects, may be sending size at the start of messages and so on things,

anybody know such crate?

1 Like

You could use ZeroMQ -- it's pretty awesome. There's several crates that provide bindings for it. I don't know which is best.

1 Like

In similar situation I used framed-serial crate with adapter like this:

struct MySocket(TcpStream);

impl NonBlockingRx for MySocket {
    type Error = io::Error;
    fn getc_try(&mut self) -> Result<Option<u8>, Self::Error> {
        let mut buf = [0_u8; 1];
        let nread = match self.0.read(&mut buf) {
            Ok(nb) => nb,
            Err(err) => {
                return match err.kind() {
                    io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut => Ok(None),
                    _ => {
                        error!("getc_try err.kind {:?}, err {:?}", err.kind(), err);
                        Err(err)
                    }
                };
            }
        };
        if nread > 0 {
            Ok(Some(buf[0]))
        } else {
            Ok(None)
        }
    }
}

impl NonBlockingTx for MySocket {
    type Error = io::Error;
    fn putc_try(&mut self, byte: u8) -> Result<Option<()>, Self::Error> {
        let buf = [byte; 1];
        let nwritten = match self.0.write(&buf) {
            Ok(nb) => nb,
            Err(err) => {
                return match err.kind() {
                    io::ErrorKind::WouldBlock | io::ErrorKind::TimedOut => Ok(None),
                    _ => {
                        error!("getc_try err.kind {:?}, err {:?}", err.kind(), err);
                        Err(err)
                    }
                };
            }
        };
        if nwritten > 0 {
            Ok(Some(()))
        } else {
            Ok(None)
        }
    }
}

but speed was not important for me, and I also looked at ZeroMQ,
but crate with "native" ZeroMQ implementation was very unstable,
and other crates depend on C library, which may give a pain in ass,
for bulding for several platforms, so I choosed framed-serial and bincode for serialization.

1 Like