-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rs
More file actions
194 lines (160 loc) · 4.89 KB
/
server.rs
File metadata and controls
194 lines (160 loc) · 4.89 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use bytes::Bytes;
use futures::stream::FuturesUnordered;
use futures::{FutureExt, StreamExt};
use std::future::Future;
use std::io::{self};
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::task::JoinHandle;
use tracing::{event, span, Level};
use super::config::Config;
use super::state::State;
use crate::proto::Decode;
use crate::session::SessionManager;
use crate::srt::socket::SrtSocket;
use crate::srt::state::ConnectionId;
use super::{Error, IsPacket, Packet};
pub struct Server<S>
where
S: SessionManager,
{
pub state: State<S>,
workers: FuturesUnordered<Worker>,
}
impl<S> Server<S>
where
S: SessionManager,
{
pub fn new<C>(session_manager: S, config: C) -> Result<Self, io::Error>
where
C: Into<Config>,
{
let config = config.into();
let socket = SrtSocket::new(config.bind)?;
let rx = socket.recv_buffer_size()?;
let tx = socket.send_buffer_size()?;
tracing::info!("Srt socket listening on {}", socket.local_addr()?);
tracing::info!("Socket Recv-Q: {}, Send-Q: {}", rx, tx);
let num_workers = config.workers.get();
let socket = Arc::new(socket);
let state = State::new(session_manager, config);
let workers = FuturesUnordered::new();
for i in 0..num_workers {
workers.push(Worker::new(i, socket.clone(), state.clone()));
}
tracing::info!("Spawned {} worker threads", num_workers);
Ok(Self { state, workers })
}
}
impl<S> Future for Server<S>
where
S: SessionManager,
{
type Output = Result<(), Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
match self.workers.poll_next_unpin(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(None) => return Poll::Ready(Ok(())),
Poll::Ready(Some(Err(err))) => return Poll::Ready(Err(err)),
_ => (),
}
}
}
}
unsafe impl<S> Send for Server<S> where S: SessionManager + Send {}
async fn handle_message<S>(
packet: Packet,
addr: SocketAddr,
socket: &SrtSocket,
state: &State<S>,
) -> Result<(), Error>
where
S: SessionManager,
{
let stream = SrtStream { socket, addr };
// A destination socket id of 0 indicates a handshake request.
if packet.header.destination_socket_id == 0 {
match packet.downcast() {
Ok(packet) => {
super::handshake::handshake(packet, stream, state).await?;
}
Err(err) => {
tracing::debug!("Failed to downcast packet: {}", err);
}
}
return Ok(());
}
let id = ConnectionId {
addr: stream.addr,
server_socket_id: packet.header.destination_socket_id.into(),
client_socket_id: packet.header.destination_socket_id.into(),
};
match state.pool.get(id) {
Some(handle) => {
handle.send(packet).await;
}
None => {
tracing::debug!("Received packet from unknown client {}", id);
}
}
Ok(())
}
#[derive(Clone, Debug)]
pub struct SrtStream<'a> {
pub socket: &'a SrtSocket,
pub addr: SocketAddr,
}
impl<'a> SrtStream<'a> {
pub async fn send<T>(&self, packet: T) -> Result<(), Error>
where
T: IsPacket,
{
self.socket.send_to(packet, self.addr).await.map(|_| ())
}
}
#[derive(Debug)]
struct Worker {
handle: JoinHandle<Result<(), Error>>,
}
impl Worker {
pub fn new<S>(ident: usize, socket: Arc<SrtSocket>, state: State<S>) -> Self
where
S: SessionManager,
{
let resource_span = span!(Level::TRACE, "Worker");
let handle = tokio::task::spawn(async move {
event!(
parent: &resource_span,
Level::INFO,
"Created worker {}",
ident
);
loop {
let mut buf = state.arena.alloc(1500);
let (len, addr) = socket.recv_from(&mut buf).await?;
tracing::trace!("[{}] Got {} bytes from {}", ident, len, addr);
buf.truncate(len);
let mut buf = buf.freeze();
let packet = match Packet::decode(&mut buf) {
Ok(packet) => packet,
Err(err) => {
tracing::debug!("[{}] Failed to decode packet: {}", ident, err);
continue;
}
};
let socket = socket.clone();
handle_message(packet, addr, &socket, &state).await?;
}
});
Self { handle }
}
}
impl Future for Worker {
type Output = Result<(), Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.handle.poll_unpin(cx).map(|res| res.unwrap())
}
}