@@ -490,7 +490,10 @@ public void register(ChannelHandlerContext ctx, CompletionHandler<Void> handler) |
490 | 490 |
|
491 | 491 | @Override |
492 | 492 | public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, CompletionHandler<Void> handler) { |
493 | | - ctx.bind(localAddress, handler); |
| 493 | + // Ensure we send the preface before we notify the bind promise as the user might try to write |
| 494 | + // directly in the listener attached to the promise and we need to ensure the preface is always the first |
| 495 | + // thing that is written. |
| 496 | + ctx.bind(localAddress, new PrefaceSendListener(ctx, handler)); |
494 | 497 | } |
495 | 498 |
|
496 | 499 | @Override |
@@ -499,23 +502,7 @@ public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, Sock |
499 | 502 | // Ensure we send the preface before we notify the connect promise as the user might try to write |
500 | 503 | // directly in the listener attached to the promise and we need to ensure the preface is always the first |
501 | 504 | // thing that is written. |
502 | | - ctx.connect(remoteAddress, localAddress, new CompletionHandler<>() { |
503 | | - @Override |
504 | | - public void success(Void result) { |
505 | | - try { |
506 | | - byteDecoder.sendPrefaceIfNeeded(ctx); |
507 | | - } catch (Throwable e) { |
508 | | - handler.failure(e); |
509 | | - return; |
510 | | - } |
511 | | - handler.success(result); |
512 | | - } |
513 | | - |
514 | | - @Override |
515 | | - public void failure(Throwable cause) { |
516 | | - handler.failure(cause); |
517 | | - } |
518 | | - }); |
| 505 | + ctx.connect(remoteAddress, localAddress, new PrefaceSendListener(ctx, handler)); |
519 | 506 | } |
520 | 507 |
|
521 | 508 | @Override |
@@ -1042,4 +1029,30 @@ private void doClose() { |
1042 | 1029 | } |
1043 | 1030 | } |
1044 | 1031 | } |
| 1032 | + |
| 1033 | + private final class PrefaceSendListener implements CompletionHandler<Void> { |
| 1034 | + private final ChannelHandlerContext ctx; |
| 1035 | + private final CompletionHandler<Void> handler; |
| 1036 | + |
| 1037 | + PrefaceSendListener(ChannelHandlerContext ctx, CompletionHandler<Void> handler) { |
| 1038 | + this.ctx = ctx; |
| 1039 | + this.handler = handler; |
| 1040 | + } |
| 1041 | + |
| 1042 | + @Override |
| 1043 | + public void success(Void result) { |
| 1044 | + try { |
| 1045 | + byteDecoder.sendPrefaceIfNeeded(ctx); |
| 1046 | + } catch (Throwable e) { |
| 1047 | + handler.failure(e); |
| 1048 | + return; |
| 1049 | + } |
| 1050 | + handler.success(result); |
| 1051 | + } |
| 1052 | + |
| 1053 | + @Override |
| 1054 | + public void failure(Throwable cause) { |
| 1055 | + handler.failure(cause); |
| 1056 | + } |
| 1057 | + } |
1045 | 1058 | } |