protected override void Decode(IChannelHandlerContext context, IByteBuffer byteBuffer, List <object> output) { if (byteBuffer.ReadableBytes >= HEAD_LENG) { int packLen = getLen(byteBuffer); if (byteBuffer.ReadableBytes >= packLen) { short flag = byteBuffer.ReadShort(); short len = byteBuffer.ReadShort(); len -= HEAD_LENG; short cmd = byteBuffer.ReadShort(); long playerId = byteBuffer.ReadLong(); int encryptId = byteBuffer.ReadInt(); PBMessage pb = new PBMessage(); pb.cmd = cmd; pb.playerId = playerId; byte[] data = new byte[len]; byteBuffer.ReadBytes(data); pb.data = data; output.Add(pb); } else { } } }
/// <summary> /// 进入的消息 /// 协议号一定要前后端对应 如果没有对应的handler则不会处理消息 /// </summary> /// <param name="context"></param> /// <param name="message"></param> protected override void ChannelRead0(IChannelHandlerContext context, object message) { PBMessage pbs = message as PBMessage; int cmd = pbs.cmd; if (handlerMap.ContainsKey(cmd)) { //Console.WriteLine("服务器接受到客户端消息 cmd ---> " + cmd); handlerMap[cmd].onExecute(context, pbs); } }
public override void onExecute(IChannelHandlerContext ctx, PBMessage pbs) { pb.SyncPlayerPosReq msg = ProtobufSerializer.DeSerialize <pb.SyncPlayerPosReq>(pbs.data); //服务器更新玩家坐标 updatePos(msg, pbs.playerId); //返回消息给客户端 广播 pb.SyncPlayerPosResp rtn = new pb.SyncPlayerPosResp(); rtn.playerId = pbs.playerId; rtn.dir = msg.dir; rtn.pos = msg.pos; rtn.speed = msg.speed; rtn.isKnock = msg.isKnock; rtn.utcTime = (long)TimerUtils.getMillTimer(); boradcast(rtn, 104, pbs.playerId); }
//请求创建玩家 public override void onExecute(IChannelHandlerContext ctx, PBMessage pbs) { pb.GetPlayerInfoMsg msg = ProtobufSerializer.DeSerialize <pb.GetPlayerInfoMsg>(pbs.data); //返回 long uid = MathUtils.UniqueID; string name = userName; PlayerInfoMsg rtn = getInfoMsg(uid, name, 2); Console.WriteLine("PlayerCreateControl msg.playerId " + rtn.playerId); //服务器创建实体 createRole(rtn); //通知客户端创建实体 send(ctx, 102, rtn, 0); // boardSuffRole(rtn); // boardPreRole(rtn.playerId, ctx); }
/// <summary> /// 处理消息 /// </summary> /// <param name="pbs"></param> public abstract void onExecute(IChannelHandlerContext ctx, PBMessage pbs);
public override void onExecute(IChannelHandlerContext ctx, PBMessage pbs) { }