krpc/
rpc.rs

1use super::*;
2use super::schema::*;
3
4use std::io::prelude::*;
5use std::net::*;
6use protobuf::*;
7
8//use quick_protobuf::{BytesReader, Writer};
9
10static RPC_HELLO_MESSAGE : [u8; 12] = [0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x2D, 0x52, 0x50, 0x43, 0x00, 0x00, 0x00];
11
12#[derive(Debug)]
13pub struct Rpc {
14    pub id : Id,
15    socket: TcpStream
16}
17
18impl Rpc {
19    pub fn new<T : ToSocketAddrs>(address : T, name : &str) -> Result<Self, ConnectionErr> {
20        // The struct we're going to fill
21        if name.len() > 32 { return Err(ConnectionErr::NameTooLong) }
22        let mut socket : TcpStream = unwrap_ret!(TcpStream::connect(address), ConnectionErr::TcpConnectionFailure);
23        // Send hello message
24        unwrap_ret!(socket.write(&RPC_HELLO_MESSAGE), ConnectionErr::HelloMessageFailed);
25        // Send name
26        {
27            // Get the name and knock off the null at the end
28            let name = &name.as_bytes();
29            let mut buf = [0; 32];
30            buf[..name.len()].copy_from_slice(name);
31            unwrap_ret!(socket.write(&buf), ConnectionErr::NameMessageFailed);
32        }
33
34        let mut id = [0; 16];
35        if unwrap_ret!(socket.read(&mut id), ConnectionErr::IdMessageFailed) != 16 {
36            return Err(ConnectionErr::IdMessageTooShort)
37        };
38        Ok(Self {
39            id : id,
40            socket : socket
41        })
42    }
43    pub fn send(&mut self, r : Request) -> Result<(), ProtobufError> { r.write_length_delimited_to_writer(&mut self.socket) }
44    pub fn receive<'a>(&'a mut self) -> Result<Option<Vec<u8>>, TransceiverError> {
45        let mut len = 0;
46        {
47            let ref mut buf = [0];
48            let mut i = 0;
49            loop {
50                self.socket.read_exact(buf)?;
51                len += ((buf[0] & 0b01111111) as usize) << i;
52                if buf[0] & 0b10000000 == 0 { break }
53                i += 7;
54            }
55        }
56        let mut buffer = vec!(0; len);
57        self.socket.read_exact(&mut buffer)?;
58        let message = parse_from_bytes::<Response>(&buffer)?;
59        unwrap_response!(message)
60    }
61    pub fn invoke(&mut self, service : String, procedure : String, args : Vec<Vec<u8>>) -> Result<Option<Vec<u8>>, TransceiverError> {
62        let request = make_request!(service, procedure, args);
63        self.send(request)?;
64        self.receive()
65    }
66}