-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom_server.rs
43 lines (35 loc) · 973 Bytes
/
custom_server.rs
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
use std::{thread::sleep, time::Duration};
use mainline::{
server::{DhtServer, Server},
Dht,
};
use tracing::{info, instrument, Level};
#[derive(Debug, Default)]
struct MyCustomServer {
inner: DhtServer,
}
impl Server for MyCustomServer {
#[instrument]
fn handle_request(
&mut self,
rpc: &mut mainline::rpc::Rpc,
from: std::net::SocketAddr,
transaction_id: u16,
request: &mainline::rpc::messages::RequestSpecific,
) {
info!(?request, ?from, "Request from");
// Do something ...
// For example, rate limiting:
// if self.rate_limiter.check() { return };
self.inner
.handle_request(rpc, from, transaction_id, request)
}
}
fn main() {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
Dht::builder()
.custom_server(Box::<MyCustomServer>::default())
.build()
.unwrap();
sleep(Duration::from_secs(5))
}