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:
		| @@ -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,23 +117,27 @@ 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> { | ||||||
|         let socket = try!(listener.local_addr()); |         with_listener(self.handler, listener, threads) | ||||||
|         let handler = self.handler; |  | ||||||
|  |  | ||||||
|         debug!("threads = {:?}", threads); |  | ||||||
|         let pool = ListenerPool::new(listener.clone()); |  | ||||||
|         let work = move |mut stream| handle_connection(&mut stream, &handler); |  | ||||||
|  |  | ||||||
|         let guard = thread::spawn(move || pool.accept(work, threads)); |  | ||||||
|  |  | ||||||
|         Ok(Listening { |  | ||||||
|             _guard: Some(guard), |  | ||||||
|             socket: socket, |  | ||||||
|         }) |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | 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()); | ||||||
|  |  | ||||||
|  |     debug!("threads = {:?}", threads); | ||||||
|  |     let pool = ListenerPool::new(listener.clone()); | ||||||
|  |     let work = move |mut stream| handle_connection(&mut stream, &handler); | ||||||
|  |  | ||||||
|  |     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) | 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 { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user