[][src]Struct talkers::Talker

pub struct Talker {
    pub chat_close: Option<Box<dyn Fn() + Send>>,
    pub msg_new: Option<Box<dyn Fn(String) + Send>>,
    pub file_incoming: Box<dyn Fn(usize) -> bool + Send>,
    pub file_failed: Option<Box<dyn Fn(String, Error) + Send>>,
    pub file_complete: Option<Box<dyn Fn(String) + Send>>,
    pub file_hash_by_peer: Option<Box<dyn Fn(String, [u8; 32]) + Send>>,
    pub file_our_hash: Option<Box<dyn Fn(String, [u8; 32]) + Send>>,
    pub hash_of_sent: Option<Box<dyn Fn([u8; 32]) + Send>>,
    pub hash_rcvd: Option<Box<dyn Fn([u8; 32]) + Send>>,
    pub payload_too_large: Option<Box<dyn Fn(usize) + Send>>,
    pub invalid_instr: Option<Box<dyn Fn(u8) + Send>>,
    // some fields omitted
}

This struct contains the connection to one talkers peer. It must be constructed with Talker::new(s), but the callbacks in the public fields can be set directly.

Fields

chat_close: Option<Box<dyn Fn() + Send>>

Invoked when the connection is closed.

msg_new: Option<Box<dyn Fn(String) + Send>>

Invoked when a new message is received.

file_incoming: Box<dyn Fn(usize) -> bool + Send>

Invoked when a file transfer has been announced by the peer. Called with the announced size. Must return a bool indicating whether or not to accept the file transfer. By default, file transfers are not accepted (except in the example app).

file_failed: Option<Box<dyn Fn(String, Error) + Send>>

Invoked when a file transfer has failed. Called with the name of the transfer file and the error.

file_complete: Option<Box<dyn Fn(String) + Send>>

Invoked when a file transfer has succeeded. Called with the name of the transfer file.

file_hash_by_peer: Option<Box<dyn Fn(String, [u8; 32]) + Send>>

Invoked upon learning the intended hash of the file from the peer.

file_our_hash: Option<Box<dyn Fn(String, [u8; 32]) + Send>>

Invoked upon having calculated the hash of the received file.

hash_of_sent: Option<Box<dyn Fn([u8; 32]) + Send>>

Invoked with the hash of the message or file that we sent.

hash_rcvd: Option<Box<dyn Fn([u8; 32]) + Send>>

Invoked upon receiving a hash from the peer.

payload_too_large: Option<Box<dyn Fn(usize) + Send>>

Invoked if the peer tried to send a message or file that is too large.

invalid_instr: Option<Box<dyn Fn(u8) + Send>>

Invoked if the peer sent an invalid instruction. Useful for debugging.

Implementations

impl Talker[src]

pub fn new(s: TcpStream) -> Self[src]

Constructs a new Talker instance from a TcpStream. The callbacks are set to "do nothing", and to reject file transfers.

pub fn close(&mut self) -> Result<()>[src]

Shuts down the connection with a talkers peer.

pub fn expect_handshake(&mut self) -> Result<()>[src]

Reads from the talkers peer and checks whether the buffer read is a talkers handshake. Should be invoked if a connection was made with us.

pub fn perform_handshake(&mut self) -> Result<()>[src]

Performs our half of the talkers handshake with the peer. Should be invoked if we initiated the connection or if we received a handshake.

pub fn read_once(&mut self) -> Result<bool>[src]

Reads precisely one instruction from the peer and process it accordingly.

pub fn read_maybe(&mut self) -> Result<bool>[src]

Sets the TCP connection to non-blocking and invokes read_once. This has the effect that a instruction might be read from the peer or not. If one is read, it will be processed in blocking mode. If not, this function returns immediately without blocking. Useful if called in a loop. Note that each invocation reads and processes at most one instruction.

pub fn send(&mut self, msg: &str) -> Result<()>[src]

Instructs the peer that a message will be forthcoming and transmits the message.

pub fn send_stream<T, U>(&mut self, stream: &mut T, len: U) -> Result<()> where
    T: Read,
    U: Display
[src]

Send a stream to the peer. While this method technically accepts all streams that implement Read, talkers currently only has dedicated support for files.

pub fn expect_hash(&mut self) -> Result<()>[src]

Blocks until a hash has been received. If no hash, but some other instruction, is received, that instruction is written into an internal queue so that it can be processed by subsequent calls to read_once. Returns Ok(()) if a hash was received and an Err variant if not.

Auto Trait Implementations

impl !RefUnwindSafe for Talker

impl Send for Talker

impl !Sync for Talker

impl Unpin for Talker

impl !UnwindSafe for Talker

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.