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
use std::error::Error;
use std::any::Any;
use rotor::{Machine, Response, EventSet, PollOpt, Evented};
use rotor::{Scope, GenericScope};
use rotor::mio::{TryAccept};
use {StreamSocket, Accept};
pub trait Accepted<S: StreamSocket>: Machine {
fn accepted(sock: S, scope: &mut Scope<Self::Context>)
-> Result<Self, Box<Error>>;
}
impl<M, A> Accept<M, A>
where A: TryAccept + Evented + Any,
M: Machine
{
pub fn new<S: GenericScope>(sock: A, scope: &mut S)
-> Result<Self, Box<Error>>
{
try!(scope.register(&sock, EventSet::readable(), PollOpt::edge()));
Ok(Accept::Server(sock))
}
}
impl<M, A, S> Machine for Accept<M, A>
where A: TryAccept<Output=S> + Evented + Any,
S: StreamSocket,
M: Machine + Accepted<S>,
{
type Context = M::Context;
type Seed = S;
fn create(sock: S, scope: &mut Scope<Self::Context>)
-> Result<Self, Box<Error>>
{
M::accepted(sock, scope).map(Accept::Connection)
}
fn ready(self, events: EventSet, scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
match self {
Accept::Server(a) => {
match a.accept() {
Ok(Some(sock)) => {
Response::spawn(Accept::Server(a), sock)
}
Ok(None) => {
Response::ok(Accept::Server(a))
}
Err(_) => {
Response::ok(Accept::Server(a))
}
}
}
Accept::Connection(m) => {
m.ready(events, scope)
.map(Accept::Connection, |_| unreachable!())
}
}
}
fn spawned(self, _scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
match self {
Accept::Server(a) => {
match a.accept() {
Ok(Some(sock)) => {
Response::spawn(Accept::Server(a), sock)
}
Ok(None) => {
Response::ok(Accept::Server(a))
}
Err(_) => {
Response::ok(Accept::Server(a))
}
}
}
Accept::Connection(_) => {
unreachable!();
}
}
}
fn timeout(self, scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
match self {
Accept::Server(_) => unreachable!(),
Accept::Connection(m) => {
m.timeout(scope).map(Accept::Connection, |_| unreachable!())
}
}
}
fn wakeup(self, scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
match self {
Accept::Server(_) => unreachable!(),
Accept::Connection(m) => {
m.wakeup(scope).map(Accept::Connection, |_| unreachable!())
}
}
}
}