Skip to content

Commit 54baa36

Browse files
committed
make Server core a RefCell and introduce bind2 method that allows external core to be passed in
1 parent 1cd8ea3 commit 54baa36

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/server/mod.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where B: Stream<Error=::Error>,
5353
{
5454
protocol: Http<B::Item>,
5555
new_service: S,
56-
core: Core,
56+
core: RefCell<Core>,
5757
listener: TcpListener,
5858
shutdown_timeout: Duration,
5959
}
@@ -91,8 +91,26 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
9191
Send + Sync + 'static,
9292
Bd: Stream<Item=B, Error=::Error>,
9393
{
94-
let core = try!(Core::new());
95-
let handle = core.handle();
94+
let core = RefCell::new(try!(Core::new()));
95+
self.bind2(core, addr, new_service)
96+
}
97+
98+
/// Bind the provided `addr` and return a server ready to handle
99+
/// connections. The server uses the provided `core` event loop.
100+
///
101+
/// This method will bind the `addr` provided with a new TCP listener ready
102+
/// to accept connections. Each connection will be processed with the
103+
/// `new_service` object provided as well, creating a new service per
104+
/// connection.
105+
///
106+
/// The returned `Server` contains one method, `run`, which is used to
107+
/// actually run the server.
108+
pub fn bind2<S, Bd>(&self, core: RefCell<Core>, addr: &SocketAddr, new_service: S) -> ::Result<Server<S, Bd>>
109+
where S: NewService<Request = Request, Response = Response<Bd>, Error = ::Error> +
110+
Send + Sync + 'static,
111+
Bd: Stream<Item=B, Error=::Error>,
112+
{
113+
let handle = core.borrow().handle();
96114
let listener = try!(TcpListener::bind(addr, &handle));
97115

98116
Ok(Server {
@@ -340,7 +358,7 @@ impl<S, B> Server<S, B>
340358
/// Returns a handle to the underlying event loop that this server will be
341359
/// running on.
342360
pub fn handle(&self) -> Handle {
343-
self.core.handle()
361+
self.core.borrow().handle()
344362
}
345363

346364
/// Configure the amount of time this server will wait for a "graceful
@@ -380,8 +398,8 @@ impl<S, B> Server<S, B>
380398
pub fn run_until<F>(self, shutdown_signal: F) -> ::Result<()>
381399
where F: Future<Item = (), Error = ()>,
382400
{
383-
let Server { protocol, new_service, mut core, listener, shutdown_timeout } = self;
384-
let handle = core.handle();
401+
let Server { protocol, new_service, core, listener, shutdown_timeout } = self;
402+
let handle = core.borrow().handle();
385403

386404
// Mini future to track the number of active services
387405
let info = Rc::new(RefCell::new(Info {
@@ -411,7 +429,7 @@ impl<S, B> Server<S, B>
411429
//
412430
// When we get a shutdown signal (`Ok`) then we drop the TCP listener to
413431
// stop accepting incoming connections.
414-
match core.run(shutdown_signal.select(srv)) {
432+
match core.borrow_mut().run(shutdown_signal.select(srv)) {
415433
Ok(((), _incoming)) => {}
416434
Err((e, _other)) => return Err(e.into()),
417435
}
@@ -425,10 +443,11 @@ impl<S, B> Server<S, B>
425443
// here have been destroyed.
426444
let timeout = try!(Timeout::new(shutdown_timeout, &handle));
427445
let wait = WaitUntilZero { info: info.clone() };
428-
match core.run(wait.select(timeout)) {
446+
let result = match core.borrow_mut().run(wait.select(timeout)) {
429447
Ok(_) => Ok(()),
430448
Err((e, _)) => return Err(e.into())
431-
}
449+
};
450+
result
432451
}
433452
}
434453

0 commit comments

Comments
 (0)