MINA2中的拆包组包的处理及一些方法

1.position

例:
position() 

第一次使用返回值为当前位置:0


position(8) 返回第8个字节以后的数据(包括第8个)可以和 limit 联合使用
如:
buffer.position(3); 
buffer.limit(7); 
ByteBuffer slice = buffer.slice(); 



再次调用 position() 返回:8


2.remaining

例:

ByteBuffer byt = ByteBuffer.allocate(128,false); 

第一次调用
byt.remaining(); 返回 128

A、使用:byt.putInt(5);或者byt.getInt()或者byt.get(字节数组)等方法

之后调用 byt.remaining(); 返回 124

B、但使用 byt.putInt(5,4);或者byt.getInt(0) 之后,调用方法
byt.remaining();返回 128


3.prefixedDataAvailable

prefixedDataAvailable(4) int

该方法很好用。判断前四字节的整型值是否大于等于整个缓冲区的数据。可以方便的判断一次
messageReceived 过来的数据是否完整。(前提是自己设计的网络通讯协议前四字节等于发送数据的长度)

prefixedDataAvailable(2) Short int


网上找到的例子:

 protected boolean doDecode(IoSession session, IoBuffer in, 
            ProtocolDecoderOutput out) throws Exception { 
        if (in.prefixedDataAvailable(4, Constants.MAX_COMMAND_LENGTH)) { 
            int length = in.getInt(); 
            byte[] bytes = new byte[length]; 
            in.get(bytes); 
            int commandNameLength = Constants.COMMAND_NAME_LENGTH; 
            byte[] cmdNameBytes = new byte[commandNameLength]; 
            System.arraycopy(bytes, 0, cmdNameBytes, 0, commandNameLength); 
            String cmdName = (new String(cmdNameBytes)).trim(); 
            AbstractTetrisCommand command = TetrisCommandFactory 
                .newCommand(cmdName); 
            if (command != null) { 
                byte[] cmdBodyBytes = new byte[length - commandNameLength]; 
                System.arraycopy(bytes, commandNameLength, cmdBodyBytes, 0, 
                    length - commandNameLength); 
                command.bodyFromBytes(cmdBodyBytes); 
                out.write(command); 
            } 
            return true; 
        } else { 
            return false; 
        } 
    } 


MINA中分段读取数据的方法:


2. ProtobufRPCRequestProtocolDecoder.java

package com.lizongbo.protobufrpc;

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

public class ProtobufRPCRequestProtocolDecoder implements ProtocolDecoder {

private static final AttributeKey BUF_BYTE = new AttributeKey(
ProtobufRPCRequestProtocolDecoder.class, "bufb");

public void decode(IoSession ioSession, IoBuffer ioBuffer,
ProtocolDecoderOutput protocolDecoderOutput) throws
Exception {
try {
IoBuffer bufTmp = null;
byte[] buf = (byte[]) ioSession.getAttribute(BUF_BYTE);
if (buf == null) {
System.out.println("没有尚未处理的数据" + ioBuffer.remaining());
bufTmp = ioBuffer;
} else {
System.out.println("合并尚未处理的数据" + ioBuffer.remaining());
bufTmp = IoBuffer.allocate(buf.length + ioBuffer.remaining());
bufTmp.setAutoExpand(true);
bufTmp.put(buf);
bufTmp.put(ioBuffer);
bufTmp.flip();
} while (bufTmp.remaining() >= 4
&& bufTmp.remaining() >= bufTmp.getInt(bufTmp.position())) { // 循环处理数据包
System.out.println("循环处理数据包");
int dataLen = bufTmp.getInt(bufTmp.position());
byte[] b = new byte[dataLen];
bufTmp.get(b);
ProtobufRPCRequest pak = new ProtobufRPCRequest();
pak.setReqByteLen(b.length);
pak.readFrom(b, 4);
System.out.println("往下传递");
protocolDecoderOutput.write(pak);
}
if (bufTmp.hasRemaining()) { // 如果有剩余的数据,则放入Session中
System.out.println("如果有剩余的数据,则放入Session中" + bufTmp.remaining());
byte[] tmpb = new byte[bufTmp.remaining()];
bufTmp.get(tmpb);
ioSession.setAttribute(BUF_BYTE, tmpb);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void dispose(IoSession session) throws Exception {
System.out.println("dispose");

}

public void finishDecode(IoSession session, ProtocolDecoderOutput out) throws
Exception {
System.out.println("finishDecode");
}

}

你可能感兴趣的:(apache,网络协议,Mina)