Expose Server API from library

This commit is contained in:
Dirkjan Ochtman
2018-04-16 12:12:38 +02:00
parent 544e8fab33
commit 676f250fff
2 changed files with 17 additions and 16 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
extern crate quinn;
fn main() {
quinn::bind("0.0.0.0", 4433);
quinn::Server::new("0.0.0.0", 4433).run();
}
+16 -15
View File
@@ -72,12 +72,27 @@ pub fn connect(server: &str, port: u16) {
println!("{:?} {:?} {:?} {:?}", sock, len, remote, buf);
}
struct Server {
pub struct Server {
socket: UdpSocket,
in_buf: Vec<u8>,
out_buf: Vec<u8>,
}
impl Server {
pub fn new(ip: &str, port: u16) -> Self {
let addr = (ip, port).to_socket_addrs().unwrap().next().unwrap();
Server {
socket: UdpSocket::bind(&addr).unwrap(),
in_buf: vec![0u8; 1600],
out_buf: vec![20u8, 2, 19, 83, 2, 1, 20, 16, 13, 6, 19, 81],
}
}
pub fn run(&mut self) {
self.wait().unwrap();
}
}
impl Future for Server {
type Item = ();
type Error = io::Error;
@@ -92,17 +107,3 @@ impl Future for Server {
}
}
}
pub fn bind(iface: &str, port: u16) {
let addr = (iface, port).to_socket_addrs().unwrap().next().unwrap();
println!("bind: {:?}", addr);
let sock = UdpSocket::bind(&addr).unwrap();
let in_buf = vec![0u8; 1600];
let out_buf = vec![20u8, 2, 19, 83, 2, 1, 20, 16, 13, 6, 19, 81];
let server = Server {
socket: sock,
in_buf,
out_buf,
};
server.wait().unwrap();
}