Socket

Struct Socket 

Source
pub struct Socket {
    pub encoder: Box<dyn Fn(Vec<u8>, &[u8; 32]) -> Vec<u8>>,
    pub decoder: Box<dyn Fn(Vec<u8>, &[u8; 32]) -> Vec<u8>>,
    /* private fields */
}

Fields§

§encoder: Box<dyn Fn(Vec<u8>, &[u8; 32]) -> Vec<u8>>

Datagrams encoder

§decoder: Box<dyn Fn(Vec<u8>, &[u8; 32]) -> Vec<u8>>

Datagrams decoder

Implementations§

Source§

impl Socket

Source

pub fn new(addr: SocketAddr) -> Result<Socket, Error>

Source

pub fn from_socket(socket: UdpSocket) -> Result<Socket, Error>

Source

pub fn socket(&self) -> &UdpSocket

Source

pub fn addr(&self) -> SocketAddr

Source

pub fn set_encoder<T: Fn(Vec<u8>, &[u8; 32]) -> Vec<u8> + 'static>( &mut self, encoder: T, )

Sets function that will encode datagrams before its transferring

Source

pub fn set_decoder<T: Fn(Vec<u8>, &[u8; 32]) -> Vec<u8> + 'static>( &mut self, decoder: T, )

Sets function that will decode received datagrams

Source

pub fn generate_secret( &mut self, addr: SocketAddr, ) -> Result<Await<Duration>, Error>

Generate shared secret with specified remote address

use udpsec::Socket;
 
let local_addr = "127.0.0.1:50000".parse().unwrap();
let remote_addr = "127.0.0.1:50001".parse().unwrap();
 
let mut socket_a = Socket::new(local_addr).unwrap();
let mut socket_b = Socket::new(remote_addr).unwrap();
 
// wait_not_await's Await
socket_a.generate_secret(remote_addr).unwrap().then(|ping| {
    println!("Ping: {} ms", ping.as_millis());
});
 
socket_b.recv(); // Remote client updates its state in a loop
 
while socket_a.shared_secret(remote_addr) == None {
    socket_a.recv();
}
 
println!("Shared secret (local): {:?}", socket_a.shared_secret(remote_addr).unwrap());
println!("Shared secret (remote): {:?}", socket_b.shared_secret(local_addr).unwrap());
Source

pub fn shared_secret(&self, addr: SocketAddr) -> Option<&[u8; 32]>

Get shared secret with a specified remote address

See Socket.generate_secret() for more details

Source

pub fn send(&self, addr: SocketAddr, data: Vec<u8>) -> Result<usize, Error>

Send data to remote address

Returns false if data couldn’t be sent, or shared secret wasn’t generated

use udpsec::Socket;
 
let local_addr = "127.0.0.1:50002".parse().unwrap();
let remote_addr = "127.0.0.1:50003".parse().unwrap();
 
let mut socket_a = Socket::new(local_addr).unwrap();
let mut socket_b = Socket::new(remote_addr).unwrap();
 
socket_a.generate_secret(remote_addr); // Send KeyExchangeInit to remote client
 
socket_b.recv(); // Process KeyExchangeInit packet from local client
socket_a.recv(); // Receive KeyExchangeDone packet from remote client
 
socket_a.send(remote_addr, "Hello, World!".as_bytes().to_vec());
 
let received = socket_b.recv().unwrap();
 
println!("[{}] {}", received.0, String::from_utf8(received.1).unwrap());
Source

pub fn recv(&mut self) -> Option<(SocketAddr, Vec<u8>)>

Receive data from remote socket

See Socket.send() for more details

Auto Trait Implementations§

§

impl Freeze for Socket

§

impl !RefUnwindSafe for Socket

§

impl !Send for Socket

§

impl !Sync for Socket

§

impl Unpin for Socket

§

impl !UnwindSafe for Socket

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.