public static async Task Remove(Channel channel) { if (channel != null) { channels.TryRemove(channel.Id, out var se); if (se == null) { return; } LOGGER.Info("移除channel {}", channel.Id); var actor = await ActorManager.Get <ComponentActor>(channel.Id); if (actor != null) { if (actor is IChannel chl) { _ = actor.SendAsync(chl.OnDisconnect); } if (actor.TransformAgent <IChannel>(out var chAgent)) { _ = actor.SendAsync(chAgent.OnDisconnect); } } } }
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 void DispatchEvent(int evtType, Func <int, TActor, Type, Task <bool> > checkDispatchFunc, Param param = null) { lineActor.SendAsync(async() => { if (!eventHandlers.ContainsKey(evtType)) { return; } Event evt = new Event(); evt.EventId = evtType; evt.Data = param; var list = eventHandlers[evtType]; foreach (var evtInfo in list) { var actor = await ActorManager.Get <TActor>(evtInfo.ActorId); if (actor == null) { continue; } var info = evtInfo; //需要存个临时变量 _ = actor.SendAsync(async() => { try { var handler = HotfixMgr.GetInstance <IEventListener>(info.AgentHandler); var agentType = handler.GetType().BaseType.GenericTypeArguments[0]; if (agentType.GetInterface(typeof(IComponentActorAgent).FullName) != null) { //actor if (checkDispatchFunc != null && !(await checkDispatchFunc(evtType, actor, null))) { return; } await handler.InternalHandleEvent(actor.GetAgent(agentType), evt); } else if (agentType.GetInterface(typeof(IComponentAgent).FullName) != null) { //component var compType = agentType.BaseType.GenericTypeArguments[0]; if (checkDispatchFunc != null && !(await checkDispatchFunc(evtType, actor, compType))) { return; } var comp = await actor.GetComponent(compType); await handler.InternalHandleEvent(actor.GetAgent(agentType), evt); } } catch (Exception e) { LOGGER.Error(e.ToString()); } }, false); } }, false); }
public static async Task RemoveAll() { var taskList = new List <Task>(); var list = channels.Values; foreach (var ch in list) { _ = ch.Ctx.CloseAsync(); var actor = await ActorManager.Get <ComponentActor>(ch.Id); if (actor != null) { var task = actor.SendAsync(() => Task.Delay(1)); taskList.Add(task); } await Remove(ch); } //保证此函数执行完后所有actor队列为空 if (await Task.WhenAll(taskList).WaitAsync(TimeSpan.FromSeconds(30))) { LOGGER.Error("remove all channel timeout"); } }
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()); } }); }