/* private ushort DomainId; // { get; } private ushort TypeId; // { get; } public MessageBase(ushort domainId, ushort typeId) { DomainId = domainId; TypeId = typeId; } */ public void Send(ulong destSteamId, MessageDestinationType destType) { Log.Trace("Sending message with domain " + _DomainId + " and type " + _TypeId, "Send"); MessageContainer container = new MessageContainer() { Body = ToBytes(), DestinationId = destSteamId, DestinationType = destType, DomainId = _DomainId, TypeId = _TypeId, Reliable = _Reliable }; container.Send(); }
/* * private ushort DomainId; // { get; } * private ushort TypeId; // { get; } * * public MessageBase(ushort domainId, ushort typeId) { * DomainId = domainId; * TypeId = typeId; * * } */ public void Send(ulong destSteamId, MessageDestinationType destType) { Log.Trace("Sending message with domain " + _DomainId + " and type " + _TypeId, "Send"); MessageContainer container = new MessageContainer() { Body = ToBytes(), DestinationId = destSteamId, DestinationType = destType, DomainId = _DomainId, TypeId = _TypeId, Reliable = _Reliable }; container.Send(); }
public static MessageContainer FromBytes(byte[] buffer) { VRage.ByteStream stream = new VRage.ByteStream(buffer, buffer.Length); MessageContainer message = new MessageContainer(); message.DomainId = stream.getUShort(); message.TypeId = stream.getUShort(); //Log.Trace("Deserialized MessageTypeId " + message.MessageTypeId, "ToBytes"); message.SourceId = stream.getUlong(); //Log.Trace("Deserialized SourceId " + message.SourceId, "ToBytes"); message.SourceType = (RunLocation)stream.getUShort(); //Log.Trace("Deserialized SourceTypeId " + message.SourceType, "ToBytes"); int length = (int)stream.getUShort(); //Log.Trace("Deserialized Body.Length " + length, "ToBytes"); message.Body = stream.getByteArray(length); //Log.Trace("Deserialized Body of length " + message.Body.Length, "ToBytes"); return message; }
private void HandleMessage(byte[] bytes) { Log.Info("Got message of size " + bytes.Length, "HandleMessage"); // Deserialize base message MessageContainer container = MessageContainer.FromBytes(bytes); using (Lock.AcquireSharedUsing()) { Dictionary <ushort, List <Action <MessageBase> > > handlersByType; if (!Handlers.TryGetValue(container.DomainId, out handlersByType)) { Log.Error("Failed to find handler for domain " + container.DomainId, "HandleMessage"); return; } List <Action <MessageBase> > handlers; if (!handlersByType.TryGetValue(container.TypeId, out handlers)) { Log.Error("Failed to find handler for type " + container.TypeId, "HandleMessage"); return; } Func <Byte[], MessageBase> ctr; if (!Constructors.TryGetValue(container.TypeId, out ctr)) { Log.Error("Failed to find constructor for type " + container.TypeId, "HandleMessage"); return; } MessageBase msg = ctr(container.Body); // should we have another try-catch so individual handlers // don't trip up others? foreach (var handler in handlers) { handler(msg); } } }
public static MessageContainer FromBytes(byte[] buffer) { VRage.ByteStream stream = new VRage.ByteStream(buffer, buffer.Length); MessageContainer message = new MessageContainer(); message.DomainId = stream.getUShort(); message.TypeId = stream.getUShort(); //Log.Trace("Deserialized MessageTypeId " + message.MessageTypeId, "ToBytes"); message.SourceId = stream.getUlong(); //Log.Trace("Deserialized SourceId " + message.SourceId, "ToBytes"); message.SourceType = (RunLocation)stream.getUShort(); //Log.Trace("Deserialized SourceTypeId " + message.SourceType, "ToBytes"); int length = (int)stream.getUShort(); //Log.Trace("Deserialized Body.Length " + length, "ToBytes"); message.Body = stream.getByteArray(length); //Log.Trace("Deserialized Body of length " + message.Body.Length, "ToBytes"); return(message); }