@@ -205,18 +205,22 @@ public final class ServerBootstrap {
205205 return eventLoop. newFailedFuture ( error: error)
206206 }
207207
208- return serverChannelInit ( serverChannel) . then {
209- serverChannel. pipeline. add ( handler: AcceptHandler ( childChannelInitializer: childChannelInit,
210- childChannelOptions: childChannelOptions) )
211- } . then {
212- serverChannelOptions. applyAll ( channel: serverChannel)
208+ return eventLoop. submit {
209+ return serverChannelInit ( serverChannel) . then {
210+ serverChannel. pipeline. add ( handler: AcceptHandler ( childChannelInitializer: childChannelInit,
211+ childChannelOptions: childChannelOptions) )
212+ } . then {
213+ serverChannelOptions. applyAll ( channel: serverChannel)
214+ } . then {
215+ register ( eventLoop, serverChannel)
216+ } . map {
217+ serverChannel as Channel
218+ } . thenIfError { error in
219+ serverChannel. close0 ( error: error, mode: . all, promise: nil )
220+ return eventLoop. newFailedFuture ( error: error)
221+ }
213222 } . then {
214- register ( eventLoop, serverChannel)
215- } . map {
216- serverChannel
217- } . thenIfError { error in
218- serverChannel. close0 ( error: error, mode: . all, promise: nil )
219- return eventLoop. newFailedFuture ( error: error)
223+ $0
220224 }
221225 }
222226
@@ -240,23 +244,42 @@ public final class ServerBootstrap {
240244
241245 func channelRead( ctx: ChannelHandlerContext , data: NIOAny ) {
242246 let accepted = self . unwrapInboundIn ( data)
243- let childChannelInit = self . childChannelInit ?? { ( _: Channel ) in ctx. eventLoop. newSucceededFuture ( result: ( ) ) }
244-
245- self . childChannelOptions. applyAll ( channel: accepted) . hopTo ( eventLoop: ctx. eventLoop) . then {
246- assert ( ctx. eventLoop. inEventLoop)
247- return childChannelInit ( accepted)
248- } . then { ( ) -> EventLoopFuture < Void > in
249- assert ( ctx. eventLoop. inEventLoop)
250- guard !ctx. pipeline. destroyed else {
251- return accepted. close ( ) . thenThrowing {
252- throw ChannelError . ioOnClosedChannel
247+ let ctxEventLoop = ctx. eventLoop
248+ let childEventLoop = accepted. eventLoop
249+ let childChannelInit = self . childChannelInit ?? { ( _: Channel ) in childEventLoop. newSucceededFuture ( result: ( ) ) }
250+
251+ @inline ( __always)
252+ func setupChildChannel( ) -> EventLoopFuture < Void > {
253+ return self . childChannelOptions. applyAll ( channel: accepted) . then { ( ) -> EventLoopFuture < Void > in
254+ assert ( childEventLoop. inEventLoop)
255+ return childChannelInit ( accepted)
256+ }
257+ }
258+
259+ @inline ( __always)
260+ func fireThroughPipeline( _ future: EventLoopFuture < Void > ) {
261+ assert ( ctxEventLoop. inEventLoop)
262+ future. then { ( _) -> EventLoopFuture < Void > in
263+ assert ( ctxEventLoop. inEventLoop)
264+ guard !ctx. pipeline. destroyed else {
265+ return accepted. close ( ) . thenThrowing {
266+ throw ChannelError . ioOnClosedChannel
267+ }
253268 }
269+ ctx. fireChannelRead ( data)
270+ return ctx. eventLoop. newSucceededFuture ( result: ( ) )
271+ } . whenFailure { error in
272+ assert ( ctx. eventLoop. inEventLoop)
273+ self . closeAndFire ( ctx: ctx, accepted: accepted, err: error)
254274 }
255- ctx. fireChannelRead ( data)
256- return ctx. eventLoop. newSucceededFuture ( result: ( ) )
257- } . whenFailure { error in
258- assert ( ctx. eventLoop. inEventLoop)
259- self . closeAndFire ( ctx: ctx, accepted: accepted, err: error)
275+ }
276+
277+ if childEventLoop === ctxEventLoop {
278+ fireThroughPipeline ( setupChildChannel ( ) )
279+ } else {
280+ fireThroughPipeline ( childEventLoop. submit {
281+ return setupChildChannel ( )
282+ } . then { $0 } . hopTo ( eventLoop: ctxEventLoop) )
260283 }
261284 }
262285
@@ -463,18 +486,27 @@ public final class ClientBootstrap {
463486 return promise. futureResult
464487 }
465488
466- channelInitializer ( channel) . then {
467- channelOptions. applyAll ( channel: channel)
468- } . then {
469- channel. registerAndDoSynchronously ( body)
470- } . map {
471- channel
472- } . thenIfError { error in
473- channel. close0 ( error: error, mode: . all, promise: nil )
474- return channel. eventLoop. newFailedFuture ( error: error)
475- } . cascade ( promise: promise)
489+ @inline ( __always)
490+ func setupChannel( ) -> EventLoopFuture < Channel > {
491+ assert ( eventLoop. inEventLoop)
492+ channelInitializer ( channel) . then {
493+ channelOptions. applyAll ( channel: channel)
494+ } . then {
495+ channel. registerAndDoSynchronously ( body)
496+ } . map {
497+ channel
498+ } . thenIfError { error in
499+ channel. close0 ( error: error, mode: . all, promise: nil )
500+ return channel. eventLoop. newFailedFuture ( error: error)
501+ } . cascade ( promise: promise)
502+ return promise. futureResult
503+ }
476504
477- return promise. futureResult
505+ if eventLoop. inEventLoop {
506+ return setupChannel ( )
507+ } else {
508+ return eventLoop. submit ( setupChannel) . then { $0 }
509+ }
478510 }
479511}
480512
0 commit comments