pub fn send_data<T,U>(&mut self,
                          in_buf: &[u8],
                          mut out_buf: T) -> Result<usize, OssuaryError>
    where T: std::ops::DerefMut<Target = U>,
          U: std::io::Write {
        // Try to send any unsent buffered data
        match write_stored_packet(self, out_buf.deref_mut()) {
            Ok(w) if w == 0 => {},
            Ok(w) => return Err(OssuaryError::WouldBlock(w)),
            Err(e) => return Err(e),
        }
        match self.state {
            ConnectionState::Encrypted => {},
            _ => {
                return Err(OssuaryError::InvalidPacket(
                    "Encrypted channel not established.".into()));
            }
        }
        let aad = [];
        let mut ciphertext = Vec::with_capacity(in_buf.len());
        let session_key = match self.local_key.session {
            Some(ref k) => k,
            None => {
                self.reset_state(None);
                return Err(OssuaryError::InvalidKey);;
            }
        };
        let tag = match encrypt(session_key.as_bytes(),
                                &self.local_key.nonce, &aad, in_buf, &mut ciphertext) {
            Ok(t) => t,
            Err(_) => {
                self.reset_state(None);
                return Err(OssuaryError::InvalidKey);;
            }
        };
        increment_nonce(&mut self.local_key.nonce);

        let pkt: EncryptedPacket = EncryptedPacket {
            tag_len: tag.len() as u16,
            data_len: ciphertext.len() as u16,
        };
        let mut buf: Vec<u8>= vec![];
        buf.extend(struct_as_slice(&pkt));
        buf.extend(&ciphertext);
        buf.extend(&tag);
        let written = write_packet(self, out_buf.deref_mut(), &buf,
                                   PacketType::EncryptedData)?;
        Ok(written)
    }
/// Encrypts data into a packet suitable for sending over the network
///
/// The caller provides unencrypted plaintext data, in any format, in the
/// `in_buf` buffer.  `send_data()` encrypts it and writes it in the proper
/// packet format into `out_buf`.
///
/// This is the core function for data transmission via ossuary.  All data
/// to be sent over an Ossuary connection should pass through this function.
///
/// Note that Ossuary does not perform network operations itself.  It is the
/// caller's responsibility to put the written data on the wire.  However,
/// you may pass a 'buf' that does this automatically, such as a TcpStream.
///
/// Returns the number of bytes written to `out_buf`, or an error.
///
/// You must handle [`OssuaryError::WouldBlock`], which is a recoverable
/// error, but indicates that some bytes were written to the buffer.  If any
/// bytes are written to `out_buf`, it can be assumed that all of `in_buf`
/// was consumed.  In the event of a `WouldBlock` error, you can either
/// continue calling `send_data()` with the next data to be sent, or you can
/// use [`OssuaryConnection::flush()`] to explicitly finish writing the
/// packet.
Encrypts data into a packet suitable for sending over the network
The caller provides unencrypted plaintext data, in any format, in the
`in_buf` buffer.  `send_data()` encrypts it and writes it in the proper
packet format into `out_buf`.

This is the core function for data transmission via ossuary.  All data
to be sent over an Ossuary connection should pass through this function.

Note that Ossuary does not perform network operations itself.  It is the
caller's responsibility to put the written data on the wire.  However,
you may pass a 'buf' that does this automatically, such as a TcpStream.

Returns the number of bytes written to `out_buf`, or an error.

You must handle [`OssuaryError::WouldBlock`], which is a recoverable
error, but indicates that some bytes were written to the buffer.  If any
bytes are written to `out_buf`, it can be assumed that all of `in_buf`
was consumed.
Encrypts data into a packet suitable for sending over the network The caller provides unencrypted plaintext data in any format in the ` in_buf ` buffer . ` send_data () ` encrypts it and writes it in the proper packet format into ` out_buf ` . This is the core function for data transmission via ossuary . All data to be sent over an Ossuary connection should pass through this function . Note that Ossuary does not perform network operations itself . It is the caller ' s responsibility to put the written data on the wire . However you may pass a ' buf ' that does this automatically such as a TcpStream . Returns the number of bytes written to ` out_buf ` or an error . You must handle [ ` OssuaryError :: WouldBlock ` ] which is a recoverable error but indicates that some bytes were written to the buffer . If any bytes are written to ` out_buf ` it can be assumed that all of ` in_buf ` was consumed .
pub fn send_data<T,U>(&mut self,
                          in_buf: &[u8],
                          mut out_buf: T) -> Result<usize, OssuaryError>
    where T: std::ops::DerefMut<Target = U>,
          U: std::io::Write {
        // Try to send any unsent buffered data
        match write_stored_packet(self, out_buf.deref_mut()) {
            Ok(w) if w == 0 => {},
            Ok(w) => return Err(OssuaryError::WouldBlock(w)),
            Err(e) => return Err(e),
        }
        match self.state {
            ConnectionState::Encrypted => {},
            _ => {
                return Err(OssuaryError::InvalidPacket(
                    "Encrypted channel not established.".into()));
            }
        }
        let aad = [];
        let mut ciphertext = Vec::with_capacity(in_buf.len());
        let session_key = match self.local_key.session {
            Some(ref k) => k,
            None => {
                self.reset_state(None);
                return Err(OssuaryError::InvalidKey);;
            }
        };
        let tag = match encrypt(session_key.as_bytes(),
                                &self.local_key.nonce, &aad, in_buf, &mut ciphertext) {
            Ok(t) => t,
            Err(_) => {
                self.reset_state(None);
                return Err(OssuaryError::InvalidKey);;
            }
        };
        increment_nonce(&mut self.local_key.nonce);
        let pkt: EncryptedPacket = EncryptedPacket {
            tag_len: tag.len() as u16,
            data_len: ciphertext.len() as u16,
        };
        let mut buf: Vec<u8>= vec![];
        buf.extend(struct_as_slice(&pkt));
        buf.extend(&ciphertext);
        buf.extend(&tag);
        let written = write_packet(self, out_buf.deref_mut(), &buf,
                                   PacketType::EncryptedData)?;
        Ok(written)
    }
pub fn send_data < T , U > ( & mut self , in_buf : & [ u8 ] , mut out_buf : T ) -> Result < usize , OssuaryError > where T : std :: ops :: DerefMut < Target = U > , U : std :: io :: Write { // Try to send any unsent buffered data match write_stored_packet ( self , out_buf . deref_mut ( ) ) { Ok ( w ) if w == 0 => { } , Ok ( w ) => return Err ( OssuaryError :: WouldBlock ( w ) ) , Err ( e ) => return Err ( e ) , } match self . state { ConnectionState :: Encrypted => { } , _ => { return Err ( OssuaryError :: InvalidPacket ( " " . into ( ) ) ) ; } } let aad = [ ] ; let mut ciphertext = Vec :: with_capacity ( in_buf . len ( ) ) ; let session_key = match self . local_key . session { Some ( ref k ) => k , None => { self . reset_state ( None ) ; return Err ( OssuaryError :: InvalidKey ) ; ; } } ; let tag = match encrypt ( session_key . as_bytes ( ) , & self . local_key . nonce , & aad , in_buf , & mut ciphertext ) { Ok ( t ) => t , Err ( _ ) => { self . reset_state ( None ) ; return Err ( OssuaryError :: InvalidKey ) ; ; } } ; increment_nonce ( & mut self . local_key . nonce ) ; let pkt : EncryptedPacket = EncryptedPacket { tag_len : tag . len ( ) as u16 , data_len : ciphertext . len ( ) as u16 , } ; let mut buf : Vec < u8 > = vec ! [ ] ; buf . extend ( struct_as_slice ( & pkt ) ) ; buf . extend ( & ciphertext ) ; buf . extend ( & tag ) ; let written = write_packet ( self , out_buf . deref_mut ( ) , & buf , PacketType :: EncryptedData ) ? ; Ok ( written ) }
Encrypts data into a packet suitable for sending over the network
The caller provides unencrypted plaintext data, in any format, in the
`in_buf` buffer.
Encrypts data into a packet suitable for sending over the network The caller provides unencrypted plaintext data in any format in the ` in_buf ` buffer .
&mut self
in_buf
&[u8]
out_buf
T
&mut self
in_buf
&[u8]
out_buf
T