Example #1
0
    public unsafe static int Main(string[] args)
    {
        try{
            const int intManaged = 1000;
            const int intNative  = 2000;
            const int intReturn  = 3000;
            const int stackGuard = 5000;

            Console.WriteLine("MarshalPointer_In");
            int       int1    = intManaged;
            int *     int1Ptr = &int1;
            HandleRef hr1     = new HandleRef(new Object(), (IntPtr)int1Ptr);
            Assert.AreEqual(intReturn, MarshalPointer_In(hr1, stackGuard), "The return value is wrong");
            Assert.AreEqual(intManaged, int1, "The parameter value is changed");

            Console.WriteLine("MarshalPointer_InOut");
            int       int2    = intManaged;
            int *     int2Ptr = &int2;
            HandleRef hr2     = new HandleRef(new Object(), (IntPtr)int2Ptr);
            Assert.AreEqual(intReturn, MarshalPointer_InOut(hr2, stackGuard), "The return value is wrong");
            Assert.AreEqual(intNative, int2, "The passed value is wrong");

            Console.WriteLine("MarshalPointer_Out");
            int       int3    = intManaged;
            int *     int3Ptr = &int3;
            HandleRef hr3     = new HandleRef(new Object(), (IntPtr)int3Ptr);
            Assert.AreEqual(intReturn, MarshalPointer_Out(hr3, stackGuard), "The return value is wrong");
            Assert.AreEqual(intNative, int3, "The passed value is wrong");

            // Note that this scenario will always pass in a debug build because all values
            // stay rooted until the end of the method.
            Console.WriteLine("TestNoGC");

            // Keep the int boxed and pinned to prevent it from getting collected.
            // That way, we can safely reference it from finalizers that run on shutdown.
            BoxedInt boxedInt = new BoxedInt();
            GCHandle.Alloc(boxedInt, GCHandleType.Normal);
            int *int4Ptr;
            fixed(int *tempIntPtr = &boxedInt.MyInt)
            {
                // Smuggle the pointer out of the fixed scope
                int4Ptr = tempIntPtr;
            }
            Console.WriteLine("2");
            *int4Ptr = intManaged;
            CollectableClass collectableClass = new CollectableClass(int4Ptr);
            HandleRef        hr4        = new HandleRef(collectableClass, (IntPtr)int4Ptr);
            Action           gcCallback = () => { Console.WriteLine("GC callback now"); GC.Collect(2, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); GC.Collect(2, GCCollectionMode.Forced); };
            Assert.AreEqual(intReturn, TestNoGC(hr4, gcCallback), "The return value is wrong");
            Console.WriteLine("Native code finished");

            return(100);
        } catch (Exception e) {
            Console.WriteLine($"Test Failure: {e}");
            return(101);
        }
    }
Example #2
0
        public BattleSceneConsole(List <Unit> party, List <Unit> monsters, BoxedInt numPotions)
        {
            // This is bad. Use an event bus instead.
            this.party      = party;
            this.monsters   = monsters;
            this.numPotions = numPotions;

            EventBus.LatestInstance.Subscribe(ChampionsEvent.PauseGame, (data) => this.IsGamePaused  = true);
            EventBus.LatestInstance.Subscribe(ChampionsEvent.ResumeGame, (data) => this.IsGamePaused = false);
        }
Example #3
0
 public AddResult AddUnique(T item)
 {
     if (set.ContainsKey(item))
     {
         return(AddResult.AlreadyContained);
     }
     else
     {
         set[item] = new BoxedInt();
         return(AddResult.AddedFirst);
     }
 }
Example #4
0
        public AddResult Add(T item)
        {
            BoxedInt count;

            if (set.TryGetValue(item, out count))
            {
                count.i++;
                return(AddResult.Added);
            }
            else
            {
                set[item] = new BoxedInt();
                return(AddResult.AddedFirst);
            }
        }
Example #5
0
        private void RegisterTagger(ITagger <T> tagger, IList <ITagger <T> > newTaggers)
        {
            if (tagger != null)
            {
                newTaggers.Add(tagger);
                if (!this.uniqueTaggers.TryGetValue(tagger, out var count))
                {
                    count = new BoxedInt();
                    this.uniqueTaggers.Add(tagger, count);

                    // We only want to subscribe once to the tags changed event
                    // (even if we get multiple instances of the same tagger).
                    tagger.TagsChanged += SourceTaggerTagsChanged;
                }

                ++(count.Value);
            }
        }
Example #6
0
        private void WriteObjectUnknown(object obj)
        {
            if (obj == null)
            {
                writer.Write((short)0);
                return;
            }
            var def          = WriteClassId(obj);
            var storage      = def.Meta.GetUnknownStorage(obj);
            var storageIndex = new BoxedInt();

            objStack.Push(obj);
            try {
                if (def.Fields.Count > 0)
                {
                    if (def.Fields[0].WriteFuncUnknown != null)
                    {
                        foreach (var d in def.Fields)
                        {
                            d.WriteFuncUnknown(obj, storage, storageIndex);
                        }
                    }
                    else
                    {
                        foreach (var d in def.Fields)
                        {
                            d.WriteFunc(obj);
                        }
                    }
                }
                writer.Write((short)0);
            }
            finally {
                objStack.Pop();
            }
        }
        /// <summary>
        /// Add sent messages from list of packets
        /// </summary>
        /// <param name="sent"></param>
        /// <returns>Amount of packages which failed to be decrypted</returns>
        private async Task <int> AddSentMessages(IList <StrippedDownEncryptedPacket> sent)
        {
            // try to link messages to contacts
            BoxedInt    boxedInt = new BoxedInt();
            List <Task> tasks    = new List <Task>(sent.Count);

            foreach (StrippedDownEncryptedPacket packet in sentPackets)
            {
                // skip if current user is both the sender and receiver
                if (packet.Receiver.Id == packet.Sender.Id)
                {
                    continue;
                }

                tasks.Add(Task.Run(() =>
                {
                    // find receiver in contact list
                    ContactPerson receiver = contactList.FirstOrDefault(c => c.Id == packet.Receiver.Id);
                    if (receiver != null)
                    {
                        if (packet.DataType == DataType.Message)
                        {
                            try
                            {
                                string message = Encoding.UTF8.GetString(HybridEncryption.Decrypt(packet.EncryptedPacket, AsymmetricEncryption.PublicKey, true));

                                lock (receiver.LockObject)
                                {
                                    receiver.Messages.Add(new Message
                                    {
                                        SenderName        = Client.UserName,
                                        SendTime          = packet.SendDateTime,
                                        MessageFromSender = message,
                                        DataType          = packet.DataType,
                                        Packet            = packet,
                                    });
                                }
                            }
                            catch (CryptoException)
                            {
                                lock (boxedInt)
                                {
                                    boxedInt.Integer++;
                                }
                            }
                        }
                        else
                        {
                            lock (receiver.LockObject)
                            {
                                receiver.Messages.Add(new Message
                                {
                                    SenderName        = receiver.UserName,
                                    SendTime          = packet.SendDateTime,
                                    MessageFromSender = $"This is a {packet.DataType}",
                                    DataType          = packet.DataType,
                                    Packet            = packet
                                });
                            }
                        }
                    }
                }));
            }

            // wait for all to finish
            await Task.WhenAll(tasks);

            return(boxedInt.Integer);
        }
        /// <summary>
        /// Add received messages from list of packets
        /// </summary>
        /// <param name="received"></param>
        /// <returns>Amount of packages which failed to be decrypted</returns>
        private async Task <int> AddReceivedMessages(IList <StrippedDownEncryptedPacket> received)
        {
            // try to link messages to contacts
            BoxedInt    boxedInt = new BoxedInt();
            List <Task> tasks    = new List <Task>(received.Count);

            foreach (StrippedDownEncryptedPacket packet in received)
            {
                // find sender in contact list
                tasks.Add(Task.Run(() =>
                {
                    ContactPerson sender = contactList.FirstOrDefault(c => c.Id == packet.Sender.Id);
                    if (sender != null)
                    {
                        // ignore duplicates
                        if (sender.Messages.All(m => m.SendTime != packet.SendDateTime))
                        {
                            if (packet.DataType == DataType.Message)
                            {
                                try
                                {
                                    string message = Encoding.UTF8.GetString(HybridEncryption.Decrypt(packet.EncryptedPacket, AsymmetricEncryption.PublicKeyFromXml(sender.PublicKey)));

                                    // add new message to chat
                                    lock (sender.LockObject)
                                    {
                                        sender.Messages.Add(new Message
                                        {
                                            SenderName        = sender.UserName,
                                            SendTime          = packet.SendDateTime,
                                            MessageFromSender = message,
                                            DataType          = packet.DataType,
                                            Packet            = packet,
                                            Sender            = sender
                                        });
                                    }
                                }
                                catch (CryptoException)
                                {
                                    lock (boxedInt)
                                    {
                                        boxedInt.Integer++;
                                    }
                                }
                            }
                            else
                            {
                                lock (sender.LockObject)
                                {
                                    sender.Messages.Add(new Message
                                    {
                                        SenderName        = sender.UserName,
                                        SendTime          = packet.SendDateTime,
                                        MessageFromSender = $"This is a {packet.DataType}",
                                        DataType          = packet.DataType,
                                        Packet            = packet,
                                        Sender            = sender
                                    });
                                }
                            }
                        }
                    }
                }));
            }

            await Task.WhenAll(tasks);

            return(boxedInt.Integer);
        }