diff --git a/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyServer.java b/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyServer.java index e2f00637f..8c7aaa397 100644 --- a/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyServer.java +++ b/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyServer.java @@ -94,7 +94,7 @@ public NettyServer(InetSocketAddress addr, Configuration conf) { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline() .addLast("encoder", NettyFrameEncoder.INSTANCE) - .addLast(TransportFrameDecoder.HANDLER_NAME, NettyUtils.createFrameDecoder()) + .addLast("frameDecoder", NettyUtils.createFrameDecoder()) .addLast("decoder", NettyFrameDecoder.INSTANCE) .addLast("handler", new NettyServerMLHandler()); } diff --git a/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyTransceiver.java b/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyTransceiver.java index 403b3162f..ebcc6b560 100644 --- a/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyTransceiver.java +++ b/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyTransceiver.java @@ -110,7 +110,7 @@ public NettyTransceiver( protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline() .addLast("encoder", NettyFrameEncoder.INSTANCE) - .addLast(TransportFrameDecoder.HANDLER_NAME, NettyUtils.createFrameDecoder()) + .addLast("frameDecoder", NettyUtils.createFrameDecoder()) .addLast("decoder", NettyFrameDecoder.INSTANCE) .addLast( "readTimeout", diff --git a/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyUtils.java b/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyUtils.java index 316f3d154..ec2e07bfe 100644 --- a/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyUtils.java +++ b/angel-ps/core/src/main/java/com/tencent/angel/ipc/NettyUtils.java @@ -30,6 +30,8 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.util.concurrent.DefaultThreadFactory; import io.netty.util.internal.PlatformDependent; @@ -84,8 +86,13 @@ public static Class extends ServerChannel> getServerChannelClass(IOMode mode) * Creates a LengthFieldBasedFrameDecoder where the first 8 bytes are the length of the frame. * This is used before all decoders. */ - public static TransportFrameDecoder createFrameDecoder() { - return new TransportFrameDecoder(); + public static ByteToMessageDecoder createFrameDecoder() { + // maxFrameLength = 2G + // lengthFieldOffset = 0 + // lengthFieldLength = 8 + // lengthAdjustment = -8, i.e. exclude the 8 byte length itself + // initialBytesToStrip = 8, i.e. strip out the length field itself + return new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, -8, 8); } /** Returns the remote address on the channel or "<unknown remote>" if none exists. */ diff --git a/angel-ps/core/src/main/java/com/tencent/angel/ipc/TransportFrameDecoder.java b/angel-ps/core/src/main/java/com/tencent/angel/ipc/TransportFrameDecoder.java deleted file mode 100644 index 32d3452c8..000000000 --- a/angel-ps/core/src/main/java/com/tencent/angel/ipc/TransportFrameDecoder.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.tencent.angel.ipc; - -import java.util.LinkedList; - -import com.google.common.base.Preconditions; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.CompositeByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelInboundHandlerAdapter; - -/** - * A customized frame decoder that allows intercepting raw data. - *
- * This behaves like Netty's frame decoder (with harcoded parameters that match this library's - * needs), except it allows an interceptor to be installed to read data directly before it's - * framed. - *
- * Unlike Netty's frame decoder, each frame is dispatched to child handlers as soon as it's - * decoded, instead of building as many frames as the current buffer allows and dispatching - * all of them. This allows a child handler to install an interceptor if needed. - *
- * If an interceptor is installed, framing stops, and data is instead fed directly to the
- * interceptor. When the interceptor indicates that it doesn't need to read any more data,
- * framing resumes. Interceptors should not hold references to the data buffers provided
- * to their handle() method.
- */
-public class TransportFrameDecoder extends ChannelInboundHandlerAdapter {
-
- public static final String HANDLER_NAME = "frameDecoder";
- private static final int LENGTH_SIZE = 8;
- private static final int MAX_FRAME_SIZE = Integer.MAX_VALUE;
- private static final int UNKNOWN_FRAME_SIZE = -1;
-
- private final LinkedList