package protocal;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
/**
* 自定义协议-解码器
* @author Administrator
*
*/
public class ProtocalDecoder implements ProtocolDecoder{
private final AttributeKey CONTEXT = new AttributeKey(this.getClass(), "context");
private final Charset charset;
private int maxPacklength = 100;
public int getMaxPacklength() {
return maxPacklength;
}
public void setMaxPacklength(int maxPacklength) {
if(maxPacklength<0){
try {
throw new IllegalAccessException("maxPacklength参数:"+maxPacklength);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
this.maxPacklength = maxPacklength;
}
public ProtocalDecoder() {
this(Charset.defaultCharset());
}
public ProtocalDecoder(Charset charset) {
this.charset = charset;
}
public Context getContext(IoSession session){
Context ctx=(Context) session.getAttribute(CONTEXT);
if(ctx==null){
ctx = new Context();
session.setAttribute(CONTEXT,ctx);
}
return ctx;
}
@Override
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
final int packHeadLength=5;
Context ctx = this.getContext(session);
ctx.append(in);
IoBuffer buf = ctx.getBuf();
buf.flip();
while(buf.remaining()>=packHeadLength){
buf.mark();
int length = buf.getInt();
byte flag = buf.get();
if(length<0||length>maxPacklength){
buf.reset();
break;
}else if(length>=packHeadLength&&length-packHeadLength<=buf.remaining()){
int oldLimit = buf.limit();
buf.limit(buf.position()+length-packHeadLength);
String content = buf.getString(ctx.getDecoder());
buf.limit(oldLimit);
ProtocalPack pakeage = new ProtocalPack(flag, content);
out.write(pakeage);
}else{//半包
buf.clear();
break;
}
}
if(buf.hasRemaining()){
IoBuffer temp = IoBuffer.allocate(maxPacklength).setAutoExpand(true);
temp.put(buf);
temp.flip();
buf.reset();
buf.put(temp);
}else{
buf.reset();
}
}
@Override
public void dispose(IoSession session) throws Exception {
Context ctx = (Context) session.getAttribute(CONTEXT);
if(ctx!=null){
session.removeAttribute(CONTEXT);
}
}
@Override
public void finishDecode(IoSession session, ProtocolDecoderOutput out)
throws Exception {
// TODO Auto-generated method stub
}
private class Context{
private final CharsetDecoder decoder;
private IoBuffer buf;
private Context() {
decoder= charset.newDecoder();
buf=IoBuffer.allocate(80).setAutoExpand(true);
}
public void append(IoBuffer in){
this.getBuf().put(in);
}
public void rest(){
decoder.reset();
}
public IoBuffer getBuf() {
return buf;
}
public void setBuf(IoBuffer buf) {
this.buf = buf;
}
public CharsetDecoder getDecoder() {
return decoder;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mina.zip (21个子文件)
mina
.project 380B
src
protocal
ProtocalFactory.java 861B
ProtocalDecoder.java 3KB
ProtocalClient.java 2KB
ProtocalEncoder.java 963B
ProtocalServer.java 2KB
ProtocalPack.java 922B
lib
mina-core-2.0.7.jar 630KB
slf4j-api-1.6.6.jar 26KB
.settings
org.eclipse.jdt.core.prefs 598B
.classpath 425B
bin
protocal
ProtocalPack.class 1KB
MyHeadler.class 1KB
ProtocalDecoder.class 5KB
ProtocalDecoder$Context.class 2KB
ProtocalServer.class 2KB
ProtocalClient$1.class 1KB
ProtocalFactory.class 1KB
ProtocalClient.class 3KB
Myhandler.class 1KB
ProtocalEncoder.class 2KB
共 21 条
- 1
资源评论
qianmozhi9228
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功