Struct cbor_protocol::Protocol [−][src]
pub struct Protocol { /* fields omitted */ }
CBOR protocol communication structure
Methods
impl Protocol
[src]
impl Protocol
pub fn new(host_url: String) -> Self
[src]
pub fn new(host_url: String) -> Self
Binds a UDP listener socket and saves it in a new protocol instance
Arguments
- host_url - The IP address and port to bind
Errors
If this function encounters any errors, it will panic
Examples
use cbor_protocol::*; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned());
pub fn new_from_socket(handle: UdpSocket) -> Self
[src]
pub fn new_from_socket(handle: UdpSocket) -> Self
Creates a protocol instance using an existing socket
Arguments
- handle - The socket to use for future communication
Examples
extern crate cbor_protocol; use cbor_protocol::*; use std::net::UdpSocket; let socket = UdpSocket::bind("0.0.0.0:8000").unwrap(); let cbor_connection = Protocol::new_from_socket(socket);
pub fn send_message(
&self,
message: &[u8],
dest: SocketAddr
) -> Result<(), String>
[src]
pub fn send_message(
&self,
message: &[u8],
dest: SocketAddr
) -> Result<(), String>
Send a CBOR packet to a specified UDP socket destination
Arguments
- message - CBOR packet to send
- dest - UDP socket destination
Errors
If this function encounters any errors, it will return an error message string
Examples
extern crate cbor_protocol; extern crate serde_cbor; use cbor_protocol::*; use serde_cbor::ser; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); let message = ser::to_vec_packed(&"ping").unwrap(); cbor_connection.send_message(&message, "0.0.0.0:8001".parse().unwrap());
pub fn send_pause(&self, dest: SocketAddr) -> Result<(), String>
[src]
pub fn send_pause(&self, dest: SocketAddr) -> Result<(), String>
Send a pause message to a specified UDP socket destination
Arguments
- dest - UDP socket destination
Errors
If this function encounters any errors, it will return an error message string
Examples
use cbor_protocol::*; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); cbor_connection.send_pause("0.0.0.0:8001".parse().unwrap());
pub fn send_resume(&self, dest: SocketAddr) -> Result<(), String>
[src]
pub fn send_resume(&self, dest: SocketAddr) -> Result<(), String>
Send a resume message to a specified UDP socket destination
Arguments
- dest - UDP socket destination
Errors
If this function encounters any errors, it will return an error message string
Examples
use cbor_protocol::*; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); cbor_connection.send_resume("0.0.0.0:8001".parse().unwrap());
pub fn recv_message(&self) -> Result<Option<Value>, Option<String>>
[src]
pub fn recv_message(&self) -> Result<Option<Value>, Option<String>>
Receive a UDP message (no timeout)
Errors
If this function encounters any errors, it will return an error message string
Examples
use cbor_protocol::*; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); let message = cbor_connection.recv_message().unwrap();
pub fn peek_peer(&self) -> Result<SocketAddr, String>
[src]
pub fn peek_peer(&self) -> Result<SocketAddr, String>
Peek at the sender information for the next message in the UDP receive buffer
Errors
If this function encounters any errors, it will return an error message string
Examples
use cbor_protocol::*; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); let source = cbor_connection.peek_peer();
pub fn recv_message_peer(&self) -> Result<(SocketAddr, Option<Value>), String>
[src]
pub fn recv_message_peer(&self) -> Result<(SocketAddr, Option<Value>), String>
Receive a UDP message and take note of the sender (no timeout)
Errors
If this function encounters any errors, it will return an error message string
Examples
use cbor_protocol::*; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); let (source, message) = cbor_connection.recv_message_peer().unwrap();
pub fn recv_message_peer_timeout(
&self,
timeout: Duration
) -> Result<(SocketAddr, Option<Value>), Option<String>>
[src]
pub fn recv_message_peer_timeout(
&self,
timeout: Duration
) -> Result<(SocketAddr, Option<Value>), Option<String>>
Receive a UDP message and take note of the sender (with timeout)
Arguments
- timeout - Maximum amount of time to wait for a UDP packet
Errors
- If this function times out, it will return Err(None)
- If this function encounters any errors, it will return an error message string
Examples
extern crate cbor_protocol; use cbor_protocol::*; use std::time::Duration; let cbor_connection = Protocol::new("0.0.0.0:8000".to_owned()); let (source, message) = match cbor_connection.recv_message_peer_timeout(Duration::from_secs(1)) { Ok(data) => data, Err(None) => { println!("Timeout waiting for message"); return; } Err(Some(err)) => panic!("Failed to receive message: {}", err), };
pub fn recv_message_timeout(
&self,
timeout: Duration
) -> Result<Option<Value>, Option<String>>
[src]
pub fn recv_message_timeout(
&self,
timeout: Duration
) -> Result<Option<Value>, Option<String>>
Receive a UDP message (with timeout)
Arguments
- timeout - Maximum amount of time to wait for a UDP packet
Errors
- If this function times out, it will return Err(None)
- If this function encounters any errors, it will return an error message string
Examples
extern crate cbor_protocol; use cbor_protocol::*; use std::time::Duration; let cbor_connection = Protocol::new("0.0.0.0:9000".to_owned()); let message = match cbor_connection.recv_message_timeout(Duration::from_secs(1)) { Ok(data) => data, Err(None) => { println!("Timeout waiting for message"); return; } Err(Some(err)) => panic!("Failed to receive message: {}", err), };