12127
|
1 |
use slab;
|
12126
|
2 |
use mio::tcp::*;
|
|
3 |
use mio::*;
|
|
4 |
use std::io::Write;
|
|
5 |
use std::io;
|
12127
|
6 |
use netbuf;
|
12126
|
7 |
|
|
8 |
use utils;
|
12128
|
9 |
use server::client::HWClient;
|
12126
|
10 |
|
12127
|
11 |
type Slab<T> = slab::Slab<T, Token>;
|
|
12 |
|
12126
|
13 |
pub struct HWServer {
|
|
14 |
listener: TcpListener,
|
|
15 |
clients: Slab<HWClient>,
|
|
16 |
rooms: Slab<HWRoom>
|
|
17 |
}
|
|
18 |
|
|
19 |
impl HWServer {
|
|
20 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
|
|
21 |
HWServer {
|
|
22 |
listener: listener,
|
|
23 |
clients: Slab::with_capacity(clients_limit),
|
|
24 |
rooms: Slab::with_capacity(rooms_limit),
|
|
25 |
}
|
|
26 |
}
|
|
27 |
|
|
28 |
pub fn register(&self, poll: &Poll) -> io::Result<()> {
|
|
29 |
poll.register(&self.listener, utils::SERVER, Ready::readable(),
|
|
30 |
PollOpt::edge())
|
|
31 |
}
|
|
32 |
|
|
33 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
|
|
34 |
let (sock, addr) = self.listener.accept().unwrap();
|
|
35 |
println!("Connected: {}", addr);
|
|
36 |
|
|
37 |
let client = HWClient::new(sock);
|
|
38 |
let token = self.clients.insert(client)
|
|
39 |
.ok().expect("could not add connection to slab");
|
|
40 |
|
12127
|
41 |
self.clients[token].register(poll, token);
|
12126
|
42 |
|
|
43 |
Ok(())
|
|
44 |
}
|
12127
|
45 |
|
|
46 |
pub fn client_readable(&mut self, poll: &Poll,
|
|
47 |
token: Token) -> io::Result<()> {
|
|
48 |
self.clients[token].readable(poll)
|
|
49 |
}
|
|
50 |
|
|
51 |
pub fn client_writable(&mut self, poll: &Poll,
|
|
52 |
token: Token) -> io::Result<()> {
|
|
53 |
self.clients[token].writable(poll)
|
|
54 |
}
|
12126
|
55 |
}
|
|
56 |
|
12127
|
57 |
|
12126
|
58 |
struct HWRoom {
|
|
59 |
name: String
|
|
60 |
}
|