示例#1
0
        private static ModPacket StartNewMessage(MessageIDs mID)
        {
            ModPacket packet = MainMod.GetPacket;

            packet.Write((byte)mID);
            return(packet);
        }
示例#2
0
        public static void GetMessage(BinaryReader reader, int whoAmI)
        {
            MessageIDs mID = (MessageIDs)reader.ReadByte();

            if (Main.netMode == 2)
            {
                Console.WriteLine("Received message: " + mID.ToString());
            }
            else
            {
                Main.NewText("Received message: " + mID.ToString());
            }
            switch (mID)
            {
            case MessageIDs.SpawnCompanionOnPlayer:
                GetSpawnCompanionOnPlayer(reader, whoAmI);
                return;

            case MessageIDs.DespawnCompanionOnPlayer:
                GetDespawnCompanionOnPlayer(reader, whoAmI);
                return;

            case MessageIDs.GuardianInventoryItem:
                GetGuardianInventoryItem(reader, whoAmI);
                return;

            case MessageIDs.GuardianEquippedItem:
                GetGuardianEquippedItem(reader, whoAmI);
                return;

            case MessageIDs.GuardianWhoAmIDUpdate:
                GetGuardianWhoAmIDUpdate(reader, whoAmI);
                return;

            case MessageIDs.GuardianMovementUpdate:
                GetGuardianMovementUpdate(reader, whoAmI);
                return;

            case MessageIDs.GuardianItemUseUpdate:
                GetGuardianItemUseUpdate(reader, whoAmI);
                return;

            case MessageIDs.GuardianHurt:
                GetGuardianHurt(reader, whoAmI);
                return;

            case MessageIDs.GuardianBuffUpdate:
                GetGuardianBuffUpdate(reader, whoAmI);
                return;
            }
        }
        protected Message?ReadMessage()
        {
            MessageIDs id = default;
            int        signature;
            int        size;

            byte[] data;

            using (MemoryMappedViewStream stream = sharedMemory.CreateViewStream()) {
                mutex.WaitOne();
                //Log($"{this} acquired mutex for read");

                BinaryReader reader = new BinaryReader(stream);
                BinaryWriter writer = new BinaryWriter(stream);

                id = (MessageIDs)reader.ReadByte();
                if (id == MessageIDs.Default)
                {
                    mutex.ReleaseMutex();
                    return(null);
                }

                //Make sure the message came from the other side
                signature = reader.ReadInt32();
                if (signature == lastSignature)
                {
                    mutex.ReleaseMutex();
                    return(null);
                }

                size = reader.ReadInt32();
                data = reader.ReadBytes(size);

                //Overwriting the first byte ensures that the data will only be read once
                stream.Position = 0;
                writer.Write((byte)0);

                mutex.ReleaseMutex();
            }


            Message message = new Message(id, data);

            if (message.ID != MessageIDs.SendState && message.ID != MessageIDs.SendHotkeyPressed)
            {
                Log($"{this} received {message.ID} with length {message.Length}");
            }

            return(message);
        }
 public Message(MessageIDs id, byte[] data)
 {
     ID     = id;
     Data   = data;
     Length = data.Length;
 }
 private bool IsHighPriority(MessageIDs ID) =>
 Attribute.IsDefined(typeof(MessageIDs).GetField(Enum.GetName(typeof(MessageIDs), ID)), typeof(HighPriorityAttribute));