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
use std::io::{Error, ErrorKind};
use std::cell::UnsafeCell;
use std::sync::{Arc, Mutex};
use std::os::unix::io::{RawFd, AsRawFd};
use libc;
use simple_slab::Slab;
use super::{Stream, Handler};
pub type ConnectionSlab = Arc<MutSlab>;
pub type NewConnectionSlab = Arc<Mutex<Slab<Connection>>>;
pub type IoQueue = Arc<Mutex<Vec<IoPair>>>;
#[derive(Clone, PartialEq, Eq)]
pub enum IoEvent {
ReadAvailable,
WriteAvailable,
ReadWriteAvailable
}
#[derive(Clone)]
pub struct IoPair {
pub event: IoEvent,
pub arc_connection: Arc<Connection>
}
pub struct Connection {
pub fd: RawFd,
pub err_mutex: Mutex<Option<Error>>,
pub tx_mutex: Mutex<()>,
pub stream: Arc<UnsafeCell<Stream>>
}
unsafe impl Send for Connection {}
unsafe impl Sync for Connection {}
pub struct MutSlab {
pub inner: UnsafeCell<Slab<Arc<Connection>>>
}
unsafe impl Send for MutSlab {}
unsafe impl Sync for MutSlab {}
pub struct EventHandler(pub *mut Handler);
unsafe impl Send for EventHandler {}
unsafe impl Sync for EventHandler {}
impl Clone for EventHandler {
fn clone(&self) -> EventHandler {
let EventHandler(ptr) = *self;
unsafe {
let same_location = &mut *ptr;
EventHandler(same_location)
}
}
}
pub struct HydrogenSocket {
arc_connection: Arc<Connection>,
rearm_fn: unsafe fn(&Arc<Connection>, i32)
}
impl Clone for HydrogenSocket {
fn clone(&self) -> HydrogenSocket {
let fn_ptr = self.rearm_fn;
HydrogenSocket {
arc_connection: self.arc_connection.clone(),
rearm_fn: fn_ptr
}
}
}
impl HydrogenSocket {
pub fn new(arc_connection: Arc<Connection>,
rearm_fn: unsafe fn(&Arc<Connection>, i32))
-> HydrogenSocket
{
HydrogenSocket {
arc_connection: arc_connection,
rearm_fn: rearm_fn
}
}
pub fn send(&self, buf: &[u8]) {
let err;
{
let _ = match self.arc_connection.tx_mutex.lock() {
Ok(g) => g,
Err(p) => p.into_inner()
};
let stream_ptr = self.arc_connection.stream.get();
let write_result = unsafe {
(*stream_ptr).send(buf)
};
if write_result.is_ok() {
trace!("HydrogenSocket.send OK");
return;
}
err = write_result.unwrap_err();
}
match err.kind() {
ErrorKind::WouldBlock => {
trace!("HydrogenSocket.send received WouldBlock");
let execute = self.rearm_fn;
unsafe {
execute(&(self.arc_connection), libc::EPOLLOUT);
}
}
_ => {
trace!("HydrogenSocket.send received err");
{
let mut err_state = match self.arc_connection.err_mutex.lock() {
Ok(g) => g,
Err(p) => p.into_inner()
};
*err_state = Some(err);
}
}
}
}
pub fn shutdown(&mut self) -> Result<(), Error> {
let stream_ptr = self.arc_connection.stream.get();
unsafe {
(*stream_ptr).shutdown()
}
}
}
impl AsRawFd for HydrogenSocket {
fn as_raw_fd(&self) -> RawFd {
self.arc_connection.fd
}
}