pub trait HolePunchConnect {
    // Required methods
    fn new_hole_punched<A>(
        server_addr: A,
        ident: &[u8]
    ) -> Result<UdpSocket, Error>
       where A: ToSocketAddrs;
    fn hole_punch_connect<A>(
        &self,
        server_addr: A,
        ident: &[u8]
    ) -> Result<(), Error>
       where A: ToSocketAddrs;
}
Expand description

A trait implemented for std::net::UdpSocket.

When a socket has been “connected” to the other client, the “connection” is directly to the client, not the server. The server is not used to pass through packages, only to pair the two clients together. (As you should know, since it’s the whole purpose of UDP hole punching).

Required Methods§

source

fn new_hole_punched<A>(server_addr: A, ident: &[u8]) -> Result<UdpSocket, Error>where A: ToSocketAddrs,

Creates a new socket, calls hole_punch_connect on it and returns it.

Examples

A simple peer-to-peer UDP chat client

use hole_punch_connect::HolePunchConnect;
use std::net::UdpSocket;

fn main() -> Result<(), std::io::Error> {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf)?;

    let socket = UdpSocket::new_hole_punched(
        "domain.name:420",
        buf.trim().as_bytes(),
    )?;

    {
        let socket = socket.try_clone()?;
        std::thread::spawn(move || loop {
            buf.clear();
            if let Ok(_len) = std::io::stdin().read_line(&mut buf) {
                socket.send(buf.trim().as_bytes()).unwrap();
            }
        });
    }

    let mut buf = [0u8; 512];
    let _ = std::thread::spawn(move || loop {
        if let Ok(len) = socket.recv(&mut buf) {
            println!("{}", String::from_utf8_lossy(&buf[..len]));
        }
    })
    .join();

    Ok(())
}
Errors

Propagates any error from creating the socket or from connecting it with hole_punch_connect.

source

fn hole_punch_connect<A>( &self, server_addr: A, ident: &[u8] ) -> Result<(), Error>where A: ToSocketAddrs,

connects the socket to another client.

The server_addr parameter is the address to the server that pairs the two clients together.

The ident parameter specifies an identifier that the server uses to know which two clients to pair together.

Examples
let socket = UdpSocket::bind("0.0.0.0:0")?;
socket.hole_punch_connect("domain.name:420")?;
socket.send(b"Hello there")?;

socket.hole_punch_connect("other.domain:6392")?;
socket.send(b"General Kenobi")?;
Errors

Propagates any error from sending or receiving data via the socket or if the data from the server was incorrect.

It does not however return any errors if the other client can’t be reached. That will produce an error at an attempt to send packets to it. (This will hopefully be fixed in the future.)

Implementations on Foreign Types§

source§

impl HolePunchConnect for UdpSocket

source§

fn new_hole_punched<A>(server_addr: A, ident: &[u8]) -> Result<UdpSocket, Error>where A: ToSocketAddrs,

source§

fn hole_punch_connect<A>( &self, server_addr: A, ident: &[u8] ) -> Result<(), Error>where A: ToSocketAddrs,

Implementors§