pub fn recv_handshake<T,U>(&mut self, buf: T) -> Result<usize, OssuaryError>
    where T: std::ops::DerefMut<Target = U>,
          U: std::io::Read {
        match self.state {
            ConnectionState::Failed(_) |
            ConnectionState::Encrypted => return Ok(0),
            // Timeout wait states
            ConnectionState::ServerWaitHandshake(t) |
            ConnectionState::ServerWaitAuthentication(t) |
            ConnectionState::ClientWaitHandshake(t)  => {
                // Wait for response, with timeout
                if self.check_timeout(t) {
                    return Ok(0);
                }
            },
            _ => {},
        }

        let (pkt, bytes_read) = match read_packet(self, buf) {
            Ok(t) => { t },
            Err(e @ OssuaryError::WouldBlock(_)) => {
                return Err(e);
            }
            Err(e) => {
                self.reset_state(Some(e.clone()));
                return Err(e);
            }
        };

        match pkt.kind() {
            PacketType::Reset => { self.handle_reset_packet(bytes_read)?; },
            PacketType::Disconnect => { self.handle_disconnect_packet(&pkt)?; },
            _ => {},
        }

        self.remote_msg_id = self.next_msg_id(&pkt)?;

        let res: Result<ConnectionState, OssuaryError> = match self.state {
            // no-op states
            ConnectionState::Failing(_) |
            ConnectionState::Failed(_) |
            ConnectionState::Resetting(_) |
            ConnectionState::ClientRaiseUntrustedServer |
            ConnectionState::ClientWaitServerApproval => {
                Ok(self.state.clone())
            },

            // Non-receive states.  Receiving handshake data is an error.
            ConnectionState::ClientSendHandshake |
            ConnectionState::ClientSendAuthentication |
            ConnectionState::ServerSendHandshake |
            ConnectionState::Encrypted => {
                Err(OssuaryError::InvalidPacket("Received unexpected handshake packet.".into()))
            },

            // Received expected handshake packet
            ConnectionState::ServerWaitHandshake(_) => {
                self.recv_client_handshake(&pkt)
            },
            ConnectionState::ClientWaitHandshake(_t) => {
                self.recv_server_handshake(&pkt)
            },
            ConnectionState::ServerWaitAuthentication(_t) => {
                self.recv_client_auth(&pkt)
            },

            // Received expected reset packet
            ConnectionState::ResetWait => {
                match pkt.kind() {
                    PacketType::Reset => {
                        self.remote_msg_id = 0;
                        match self.conn_type {
                            ConnectionType::Client => Ok(ConnectionState::ClientSendHandshake),
                            _ => Ok(ConnectionState::ServerWaitHandshake(std::time::SystemTime::now())),
                        }
                    },
                    _ => {
                        Ok(self.state.clone())
                    },
                }
            }
        };
        match res {
            Ok(s) => {
                self.state = s;
                Ok(bytes_read)
            },
            Err(e) => {
                self.reset_state(None);
                // bytes_read not returned on error, but will be consumed again
                // when the handshake restarts.
                Err(e)
            }
        }
    }
/// Read the next handshake packet from the given buffer
///
/// If a handshake packet has been received, this function reads and parses
/// the encrypted packet from the provided buffer and updates its internal
/// connection state.
///
/// This is a critical part of the handshaking stage, when a connection to
/// a remote host is securely established.  Each side of the connection must
/// call recv_handshake() whenever data is received from the network until
/// [`OssuaryConnection::handshake_done()`] returns true.
///
/// Returns the number of bytes read from `buf`, or an error.  It is the
/// caller's responsibility to ensure that the consumed bytes are removed
/// from the data buffer before it is used again.  You must handle
/// [`OssuaryError::WouldBlock`], which is a recoverable error, but
/// indicates that some bytes were also read from the buffer.
Read the next handshake packet from the given buffer
If a handshake packet has been received, this function reads and parses
the encrypted packet from the provided buffer and updates its internal
connection state.

This is a critical part of the handshaking stage, when a connection to
a remote host is securely established.  Each side of the connection must
call recv_handshake() whenever data is received from the network until
[`OssuaryConnection::handshake_done()`] returns true.

Returns the number of bytes read from `buf`, or an error.  It is the
caller's responsibility to ensure that the consumed bytes are removed
from the data buffer before it is used again.  You must handle
[`OssuaryError::WouldBlock`], which is a recoverable error, but
indicates that some bytes were also read from the buffer.
Read the next handshake packet from the given buffer If a handshake packet has been received this function reads and parses the encrypted packet from the provided buffer and updates its internal connection state . This is a critical part of the handshaking stage when a connection to a remote host is securely established . Each side of the connection must call recv_handshake () whenever data is received from the network until [ ` OssuaryConnection :: handshake_done () ` ] returns true . Returns the number of bytes read from ` buf ` or an error . It is the caller ' s responsibility to ensure that the consumed bytes are removed from the data buffer before it is used again . You must handle [ ` OssuaryError :: WouldBlock ` ] which is a recoverable error but indicates that some bytes were also read from the buffer .
pub fn recv_handshake<T,U>(&mut self, buf: T) -> Result<usize, OssuaryError>
    where T: std::ops::DerefMut<Target = U>,
          U: std::io::Read {
        match self.state {
            ConnectionState::Failed(_) |
            ConnectionState::Encrypted => return Ok(0),
            // Timeout wait states
            ConnectionState::ServerWaitHandshake(t) |
            ConnectionState::ServerWaitAuthentication(t) |
            ConnectionState::ClientWaitHandshake(t)  => {
                // Wait for response, with timeout
                if self.check_timeout(t) {
                    return Ok(0);
                }
            },
            _ => {},
        }
        let (pkt, bytes_read) = match read_packet(self, buf) {
            Ok(t) => { t },
            Err(e @ OssuaryError::WouldBlock(_)) => {
                return Err(e);
            }
            Err(e) => {
                self.reset_state(Some(e.clone()));
                return Err(e);
            }
        };
        match pkt.kind() {
            PacketType::Reset => { self.handle_reset_packet(bytes_read)?; },
            PacketType::Disconnect => { self.handle_disconnect_packet(&pkt)?; },
            _ => {},
        }
        self.remote_msg_id = self.next_msg_id(&pkt)?;
        let res: Result<ConnectionState, OssuaryError> = match self.state {
            // no-op states
            ConnectionState::Failing(_) |
            ConnectionState::Failed(_) |
            ConnectionState::Resetting(_) |
            ConnectionState::ClientRaiseUntrustedServer |
            ConnectionState::ClientWaitServerApproval => {
                Ok(self.state.clone())
            },
            // Non-receive states.  Receiving handshake data is an error.
            ConnectionState::ClientSendHandshake |
            ConnectionState::ClientSendAuthentication |
            ConnectionState::ServerSendHandshake |
            ConnectionState::Encrypted => {
                Err(OssuaryError::InvalidPacket("Received unexpected handshake packet.".into()))
            },
            // Received expected handshake packet
            ConnectionState::ServerWaitHandshake(_) => {
                self.recv_client_handshake(&pkt)
            },
            ConnectionState::ClientWaitHandshake(_t) => {
                self.recv_server_handshake(&pkt)
            },
            ConnectionState::ServerWaitAuthentication(_t) => {
                self.recv_client_auth(&pkt)
            },
            // Received expected reset packet
            ConnectionState::ResetWait => {
                match pkt.kind() {
                    PacketType::Reset => {
                        self.remote_msg_id = 0;
                        match self.conn_type {
                            ConnectionType::Client => Ok(ConnectionState::ClientSendHandshake),
                            _ => Ok(ConnectionState::ServerWaitHandshake(std::time::SystemTime::now())),
                        }
                    },
                    _ => {
                        Ok(self.state.clone())
                    },
                }
            }
        };
        match res {
            Ok(s) => {
                self.state = s;
                Ok(bytes_read)
            },
            Err(e) => {
                self.reset_state(None);
                // bytes_read not returned on error, but will be consumed again
                // when the handshake restarts.
                Err(e)
            }
        }
    }
pub fn recv_handshake < T , U > ( & mut self , buf : T ) -> Result < usize , OssuaryError > where T : std :: ops :: DerefMut < Target = U > , U : std :: io :: Read { match self . state { ConnectionState :: Failed ( _ ) | ConnectionState :: Encrypted => return Ok ( 0 ) , // Timeout wait states ConnectionState :: ServerWaitHandshake ( t ) | ConnectionState :: ServerWaitAuthentication ( t ) | ConnectionState :: ClientWaitHandshake ( t ) => { // Wait for response, with timeout if self . check_timeout ( t ) { return Ok ( 0 ) ; } } , _ => { } , } let ( pkt , bytes_read ) = match read_packet ( self , buf ) { Ok ( t ) => { t } , Err ( e @ OssuaryError :: WouldBlock ( _ ) ) => { return Err ( e ) ; } Err ( e ) => { self . reset_state ( Some ( e . clone ( ) ) ) ; return Err ( e ) ; } } ; match pkt . kind ( ) { PacketType :: Reset => { self . handle_reset_packet ( bytes_read ) ? ; } , PacketType :: Disconnect => { self . handle_disconnect_packet ( & pkt ) ? ; } , _ => { } , } self . remote_msg_id = self . next_msg_id ( & pkt ) ? ; let res : Result < ConnectionState , OssuaryError > = match self . state { // no-op states ConnectionState :: Failing ( _ ) | ConnectionState :: Failed ( _ ) | ConnectionState :: Resetting ( _ ) | ConnectionState :: ClientRaiseUntrustedServer | ConnectionState :: ClientWaitServerApproval => { Ok ( self . state . clone ( ) ) } , // Non-receive states.  Receiving handshake data is an error. ConnectionState :: ClientSendHandshake | ConnectionState :: ClientSendAuthentication | ConnectionState :: ServerSendHandshake | ConnectionState :: Encrypted => { Err ( OssuaryError :: InvalidPacket ( " " . into ( ) ) ) } , // Received expected handshake packet ConnectionState :: ServerWaitHandshake ( _ ) => { self . recv_client_handshake ( & pkt ) } , ConnectionState :: ClientWaitHandshake ( _t ) => { self . recv_server_handshake ( & pkt ) } , ConnectionState :: ServerWaitAuthentication ( _t ) => { self . recv_client_auth ( & pkt ) } , // Received expected reset packet ConnectionState :: ResetWait => { match pkt . kind ( ) { PacketType :: Reset => { self . remote_msg_id = 0 ; match self . conn_type { ConnectionType :: Client => Ok ( ConnectionState :: ClientSendHandshake ) , _ => Ok ( ConnectionState :: ServerWaitHandshake ( std :: time :: SystemTime :: now ( ) ) ) , } } , _ => { Ok ( self . state . clone ( ) ) } , } } } ; match res { Ok ( s ) => { self . state = s ; Ok ( bytes_read ) } , Err ( e ) => { self . reset_state ( None ) ; // bytes_read not returned on error, but will be consumed again // when the handshake restarts. Err ( e ) } } }
Read the next handshake packet from the given buffer
If a handshake packet has been received, this function reads and parses
the encrypted packet from the provided buffer and updates its internal
connection state.
Read the next handshake packet from the given buffer If a handshake packet has been received this function reads and parses the encrypted packet from the provided buffer and updates its internal connection state .
&mut self
buf
T
&mut self
buf
T