2011-10-17 15:12:37 +11:00
|
|
|
/*
|
2012-06-04 13:31:44 -07:00
|
|
|
* Copyright 2012 The Netty Project
|
2011-10-17 15:12:37 +11:00
|
|
|
*
|
2011-12-09 14:18:34 +09:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2011-10-17 15:12:37 +11:00
|
|
|
*
|
2012-06-04 13:31:44 -07:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-10-17 15:12:37 +11:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 14:18:34 +09:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2011-10-17 15:12:37 +11:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2017-02-20 14:06:48 +01:00
|
|
|
package io.netty.testsuite.autobahn;
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2013-07-14 22:50:53 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 17:02:00 +09:00
|
|
|
import io.netty.buffer.Unpooled;
|
2011-12-09 12:38:59 +09:00
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
import io.netty.channel.ChannelFutureListener;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 20:40:19 +09:00
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2013-01-16 13:22:50 +09:00
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
2014-10-31 16:48:28 +09:00
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
2015-01-06 06:55:01 +01:00
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
2011-12-09 12:38:59 +09:00
|
|
|
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
|
|
|
|
import io.netty.util.CharsetUtil;
|
2013-11-04 19:42:33 +09:00
|
|
|
import io.netty.util.internal.StringUtil;
|
2013-02-11 20:17:35 +09:00
|
|
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2015-08-22 08:25:57 -07:00
|
|
|
import static io.netty.handler.codec.http.HttpUtil.*;
|
2012-11-12 11:23:06 +09:00
|
|
|
import static io.netty.handler.codec.http.HttpMethod.*;
|
|
|
|
import static io.netty.handler.codec.http.HttpResponseStatus.*;
|
|
|
|
import static io.netty.handler.codec.http.HttpVersion.*;
|
|
|
|
|
2011-10-17 15:12:37 +11:00
|
|
|
/**
|
|
|
|
* Handles handshakes and messages
|
|
|
|
*/
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 20:40:19 +09:00
|
|
|
public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
2013-02-11 20:17:35 +09:00
|
|
|
private static final Logger logger = Logger.getLogger(AutobahnServerHandler.class.getName());
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2012-01-11 20:16:14 +09:00
|
|
|
private WebSocketServerHandshaker handshaker;
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2011-12-15 22:25:40 +11:00
|
|
|
@Override
|
2013-07-09 23:09:28 +09:00
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
2015-01-06 06:55:01 +01:00
|
|
|
if (msg instanceof HttpRequest) {
|
|
|
|
handleHttpRequest(ctx, (HttpRequest) msg);
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
} else if (msg instanceof WebSocketFrame) {
|
|
|
|
handleWebSocketFrame(ctx, (WebSocketFrame) msg);
|
2013-11-15 12:00:33 +01:00
|
|
|
} else {
|
2014-04-24 21:13:19 +09:00
|
|
|
throw new IllegalStateException("unknown message: " + msg);
|
2011-12-15 22:25:40 +11:00
|
|
|
}
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-09 23:09:28 +09:00
|
|
|
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
ctx.flush();
|
2011-12-15 22:25:40 +11:00
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2015-01-06 06:55:01 +01:00
|
|
|
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req)
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 20:40:19 +09:00
|
|
|
throws Exception {
|
2012-09-28 15:42:38 +09:00
|
|
|
// Handle a bad request.
|
2014-06-04 18:34:57 +09:00
|
|
|
if (!req.decoderResult().isSuccess()) {
|
2013-01-16 13:22:50 +09:00
|
|
|
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
|
2012-09-28 15:42:38 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-15 22:25:40 +11:00
|
|
|
// Allow only GET methods.
|
2014-06-24 17:39:46 +09:00
|
|
|
if (req.method() != GET) {
|
2013-01-16 13:22:50 +09:00
|
|
|
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
|
2011-12-15 22:25:40 +11:00
|
|
|
return;
|
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2011-12-15 22:25:40 +11:00
|
|
|
// Handshake
|
|
|
|
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
|
2012-07-19 12:40:54 +10:00
|
|
|
getWebSocketLocation(req), null, false, Integer.MAX_VALUE);
|
2012-05-29 16:41:26 -07:00
|
|
|
handshaker = wsFactory.newHandshaker(req);
|
|
|
|
if (handshaker == null) {
|
2014-01-12 16:28:03 -08:00
|
|
|
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
|
2011-12-15 22:25:40 +11:00
|
|
|
} else {
|
2012-05-29 16:41:26 -07:00
|
|
|
handshaker.handshake(ctx.channel(), req);
|
2011-12-15 22:25:40 +11:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
|
Remove MessageList from public API and change ChannelInbound/OutboundHandler accordingly
I must admit MesageList was pain in the ass. Instead of forcing a
handler always loop over the list of messages, this commit splits
messageReceived(ctx, list) into two event handlers:
- messageReceived(ctx, msg)
- mmessageReceivedLast(ctx)
When Netty reads one or more messages, messageReceived(ctx, msg) event
is triggered for each message. Once the current read operation is
finished, messageReceivedLast() is triggered to tell the handler that
the last messageReceived() was the last message in the current batch.
Similarly, for outbound, write(ctx, list) has been split into two:
- write(ctx, msg)
- flush(ctx, promise)
Instead of writing a list of message with a promise, a user is now
supposed to call write(msg) multiple times and then call flush() to
actually flush the buffered messages.
Please note that write() doesn't have a promise with it. You must call
flush() to get notified on completion. (or you can use writeAndFlush())
Other changes:
- Because MessageList is completely hidden, codec framework uses
List<Object> instead of MessageList as an output parameter.
2013-07-08 19:03:40 +09:00
|
|
|
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
2013-02-11 20:17:35 +09:00
|
|
|
if (logger.isLoggable(Level.FINE)) {
|
|
|
|
logger.fine(String.format(
|
2013-11-04 19:42:33 +09:00
|
|
|
"Channel %s received %s", ctx.channel().hashCode(), StringUtil.simpleClassName(frame)));
|
2012-02-17 10:37:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-15 22:25:40 +11:00
|
|
|
if (frame instanceof CloseWebSocketFrame) {
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 20:40:19 +09:00
|
|
|
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
|
2011-12-15 22:25:40 +11:00
|
|
|
} else if (frame instanceof PingWebSocketFrame) {
|
2014-04-24 21:13:19 +09:00
|
|
|
ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()));
|
2014-06-04 18:34:57 +09:00
|
|
|
} else if (frame instanceof TextWebSocketFrame ||
|
|
|
|
frame instanceof BinaryWebSocketFrame ||
|
|
|
|
frame instanceof ContinuationWebSocketFrame) {
|
2014-04-24 21:13:19 +09:00
|
|
|
ctx.write(frame);
|
2011-12-15 22:25:40 +11:00
|
|
|
} else if (frame instanceof PongWebSocketFrame) {
|
Revamp the core API to reduce memory footprint and consumption
The API changes made so far turned out to increase the memory footprint
and consumption while our intention was actually decreasing them.
Memory consumption issue:
When there are many connections which does not exchange data frequently,
the old Netty 4 API spent a lot more memory than 3 because it always
allocates per-handler buffer for each connection unless otherwise
explicitly stated by a user. In a usual real world load, a client
doesn't always send requests without pausing, so the idea of having a
buffer whose life cycle if bound to the life cycle of a connection
didn't work as expected.
Memory footprint issue:
The old Netty 4 API decreased overall memory footprint by a great deal
in many cases. It was mainly because the old Netty 4 API did not
allocate a new buffer and event object for each read. Instead, it
created a new buffer for each handler in a pipeline. This works pretty
well as long as the number of handlers in a pipeline is only a few.
However, for a highly modular application with many handlers which
handles connections which lasts for relatively short period, it actually
makes the memory footprint issue much worse.
Changes:
All in all, this is about retaining all the good changes we made in 4 so
far such as better thread model and going back to the way how we dealt
with message events in 3.
To fix the memory consumption/footprint issue mentioned above, we made a
hard decision to break the backward compatibility again with the
following changes:
- Remove MessageBuf
- Merge Buf into ByteBuf
- Merge ChannelInboundByte/MessageHandler and ChannelStateHandler into ChannelInboundHandler
- Similar changes were made to the adapter classes
- Merge ChannelOutboundByte/MessageHandler and ChannelOperationHandler into ChannelOutboundHandler
- Similar changes were made to the adapter classes
- Introduce MessageList which is similar to `MessageEvent` in Netty 3
- Replace inboundBufferUpdated(ctx) with messageReceived(ctx, MessageList)
- Replace flush(ctx, promise) with write(ctx, MessageList, promise)
- Remove ByteToByteEncoder/Decoder/Codec
- Replaced by MessageToByteEncoder<ByteBuf>, ByteToMessageDecoder<ByteBuf>, and ByteMessageCodec<ByteBuf>
- Merge EmbeddedByteChannel and EmbeddedMessageChannel into EmbeddedChannel
- Add SimpleChannelInboundHandler which is sometimes more useful than
ChannelInboundHandlerAdapter
- Bring back Channel.isWritable() from Netty 3
- Add ChannelInboundHandler.channelWritabilityChanges() event
- Add RecvByteBufAllocator configuration property
- Similar to ReceiveBufferSizePredictor in Netty 3
- Some existing configuration properties such as
DatagramChannelConfig.receivePacketSize is gone now.
- Remove suspend/resumeIntermediaryDeallocation() in ByteBuf
This change would have been impossible without @normanmaurer's help. He
fixed, ported, and improved many parts of the changes.
2013-05-28 20:40:19 +09:00
|
|
|
frame.release();
|
2011-12-15 22:25:40 +11:00
|
|
|
// Ignore
|
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass()
|
|
|
|
.getName()));
|
|
|
|
}
|
2013-02-20 07:05:35 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 13:22:50 +09:00
|
|
|
private static void sendHttpResponse(
|
2015-01-06 06:55:01 +01:00
|
|
|
ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) {
|
2014-06-24 17:39:46 +09:00
|
|
|
// Generate an error page if response status code is not OK (200).
|
|
|
|
if (res.status().code() != 200) {
|
|
|
|
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
|
2013-07-14 22:50:53 +02:00
|
|
|
res.content().writeBytes(buf);
|
|
|
|
buf.release();
|
2013-05-01 17:04:43 +09:00
|
|
|
setContentLength(res, res.content().readableBytes());
|
2011-12-15 22:25:40 +11:00
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2011-12-15 22:25:40 +11:00
|
|
|
// Send the response and close the connection if necessary.
|
2013-07-10 13:00:42 +02:00
|
|
|
ChannelFuture f = ctx.channel().writeAndFlush(res);
|
2014-06-24 17:39:46 +09:00
|
|
|
if (!isKeepAlive(req) || res.status().code() != 200) {
|
2011-12-15 22:25:40 +11:00
|
|
|
f.addListener(ChannelFutureListener.CLOSE);
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2011-12-15 22:25:40 +11:00
|
|
|
@Override
|
2012-06-07 14:52:33 +09:00
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
2012-05-29 16:41:26 -07:00
|
|
|
ctx.close();
|
2011-12-15 22:25:40 +11:00
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
|
2015-01-06 06:55:01 +01:00
|
|
|
private static String getWebSocketLocation(HttpRequest req) {
|
2014-10-31 16:48:28 +09:00
|
|
|
return "ws://" + req.headers().get(HttpHeaderNames.HOST);
|
2011-12-15 22:25:40 +11:00
|
|
|
}
|
2011-10-17 15:12:37 +11:00
|
|
|
}
|