public void Execute(IEventInput input, IEventCompleted callBackEvent) { if (input.ID == 0) { input.ID = GetInputID(); } EventOutput output = new EventOutput(); output.Token = input.Token; output.ID = input.ID; output.EventError = EventError.Success; EventActionHandler handler = GetActionHandler(input.EventPath); if (handler == null) { output.EventError = EventError.NotFound; output.Data = new object[] { $"Process event error {input.EventPath} not found!" }; if (EnabledLog(LogType.Warring)) { Log(LogType.Warring, $"[{input.ID}]{input.Token} Process event error {input.EventPath} not found!"); } callBackEvent.Completed(output); } else { OnExecute(input, output, handler, callBackEvent); } }
public EventActionHandlerContext(EventCenter server, IEventInput input, EventActionHandler handler, object controller, NextQueue nextQueue) { Input = input; EventCenter = server; Handler = handler; Controller = controller; NextQueue = nextQueue; }
public EventActionHandlerContext(EventCenter server, IEventInput input, EventActionHandler handler, object controller, NextQueue nextQueue, ActorCollection.ActorItem actorItem) { Input = input; EventCenter = server; Handler = handler; Controller = controller; NextQueue = nextQueue; ActorItem = actorItem; }
public void Execute(IEventInput input, IEventCompleted callBackEvent) { if (input.ID == 0) { input.ID = GetInputID(); } EventOutput output = new EventOutput(); output.Token = input.Token; output.ID = input.ID; output.EventError = EventError.Success; EventActionHandler handler = GetActionHandler(input.EventPath); if (handler == null) { output.EventError = EventError.NotFound; output.Data = new object[] { $"Process event error {input.EventPath} not found!" }; if (EnabledLog(LogType.Warring)) { Log(LogType.Warring, $"{input.Token} Process event error {input.EventPath} not found!"); } callBackEvent.Completed(output); } else { try { OnExecute(input, output, handler, callBackEvent); } catch (Exception e_) { output.EventError = EventError.InnerError; output.Data = new object[] { $"Process event {input.EventPath} error {e_.Message}" }; if (EnabledLog(LogType.Error)) { Log(LogType.Error, $"{input.Token} process event {input.EventPath} error {e_.Message}@{e_.StackTrace}"); } callBackEvent.Completed(output); } } }
public virtual void Executed(EventCenter center, EventActionHandler handler, IEventInput input, IEventOutput output) { }
public virtual bool Executing(EventCenter center, EventActionHandler handler, IEventInput input, IEventOutput output) { return(true); }
private void OnExecute(IEventInput input, EventOutput output, EventActionHandler handler, IEventCompleted callBackEvent) { var actorID = input.Properties?[ACTOR_TAG]; string actorPath = null; object controller = null; NextQueue nextQueue = null; if (string.IsNullOrEmpty(actorID)) { nextQueue = this.InputNextQueue.Next(this.NextQueueWaits); if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"{input.Token} Process event {input.EventPath}"); } controller = handler.Controller; if (handler.ThreadType == ThreadType.SingleQueue) { nextQueue = handler.GetNextQueue(input.Data); } if (!handler.SingleInstance) { controller = CreateController(handler.ControllerType); } } else { ActorCollection.ActorCollectionItem item; item = handler.Actors.Get(actorID); if (item == null) { actorPath = "/" + handler.ServiceName + "/" + actorID; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"{input.Token} {handler.ControllerType.Name}@{actorPath} create actor"); } item = new ActorCollection.ActorCollectionItem(); item.ActorID = actorID; item.Actor = CreateController(handler.Actors.ServiceType); item.ServiceName = handler.ServiceName; item.Interface = handler.Interface; item = handler.Actors.Set(actorID, item); item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; controller = item.Actor; if (controller is IActorState state) { state.ActorPath = actorPath; state.EventCenter = this; state.EventPath = input.EventPath; state.Token = input.Token; state.ActorInit(actorID); if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"{input.Token} {handler.ControllerType.Name}@{actorPath} actor initialized"); } } } else { item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; controller = item.Actor; if (controller is IActorState state) { state.EventPath = input.EventPath; state.Token = input.Token; } } nextQueue = item.NextQueue; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"{input.Token} Process event {input.EventPath} in /{item.ServiceName}/{item.ActorID} actor"); } } EventActionHandlerContext context = new EventActionHandlerContext(this, input, handler, controller, nextQueue); context.Execute(output, callBackEvent); }
private void OnRegister(ServiceAttribute attribute, Type type, object controller) { foreach (Type itype in attribute.Types) { if (!itype.IsInterface) { continue; } if (type.GetInterface(itype.Name) == null) { continue; } string serviceName = (attribute.Name ?? itype.Name); string url = "/" + serviceName + "/"; foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)) { try { if (string.Compare("Equals", method.Name, true) == 0 || string.Compare("GetHashCode", method.Name, true) == 0 || string.Compare("GetType", method.Name, true) == 0 || string.Compare("ToString", method.Name, true) == 0 || method.Name.IndexOf("set_") >= 0 || method.Name.IndexOf("get_") >= 0) { continue; } var resultType = method.ReturnType; if (resultType.BaseType == typeof(Task) || resultType == typeof(Task)) { Type serviceType = controller.GetType(); if (!mActorsCollection.TryGetValue(serviceType, out ActorCollection serviceCollection)) { serviceCollection = new ActorCollection(serviceType); mActorsCollection[serviceType] = serviceCollection; mActorsCollection[itype] = serviceCollection; } ActionAttribute aa = method.GetCustomAttribute <ActionAttribute>(false); var actionUrl = url + (aa == null ? method.Name : aa.Name); if (mActionHadlers.TryGetValue(actionUrl, out EventActionHandler handler)) { Log(LogType.Warring, $"{itype.Name}->{type.Name}.{method.Name} action already exists, can add ActionAttribute on the method"); } else { handler = new EventActionHandler(type, method, controller); handler.ServiceName = serviceName; handler.ActionName = (aa == null ? method.Name : aa.Name); handler.SingleInstance = attribute.SingleInstance; handler.Actors = serviceCollection; handler.Interface = itype; mActionHadlers[actionUrl] = handler; string threadUniqueID = ""; foreach (var index in handler.ThreadUniqueID) { if (!string.IsNullOrEmpty(threadUniqueID)) { threadUniqueID += "."; } threadUniqueID += handler.Parameters[index].ParameterInfo.Name; } Log(LogType.Info, $"Register {itype.Name}->{type.Name}@{method.Name} to {actionUrl}[ThreadType:{handler.ThreadType}|UniqueID:{threadUniqueID}]"); } } else { Log(LogType.Error, $"Register {itype.Name}->{type.Name}.{method.Name} error, method result type not a Task!"); } } catch (Exception e_) { Log(LogType.Error, $"Register {itype.Name}->{type.Name}@{method.Name} action error {e_.Message}@{e_.StackTrace}"); } } } }
private async void OnExecute(IEventInput input, EventOutput output, EventActionHandler handler, IEventCompleted callBackEvent) { try { string actorID = null; input.Properties?.TryGetValue(ACTOR_TAG, out actorID); string actorPath = null; object controller = null; NextQueue nextQueue = null; ActorCollection.ActorItem item = null; if (string.IsNullOrEmpty(actorID)) { nextQueue = this.InputNextQueue.Next(this.NextQueueWaits); if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} Process event {input.EventPath}"); } controller = handler.Controller; if (handler.ThreadType == ThreadType.SingleQueue) { nextQueue = handler.GetNextQueue(input.Data); } if (!handler.SingleInstance) { controller = CreateController(handler.ControllerType); } } else { item = handler.Actors.Get(actorID); if (item == null) { actorPath = "/" + handler.ServiceName + "/" + actorID; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} {handler.ControllerType.Name}@{actorPath} create actor"); } item = new ActorCollection.ActorItem(); item.ActorID = actorID; item.Actor = CreateController(handler.Actors.ServiceType); item.ServiceName = handler.ServiceName; item.Interface = handler.Interface; item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; item = handler.Actors.Set(actorID, item, out bool add); controller = item.Actor; if (controller is IActorState state) { state.ActorID = actorID; state.ActorPath = actorPath; state.EventCenter = this; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} {handler.ControllerType.Name}@{actorPath} actor initialized"); } } } else { item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; controller = item.Actor; } nextQueue = item.NextQueue; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} Process event {input.EventPath} in /{item.ServiceName}/{item.ActorID} actor"); } await item.Initialize(); } EventActionHandlerContext context = new EventActionHandlerContext(this, input, handler, controller, nextQueue, item); context.Execute(output, callBackEvent); } catch (Exception e_) { output.EventError = EventError.InnerError; output.Data = new object[] { $"Process event {input.EventPath} error {e_.Message}" }; if (EnabledLog(LogType.Error)) { Log(LogType.Error, $"[{input.ID}]{input.Token} process event {input.EventPath} error {e_.Message}@{e_.StackTrace}"); } callBackEvent.Completed(output); } }