1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::marker::PhantomData;
use std::sync::Arc;
use crate::{Address, Event, Error, EnetKeepAlive, Peer};
use enet_sys::{
enet_host_bandwidth_limit, enet_host_channel_limit, enet_host_check_events, enet_host_connect,
enet_host_destroy, enet_host_flush, enet_host_service, ENetEvent, ENetHost, ENetPeer,
ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BandwidthLimit {
Unlimited,
Limited(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ChannelLimit {
Maximum,
Limited(usize),
}
impl ChannelLimit {
pub(in crate) fn to_enet_usize(&self) -> usize {
match *self {
ChannelLimit::Maximum => 0,
ChannelLimit::Limited(l) => l,
}
}
fn from_enet_usize(enet_val: usize) -> ChannelLimit {
const MAX_COUNT: usize = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT as usize;
match enet_val {
MAX_COUNT => ChannelLimit::Maximum,
0 => panic!("ChannelLimit::from_enet_usize: got 0"),
lim => ChannelLimit::Limited(lim),
}
}
}
impl BandwidthLimit {
pub(in crate) fn to_enet_u32(&self) -> u32 {
match *self {
BandwidthLimit::Unlimited => 0,
BandwidthLimit::Limited(l) => l,
}
}
}
pub struct Host<T> {
inner: *mut ENetHost,
_keep_alive: Arc<EnetKeepAlive>,
_peer_data: PhantomData<*const T>,
}
impl<T> Host<T> {
pub(in crate) fn new(_keep_alive: Arc<EnetKeepAlive>, inner: *mut ENetHost) -> Host<T> {
assert!(!inner.is_null());
Host {
inner,
_keep_alive,
_peer_data: PhantomData,
}
}
pub fn flush(&mut self) {
unsafe {
enet_host_flush(self.inner);
}
}
pub fn set_bandwith_limits(
&mut self,
incoming_bandwith: BandwidthLimit,
outgoing_bandwidth: BandwidthLimit,
) {
unsafe {
enet_host_bandwidth_limit(
self.inner,
incoming_bandwith.to_enet_u32(),
outgoing_bandwidth.to_enet_u32(),
);
}
}
pub fn set_channel_limit(&mut self, max_channel_count: ChannelLimit) {
unsafe {
enet_host_channel_limit(self.inner, max_channel_count.to_enet_usize());
}
}
pub fn channel_limit(&self) -> ChannelLimit {
ChannelLimit::from_enet_usize(unsafe { (*self.inner).channelLimit })
}
pub fn incoming_bandwidth(&self) -> u32 {
unsafe { (*self.inner).incomingBandwidth }
}
pub fn outgoing_bandwidth(&self) -> u32 {
unsafe { (*self.inner).outgoingBandwidth }
}
pub fn address(&self) -> Address {
Address::from_enet_address(&unsafe { (*self.inner).address })
}
pub fn peer_count(&self) -> usize {
unsafe { (*self.inner).peerCount }
}
pub fn peers(&'_ mut self) -> impl Iterator<Item = Peer<'_, T>> {
let raw_peers =
unsafe { std::slice::from_raw_parts_mut((*self.inner).peers, (*self.inner).peerCount) };
raw_peers.into_iter().map(|rp| Peer::new(rp))
}
pub fn service(&'_ mut self, timeout_ms: u32) -> Result<Option<Event<'_, T>>, Error> {
let mut sys_event: ENetEvent = unsafe { std::mem::uninitialized() };
let res =
unsafe { enet_host_service(self.inner, &mut sys_event as *mut ENetEvent, timeout_ms) };
match res {
r if r > 0 => Ok(Event::from_sys_event(&sys_event)),
0 => Ok(None),
r if r < 0 => Err(Error(r)),
_ => panic!("unreachable"),
}
}
pub fn check_events(&'_ mut self) -> Result<Option<Event<'_, T>>, Error> {
let mut sys_event: ENetEvent = unsafe { std::mem::uninitialized() };
let res = unsafe { enet_host_check_events(self.inner, &mut sys_event as *mut ENetEvent) };
match res {
r if r > 0 => Ok(Event::from_sys_event(&sys_event)),
0 => Ok(None),
r if r < 0 => Err(Error(r)),
_ => panic!("unreachable"),
}
}
pub fn connect(
&mut self,
address: &Address,
channel_count: usize,
user_data: u32,
) -> Result<Peer<'_, T>, Error> {
let res: *mut ENetPeer = unsafe {
enet_host_connect(
self.inner,
&address.to_enet_address() as *const _,
channel_count,
user_data,
)
};
if res.is_null() {
return Err(Error(0));
}
Ok(Peer::new(res))
}
}
impl<T> Drop for Host<T> {
fn drop(&mut self) {
unsafe {
enet_host_destroy(self.inner);
}
}
}