Socket通信自定义mina 框架过滤器解析(处理粘包、断包问题)

在与硬件打交道的通信中会经常遇到找不到合适的过滤器来接受服务传来的信息,为了解决这个问题,在网上找了很多资料,最后总结出一个自定义过滤器。我现在要用的是接收byte[]。
自定义一个过滤器只需要对应继承的三个文件

  • ProtocolCodecFactory
  • ProtocolEncoderAdapter
  • CumulativeProtocolDecoder

工厂就不用多说了,ProtocolEncoderAdapter 不做处理 直接输出

public class ByteArrayEncoder extends ProtocolEncoderAdapter {

    @Override
    public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
        out.write(message);
        out.flush();
    }
}

最重要的是继承 CumulativeProtocolDecoder的适配文件

@Override
    protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
        if (in.remaining() > 1) {// 前1字节是包头
            // 标记当前position的快照标记mark,以便后继的reset操作能恢复position位置
            in.mark();
            if (!isComplete) {
                byte[] l = new byte[header];
                //我的第一位是len位  
                in.get(l);
                // 取包体数据长度
                len =  l[0]&0xff; ; // 将byte转成INT
                System.out.println("Len:"+len);
            }
            // 注意上面的get操作会导致下面的remaining()值发生变化
            if (in.remaining() < len-1) {
                // 如果消息内容不够,则重置恢复position位置到操作前,进入下一轮, 接收新数据,以拼凑成完整数据
                in.reset();
                return false;
            } else {
                // 消息内容足够
                in.reset();// 重置恢复position位置到操作前

                int sumlen = len;
                byte[] packArr = new byte[sumlen];
                in.get(packArr, 0, sumlen);
                IoBuffer buffer = IoBuffer.allocate(sumlen);
                buffer.put(packArr);
                buffer.flip();
                out.write(buffer);
                buffer.free();
                if (in.remaining() > 0) {
                    // 如果读取一个完整包内容后还粘了包,就让父类再调用一次,进行下一次解析
                    return true;
                }

            }
        }
        return false;// 处理成功,让父类进行接收下个包
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
双向的TCP流量过滤软件,它允许您添加自定义正则表达式(正则表达式)过滤器。预置过滤包括:HTTP头信息,POST和GET数据,域名或即使*过滤*在任何连接传递的数据。 为了对付新的威胁,TCP过滤器包括一个强大的威胁检测引擎,用于检测和阻止黑洞,网络攻击,恶意URL和其他基于Web的威胁。 本软件会经常更新数据库,为了您的安全。 [10-01-2012] v1.4.0.0 Added "Password Protect Websites" Password is saved encrypted Added "Status" TAB Added "Execute Action after X minutes of idle activity" (RegEx Rules) Added "Domain:" info in alert dialog (RegEx Rules) Added "URL:" info in alert dialog (RegEx Rules) Improved "Threats Detection Engine (TDE)" Optimized UI Added "Actions" links in Status TAB Added right-click option "Set Password" on Domains->Protected TAB Added right-click option "Options" on RegEx Rules TAB Block download of executable files Block download of PDF files with JavaScript code Block download of PDF files Block download of Java (JAR) files Block download of Wordpad (RTF) files Block download of Video (AVI, FLV, MPG, MOV) files Block download of Flash (SWF) files Block download of ZIP and RAR files Block download of Microsoft Word and Excel files Block a website by TLD Disable task manager when in stealth mode Added "Menu"->"Disable Task Manager" Added "Menu"->"Enable Task Manager" Disable cmd dos prompt when in stealth mode Lock all cdroms when in stealth mode Added "Menu"->"Disable CMD Dos Prompt" Added "Menu"->"Enable CMD Dos Prompt" Added "Menu"->"Lock CD-ROMs" Added "Menu"->"UnLock CD-ROMs" Block download of JavaScript (JS) files Block IRC traffic Block FTP traffic Added "Rules"->"ADS" TAB to manage regexes to block ADS links Added "Threats"->Process Behavioral Analysis (Block connections of suspicious processes) Added "Block all unknown websites" (allow only whitelisted domains) Block IMs traffic (MSN Messenger, Y! Messenger) Updated "Reset Settings" Disabled "Threats Detection Engine (TDE)" (will be available in final version) Minor fixes and optimizations

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值