mina nettty codec prevents sticky packages

mina codec prevents sticky packages

 

 

 

 

I have characters inserted at the head and tail, similar to big and small 

 

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:[email protected]
 * @version on April 21, 2017 at 10:35:05 AM
 * 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:[email protected]
 * @version on April 21, 2017 at 10:35:14 AM
 * 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:[email protected]
 * @version on April 21, 2017 at 10:35:19 AM
 * 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 {

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay and WeChat donations, join the it technology buckle group), but have no money to support a personal field, thank you.



 
 
 Thank you for your sponsorship, I will do better!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449898&siteId=291194637