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
use std::collections::{VecDeque};
use std::net::{SocketAddr};
use mio::{EventLoop, Sender, TimerResult, Timeout};
use buffer::{BufferPool, Buffer};
use dispatcher::{Dispatcher, DispatchHandler};
pub struct Provider<'a, D: Dispatcher + 'a> {
buffer_pool: &'a mut BufferPool,
out_queue: &'a mut VecDeque<(Buffer, SocketAddr)>,
event_loop: &'a mut EventLoop<DispatchHandler<D>>
}
pub fn new<'a, D: Dispatcher>(buffer_pool: &'a mut BufferPool, out_queue: &'a mut VecDeque<(Buffer, SocketAddr)>,
event_loop: &'a mut EventLoop<DispatchHandler<D>>) -> Provider<'a, D> {
Provider{ buffer_pool: buffer_pool, out_queue: out_queue, event_loop: event_loop }
}
impl<'a, D: Dispatcher> Provider<'a, D> {
pub fn channel(&self) -> Sender<D::Message> {
self.event_loop.channel()
}
pub fn outgoing<F>(&mut self, out: F)
where F: FnOnce(&mut [u8]) -> Option<(usize, SocketAddr)> {
let mut buffer = self.buffer_pool.pop();
let opt_send_to = out(buffer.as_mut());
match opt_send_to {
None => self.buffer_pool.push(buffer),
Some((bytes, addr)) => {
buffer.set_written(bytes);
self.out_queue.push_back((buffer, addr));
}
}
}
pub fn set_timeout(&mut self, token: D::Timeout, delay: u64) -> TimerResult<Timeout> {
self.event_loop.timeout_ms(token, delay)
}
pub fn clear_timeout(&mut self, timeout: Timeout) -> bool {
self.event_loop.clear_timeout(timeout)
}
pub fn shutdown(&mut self) {
self.event_loop.shutdown()
}
}