12132
|
1 |
use slab;
|
12131
|
2 |
use mio::tcp::*;
|
|
3 |
use mio::*;
|
|
4 |
use std::io::Write;
|
|
5 |
use std::io;
|
12132
|
6 |
use netbuf;
|
12131
|
7 |
|
|
8 |
use utils;
|
|
9 |
|
12132
|
10 |
type Slab<T> = slab::Slab<T, Token>;
|
|
11 |
|
12131
|
12 |
pub struct HWServer {
|
|
13 |
listener: TcpListener,
|
|
14 |
clients: Slab<HWClient>,
|
|
15 |
rooms: Slab<HWRoom>
|
|
16 |
}
|
|
17 |
|
|
18 |
impl HWServer {
|
|
19 |
pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
|
|
20 |
HWServer {
|
|
21 |
listener: listener,
|
|
22 |
clients: Slab::with_capacity(clients_limit),
|
|
23 |
rooms: Slab::with_capacity(rooms_limit),
|
|
24 |
}
|
|
25 |
}
|
|
26 |
|
|
27 |
pub fn register(&self, poll: &Poll) -> io::Result<()> {
|
|
28 |
poll.register(&self.listener, utils::SERVER, Ready::readable(),
|
|
29 |
PollOpt::edge())
|
|
30 |
}
|
|
31 |
|
|
32 |
pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
|
|
33 |
let (sock, addr) = self.listener.accept().unwrap();
|
|
34 |
println!("Connected: {}", addr);
|
|
35 |
|
|
36 |
let client = HWClient::new(sock);
|
|
37 |
let token = self.clients.insert(client)
|
|
38 |
.ok().expect("could not add connection to slab");
|
|
39 |
|
|
40 |
self.clients[token].send_raw_msg(
|
|
41 |
format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
|
|
42 |
, utils::PROTOCOL_VERSION).as_bytes());
|
12132
|
43 |
self.clients[token].register(poll, token);
|
12131
|
44 |
|
|
45 |
Ok(())
|
|
46 |
}
|
12132
|
47 |
|
|
48 |
pub fn client_readable(&mut self, poll: &Poll,
|
|
49 |
token: Token) -> io::Result<()> {
|
|
50 |
self.clients[token].readable(poll)
|
|
51 |
}
|
|
52 |
|
|
53 |
pub fn client_writable(&mut self, poll: &Poll,
|
|
54 |
token: Token) -> io::Result<()> {
|
|
55 |
self.clients[token].writable(poll)
|
|
56 |
}
|
12131
|
57 |
}
|
|
58 |
|
12132
|
59 |
|
12131
|
60 |
struct HWClient {
|
|
61 |
sock: TcpStream,
|
12132
|
62 |
buf_in: netbuf::Buf,
|
|
63 |
buf_out: netbuf::Buf
|
12131
|
64 |
}
|
|
65 |
|
|
66 |
impl HWClient {
|
|
67 |
fn new(sock: TcpStream) -> HWClient {
|
|
68 |
HWClient {
|
|
69 |
sock: sock,
|
12132
|
70 |
buf_in: netbuf::Buf::new(),
|
|
71 |
buf_out: netbuf::Buf::new(),
|
12131
|
72 |
}
|
|
73 |
}
|
|
74 |
|
12132
|
75 |
fn register(&self, poll: &Poll, token: Token) {
|
|
76 |
poll.register(&self.sock, token, Ready::readable(),
|
|
77 |
PollOpt::edge())
|
|
78 |
.ok().expect("could not register socket with event loop");
|
|
79 |
}
|
|
80 |
|
12131
|
81 |
fn send_raw_msg(&mut self, msg: &[u8]) {
|
12132
|
82 |
self.buf_out.write(msg).unwrap();
|
|
83 |
self.flush();
|
|
84 |
}
|
|
85 |
|
|
86 |
fn flush(&mut self) {
|
|
87 |
self.buf_out.write_to(&mut self.sock).unwrap();
|
|
88 |
self.sock.flush();
|
|
89 |
}
|
|
90 |
|
|
91 |
fn readable(&mut self, poll: &Poll) -> io::Result<()> {
|
|
92 |
self.buf_in.read_from(&mut self.sock)?;
|
|
93 |
println!("Incoming buffer size: {}", self.buf_in.len());
|
|
94 |
Ok(())
|
|
95 |
}
|
|
96 |
|
|
97 |
fn writable(&mut self, poll: &Poll) -> io::Result<()> {
|
|
98 |
self.buf_out.write_to(&mut self.sock)?;
|
|
99 |
Ok(())
|
12131
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
struct HWRoom {
|
|
104 |
name: String
|
|
105 |
}
|