fn serve(&mut self, mut x: X) -> Result<()> {
        loop {
            // First receive a payload from client
            let recv_buf: Vec<u8> = receive_vec(self)?;

            // Now we received a payload in recv_buf
            // recv_buf should be a serialized incoming request U
            // The server needs deser it into U first
            let request: U = serde_json::from_slice(&recv_buf)?;
            debug!("SERVER get request: {:?}", request);
            let result: Result<V> = x.handle_invoke(request).map_err(|e| e.into_simple_error());
            debug!("SERVER handle_invoke result: {:?}", result);

            let response = match serde_json::to_vec(&result) {
                Ok(resp) => resp,
                Err(_) => {
                    let r: Result<V> = Err(Error::from(ErrorKind::InternalRPCError));
                    serde_json::to_vec(&r).expect("infallable")
                }
            };
            debug!("SERVER send response {:?}", response);

            // Now the result is stored in ret and we need to sent it back.
            // `ret` is cleared here. Performance is not very good.
            send_vec(self, response)?;
        }
    }
// This call would block -- contains main loop
// Returns error on socket close or any exceptions.
// The `loop` here is used for multi-round communications:
// Assuming that we have the following codes on the client side
// ```
// let _ = conn.invoke(first_req).unwrap();
// let _ = conn.invoke(second_req).unwrap();
// let _ = conn.invoke(third_req).unwrap();
// ```
// The `serve` function would loop its body for 3 times.
This call would block -- contains main loop
Returns error on socket close or any exceptions.
This call would block -- contains main loop Returns error on socket close or any exceptions .
fn serve(&mut self, mut x: X) -> Result<()> {
        loop {
            // First receive a payload from client
            let recv_buf: Vec<u8> = receive_vec(self)?;
            // Now we received a payload in recv_buf
            // recv_buf should be a serialized incoming request U
            // The server needs deser it into U first
            let request: U = serde_json::from_slice(&recv_buf)?;
            debug!("SERVER get request: {:?}", request);
            let result: Result<V> = x.handle_invoke(request).map_err(|e| e.into_simple_error());
            debug!("SERVER handle_invoke result: {:?}", result);
            let response = match serde_json::to_vec(&result) {
                Ok(resp) => resp,
                Err(_) => {
                    let r: Result<V> = Err(Error::from(ErrorKind::InternalRPCError));
                    serde_json::to_vec(&r).expect("infallable")
                }
            };
            debug!("SERVER send response {:?}", response);
            // Now the result is stored in ret and we need to sent it back.
            // `ret` is cleared here. Performance is not very good.
            send_vec(self, response)?;
        }
    }
fn serve ( & mut self , mut x : X ) -> Result < ( ) > { loop { // First receive a payload from client let recv_buf : Vec < u8 > = receive_vec ( self ) ? ; // Now we received a payload in recv_buf // recv_buf should be a serialized incoming request U // The server needs deser it into U first let request : U = serde_json :: from_slice ( & recv_buf ) ? ; debug ! ( " " request ) ; let result : Result < V > = x . handle_invoke ( request ) . map_err ( | e | e . into_simple_error ( ) ) ; debug ! ( " " result ) ; let response = match serde_json :: to_vec ( & result ) { Ok ( resp ) => resp , Err ( _ ) => { let r : Result < V > = Err ( Error :: from ( ErrorKind :: InternalRPCError ) ) ; serde_json :: to_vec ( & r ) . expect ( " " ) } } ; debug ! ( " " response ) ; // Now the result is stored in ret and we need to sent it back. // `ret` is cleared here. Performance is not very good. send_vec ( self , response ) ? ; } }
This call would block -- contains main loop
Returns error on socket close or any exceptions.
This call would block -- contains main loop Returns error on socket close or any exceptions .
&mut self
x
X
&mut self
x
X