Skip to content

Commit

Permalink
refactor(server): make with_listener a free function
Browse files Browse the repository at this point in the history
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 hyperium#471
  • Loading branch information
mikedilger committed Apr 26, 2015
1 parent 1a076d1 commit fef04d2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<'a, H: Handler + 'static> Server<'a, H, HttpListener> {
Some((cert, key)) => HttpListener::https(addr, cert, key),
None => HttpListener::http(addr)
});
self.with_listener(listener, threads)
with_listener(self.handler, listener, threads)
}

/// Binds to a socket and starts handling connections.
Expand All @@ -117,23 +117,27 @@ H: Handler + 'static,
L: NetworkListener<Stream=S> + Send + 'static,
S: NetworkStream + Clone + Send> Server<'a, H, L> {
/// Creates a new server that will handle `HttpStream`s.
pub fn with_listener(self, mut listener: L, threads: usize) -> HttpResult<Listening> {
let socket = try!(listener.local_addr());
let handler = self.handler;
pub fn with_listener(self, listener: L, threads: usize) -> HttpResult<Listening> {
with_listener(self.handler, listener, threads)
}
}

debug!("threads = {:?}", threads);
let pool = ListenerPool::new(listener.clone());
let work = move |mut stream| handle_connection(&mut stream, &handler);
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 guard = thread::spawn(move || pool.accept(work, threads));
debug!("threads = {:?}", threads);
let pool = ListenerPool::new(listener.clone());
let work = move |mut stream| handle_connection(&mut stream, &handler);

Ok(Listening {
_guard: Some(guard),
socket: socket,
})
}
}
let guard = thread::spawn(move || pool.accept(work, threads));

Ok(Listening {
_guard: Some(guard),
socket: socket,
})
}

fn handle_connection<'h, S, H>(mut stream: &mut S, handler: &'h H)
where S: NetworkStream + Clone, H: Handler {
Expand Down

0 comments on commit fef04d2

Please sign in to comment.