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
use crate::error::MessageError;

use std::fmt::Debug;
use std::hash::Hash;

use stream::packet::{
	self, Flags, Packet,
	PacketHeader, BodyBytes, BodyBytesMut, PacketError
};
pub use stream::packet::PacketBytes;

use bytes::{Bytes, BytesMut, BytesRead, BytesWrite};


/// ## Important
/// the number *zero* is not allowed to be used
pub trait Action: Debug + Copy + Eq + Hash {
	/// should try to convert an u16 to the action
	/// 
	/// 0 will never be passed as num
	fn from_u16(num: u16) -> Option<Self>;

	fn as_u16(&self) -> u16;

	fn max_body_size(_header: &Header<Self>) -> Option<u32> {
		None
	}
}

pub trait IntoMessage<A, B> {
	fn into_message(self) -> Result<Message<A, B>, MessageError>;
}

pub trait FromMessage<A, B>: Sized {
	fn from_message(msg: Message<A, B>) -> Result<Self, MessageError>;
}


#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message<A, B> {
	header: Header<A>,
	bytes: B
}

impl<A, B> Message<A, B>
where
	A: Action,
	B: PacketBytes
{
	pub fn new() -> Self {
		Self::empty()
	}
}

impl<A, B> Message<A, B>
where B: PacketBytes {
	pub fn set_success(&mut self, success: bool) {
		self.header.msg_flags.set_success(success);
	}

	pub fn is_success(&self) -> bool {
		self.header.msg_flags.is_success()
	}

	pub fn body(&self) -> BodyBytes<'_> {
		self.bytes.body()
	}

	pub fn body_mut(&mut self) -> BodyBytesMut<'_> {
		self.bytes.body_mut()
	}
}

impl<A, B> Message<A, B> {
	pub fn action(&self) -> Option<&A> {
		match &self.header.action {
			MaybeAction::Action(a) => Some(a),
			_ => None
		}
	}
}

impl<A, B> IntoMessage<A, B> for Message<A, B> {
	fn into_message(self) -> Result<Self, MessageError> {
		Ok(self)
	}
}

impl<A, B> FromMessage<A, B> for Message<A, B> {
	fn from_message(me: Self) -> Result<Self, MessageError> {
		Ok(me)
	}
}

impl<A, B> Packet<B> for Message<A, B>
where
	A: Action,
	B: PacketBytes
{
	type Header = Header<A>;

	fn header(&self) -> &Self::Header {
		&self.header
	}

	fn header_mut(&mut self) -> &mut Self::Header {
		&mut self.header
	}

	fn empty() -> Self {
		Self {
			header: Self::Header::empty(),
			bytes: B::new(Self::Header::LEN as usize)
		}
	}

	fn from_bytes_and_header(
		bytes: B,
		header: Self::Header
	) -> packet::Result<Self> {
		// todo probably check if the action is correct
		Ok(Self { header, bytes })
	}

	fn into_bytes(mut self) -> B {
		let body_len = self.bytes.body().len();
		self.header.body_len = body_len as u32;
		self.header.to_bytes(self.bytes.header_mut());
		self.bytes
	}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Header<A> {
	body_len: u32,
	flags: Flags,
	msg_flags: MessageFlags,
	id: u32,
	action: MaybeAction<A>
}

impl<A> Header<A>
where A: Action {
	pub fn empty() -> Self {
		Self {
			body_len: 0,
			flags: Flags::empty(),
			msg_flags: MessageFlags::new(true),
			id: 0,
			action: MaybeAction::None
		}
	}

	pub fn to_bytes(&self, mut bytes: BytesMut) {
		bytes.write_u32(self.body_len);
		bytes.write_u8(self.flags.as_u8());
		bytes.write_u8(self.msg_flags.as_u8());
		bytes.write_u32(self.id);
		bytes.write_u16(self.action.as_u16());
	}

	pub fn set_action(&mut self, action: A) {
		self.action = MaybeAction::Action(action);
	}
}

impl<A> PacketHeader for Header<A>
where A: Action {
	const LEN: u32 = 4 + 1 + 1 + 4 + 2;

	fn from_bytes(mut bytes: Bytes) -> packet::Result<Self> {
		let me = Self {
			body_len: bytes.read_u32(),
			flags: Flags::from_u8(bytes.read_u8())?,
			msg_flags: MessageFlags::from_u8(bytes.read_u8()),
			id: bytes.read_u32(),
			action: {
				let action_num = bytes.read_u16();
				if action_num == 0 {
					MaybeAction::None
				} else if let Some(action) = A::from_u16(action_num) {
					// we don't return an error here
					// since we wan't to get the package
					// and can still discard it later
					// this allows the connection to continue to exist
					// without closing it
					MaybeAction::Action(action)
				} else {
					MaybeAction::Unknown(action_num)
				}
			}
		};

		if let Some(max) = A::max_body_size(&me) {
			if me.body_len > max {
				return Err(PacketError::BodyLimitReached(max))
			}
		}

		Ok(me)
	}

	fn body_len(&self) -> u32 {
		self.body_len
	}

	fn flags(&self) -> &Flags {
		&self.flags
	}

	fn set_flags(&mut self, flags: Flags) {
		self.flags = flags;
	}

	fn id(&self) -> u32 {
		self.id
	}

	fn set_id(&mut self, id: u32) {
		self.id = id;
	}
}

/// In bits
/// +----------+---------+
/// | reserved | success |
/// +----------+---------+
/// |    7     |    1    |
/// +----------+---------+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct MessageFlags {
	inner: u8
}

impl MessageFlags {
	const SUCCESS_BIT: u8 = 0b0000_0001;

	pub fn new(success: bool) -> Self {
		let mut me = Self { inner: 0 };
		me.set_success(success);

		me
	}

	pub fn from_u8(inner: u8) -> Self {
		Self { inner }
	}

	pub fn is_success(&self) -> bool {
		self.inner & Self::SUCCESS_BIT != 0
	}

	pub fn set_success(&mut self, success: bool) {
		if success {
			self.inner |= Self::SUCCESS_BIT;
		} else {
			self.inner &= !Self::SUCCESS_BIT;
		}
	}

	pub fn as_u8(&self) -> u8 {
		self.inner
	}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum MaybeAction<A> {
	Action(A),
	/// zero will never be here
	Unknown(u16),
	/// Represented as 0
	None
}

impl<A> MaybeAction<A>
where A: Action {
	pub fn as_u16(&self) -> u16 {
		match self {
			Self::Action(a) => a.as_u16(),
			Self::Unknown(u) => *u,
			Self::None => 0
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	use stream::packet::PlainBytes;

	#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
	enum SomeAction {
		One,
		Two
	}

	impl Action for SomeAction {
		fn from_u16(num: u16) -> Option<Self> {
			match num {
				1 => Some(Self::One),
				2 => Some(Self::Two),
				_ => None
			}
		}

		fn as_u16(&self) -> u16 {
			match self {
				Self::One => 1,
				Self::Two => 2
			}
		}
	}

	#[test]
	fn msg_from_to_bytes() {
		let mut msg = Message::<_, PlainBytes>::new();
		msg.header_mut().set_action(SomeAction::Two);
		msg.body_mut().write_u16(u16::MAX);

		let bytes = msg.clone().into_bytes();
		let header = Header::from_bytes(bytes.header()).unwrap();
		let n_msg = Message::from_bytes_and_header(bytes, header).unwrap();
		assert_eq!(msg.action(), n_msg.action());
		assert_eq!(msg.header().flags(), n_msg.header().flags());

		assert_eq!(n_msg.body().read_u16(), u16::MAX);
	}
}