public void QueueReceivedPacket(System.IO.BinaryReader br)
            {
                if (callback != null)
                {
                    //This requester has a callback, so it must reply a packet

                    int requestID = br.ReadInt32();

                    TReceive reply = callback(sendFactory.FromStream(br));

                    manager.SendReplyPacket(this, reply, requestID);
                    return;
                }
                //Otherwise, this is a reply to a request
                lock ( newPacketLock )
                {
                    // Wait until previous packet is processed
                    while (newestPacket != null)
                    {
                        Monitor.Wait(newPacketLock);
                    }

                    newestPacket = br;

                    Monitor.PulseAll(newPacketLock);
                }
                // This does not actually queue, another reason to reshape the IClientPacketTransporterNetworked interface
                //      to something more uniform
            }
Esempio n. 2
0
        public void TestGenerateFactoryProperties()
        {
            NetworkPacketFactoryCodeGenerater gen = new NetworkPacketFactoryCodeGenerater(
                System.Windows.Forms.Application.StartupPath + "\\Test\\Networking\\TestFactoriesGeneratedProperties.dll");

            INetworkPacketFactory <PacketPropertiesTest> factory = gen.GetFactory <PacketPropertiesTest>();

            gen.BuildFactoriesAssembly();

            PacketPropertiesTest p = new PacketPropertiesTest();

            p.Text  = "Hello";
            p.Getal = 38;

            MemoryStream memStrm = new MemoryStream();
            BinaryWriter bw      = new BinaryWriter(memStrm);
            BinaryReader br      = new BinaryReader(memStrm);

            factory.ToStream(bw, p);

            memStrm.Position = 0;

            PacketPropertiesTest pCheck = factory.FromStream(br);

            Assert.AreEqual(pCheck, p);
        }
Esempio n. 3
0
        public void TestGenerateFactoryFields()
        {
            NetworkPacketFactoryCodeGenerater gen = new NetworkPacketFactoryCodeGenerater(
                System.Windows.Forms.Application.StartupPath + "\\Test\\Networking\\TestFactoriesGeneratedFields.dll");

            INetworkPacketFactory <PacketFieldsTest> factory = gen.GetFactory <PacketFieldsTest>();

            gen.BuildFactoriesAssembly();

            PacketFieldsTest p = new PacketFieldsTest();

            p.Text       = "Hello";
            p.Getal      = 38;
            p.Boolean    = true;
            p.GetalFloat = 456f;
            p.Buffer     = new byte[] { 1, 1, 2, 3, 5, 7, 13, 21 };
            p.Guid       = Guid.NewGuid();
            p.Enum       = ByteEnum.Second;
            p.Array      = new int[] { 1, 3, 6, 10, 15, 21 };

            MemoryStream memStrm = new MemoryStream();
            BinaryWriter bw      = new BinaryWriter(memStrm);
            BinaryReader br      = new BinaryReader(memStrm);

            factory.ToStream(bw, p);

            memStrm.Position = 0;

            PacketFieldsTest pCheck = factory.FromStream(br);

            Assert.AreEqual(pCheck, p);
        }
 public T FromStream(BinaryReader reader)
 {
     if (internalFactory == null)
     {
         assignInternalFactory();
     }
     return(internalFactory.FromStream(reader));
 }
Esempio n. 5
0
        public void TestGenerateMultiple()
        {
            NetworkPacketFactoryCodeGenerater gen = new NetworkPacketFactoryCodeGenerater(
                Application.StartupPath + "\\Test\\Networking\\TestGenerateMultiple.dll");

            INetworkPacketFactory <PacketFieldsTest>     factory1 = gen.GetFactory <PacketFieldsTest>();
            INetworkPacketFactory <PacketPropertiesTest> factory2 = gen.GetFactory <PacketPropertiesTest>();

            gen.BuildFactoriesAssembly();

            PacketFieldsTest p1 = new PacketFieldsTest();

            p1.Text  = "Hello";
            p1.Getal = 38;

            MemoryStream memStrm; BinaryWriter bw; BinaryReader br;

            memStrm = new MemoryStream();
            bw      = new BinaryWriter(memStrm);
            br      = new BinaryReader(memStrm);
            factory1.ToStream(bw, p1);

            memStrm.Position = 0;

            PacketFieldsTest pCheck1 = factory1.FromStream(br);

            Assert.AreEqual(pCheck1, p1);


            PacketPropertiesTest p2 = new PacketPropertiesTest();

            p2.Text  = "Hello";
            p2.Getal = 38;
            memStrm  = new MemoryStream();
            bw       = new BinaryWriter(memStrm);
            br       = new BinaryReader(memStrm);
            factory2.ToStream(bw, p2);

            memStrm.Position = 0;

            PacketPropertiesTest pCheck2 = factory2.FromStream(br);

            Assert.AreEqual(pCheck2, p2);
        }
            public TReceive SendRequest(TSend packet)
            {
                int thisRequestID = nextRequestID;

                nextRequestID++;

                manager.SendRequestPacket(this, packet, thisRequestID);

                TReceive ret;

                lock ( newPacketLock )
                {
                    while (true)
                    {
                        if (newestPacket != null)
                        {
                            long startPos  = newestPacket.BaseStream.Position;
                            int  requestID = newestPacket.ReadInt32();
                            if (requestID == thisRequestID)
                            {
                                // Request received
                                ret = receiveFactory.FromStream(newestPacket);

                                newestPacket = null;
                                Monitor.PulseAll(newPacketLock);

                                return(ret);
                            }

                            newestPacket.BaseStream.Position = startPos;
                        }

                        Monitor.Wait(newPacketLock);
                    }
                }
            }
Esempio n. 7
0
            /// <summary>
            /// Internal use only!
            /// Should be thread safe!
            /// </summary>
            void IClientPacketTransporterNetworked.QueueReceivedPacket(BinaryReader br)
            {
                T packet = factory.FromStream(br);

                internalTransporter.QueueReceivedPacket(packet);
            }