refactor(server): make with_listener a free function

Allow a Server to operate without requiring the entire Server struct
to move into the with_listener function (instead only the handler
function needs to move). This, allows other members to not move, or
move separately, which will be needed for the next commit.  See #471
This commit is contained in:
Mike Dilger
2015-04-26 20:59:04 +12:00
parent 1a076d1bc7
commit fef04d282f

View File

@@ -103,7 +103,7 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
Some((cert, key)) => HttpListener::https(addr, cert, key), Some((cert, key)) => HttpListener::https(addr, cert, key),
None => HttpListener::http(addr) None => HttpListener::http(addr)
}); });
self.with_listener(listener, threads) with_listener(self.handler, listener, threads)
} }
/// Binds to a socket and starts handling connections. /// Binds to a socket and starts handling connections.
@@ -117,9 +117,15 @@ H: Handler + 'static,
L: NetworkListener<Stream=S> + Send + 'static, L: NetworkListener<Stream=S> + Send + 'static,
S: NetworkStream + Clone + Send> Server<'a, H, L> { S: NetworkStream + Clone + Send> Server<'a, H, L> {
/// Creates a new server that will handle `HttpStream`s. /// Creates a new server that will handle `HttpStream`s.
pub fn with_listener(self, mut listener: L, threads: usize) -> HttpResult<Listening> { pub fn with_listener(self, listener: L, threads: usize) -> HttpResult<Listening> {
with_listener(self.handler, listener, threads)
}
}
fn with_listener<H, L>(handler: H, mut listener: L, threads: usize) -> HttpResult<Listening>
where H: Handler + 'static,
L: NetworkListener + Send + 'static {
let socket = try!(listener.local_addr()); let socket = try!(listener.local_addr());
let handler = self.handler;
debug!("threads = {:?}", threads); debug!("threads = {:?}", threads);
let pool = ListenerPool::new(listener.clone()); let pool = ListenerPool::new(listener.clone());
@@ -132,8 +138,6 @@ S: NetworkStream + Clone + Send> Server<'a, H, L> {
socket: socket, socket: socket,
}) })
} }
}
fn handle_connection<'h, S, H>(mut stream: &mut S, handler: &'h H) fn handle_connection<'h, S, H>(mut stream: &mut S, handler: &'h H)
where S: NetworkStream + Clone, H: Handler { where S: NetworkStream + Clone, H: Handler {