@Override
protected boolean doDecode(IoSession session, IoBuffer buffer,
ProtocolDecoderOutput out) throws Exception {
int position = buffer.position();
//第一次读取数据的时候
if(session.getAttribute("fileName")==null){
session.setAttribute("fileName",buffer.getInt());
session.setAttribute("fileContent", buffer.getInt());
return false;
}
else{
int fileName = (Integer) session.getAttribute("fileName");
int fileContent = (Integer) session.getAttribute("fileContent");
//数据全部传输完,开始解码
if(buffer.limit()-buffer.position()>=fileContent+fileName){
byte[] nameByte = new byte[fileName];
byte[] contentByte = new byte[fileContent];
PictureBean picture = new PictureBean();
int start = buffer.position();
int limit = buffer.limit();
buffer.limit(start+fileName);
buffer.slice().get(nameByte);
picture.setFileName(nameByte);
buffer.position(start+fileName);
buffer.limit(limit);
buffer.slice().get(contentByte);
picture.setFileContent(contentByte);
session.removeAttribute("fileName");
session.removeAttribute("fileContent");
out.write(picture);
return true;
}
buffer.position(position);
return false;
}
}
刚刚学mina,用mina来传输文件可以了,客户端的encoder和服务端的Decoder是自定义的。
但是在想加入心跳的时候发现,心跳的内容会和业务的内容冲突,就是解码器那里很难把这两个完全分开。没用XMPP协议。想问下怎么不影响业务的情况下实现心跳