mina nettty codec 防止粘包

mina codec 防止粘包

 
mina  nettty 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技术扣扣群),没钱捧个人场,谢谢各位。


mina  nettty codec 防止粘包mina  nettty codec 防止粘包mina  nettty codec 防止粘包
 
 
 谢谢您的赞助,我会做的更好!

 

相关推荐