protected override void ChannelRead0(IChannelHandlerContext ctx, IMessage msg) { if (!Settings.Ins.AppRunning) { return; } //直接在当前io线程处理 IEventLoop group = ctx.Channel.EventLoop; group.Execute(async() => { var handler = TcpHandlerFactory.GetHandler(msg.GetMsgId()); LOGGER.Debug($"-------------server msg {msg.GetMsgId()} {msg.GetType()}"); if (handler == null) { LOGGER.Error("找不到对应的handler " + msg.GetMsgId()); return; } //握手 var channel = ctx.Channel.GetAttribute(ChannelManager.Att_Channel).Get(); if (channel != null) { var actor = await ActorManager.Get <ComponentActor>(channel.Id); if (actor != null) { if (actor is IChannel ise) { _ = actor.SendAsync(ise.Hand); } if (actor.TransformAgent <IChannel>(out var seAgent)) { _ = actor.SendAsync(seAgent.Hand); } } } handler.Time = DateTime.Now; handler.Ctx = ctx; handler.Msg = msg; if (handler is TcpActorHandler actorHandler) { actorHandler.Actor = await actorHandler.CacheActor(); if (actorHandler.Actor != null) { await actorHandler.Actor.SendAsync(actorHandler.ActionAsync); } else { LOGGER.Error($"handler actor 为空 {msg.GetMsgId()} {handler.GetType()}"); } } else { await handler.ActionAsync(); } }); }
public async Task <bool> OnLoadSucceed(bool isReload) { try { HttpHandlerFactory.SetExtraHandlerGetter(HotfixMgr.GetHttpHandler); TcpHandlerFactory.SetExtraHandlerGetter(Geek.Server.Message.MsgFactory.Create, msgId => HotfixMgr.GetHandler <BaseTcpHandler>(msgId)); if (isReload) { //热更 LOGGER.Info("hotfix load success"); await ActorManager.ActorsForeach((actor) => { actor.SendAsync(actor.ClearCacheAgent, false); return(Task.CompletedTask); }); } else { //起服 if (!await Start()) { return(false); } } return(true); } catch (Exception e) { LOGGER.Fatal("OnLoadSucceed执行异常"); LOGGER.Fatal(e.ToString()); return(false); } }
/// <summary> /// 解析消息包 /// </summary> protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { try { //数据包长度为一个int至少4个字节 if (input.ReadableBytes < 4) { return; } //仅仅是Get没有Read int msgLen = input.GetInt(input.ReaderIndex); if (!CheckMsgLen(msgLen)) { context.CloseAsync(); return; } //如果接受数据不够,等待下次 if (input.ReadableBytes < msgLen) { return; } //在buffer中读取掉消息长度 input.ReadInt(); // 时间合法性检查 long time = input.ReadLong(); if (!CheckTime(context, time)) { context.CloseAsync(); return; } // 序列号检查 int order = input.ReadInt(); if (!CheckMagicNumber(context, order, msgLen)) { context.CloseAsync(); return; } //消息id int msgId = input.ReadInt(); byte[] msgData = null; msgData = new byte[msgLen - 20]; input.ReadBytes(msgData); var msg = TcpHandlerFactory.GetMsg(msgId); if (msg == null) { LOGGER.Error("消息ID:{} 找不到对应的Msg.", msgId); return; } else { if (msg.GetMsgId() == msgId) { msg.Deserialize(msgData); } else { LOGGER.Error("后台解析消息失败,注册消息id和消息无法对应.real:{0}, register:{1}", msg.GetMsgId(), msgId); return; } } output.Add(msg); } catch (Exception e) { LOGGER.Error(e, "解析数据异常," + e.Message + "\n" + e.StackTrace); } }
protected override void ChannelRead0(IChannelHandlerContext ctx, IMessage msg) { if (!Settings.Ins.AppRunning) { return; } //直接在当前io线程处理 IEventLoop group = ctx.Channel.EventLoop; group.Execute(async() => { try { var handler = TcpHandlerFactory.GetHandler(msg.GetMsgId()); LOGGER.Debug($"-------------get msg {msg.GetMsgId()} {msg.GetType()}"); if (handler == null) { LOGGER.Error("找不到对应的handler " + msg.GetMsgId()); return; } //握手 var channel = ctx.Channel.GetAttribute(ChannelManager.Att_Channel).Get(); if (channel != null) { var actor = await ActorManager.Get <ComponentActor>(channel.Id); if (actor != null) { if (actor is IChannel ise) { _ = actor.SendAsync(ise.Hand); } if (actor.TransformAgent <IChannel>(out var seAgent)) { _ = actor.SendAsync(seAgent.Hand); } } } handler.Time = DateTime.Now; handler.Ctx = ctx; handler.Msg = msg; if (handler.GetType().Assembly != msg.GetType().Assembly) { //仅热更瞬间有极小几率触发 LOGGER.Debug("热更过程替换msg和handler 重新构造msg让msg和handler来自同一个dll"); var data = msg.Serialize(); var newMsg = TcpHandlerFactory.GetMsg(msg.GetMsgId()); newMsg.Deserialize(data); handler.Msg = newMsg; } if (handler is TcpActorHandler actorHandler) { actorHandler.Actor = await actorHandler.CacheActor(); if (actorHandler.Actor != null) { await actorHandler.Actor.SendAsync(actorHandler.ActionAsync); } else { LOGGER.Error($"handler actor 为空 {msg.GetMsgId()} {handler.GetType()}"); } } else { await handler.ActionAsync(); } } catch (Exception e) { LOGGER.Error(e.ToString()); } }); }