aboutsummaryrefslogtreecommitdiff
path: root/src/server_dev/auto/____server.rs
blob: 586bc16f0ee78038f4ff50d163189d1d6b779c29 (plain) (blame)
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
381
382
383
384
use std::error::Error;
use crate::server::auto::cmd::property::{
	create_property_update_command,
	create_property_request_command
};
use crate::server::cmd::text::{create_text_command_with_action, create_text_command};
use std::str::from_utf8;
use crate::server::cmd::buddy_list::create_buddy_list_notify_command;
use crate::server::auto::cmd::room::create_room_id_redirect_command;
use crate::server::auto::cmd::session::parse_session_initialization_command;
use crate::server::parser::get_commands_from_buffer;
use crate::server::cmd::property::parse_property_set_command;
use crate::config::get_config;
use mio::{Poll, Events, Token, Interest, Registry};
use mio::net::{TcpListener, TcpStream};
use std::collections::{HashMap, HashSet};
use mio::event::Event;
use std::io::{Read, ErrorKind, Write};
use bytes::BytesMut;

const SERVER: Token = Token(0);

pub struct AutoServer {
	pub clients: HashMap<Token, String>,
	pub connections: HashMap<Token, TcpStream>,
	pub room_ids: Vec<String>,
}
impl AutoServer {
	pub fn listen(&mut self, addr: &str) -> Result<(), Box<dyn Error>> {
		let mut listener = TcpListener::bind(addr.parse().unwrap())?;
		let mut poll = Poll::new()?;
		let mut events = Events::with_capacity(1024);
		let mut counter: usize = 0;
		// let mut sockets = HashMap::new();
		let mut requests = HashMap::new();
		let mut buffer = [0 as u8; 1024];
		// let mut room_ids = vec![];

		poll.registry().register(
			&mut listener,
			Token(0),
			Interest::READABLE,
		)?;

		debug!("AutoServer now listening on {}", listener.local_addr().unwrap());

		loop {
			poll.poll(&mut events, None)?;

			for event in &events {
				match event.token() {
					Token(0) => loop {
						match listener.accept() {
							Ok((mut stream, address)) => {
								counter += 1;
								let token = Token(counter);

								poll.registry().register(
									&mut stream,
									token,
									Interest::READABLE,
								)?;

								debug!("registered peer with address '{}' as '{}'", address, token.0);

								// sockets.insert(token, stream);
								self.connections.insert(token, stream);
								requests.insert(token, Vec::with_capacity(192));
							}
							Err(ref err) if err.kind() == ErrorKind::WouldBlock => break,
							Err(err) => {
								error!("unexpected error: {}", err);
								poll.registry().deregister(
									self.connections.get_mut(&Token(counter)).unwrap(),
								)?;
								break;
							}
						}
					},
					token if event.is_readable() => {
						loop {
							let read = self.connections.get_mut(&token).unwrap()
								.read(&mut buffer);
							match read {
								Ok(0) => { self.connections.remove(&token); break; }
								Ok(n) => {
									let req = requests.get_mut(&token).unwrap();
									for b in &buffer[0..n] { req.push(*b); }

									for cmd in get_commands_from_buffer(BytesMut::from(&buffer[..n])) {
										match cmd.get(2).unwrap() {
											10 => { // PROPREQ
												debug!("received property request command from client 'null'");
												self.connections.get_mut(&token).unwrap()
													.write_all(&create_property_update_command()).unwrap();
												debug!("sent property update command to client 'null'");
											}
											6 => { // SESSINIT
												let local_username =
													parse_session_initialization_command(cmd).username;
												self.clients.insert(token, local_username.clone());
												debug!(
													"received session initialization command from client '{}'",
													local_username,
												);
												self.connections.get_mut(&token).unwrap()
													.write_all(&create_property_request_command()).unwrap();
												debug!("sent session initialization command to client '{}'", local_username);
											}
											15 => { // PROPSET
												let avatar = parse_property_set_command(cmd);
												debug!(
													"received property set command from client '{}': {}",
													self.clients.get(&token).unwrap(),
													avatar,
												);
												self.connections.get_mut(&token).unwrap()
													.write_all(&create_text_command_with_action(
														"WORLDSMASTER", &get_config().unwrap().worldsmaster_greeting,
													)).unwrap();
												debug!(
													"sent session initialization command to client '{}'",
													self.clients.get(&token).unwrap(),
												);
											}
											29 =>  { // BUDDYLISTUPDATE
												let received_buddy = from_utf8(
													cmd.get(4..cmd.get(0).unwrap().to_owned() as usize - 1).unwrap()
												).unwrap();
												debug!(
													"received buddy list update command from client '{}': {}",
													self.clients.get(&token).unwrap(),
													received_buddy,
												);
												self.connections.get_mut(&token).unwrap()
													.write_all(&create_buddy_list_notify_command(received_buddy)).unwrap();
												debug!(
													"sent buddy list notify command to client '{}'",
													self.clients.get(&token).unwrap(),
												);
											}
											20 => { // ROOMIDRQ
												let room_name = from_utf8(
													cmd.get(4..cmd.get(0).unwrap().to_owned() as usize).unwrap()
												).unwrap();
												debug!(
													"received room id request command from client '{}': {}",
													self.clients.get(&token).unwrap(),
													room_name,
												);
												let room_id;
												if !self.room_ids.contains(&room_name.to_string()) {
													self.room_ids.push(room_name.to_string());
													room_id = self.room_ids.iter()
														.position(|i| i == &room_name.to_string())
														.unwrap();
													trace!("inserted room '{}' as '{}'", room_name, room_id);
												} else {
													let position = self.room_ids.iter()
														.position(|i| i == &room_name.to_string())
														.unwrap();
													trace!("found room '{}' as '{}'", room_name, position);
													room_id = position;
												}
												trace!("room name: {}, room id: {}", room_name, room_id);
												trace!("{:?}", self.room_ids);
												self.connections.get_mut(&token).unwrap()
													.write_all(&create_room_id_redirect_command(
														room_name, room_id,
													)).unwrap();
											}
											14 => { // TEXT
												let text = from_utf8(
													cmd.get(6..cmd.get(0).unwrap().to_owned() as usize).unwrap()
												).unwrap();
												let username = self.clients.get(&token).unwrap().clone();
												debug!(
													"received text command from client '{}': {}",
													username, text,
												);
												self.connections.iter_mut().for_each(|t|
													t.1.write_all(&create_text_command(
														&username,
														text,
													)).unwrap()
												);
												debug!("broadcasted text command to clients");
											}
											7 => { // SESSEXIT
												debug!(
													"received session exit command from client '{}'",
													self.clients.get(&token).unwrap(),
												);
											}
											_ => (),
										}
									}
								}
								Err(ref err) if err.kind() == ErrorKind::WouldBlock =>
									break,
								Err(err) => { error!("unexpected error: {}", err); break; }
							}
						}
					}
					_ => (),
				}
			}
		}
	}

	fn broadcast(
		sockets: &HashMap<Token, TcpStream>,
		cmd: &[u8],
	) -> () {
		for mut socket in sockets {
			socket.1.write_all(cmd).unwrap();
		}
	}

	// fn process(
	// 	&mut self,
	// 	_registry: &Registry,
	// 	event: &Event,
	// 	token: Token,
	// ) -> Result<bool, Box<dyn Error>> {
	// 	if event.is_readable() {
	// 		let mut connection_closed = false;
	// 		let mut received_data = vec![0; 4096];
	// 		let mut bytes_read = 0;
	//
	// 		let stream = self.connections.get_mut(&token).unwrap();
	//
	// 		loop {
	// 			match stream.read(&mut received_data[bytes_read..]) {
	// 				Ok(0) => {
	// 					connection_closed = true;
	// 					break;
	// 				}
	// 				Ok(n) => {
	// 					bytes_read += n;
	// 					if bytes_read ==  received_data.len() {
	// 						received_data.resize(received_data.len() + 1024, 0);
	// 					}
	// 				}
	// 				Err(ref err) if err.kind() == ErrorKind::WouldBlock => break,
	// 				Err(ref err) if err.kind() == ErrorKind::Interrupted => continue,
	// 				Err(err) => return Err(Box::new(err)),
	// 			}
	// 		}
	//
	// 		if bytes_read != 0 {
	// 			self.handle(
	// 				&mut received_data[..bytes_read],
	// 				token,
	// 			);
	// 		}
	// 		if connection_closed {
	// 			println!("de-registered peer with token '{}'", token.0);
	// 			return Ok(true);
	// 		}
	// 	}
	//
	// 	Ok(false)
	// }

	// fn handle(
	// 	&mut self,
	// 	data: &[u8],
	// 	// stream: &mut TcpStream,
	// 	token: Token,
	// ) -> () {
	// 	// trace!("i am client: {:?}", self.clients.get(&token));
	// 	// debug!("{:?}", self.connections);
	// 	for cmd in get_commands_from_buffer(BytesMut::from(data)) {
	// 		debug!("received: {:?}", cmd);
	// 		match cmd.get(2).unwrap() {
	// 			10 => { // PROPREQ
	// 				debug!("received property request command from client 'null'");
	// 				self.connections.get_mut(&token).unwrap()
	// 					.write_all(&create_property_update_command()).unwrap();
	// 				debug!("sent property update command to client 'null'");
	// 			}
	// 			6 => { // SESSINIT
	// 				let local_username =
	// 					parse_session_initialization_command(cmd).username;
	// 				self.clients.insert(token, local_username.clone());
	// 				debug!(
	// 					"received session initialization command from client '{}'",
	// 					local_username,
	// 				);
	// 				self.connections.get_mut(&token).unwrap()
	// 					.write_all(&create_property_request_command()).unwrap();
	// 				debug!("sent session initialization command to client '{}'", local_username);
	// 			}
	// 			15 => { // PROPSET
	// 				let avatar = parse_property_set_command(cmd);
	// 				debug!(
	// 					"received property set command from client '{}': {}",
	// 					self.clients.get(&token).unwrap(),
	// 					avatar,
	// 				);
	// 				self.connections.get_mut(&token).unwrap()
	// 					.write_all(&create_text_command_with_action(
	// 						"WORLDSMASTER", &get_config().unwrap().worldsmaster_greeting,
	// 					)).unwrap();
	// 				debug!(
	// 					"sent session initialization command to client '{}'",
	// 					self.clients.get(&token).unwrap(),
	// 				);
	// 			}
	// 			29 =>  { // BUDDYLISTUPDATE
	// 				let received_buddy = from_utf8(
	// 					cmd.get(4..cmd.get(0).unwrap().to_owned() as usize - 1).unwrap()
	// 				).unwrap();
	// 				debug!(
	// 					"received buddy list update command from client '{}': {}",
	// 					self.clients.get(&token).unwrap(),
	// 					received_buddy,
	// 				);
	// 				self.connections.get_mut(&token).unwrap()
	// 					.write_all(&create_buddy_list_notify_command(received_buddy)).unwrap();
	// 				debug!(
	// 					"sent buddy list notify command to client '{}'",
	// 					self.clients.get(&token).unwrap(),
	// 				);
	// 			}
	// 			20 => { // ROOMIDRQ
	// 				let room_name = from_utf8(
	// 					cmd.get(4..cmd.get(0).unwrap().to_owned() as usize).unwrap()
	// 				).unwrap();
	// 				debug!(
	// 					"received room id request command from client '{}': {}",
	// 					self.clients.get(&token).unwrap(),
	// 					room_name,
	// 				);
	// 				let room_id;
	// 				if !self.room_ids.contains(&room_name.to_string()) {
	// 					self.room_ids.push(room_name.to_string());
	// 					room_id = self.room_ids.iter()
	// 						.position(|i| i == &room_name.to_string())
	// 						.unwrap();
	// 					trace!("inserted room '{}' as '{}'", room_name, room_id);
	// 				} else {
	// 					let position = self.room_ids.iter()
	// 						.position(|i| i == &room_name.to_string())
	// 						.unwrap();
	// 					trace!("found room '{}' as '{}'", room_name, position);
	// 					room_id = position;
	// 				}
	// 				trace!("room name: {}, room id: {}", room_name, room_id);
	// 				trace!("{:?}", self.room_ids);
	// 				self.connections.get_mut(&token).unwrap()
	// 					.write_all(&create_room_id_redirect_command(
	// 						room_name, room_id,
	// 					)).unwrap();
	// 			}
	// 			14 => { // TEXT
	// 				let text = from_utf8(
	// 					cmd.get(6..cmd.get(0).unwrap().to_owned() as usize).unwrap()
	// 				).unwrap();
	// 				let username = self.clients.get(&token).unwrap().clone();
	// 				debug!(
	// 					"received text command from client '{}': {}",
	// 					username, text,
	// 				);
	// 				self.connections.iter_mut().for_each(|t|
	// 					t.1.write_all(&create_text_command(
	// 						&username,
	// 						text,
	// 					)).unwrap()
	// 				);
	// 				debug!("broadcasted text command to clients");
	// 			}
	// 			7 => { // SESSEXIT
	// 				debug!(
	// 					"received session exit command from client '{}'",
	// 					self.clients.get(&token).unwrap(),
	// 				);
	// 			}
	// 			_ => (),
	// 		}
	// 	}
	// }
}