encoder and decoder in client

This commit is contained in:
lyr90329 2014-08-10 11:30:46 +08:00
parent c3ed5db164
commit 98c83ea8c5
3 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package server;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import server.NetMsg;
import common.EMSGID;
public class MDecoder extends FrameDecoder
{
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
if (buffer.readableBytes() < 4)
{
return null;//(1)
}
int dataLength = buffer.getInt(buffer.readerIndex());
if (buffer.readableBytes() < dataLength + 4)
{
return null;//(2)
}
buffer.skipBytes(4);//(3)
int id = buffer.readInt();
byte[] decoded = new byte[dataLength-4];
buffer.readBytes(decoded);
NetMsg msg = new NetMsg(decoded, id);//(4)
msg.setMsgID(EMSGID.values()[id]);
return msg;
}
}

View File

@ -0,0 +1,29 @@
package server;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import server.NetMsg;
public class MEncoder extends OneToOneEncoder
{
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception
{
if (!(msg instanceof NetMsg))
{
return msg;//(1)
}
NetMsg res = (NetMsg)msg;
byte[] data = res.getBytes();
int dataLength = data.length+4;
ChannelBuffer buf = ChannelBuffers.dynamicBuffer();//(2)
buf.writeInt(dataLength);
buf.writeInt(res.msgID.ordinal());
buf.writeBytes(data);
return buf;//(3)
}
}

View File

@ -0,0 +1,55 @@
package server;
import messageBody.requestMsg.nr_Read_res;
import messageBody.requestMsg.nr_write_res;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import server.NetMsg;
import common.EMSGID;
public class MServerHandler extends SimpleChannelUpstreamHandler
{
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
{
if (!(e.getMessage() instanceof NetMsg))
{
return;
}
webSession.getInstance().addSession(e);
// NetMsg msg = (NetMsg)e.getMessage();
// if (msg.getMsgID() == EMSGID.nr_read_res)
// {
// nr_Read_res msgLite = msg.getMessageLite();
// System.out.println(System.nanoTime()-msgLite.getTime());
// }
// else if (msg.getMsgID() == EMSGID.nr_write_res){
// nr_write_res msgLite = msg.getMessageLite();
// System.out.println(System.nanoTime()-msgLite.getTime());
// }
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
Channel channel = e.getChannel();
webSession.getInstance().removeRequestChannel(channel);
channel.close();
}
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)throws Exception
{
webSession.getInstance().addRequestChannel(e.getChannel());
}
}