Skip to content

Commit b57d9f3

Browse files
Norman Maurertrustin
authored andcommitted
Allow per-write promises and disallow promises on flush()
- write() now accepts a ChannelPromise and returns ChannelFuture as most users expected. It makes the user's life much easier because it is now much easier to get notified when a specific message has been written. - flush() does not create a ChannelPromise nor returns ChannelFuture. It is now similar to what read() looks like.
1 parent dd76369 commit b57d9f3

87 files changed

Lines changed: 451 additions & 433 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
/target
1010
*/target
1111
/reports
12-
*/reports
12+
*/reports
13+
.DS_Store
14+

codec-http/src/main/java/io/netty/handler/codec/http/HttpContentEncoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ private void fetchEncoderOutput(List<Object> out) {
275275
if (buf == null) {
276276
break;
277277
}
278+
if (!buf.isReadable()) {
279+
continue;
280+
}
278281
out.add(new DefaultHttpContent(buf));
279282
}
280283
}

codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package io.netty.handler.codec.http;
1717

18-
import static io.netty.handler.codec.http.HttpConstants.COLON;
19-
import static io.netty.handler.codec.http.HttpConstants.CR;
20-
import static io.netty.handler.codec.http.HttpConstants.LF;
21-
import static io.netty.handler.codec.http.HttpConstants.SP;
2218
import io.netty.buffer.ByteBuf;
2319
import io.netty.channel.ChannelHandlerContext;
2420
import io.netty.handler.codec.MessageToMessageEncoder;
@@ -27,6 +23,8 @@
2723
import java.util.List;
2824
import java.util.Map;
2925

26+
import static io.netty.handler.codec.http.HttpConstants.*;
27+
3028
/**
3129
* Encodes an {@link HttpMessage} or an {@link HttpContent} into
3230
* a {@link ByteBuf}.

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameDecoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
package io.netty.handler.codec.http.websocketx;
5555

5656
import io.netty.buffer.ByteBuf;
57+
import io.netty.buffer.Unpooled;
5758
import io.netty.channel.ChannelFutureListener;
5859
import io.netty.channel.ChannelHandlerContext;
5960
import io.netty.handler.codec.CorruptedFrameException;
@@ -403,7 +404,7 @@ private void unmask(ByteBuf frame) {
403404
private void protocolViolation(ChannelHandlerContext ctx, String reason) {
404405
checkpoint(State.CORRUPT);
405406
if (ctx.channel().isActive()) {
406-
ctx.flush().addListener(ChannelFutureListener.CLOSE);
407+
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
407408
}
408409
throw new CorruptedFrameException(reason);
409410
}

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public final ChannelFuture handshake(Channel channel, final ChannelPromise promi
159159
} else {
160160
decoder.setSingleDecode(true);
161161
}
162-
channel.write(request).flush().addListener(new ChannelFutureListener() {
162+
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
163163
@Override
164164
public void operationComplete(ChannelFuture future) {
165165
if (future.isSuccess()) {
@@ -264,6 +264,6 @@ public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPr
264264
if (channel == null) {
265265
throw new NullPointerException("channel");
266266
}
267-
return channel.write(frame).flush(promise);
267+
return channel.writeAndFlush(frame, promise);
268268
}
269269
}

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketProtocolHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class WebSocketProtocolHandler extends MessageToMessageDecoder<WebSocke
2626
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
2727
if (frame instanceof PingWebSocketFrame) {
2828
frame.content().retain();
29-
ctx.channel().write(new PongWebSocketFrame(frame.content())).flush();
29+
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
3030
return;
3131
}
3232
if (frame instanceof PongWebSocketFrame) {

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public final ChannelFuture handshake(Channel channel, FullHttpRequest req,
158158
logger.debug(String.format("%s WS Version %s server handshake", channel, version()));
159159
}
160160
FullHttpResponse response = newHandshakeResponse(req, responseHeaders);
161-
channel.write(response).flush().addListener(new ChannelFutureListener() {
161+
channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
162162
@Override
163163
public void operationComplete(ChannelFuture future) throws Exception {
164164
if (future.isSuccess()) {
@@ -225,7 +225,7 @@ public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPr
225225
if (channel == null) {
226226
throw new NullPointerException("channel");
227227
}
228-
return channel.write(frame).flush(promise).addListener(ChannelFutureListener.CLOSE);
228+
return channel.writeAndFlush(frame, promise).addListener(ChannelFutureListener.CLOSE);
229229
}
230230

231231
/**

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders
177177
*/
178178
@Override
179179
public ChannelFuture close(Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) {
180-
return channel.write(frame).flush(promise);
180+
return channel.writeAndFlush(frame, promise);
181181
}
182182

183183
@Override

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
107107
if (cause instanceof WebSocketHandshakeException) {
108108
FullHttpResponse response = new DefaultFullHttpResponse(
109109
HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes()));
110-
ctx.channel().write(response).flush().addListener(ChannelFutureListener.CLOSE);
110+
ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
111111
} else {
112112
ctx.close();
113113
}
@@ -128,7 +128,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
128128
if (msg instanceof FullHttpRequest) {
129129
FullHttpResponse response =
130130
new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN);
131-
ctx.channel().write(response).flush();
131+
ctx.channel().writeAndFlush(response);
132132
} else {
133133
ctx.fireChannelRead(msg);
134134
}

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandshakeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
8282
}
8383

8484
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
85-
ChannelFuture f = ctx.channel().write(res).flush();
85+
ChannelFuture f = ctx.channel().writeAndFlush(res);
8686
if (!isKeepAlive(req) || res.getStatus().code() != 200) {
8787
f.addListener(ChannelFutureListener.CLOSE);
8888
}

0 commit comments

Comments
 (0)