예제 #1
0
        Tell(IEntityMessage message, Type messageType)
        {
            var invoker =
#if ECS_SERVER
                (Func <IComponentIntern, IEntityMessage, Task>
#else
                (Action <IComponentIntern, IEntityMessage>
#endif
                )GetOrAddMessageCaller(messageType);

            if (_messageHandlers.TryGetValue(messageType, out var handlers))
            {
                foreach (var handler in from h in handlers
                         orderby h.GetMessageOrder(message), _indexes[h]
                         select h)
                {
#if ECS_SERVER
                    await
#endif
                    invoker(handler, message);
                }
            }

#if ECS_SERVER
            await ClearOperationQueue();
#endif
        }
예제 #2
0
        TryAsk <TResponse>(IEntityMessage <TResponse> message)
        {
            var messageType = message.GetType();
            var invoker     =
#if ECS_SERVER
                (Func <IComponentIntern, IEntityMessage <TResponse>, Task <TResponse> >
#else
                (Func <IComponentIntern, IEntityMessage <TResponse>, TResponse>
#endif
                )GetOrAddMessageCaller(messageType);

            if (_messageHandlers.TryGetValue(messageType, out var handlers))
            {
                foreach (var handler in from h in handlers
                         orderby h.GetMessageOrder(message), _indexes[h]
                         select h)
                {
                    var response =
#if ECS_SERVER
                        await
#endif
                        invoker(handler, message);
#if ECS_SERVER
                    await ClearOperationQueue();
#endif
                    return(new AskResult <TResponse> {
                        Succeeded = true, Response = response
                    });
                }
            }

            return(AskResult <TResponse> .Failed);
        }
예제 #3
0
        Tell(IEntityMessage message)
        {
#if ECS_SERVER
            return
                (#endif
            Tell(message, message.GetType()));
        }
예제 #4
0
 public static string GetEntityText(this IEntityMessage msg)
 {
     Sandbox.Game.Entities.MyEntity entity;
     if (Sandbox.Game.Entities.MyEntities.TryGetEntityById(msg.GetEntityId(), out entity))
     {
         return(entity.ToString());
     }
     return("null");
 }
예제 #5
0
        public async Task <TResponse> Ask <TResponse>(IEntityMessage <TResponse> message)
        {
            var response = await TryAsk(message);

            if (!response.Succeeded)
            {
                throw new ReceiverNotFoundException();
            }
            return(response.Response);
        }
예제 #6
0
        /// <summary>
        ///     Send an entity message to other network handler.
        /// </summary>
        /// <param name="message">The message sent to others.</param>
        public void Sync(IEntityMessage message)
        {
            if (!Active)
            {
                return;
            }

            var bytes = WrapAndSerialize(message);

            MyAPIGateway.Multiplayer.SendMessageToOthers(_id, bytes);
        }
예제 #7
0
        /// <summary>
        ///     Sends an entity message to given recipient.
        /// </summary>
        /// <param name="message">The entity message send to recipient.</param>
        /// <param name="recipient">The recipient who should receive this message.</param>
        public void Send(IEntityMessage message, ulong recipient)
        {
            if (!Active)
            {
                return;
            }

            var bytes = WrapAndSerialize(message);

            MyAPIGateway.Multiplayer.SendMessageTo(_id, bytes, recipient);
        }
예제 #8
0
        /// <summary>
        ///     Wrap a message and serialize the wrapped message.
        /// </summary>
        /// <param name="message">The message that get wrapped.</param>
        /// <returns>Returns the serialized wrapper as a byte[].</returns>
        private byte[] WrapAndSerialize(IEntityMessage message)
        {
            var wrapper = new EntityMessageWrapper {
                EntityId = message.EntityId,
                Sender   = MyId,
                Content  = message.Serialze()
            };

            return(WrapAndSerialize(new EntityMessage {
                Wrapper = wrapper
            }));
        }
예제 #9
0
        Ask <TResponse>(IEntityMessage <TResponse> message)
        {
            var response =
#if ECS_SERVER
                await
#endif
                TryAsk(message);

            if (!response.Succeeded)
            {
                throw new ReceiverNotFoundException();
            }
            return(response.Response);
        }
예제 #10
0
        public async Task Tell(IEntityMessage message)
        {
            var messageType = message.GetType();
            var invoker     = (Func <IComponentIntern, IEntityMessage, Task>)GetOrAddMessageCaller(messageType);

            if (_messageHandlers.TryGetValue(messageType, out var handlers))
            {
                foreach (var handler in from h in handlers
                         orderby h.GetMessageOrder(message), _indexes[h]
                         select h)
                {
                    await invoker(handler, message);
                }
            }
        }
예제 #11
0
        public async Task <AskResult <TResponse> > TryAsk <TResponse>(IEntityMessage <TResponse> message)
        {
            var messageType = message.GetType();
            var invoker     = (Func <IComponentIntern, IEntityMessage <TResponse>, Task <TResponse> >)GetOrAddMessageCaller(messageType);

            if (_messageHandlers.TryGetValue(messageType, out var handlers))
            {
                foreach (var handler in from h in handlers
                         orderby h.GetMessageOrder(message), _indexes[h]
                         select h)
                {
                    return new AskResult <TResponse> {
                               Succeeded = true, Response = await invoker(handler, message)
                    }
                }
                ;
            }

            return(AskResult <TResponse> .Failed);
        }