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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use crate::message_table::MsgRegError::TypeAlreadyRegistered;
use crate::net::{DeserFn, SerFn, Transport};
use crate::MId;
use hashbrown::HashMap;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::any::{Any, TypeId};
use std::fmt::{Display, Formatter};
use std::io;
use MsgRegError::NonUniqueIdentifier;

/// A type for collecting the parts needed to send a struct over the network.
///
/// IMPORTANT: The Message tables on all clients and the server **need** to have exactly the same
/// types registered **in the same order**. If this is not possible, use [`SortedMsgTable`].
#[derive(Clone)]
pub struct MsgTable {
    table: Vec<(TypeId, Transport, SerFn, DeserFn)>,
}

/// A type for collecting the parts needed to send a struct over the network.
///
/// This is a variation of [`MsgTable`]. You should use this type only when you don't know the
/// order of registration. In place of a constant registration order, types must be registered
/// with a unique string identifier. The list is then sorted on this identifier when built.
///
/// If a type is registered with the same name, it will be ignored, therefore namespacing is
/// encouraged if you are allowing mods or external plugins to add networking types.
///
/// IMPORTANT: The Message tables on all clients and the server **need** to have exactly the
/// same types registered, although they do **not** need to be registered in the same order.
#[derive(Clone)]
pub struct SortedMsgTable {
    table: Vec<(String, TypeId, Transport, SerFn, DeserFn)>,
}

/// The useful parts of the [`MsgTable`] (or [`SortedMsgTable`]).
///
/// You can build this by registering your types with a [`MsgTable`], then building it with
/// [`MsgTable::build()`].
#[derive(Clone)]
pub struct MsgTableParts {
    /// The mapping from TypeId to MessageId.
    pub tid_map: HashMap<TypeId, MId>,
    /// The transport associated with each message type.
    pub transports: Vec<Transport>,
    /// The serialization functions associated with each message type.
    pub ser: Vec<SerFn>,
    /// The deserialization functions associated with each message type.
    pub deser: Vec<DeserFn>,
}

pub const CONNECTION_TYPE_MID: MId = 0;
pub const RESPONSE_TYPE_MID: MId = 1;
pub const DISCONNECT_TYPE_MID: MId = 2;

impl MsgTable {
    /// Creates a new [`MsgTable`].
    pub fn new() -> Self {
        MsgTable { table: vec![] }
    }

    /// Adds all registrations from `other` into this table.

    /// All errors are thrown before mutating self. If no errors are thrown, all entries are added;
    /// if an error is thrown, no entries are added.
    pub fn join(&mut self, other: &MsgTable) -> Result<(), MsgRegError> {
        // Validate
        if other
            .table
            .iter()
            .any(|(tid, _, _, _)| self.tid_registered(*tid))
        {
            return Err(TypeAlreadyRegistered);
        }

        // Join
        for entry in other.table.iter() {
            self.table.push(entry.clone());
        }
        Ok(())
    }

    /// If type `T` has been registered or not.
    pub fn is_registered<T>(&self) -> bool
    where
        T: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        let tid = TypeId::of::<T>();
        self.tid_registered(tid)
    }

    /// If the type with [`TypeId`] `tid` has been registered or not.
    pub fn tid_registered(&self, tid: TypeId) -> bool {
        self.table.iter().any(|(o_tid, _, _, _)| tid == *o_tid)
    }

    /// Registers a message type so that it can be sent over the network.
    pub fn register<T>(&mut self, transport: Transport) -> Result<(), MsgRegError>
    where
        T: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        self.table.push(self.get_registration::<T>(transport)?);
        Ok(())
    }

    /// Builds the things needed for the registration.
    fn get_registration<T>(
        &self,
        transport: Transport,
    ) -> Result<(TypeId, Transport, SerFn, DeserFn), MsgRegError>
    where
        T: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        // Get the type.
        let tid = TypeId::of::<T>();

        // Check if it has been registered already.
        if self.tid_registered(tid) {
            return Err(TypeAlreadyRegistered);
        }

        // Get the serialize and deserialize functions
        let deser: DeserFn = |bytes: &[u8]| {
            bincode::deserialize::<T>(bytes)
                .map(|d| Box::new(d) as Box<dyn Any + Send + Sync>)
                .map_err(|o| {
                    io::Error::new(io::ErrorKind::InvalidData, format!("Deser Error: {}", o))
                })
        };
        let ser: SerFn = |m: &(dyn Any + Send + Sync)| {
            bincode::serialize(m.downcast_ref::<T>().unwrap()).map_err(|o| {
                io::Error::new(io::ErrorKind::InvalidData, format!("Ser Error: {}", o))
            })
        };

        Ok((tid, transport, ser, deser))
    }

    /// Builds the [`MsgTable`] into useful parts.
    ///
    /// Consumes the Message table, and turns it into a [`MsgTableParts`].
    ///
    /// This should be called with the generic parameters:
    ///  - `C` is the connection message type.
    ///  - `R` is the response message type.
    ///  - `D` is the disconnect message type.
    ///
    /// The generic parameters should **not** be registered before hand.
    pub fn build<C, R, D>(self) -> Result<MsgTableParts, MsgRegError>
    where
        C: Any + Send + Sync + DeserializeOwned + Serialize,
        R: Any + Send + Sync + DeserializeOwned + Serialize,
        D: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        // Always prepend the Connection and Disconnect types first.
        // This gives them universal MIds.
        let con_discon_types = [
            self.get_registration::<C>(Transport::TCP)?,
            self.get_registration::<R>(Transport::TCP)?,
            self.get_registration::<D>(Transport::TCP)?,
        ];

        let mut tid_map = HashMap::with_capacity(self.table.len() + 3);
        let mut transports = Vec::with_capacity(self.table.len() + 3);
        let mut ser = Vec::with_capacity(self.table.len() + 3);
        let mut deser = Vec::with_capacity(self.table.len() + 3);

        // Add all types to parts. Connect type first, disconnect type second, all other types after
        for (idx, (tid, transport, s_fn, d_fn)) in con_discon_types
            .into_iter()
            .chain(self.table.into_iter())
            .enumerate()
        {
            tid_map.insert(tid, idx);
            transports.push(transport);
            ser.push(s_fn);
            deser.push(d_fn);
        }

        Ok(MsgTableParts {
            tid_map,
            transports,
            ser,
            deser,
        })
    }
}

impl SortedMsgTable {
    /// Creates a new [`SortedMsgTable`].
    pub fn new() -> Self {
        SortedMsgTable { table: vec![] }
    }

    /// Adds all registrations from `other` into this table.
    ///
    /// All errors are thrown before mutating self. If no errors are thrown, all entries are added;
    /// if an error is thrown, no entries are added.
    pub fn join(&mut self, other: &SortedMsgTable) -> Result<(), MsgRegError> {
        // Validate
        if other
            .table
            .iter()
            .any(|(_, tid, _, _, _)| self.tid_registered(*tid))
        {
            return Err(TypeAlreadyRegistered);
        }

        if other
            .table
            .iter()
            .any(|(id, _, _, _, _)| self.identifier_registered(&*id))
        {
            return Err(NonUniqueIdentifier);
        }

        // Join
        for entry in other.table.iter() {
            self.table.push(entry.clone());
        }
        Ok(())
    }

    /// If type `T` has been registered or not.
    pub fn is_registered<T>(&self) -> bool
    where
        T: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        let tid = TypeId::of::<T>();
        self.tid_registered(tid)
    }

    /// If the type with [`TypeId`] `tid` has been registered or not.
    pub fn tid_registered(&self, tid: TypeId) -> bool {
        self.table.iter().any(|(_, o_tid, _, _, _)| tid == *o_tid)
    }

    /// If the type with [`TypeId`] `tid` has been registered or not.
    pub fn identifier_registered(&self, identifier: &str) -> bool {
        self.table.iter().any(|(id, _, _, _, _)| identifier == &*id)
    }

    /// Registers a message type so that it can be sent over the network.
    pub fn register<T>(&mut self, transport: Transport, identifier: &str) -> Result<(), MsgRegError>
    where
        T: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        self.table
            .push(self.get_registration::<T>(identifier.into(), transport)?);
        Ok(())
    }

    /// Builds the things needed for the registration.
    fn get_registration<T>(
        &self,
        identifier: String,
        transport: Transport,
    ) -> Result<(String, TypeId, Transport, SerFn, DeserFn), MsgRegError>
    where
        T: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        // Get the serialize and deserialize functions
        let deser: DeserFn = |bytes: &[u8]| {
            bincode::deserialize::<T>(bytes)
                .map(|d| Box::new(d) as Box<dyn Any + Send + Sync>)
                .map_err(|o| {
                    io::Error::new(io::ErrorKind::InvalidData, format!("Deser Error: {}", o))
                })
        };
        let ser: SerFn = |m: &(dyn Any + Send + Sync)| {
            bincode::serialize(m.downcast_ref::<T>().unwrap()).map_err(|o| {
                io::Error::new(io::ErrorKind::InvalidData, format!("Ser Error: {}", o))
            })
        };

        // Check if the identifier has been registered already.
        if self.identifier_registered(&*identifier) {
            return Err(NonUniqueIdentifier);
        }

        // Get the type.
        let tid = TypeId::of::<T>();

        // Check if it has been registered already.
        if self.tid_registered(tid) {
            return Err(TypeAlreadyRegistered);
        }

        Ok((identifier, tid, transport, ser, deser))
    }

    /// Builds the [`SortedMsgTable`] into useful parts.
    ///
    /// Consumes the Message table, and turns it into a [`MsgTableParts`].
    ///
    /// This should be called with the generic parameters:
    ///  - `C` is the connection message type.
    ///  - `R` is the response message type.
    ///  - `f` is the disconnect message type.
    ///
    /// The generic parameters should **not** be registered before hand.
    pub fn build<C, R, D>(mut self) -> Result<MsgTableParts, MsgRegError>
    where
        C: Any + Send + Sync + DeserializeOwned + Serialize,
        R: Any + Send + Sync + DeserializeOwned + Serialize,
        D: Any + Send + Sync + DeserializeOwned + Serialize,
    {
        // Always prepend the Connection and Disconnect types first.
        // This gives them universal MIds.
        let con_discon_types = [
            self.get_registration::<C>("carrier-pigeon::connection".to_owned(), Transport::TCP)?,
            self.get_registration::<R>("carrier-pigeon::response".to_owned(), Transport::TCP)?,
            self.get_registration::<D>("carrier-pigeon::disconnect".to_owned(), Transport::TCP)?,
        ];

        // Sort by identifier string so that registration order doesn't matter.
        self.table
            .sort_by(|(id0, _, _, _, _), (id1, _, _, _, _)| id0.cmp(id1));

        let mut tid_map = HashMap::with_capacity(self.table.len() + 3);
        let mut transports = Vec::with_capacity(self.table.len() + 3);
        let mut ser = Vec::with_capacity(self.table.len() + 3);
        let mut deser = Vec::with_capacity(self.table.len() + 3);

        // Add all types to parts. Connect type first, disconnect type second, all other types after
        for (idx, (_identifier, tid, transport, s_fn, d_fn)) in con_discon_types
            .into_iter()
            .chain(self.table.into_iter())
            .enumerate()
        {
            tid_map.insert(tid, idx);
            transports.push(transport);
            ser.push(s_fn);
            deser.push(d_fn);
        }

        Ok(MsgTableParts {
            tid_map,
            transports,
            ser,
            deser,
        })
    }
}

impl MsgTableParts {
    /// Gets the number of registered `MId`s.
    pub fn mid_count(&self) -> usize {
        self.transports.len()
    }

    /// Checks if the [`MId`] `mid` is valid.
    pub fn valid_mid(&self, mid: MId) -> bool {
        mid <= self.mid_count()
    }

    /// Checks if the [`TypeId`] `tid` is registered.
    pub fn valid_tid(&self, tid: TypeId) -> bool {
        self.tid_map.contains_key(&tid)
    }
}

/// The possible errors when registering a type.
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum MsgRegError {
    /// The type was already registered.
    TypeAlreadyRegistered,
    /// The identifier string was already used.
    NonUniqueIdentifier,
}

impl Display for MsgRegError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            TypeAlreadyRegistered => write!(f, "Type was already registered."),
            NonUniqueIdentifier => write!(f, "The identifier was not unique."),
        }
    }
}