From 676f250fff49a1e5bfd76dec15dc7ec7c4e3aaa9 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 16 Apr 2018 12:12:38 +0200 Subject: [PATCH] Expose Server API from library --- examples/server.rs | 2 +- src/lib.rs | 31 ++++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/server.rs b/examples/server.rs index 5023ff864..7474aa77e 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -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(); } diff --git a/src/lib.rs b/src/lib.rs index b2c907829..11fd99f7f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, out_buf: Vec, } +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(); -}