Crate knet_bindings[][src]

Expand description

This crate provides access to the kronosnet library ‘libknet’ from Rust. They are a fairly thin layer around the actual API calls but with Rust data types and iterators.

No more information about knet itself will be provided here, it is expected that if you feel you need access to the knet API calls, you know what they do :)

Example

use knet_bindings::knet_bindings as knet;
use std::net::{SocketAddr, IpAddr, Ipv4Addr};
use std::thread::spawn;
use std::sync::mpsc::Receiver;
use std::sync::mpsc::channel;
use std::io::{Result, ErrorKind, Error};
use std::{thread, time};

const CHANNEL: i8 = 1;

pub fn main() -> Result<()>
{
    let host_id = knet::HostId::new(1);
    let other_host_id = knet::HostId::new(2);

    let (log_sender, log_receiver) = channel::<knet::LogMsg>();
    spawn(move || logging_thread(log_receiver));

    let knet_handle = match knet::handle_new(&our_hostid, Some(log_sender),
                                             knet::LogLevel::Debug, knet::HandleFlags::NONE) {
        Ok(h) => h,
        Err(e) => {
            return Err(e);
        }
    };

    if let Err(e) = knet::host_add(knet_handle, &other_hostid) {
        return Err(e);
    }
    if let Err(e) = knet::link_set_config(knet_handle, &other_hostid, 0,
                                knet::TransportId::Udp,
                                &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000+(our_hostid.to_u16())),
                                &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000+(other_hostid.to_u16())),
                                knet::LinkFlags::NONE) {
        return Err(e);
    }
    if let Err(e) = knet::handle_add_datafd(knet_handle, 0, CHANNEL) {
        return Err(e);
    }

    if let Err(e) = knet::handle_crypto_rx_clear_traffic(knet_handle, knet::RxClearTraffic::Allow) {
        return Err(e);
    }

    if let Err(e) = knet::link_set_enable(knet_handle, &other_hostid, 0, true) {
        return Err(e);
    }

    if let Err(e) = knet::handle_setfwd(knet_handle, true) {
        return Err(e);
    }

    Ok()
}

Modules