mina nettty codec 防止粘包

简介: mina codec 防止粘包        我在头部 和尾部都插有字符,类似 大小端    package com.baoy.code; import org.apache.

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



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

 

 

目录
相关文章
|
5天前
|
JSON 移动开发 网络协议
数据拆散与黏连:深入剖析Netty中的半包与粘包问题
数据拆散与黏连:深入剖析Netty中的半包与粘包问题
12 0
|
4月前
|
设计模式 JSON 编解码
Netty使用篇:编解码器
Netty使用篇:编解码器
|
4月前
|
存储 网络协议 算法
Netty使用篇:半包&粘包
Netty使用篇:半包&粘包
|
10月前
|
缓存 移动开发 网络协议
Netty 中的粘包和拆包详解
Netty 中的粘包和拆包详解
4154 1
|
编解码 网络协议 算法
Netty(二)Netty编解码与粘包拆包
Netty(二)Netty编解码与粘包拆包
73 0
Netty(三)之数据之粘包拆包
Netty(三)之数据之粘包拆包
64 0
Netty(三)之数据之粘包拆包
|
网络协议
netty系列之:kequeue传输协议详解
在前面的章节中,我们介绍了在netty中可以使用kequeue或者epoll来实现更为高效的native传输方式。那么kequeue和epoll和NIO传输协议有什么不同呢? 本章将会以kequeue为例进行深入探讨。
netty系列之:protobuf在UDP协议中的使用
netty中提供的protobuf编码解码器可以让我们直接在netty中传递protobuf对象。同时netty也提供了支持UDP协议的channel叫做NioDatagramChannel。如果直接使用NioDatagramChannel,那么我们可以直接从channel中读写UDP对象:DatagramPacket。
|
移动开发 Unix Java
netty系列之:netty中常用的字符串编码解码器
字符串是我们程序中最常用到的消息格式,也是最简单的消息格式,但是正因为字符串string太过简单,不能附加更多的信息,所以在netty中选择的是使用byteBuf作为最底层的消息传递载体。 虽然底层使用的ByteBuf,但是对于程序员来说,还是希望能够使用这种最简单的字符串格式,那么有什么简单的方法吗?
|
编解码 缓存 安全
【Netty】ChannelHandler和codec
前面学习了Netty的codec框架,下面接着学习ChannelHandler与codec之间的关联。
101 0
【Netty】ChannelHandler和codec