@@ -53,7 +53,7 @@ where B: Stream<Error=::Error>,
53
53
{
54
54
protocol : Http < B :: Item > ,
55
55
new_service : S ,
56
- core : Core ,
56
+ core : RefCell < Core > ,
57
57
listener : TcpListener ,
58
58
shutdown_timeout : Duration ,
59
59
}
@@ -91,8 +91,26 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
91
91
Send + Sync + ' static ,
92
92
Bd : Stream < Item =B , Error =:: Error > ,
93
93
{
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 ( ) ;
96
114
let listener = try!( TcpListener :: bind ( addr, & handle) ) ;
97
115
98
116
Ok ( Server {
@@ -340,7 +358,7 @@ impl<S, B> Server<S, B>
340
358
/// Returns a handle to the underlying event loop that this server will be
341
359
/// running on.
342
360
pub fn handle ( & self ) -> Handle {
343
- self . core . handle ( )
361
+ self . core . borrow ( ) . handle ( )
344
362
}
345
363
346
364
/// Configure the amount of time this server will wait for a "graceful
@@ -380,8 +398,8 @@ impl<S, B> Server<S, B>
380
398
pub fn run_until < F > ( self , shutdown_signal : F ) -> :: Result < ( ) >
381
399
where F : Future < Item = ( ) , Error = ( ) > ,
382
400
{
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 ( ) ;
385
403
386
404
// Mini future to track the number of active services
387
405
let info = Rc :: new ( RefCell :: new ( Info {
@@ -411,7 +429,7 @@ impl<S, B> Server<S, B>
411
429
//
412
430
// When we get a shutdown signal (`Ok`) then we drop the TCP listener to
413
431
// stop accepting incoming connections.
414
- match core. run ( shutdown_signal. select ( srv) ) {
432
+ match core. borrow_mut ( ) . run ( shutdown_signal. select ( srv) ) {
415
433
Ok ( ( ( ) , _incoming) ) => { }
416
434
Err ( ( e, _other) ) => return Err ( e. into ( ) ) ,
417
435
}
@@ -425,10 +443,11 @@ impl<S, B> Server<S, B>
425
443
// here have been destroyed.
426
444
let timeout = try!( Timeout :: new ( shutdown_timeout, & handle) ) ;
427
445
let wait = WaitUntilZero { info : info. clone ( ) } ;
428
- match core. run ( wait. select ( timeout) ) {
446
+ let result = match core. borrow_mut ( ) . run ( wait. select ( timeout) ) {
429
447
Ok ( _) => Ok ( ( ) ) ,
430
448
Err ( ( e, _) ) => return Err ( e. into ( ) )
431
- }
449
+ } ;
450
+ result
432
451
}
433
452
}
434
453
0 commit comments