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 }
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); }
Tell(IEntityMessage message) { #if ECS_SERVER return (#endif Tell(message, message.GetType())); }
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"); }
public async Task <TResponse> Ask <TResponse>(IEntityMessage <TResponse> message) { var response = await TryAsk(message); if (!response.Succeeded) { throw new ReceiverNotFoundException(); } return(response.Response); }
/// <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); }
/// <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); }
/// <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 })); }
Ask <TResponse>(IEntityMessage <TResponse> message) { var response = #if ECS_SERVER await #endif TryAsk(message); if (!response.Succeeded) { throw new ReceiverNotFoundException(); } return(response.Response); }
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); } } }
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); }