Mina+AMF3 解码粘包解决方法

废话少说上代码
AMF3Decoder.java

package com.eunut.server.filter;

import java.io.DataInputStream;
import java.util.zip.InflaterInputStream;

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;

import com.eunut.server.vo.RequestMessage;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;

public class AMF3Decoder extends CumulativeProtocolDecoder
{
private final SerializationContext context = new SerializationContext();
private Amf3Input amf3in;
public AMF3Decoder()
{
}

@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception
{
long sid = session.getId();
if(in.prefixedDataAvailable(4)){
amf3in = new Amf3Input(context);
// 正常读取包头制定长度数据
int len = in.getInt();
Object message = null;
byte bytes[] = new byte[len];
in.get(bytes, 0, len);
IoBuffer tempIn = IoBuffer.wrap(bytes);
try {
amf3in.setInputStream(new InflaterInputStream(new DataInputStream(tempIn.asInputStream())));
message = amf3in.readObject();
if (message != null && (message instanceof RequestMessage)) {
out.write(message);
amf3in.close();
amf3in = null;
} else {
System.out.println(sid+":不是amf3 RequestMessage 跳出");
}
} catch (Exception e) {
System.out.println(sid+":AMF3Decoder doDecode Exception:" + e);
return false;
}finally{
tempIn.free();
tempIn = null;
bytes = null;
}
return true;
}else{
System.err.println("又粘包了!");
return false;
}
}
}

8 Responses to Mina+AMF3 解码粘包解决方法

  1. 施 常亮 says:

    :mrgreen: 这个问题困扰了我一天,结果只要每次执行doDecode初始化一次amf3in,用完直接关闭,就完活。。。。
    睡觉喽。。。 😆

  2. Hi i am kavin, its my first occasion to commenting anywhere,
    when i read this article i thought i could also make comment due to this
    good article.

  3. I almost never drop comments, but i did a few searching
    and wound up here Mina+AMF3 解码粘包解决方法

  4. I’m curious to find out what blog platform you have been utilizing?
    I’m experiencing some small security issues with my latest website and I would like to find something more safeguarded.
    Do you have any solutions?

    Here is my webpage watch or download belle

  5. With havin so much content and articles do you ever run into any issues of plagorism or
    copyright infringement? My website has a lot of completely unique content I’ve either written myself
    or outsourced but it looks like a lot of it is popping it up all over the
    internet without my authorization. Do you know any methods
    to help stop content from being ripped off? I’d certainly appreciate it.

  6. Zombie Parkour RunnerThis may also acquire extra outfit plague inc. cheats on your motile phones.
    Mobiles will be familiar. There are some unfortunate situations which compel
    you to suspend the game much, you can test
    your skill levels and bosses.

  7. What’s Happening i am new to this, I stumbled upon this I have discovered
    It positively helpful and it has helped me out loads. I
    hope to contribute & assist different users like its aided me.

    Great job.

  8. aetherforce.com

    Mina+AMF3 解码粘包解决方法 « EUNUT – 生活路上点点滴

Leave a Reply

Your email address will not be published. Required fields are marked *