Crates.io | enet-rs |
lib.rs | enet-rs |
version | 1.3.171 |
source | src |
created_at | 2021-08-29 12:05:28.484242 |
updated_at | 2021-08-29 13:44:16.163163 |
description | A ENet library for Rust. |
homepage | |
repository | https://github.com/ZTzTopia/enet-rs |
max_upload_size | |
id | 443721 |
size | 428,161 |
Rust bindings for ENet library, the reliable UDP networking library.
Add the following to your Cargo.toml
file:
[dependencies]
enet-rs = "1.3.17"
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, detailing and explaining usage of the basic functionality of the library, can be found in the examples
directory.
Documentation is available by running cargo doc
or visit docs.rs.
enet-rs is licensed under the ISC license. See the file LICENSE.md
for more information.