mina nettty codec 防止粘包

  1. 云栖社区>
  2. 博客>
  3. 正文

mina nettty codec 防止粘包

curiousby 2017-04-21 10:29:00 浏览820
展开阅读全文

mina codec 防止粘包

 

 

 

 

我在头部 和尾部都插有字符,类似 大小端 

 

package com.baoy.code;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

/**
 * 
 * @author baoyou E-mail:curiousby@163.com
 * @version 2017年4月21日 上午10:35:05
 * desc:
 */
public class CodecFactory  implements ProtocolCodecFactory {

    private static ProtocolEncoder encoder = new CodeProtocolEncoder();
    private static ProtocolDecoder decoder = new CodeProtocolDecoder();

    @Override
    public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {
        return encoder;
    }

    @Override
    public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
        return decoder;
    }
}

 

 

 

package com.baoy.code;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

/**
 * 
 * @author baoyou E-mail:curiousby@163.com
 * @version 2017年4月21日 上午10:35:14
 * desc:
 */
public class CodeProtocolDecoder  extends CumulativeProtocolDecoder {

    @Override
    protected boolean doDecode(IoSession ioSession, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
        
        
        while (in.remaining() >= 4) {
            in.mark();
            byte[] sign = new byte[1];
            in.get(sign, 0, 1);
            if (sign[0] != (byte) 0xF0 ) {
                throw new Exception("Bad package header, need 0xF0");
            }
            
           
            byte[] lenBytes = new byte[2];
            in.get(lenBytes, 0, 2);
            short length = CodeProtocolDecoder.getShort(lenBytes, 0);
            if (length > in.remaining()) {
                in.reset();
                return false;
            } 
            byte[] buffer = new byte[length];
            in.get(buffer, 0, length);
            String msg = new String(buffer,"UTF-8");
            byte[] sign2 = new byte[1];
            in.get(sign2, 0, 1);
            if (sign2[0] != (byte) 0x80 ) {
                throw new Exception("Bad package tail, need 0x80");
            }
            out.write(msg);
        }
        
        return false;
    }
    
    
    public static short getShort(byte[] buff, int pos) throws Exception {
        if (pos > buff.length - 2 || pos < 0)
            throw new Exception("Socket buffer Overflow");

        int num = 0;
        for (int ix = 0; ix < 2; ++ix) {
            num <<= 8;
            num |= (buff[pos + ix] & 0xff);
        }
        return (short) num;
    }
    
}

 

 

package com.baoy.code;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * 
 * @author baoyou E-mail:curiousby@163.com
 * @version 2017年4月21日 上午10:35:19
 * desc:
 */
public class CodeProtocolEncoder  implements ProtocolEncoder {

    @Override
    public void encode(IoSession ioSession, Object msg, ProtocolEncoderOutput out) throws Exception {
 
        byte[] bytes =((String)msg).getBytes("UTF-8");
        IoBuffer buffer = IoBuffer.allocate(bytes.length + 4, false);
        buffer.put((byte) 0xF0);
        buffer.put((byte) (bytes.length >> 8));
        buffer.put((byte) (bytes.length));
        buffer.put(bytes, 0, bytes.length);
        buffer.put((byte) 0x80);
        buffer.flip();
        out.write(buffer);  
    }

    @Override
    public void dispose(IoSession ioSession) throws Exception {

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信捐助,加入it技术扣扣群),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

网友评论

登录后评论
0/500
评论
curiousby
+ 关注