flugrost 0.0.1

A tiny RPC library
Documentation

use message::Message;

/*
/// Implements protocol handling, send and recv queues.
pub struct Protocol<'a, R: Read + 'a, W: Write + 'a> {
    recv_queue: &'a mut RecvQueue<R>,
    send_queue: &'a mut SendQueue<W>,
}


impl<'a, R: Read, W: Write> Protocol<'a, R, W> {
    /// Creates a new instance from a given reader and writer.
    pub fn new(recv_queue: &'a mut RecvQueue<R>, send_queue: &'a mut SendQueue<W>) -> Self {
        assert_eq!(recv_queue.max_len(), MAX_PACKET_LEN);
        Self {
            recv_queue: recv_queue,
            send_queue: send_queue,
        }
    }

    /// Converts the message to sendable bytes and adds it to the sendqueue.
    /// It is unspecified whether the message will be send immediately or not.
    pub fn send(&mut self, msg: Message) -> io::Result<()> {
        let data = msg.into_buffer();
        self.send_queue.send(&data)?;
        Ok(())
    }

    /// Try to decode one message from the recv queue.
    /// Call this method whenever new data has been received.
    pub fn fetch(&mut self) -> Result<Option<Message>, RPCError> {
        let msg = Message::from_buffer(self.recv_queue.peek())?;
        Ok(match msg {
            None => None,
            Some((msg, cnt)) => {
                self.recv_queue.drop(cnt);
                Some(msg)
            }
        })
    }
}*/


// Future plans?
pub trait Protocol {
    fn decode_message(&self, buf: &[u8]) -> Result<Option<(Message, usize)>, ()>;
    fn encode_message(&self, msg: &Message) -> Result<Vec<u8>, ()>;
    fn max_packet_len(&self) -> usize;
}

pub const MAX_PACKET_LEN: usize = 16 * 1024 * 1024; // 16 MiB


pub struct FlugrostProtocol;


impl FlugrostProtocol {
    pub fn new() -> Self {
        Self {}
    }
}


impl Protocol for FlugrostProtocol {
    fn decode_message(&self, buf: &[u8]) -> Result<Option<(Message, usize)>, ()> {
        Message::from_buffer(buf)
    }

    fn encode_message(&self, msg: &Message) -> Result<Vec<u8>, ()> {
        Ok(msg.into_buffer())
    }

    fn max_packet_len(&self) -> usize {
        MAX_PACKET_LEN
    }
}