enet-rs

Crates.ioenet-rs
lib.rsenet-rs
version1.3.171
sourcesrc
created_at2021-08-29 12:05:28.484242
updated_at2021-08-29 13:44:16.163163
descriptionA ENet library for Rust.
homepage
repositoryhttps://github.com/ZTzTopia/enet-rs
max_upload_size
id443721
size428,161
ZTz (ZTzTopia)

documentation

https://docs.rs/enet-rs

README

enet-rs

Documentation Crates.io License

Rust bindings for ENet library, the reliable UDP networking library.

Installation

Add the following to your Cargo.toml file:

[dependencies]
enet-rs = "1.3.17"

Examples

server.rs:

use enet_rs::enet::{
    ENET_HOST_ANY,
    ENetAddress,
    enet_host_create,
};

fn main() {
    let address = ENetAddress {
        host: ENET_HOST_ANY,
        port: 2555
    };

    unsafe {
        let server = enet_host_create(&address,
            32,
            2,
            0,
            0);
        if server.is_null() {
            println!("An error occurred while trying to create an ENet server host.");
        }
    }
    
    loop {
        /* does nothing */
    }
}

client.rs:

use std::ptr::null;
use std::mem::MaybeUninit;
use std::ffi::CString;
use enet_rs::enet::{
    ENetEventType,
    enet_host_create,
    enet_address_set_host,
    enet_host_connect,
    enet_host_service,
    enet_peer_reset
};

fn main() {
    unsafe {
        let client = enet_host_create(null(),
            1,
            2,
            0,
            0);
        if client.is_null() {
            panic!("An error occurred while trying to create an ENet client host.")
        }

        let mut address_ = MaybeUninit::uninit();
        enet_address_set_host(address_.as_mut_ptr(), (&CString::new("127.0.0.1").unwrap()).as_ptr());
        let mut address = address_.assume_init();
        address.port = 2555;

        let peer = enet_host_connect(client, &address, 2, 0);
        if peer.is_null() {
            println!("No available peers for initiating an ENet connection.\n");
        }

        let mut event_ = MaybeUninit::uninit();
        loop {
            if enet_host_service(client, event_.as_mut_ptr(), 5000) > 0 {
                let event = event_.assume_init();
                match event.type_ {
                    ENetEventType::ENET_EVENT_TYPE_CONNECT => {
                        println!("connected to server.")
                    }
                    _ => enet_peer_reset(peer)
                }
            }
        }
    }
}

Full Examples

Full examples, detailing and explaining usage of the basic functionality of the library, can be found in the examples directory.

Documentation

Documentation is available by running cargo doc or visit docs.rs.

License

enet-rs is licensed under the ISC license. See the file LICENSE.md for more information.

Commit count: 8

cargo fmt