Example #1
0
        void EnqueueMessage <TData>(NetContext <TData> ctx)
            where TData : class
        {
            m_ReceivedMessages.Enqueue(Unsafe.As <NetContext>(ctx));

            // Awake itself when another thread enqueue a message in this queue
            m_Scheduler.AwakeActor(m_ActorRef);
        }
Example #2
0
        void OnEventMessage(NetContext <EventMessage <object> > ctx)
        {
            if (!m_Receivers.TryGetValue(ctx.Data.Data.GetType(), out var receivers))
            {
                return;
            }

            // Todo: need a forward function from NetComponent to be able to pool correctly this forwarded message
            m_Net.Send(receivers, ctx.Data);
        }
Example #3
0
 void ExecuteAction(Action <NetContext> action, NetContext ctx)
 {
     try
     {
         action(ctx);
     }
     catch (Exception ex)
     {
         // Todo let the user input an Action for logging when action crashes
         Debug.LogError($"Error while processing message {ctx.Data.GetType().Name} in actor {m_ActorRef.Type.Name} (from actor {ctx.SourceId.Type.Name}): {ex}");
     }
 }
Example #4
0
        void OnUnsubscribeFromEvent(NetContext <EventComponent.UnsubscribeFromEvent> ctx)
        {
            if (!m_Receivers.TryGetValue(ctx.Data.EventType, out var receivers))
            {
                Debug.LogWarning("No subscription");
                return;
            }

            if (!receivers.Remove(ctx.Data.ReceiverRef))
            {
                Debug.LogWarning("No subscription");
            }
        }
Example #5
0
        void OnSubscribeToEvent(NetContext <EventComponent.SubscribeToEvent> ctx)
        {
            if (!m_Receivers.TryGetValue(ctx.Data.EventType, out var receivers))
            {
                receivers = new List <ActorRef>();
                m_Receivers.Add(ctx.Data.EventType, receivers);
            }

            if (receivers.Contains(ctx.Data.ReceiverRef))
            {
                Debug.LogWarning("Already subscribed");
                return;
            }

            receivers.Add(ctx.Data.ReceiverRef);
        }
        void OnEventMessage(NetContext <EventMessage <object> > ctx)
        {
            if (TryGetAction(ctx, out var action))
            {
                var eventCtx = Unsafe.As <EventContext>(new EventContext <object>(ctx));

                try
                {
                    action(eventCtx);
                }
                catch (Exception ex)
                {
                    Debug.LogException(ex);
                }
            }
            else
            {
                Debug.LogError($"Nobody has registered an action for data type {GetMessageType(ctx).Name}. Discarding event message.");
            }
        }
Example #7
0
 static Type GetDataType(NetContext <object> ctx)
 {
     return(ctx.Data.GetType());
 }
 public EventContext(NetContext <EventMessage <TData> > parentContext)
 {
     Ctx = parentContext;
 }
 static Type GetMessageType(NetContext <EventMessage <object> > ctx)
 {
     return(ctx.Data.GetType().GetGenericArguments()[0]);
 }
 bool TryGetAction(NetContext <EventMessage <object> > ctx, out Action <EventContext> action)
 {
     return(m_Actions.TryGetValue(ctx.Data.Data.GetType(), out action));
 }