当前位置: 代码迷 >> 高性能WEB开发 >> MINA 传 AMF3 的粘包 和短包有关问题
  详细解决方案

MINA 传 AMF3 的粘包 和短包有关问题

热度:872   发布时间:2012-02-14 19:19:19.0
MINA 传 AMF3 的粘包 和短包问题
客户端这样写

Java code

for (var i = 0; i < 100; i++ )
{
var bytes:ByteArray = new ByteArray();  
bytes.writeObject( {x:1, y:2 } );  
bytes.compress();  
_socket.writeBytes(bytes);  
_socket.flush(); 
}



服务端的代码是网上找的
其中AMF3Decoder 如下

Java code

public class AMF3Decoder extends CumulativeProtocolDecoder
{

    private final SerializationContext context = new SerializationContext();
    private final Amf3Input amf3in;

    public AMF3Decoder()
    {
        amf3in = new Amf3Input(context);
    }

    protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
    {
        boolean temp = false;
        try
        {
            in.position(0);
            amf3in.setInputStream(new InflaterInputStream(new DataInputStream(in.asInputStream())));
            Object message = amf3in.readObject();
            if (message instanceof ASObject)
            {
                out.write(message);
                in.free();
                temp = true;
            }
            else
            {
                in.free();
                temp = false;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return temp;
    }
}



粘包和断包的问题不会处理 
报这样的错误:

org.apache.mina.filter.codec.ProtocolDecoderException: java.lang.IllegalStateException: doDecode() can't return true when buffer is not consumed. (Hexdump: 78 DA E3 E2 66 64 AE 60 61 64 AE 64 61 62 04 00 07 08 01 1A 78 DA E3 E2 66 64 AE 60 61 ............................



  at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:180)
  at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:414)
  at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:49)
  at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:832)
  at org.apache.mina.core.filterchain.DefaultIoFilterChain$HeadFilter.messageReceived(DefaultIoFilterChain.java:616)
  at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:414)
  at org.apache.mina.core.filterchain.DefaultIoFilterChain.fireMessageReceived(DefaultIoFilterChain.java:408)
  at org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:578)
  at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:540)
  at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:532)
  at org.apache.mina.core.polling.AbstractPollingIoProcessor.access$400(AbstractPollingIoProcessor.java:58)
  at org.apache.mina.core.polling.AbstractPollingIoProcessor$Worker.run(AbstractPollingIoProcessor.java:857)
  at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:51)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalStateException: doDecode() can't return true when buffer is not consumed.
  at org.apache.mina.filter.codec.CumulativeProtocolDecoder.decode(CumulativeProtocolDecoder.java:176)
  at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:170)