第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

netty自定義Decoder用于自定義協(xié)議

標簽:
Java

在使用netty时由于自定义协议的分割符是在数据包体的头部,netty提供的DelimiterBasedFrameDecoder并不能满足我们的需求。


自定义的decode如下

package com.llvision.netty.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author yd
 * 2017/04/08
 */
public class CustomFrameDecoder  extends ByteToMessageDecoder {
    private final Logger logger= LoggerFactory.getLogger(CustomFrameDecoder.class);
    private static int HEADER_SIZE = 4;
    private final ByteBuf delimiter;
    private final int maxFrameLength;
    private static ByteBuf buf = Unpooled.buffer();

    public CustomFrameDecoder(int maxFrameLength,ByteBuf delimiter ) {
        this.delimiter = delimiter;
        this.maxFrameLength = maxFrameLength;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        Object decoded = this.decode(ctx, in);
        if(decoded != null) {
            out.add(decoded);
        }
    }

    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        in.markReaderIndex();
        int headerOffset = indexOf(in, delimiter);
        if (headerOffset < 0) {
            in.resetReaderIndex();
            return null;
        } else {
            in.skipBytes(headerOffset + 5);
        }
       int bodyLength=bodyLenght(headerOffset,in);
        if(in.readableBytes()<bodyLength){
            in.resetReaderIndex();
            return null;
        }else {
            in.readBytes(buf, bodyLength);
            ByteBuf frame = ctx.alloc().buffer(bodyLength);
            frame.writeBytes(buf);
            buf.clear();
            return frame;
        }
    }

    private static int indexOf(ByteBuf haystack, ByteBuf needle) {
        for(int i = haystack.readerIndex(); i < haystack.writerIndex(); ++i) {
            int haystackIndex = i;
            int needleIndex;
            for(needleIndex = 0; needleIndex < needle.capacity() && haystack.getByte(haystackIndex) == needle.getByte(needleIndex); ++needleIndex) {
                ++haystackIndex;
                if(haystackIndex == haystack.writerIndex() && needleIndex != needle.capacity() - 1) {
                    return -1;
                }
            }
            if(needleIndex == needle.capacity()) {
                return i - haystack.readerIndex();
            }
        }

        return -1;
    }

    /**
    * 功能:获取信息头中的长度
    * */
    private  int bodyLenght(int header,ByteBuf buf){
        int bodyLength=0;
        int headerIndex=header+1,headerEnd=header+4;
        for(;headerIndex<=headerEnd;headerIndex++){
            bodyLength*=10;
            bodyLength+=(int)buf.getByte(headerIndex)-48;
        }
        return bodyLength;
    }
}

但在使用该解码器用客户端施加压力时,可达到1s 2000个的数据并发量。当大于这个量时会方式解析崩溃的问题,猜测是Tcp粘包的问题。也可能是机器性能问题。还请解答

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消