暧瓠子

想赚积分都不知道从何下手,好久没写东西了

MINA通信enCode实现

public class SocketServerEncoder extends ProtocolEncoderAdapter {
private static final Logger log = Logger
.getLogger(SocketServerEncoder.class);
private Charset charset = null;


public SocketServerEncoder(Charset charset) {
this.charset = charset;
log.debug("Server端#SocketServerEncoder构造方法,当前线程:"
+ Thread.currentThread().getId());
}


public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {
if (message instanceof MessageInfo) {
MessageInfo hmi = (MessageInfo) message;
if (null != hmi) {
IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
buf.order(ByteOrder.LITTLE_ENDIAN);
String smsgbody = hmi.getMsgBody();
byte[] bytearr = smsgbody.getBytes(charset);
hmi.setMsgBodySize((short) bytearr.length);
buf.putShort(hmi.getMsgBodySize());
buf.putShort(hmi.getMsgIdentify());
buf.put(bytearr);
buf.flip();
log.debug("Server端#encode#MessageInfo#消息大小:" + buf.remaining());
out.write(buf);
out.flush();
}
}else{
log.error("Server端#encode#未知的消息类型!");
IoBuffer buf = IoBuffer.allocate(10).setAutoExpand(true);
buf.order(ByteOrder.LITTLE_ENDIAN);
String msgBody="未知的消息编码类型";
byte[] bytearr=msgBody.getBytes(charset);
buf.putShort((short)bytearr.length);
buf.putShort((short)0);//未知的消息类型
buf.put(bytearr);
buf.flip();
log.debug("Server端#encode#重造消息大小:" + buf.remaining());
out.write(buf);
out.flush();
}
}
}
阅读更多
文章标签: 通信 session 数据
个人分类: 通讯
上一篇MINA通信deCode实现
下一篇LINUX经验汇总
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭