Run() public static method

public static Run ( NetPeer peer ) : void
peer Lidgren.Network.NetPeer
return void
コード例 #1
0
ファイル: Program.cs プロジェクト: SabariKumar/MonopolyDeal
        static void Main(string[] args)
        {
            NetPeerConfiguration config = new NetPeerConfiguration("unittests");

            config.EnableUPnP = true;
            NetPeer peer = new NetPeer(config);

            peer.Start();             // needed for initialization

            Console.WriteLine("Unique identifier is " + NetUtility.ToHexString(peer.UniqueIdentifier));

            ReadWriteTests.Run(peer);

            NetQueueTests.Run();

            MiscTests.Run(peer);

            BitVectorTests.Run();

            EncryptionTests.Run(peer);

            var om = peer.CreateMessage();

            peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, 14242));
            try
            {
                peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, 14242));
            }
            catch (NetException nex)
            {
                if (nex.Message != "This message has already been sent! Use NetPeer.SendMessage() to send to multiple recipients efficiently")
                {
                    throw nex;
                }
            }

            peer.Shutdown("bye");

            // read all message
            NetIncomingMessage inc;

            while ((inc = peer.ReadMessage()) != null)
            {
                switch (inc.MessageType)
                {
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                    Console.WriteLine("Peer message: " + inc.ReadString());
                    break;

                case NetIncomingMessageType.Error:
                    throw new Exception("Received error message!");
                }
            }

            Console.WriteLine("Done");
            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            NetPeerConfiguration config = new NetPeerConfiguration("unittests");
            NetPeer peer = new NetPeer(config);

            peer.Start();             // needed for initialization

            System.Threading.Thread.Sleep(50);

            Console.WriteLine("Unique identifier is " + NetUtility.ToHexString(peer.UniqueIdentifier));

            ReadWriteTests.Run(peer);

            NetQueueTests.Run();

            MiscTests.Run(peer);

            BitVectorTests.Run();

            EncryptionTests.Run(peer);

            peer.Shutdown("bye");

            // read all message
            NetIncomingMessage inc;

            while ((inc = peer.ReadMessage()) != null)
            {
                switch (inc.MessageType)
                {
                case NetIncomingMessageType.DebugMessage:
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.WarningMessage:
                case NetIncomingMessageType.ErrorMessage:
                    Console.WriteLine("Peer message: " + inc.ReadString());
                    break;

                case NetIncomingMessageType.Error:
                    throw new Exception("Received error message!");
                }
            }

            Console.WriteLine("Done");
            Console.ReadKey();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            //Span<byte> src = new byte[3] { 254, 255, 255 };
            //Span<byte> dst = new byte[3];
            //NetBitWriter.CopyBits(src, 1, 17, dst, 1);

            // TODO: check use of GetAddressBytes and optimize with span

            //Span<byte> src = new byte[] { 255, 255 }; // 0b00110010, 0b00111100 };
            //Span<byte> dst = new byte[3] { 0, 0, 0 };
            //NetBitWriter.CopyBits(src, 0, 16, dst, 9);
            //string r =
            //    Convert.ToString(dst[0], 2).PadLeft(8, '0') + "_" +
            //    Convert.ToString(dst[1], 2).PadLeft(8, '0') + "_" +
            //    Convert.ToString(dst[2], 2).PadLeft(8, '0');
            //Console.WriteLine(r);

            NetQueueTests.Run();

            BitArrayTests.Run();

            var config = new NetPeerConfiguration("unittests");

            config.EnableMessageType(NetIncomingMessageType.UnconnectedData);

            var peer = new NetPeer(config);

            peer.Start();

            peer.UPnP.DiscoverAsync().ContinueWith(async(t) =>
            {
                if (t.Result.Status == UPnPStatus.Available)
                {
                    var upnp = t.Result.UPnP;
                    int port = 9000;

                    Console.WriteLine("forwarded: " + await upnp.ForwardPortAsync(port, port, "hello"));

                    await Task.Delay(3000);

                    Console.WriteLine("deleted: " + await upnp.DeleteForwardingRuleAsync(port));
                }
            });

            Console.WriteLine("Unique identifier is " + NetUtility.ToHexString(peer.UniqueIdentifier));

            ReadWriteTests.Run(peer);

            MiscTests.Run();

            NetStreamingMessageTest.Run();

            //NetStreamTests.Run();

            //EncryptionTests.Run(peer);

            Console.WriteLine();

            // create threads that read all messages to test concurrency
            int readTimeout = 5000;
            var readThreads = new List <Thread>();

            for (int i = 0; i < 100; i++)
            {
                var thread = new Thread(() =>
                {
                    void Peer_ErrorMessage(NetPeer sender, NetLogLevel level, in NetLogMessage message)
                    {
                        Console.WriteLine("Peer " + level + ": " + message.Code);
                    }
                    peer.DebugMessage   += Peer_ErrorMessage;
                    peer.WarningMessage += Peer_ErrorMessage;
                    peer.ErrorMessage   += Peer_ErrorMessage;

                    while (peer.TryReadMessage(readTimeout, out var message))
                    {
                        switch (message.MessageType)
                        {
                        case NetIncomingMessageType.Error:
                            throw new Exception("Received error!");

                        case NetIncomingMessageType.Data:
                            Console.WriteLine("Data: " + message.ReadString());
                            break;

                        case NetIncomingMessageType.UnconnectedData:
                            Console.WriteLine("UnconnectedData: " + message.ReadString());
                            break;

                        default:
                            Console.WriteLine(message.MessageType);
                            break;
                        }
                    }
                });
                thread.Start();
                readThreads.Add(thread);
            }

            var om = peer.CreateMessage("henlo from myself");

            peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, peer.Port));
            try
            {
                peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, peer.Port));

                Console.WriteLine(nameof(CannotResendException) + " check failed");
            }
            catch (CannotResendException)
            {
                Console.WriteLine(nameof(CannotResendException) + " check OK");
            }

            Console.WriteLine($"Waiting for messages with {readTimeout}ms timeout...");
            foreach (var thread in readThreads)
            {
                thread.Join();
            }

            Console.WriteLine();
            Console.WriteLine("Tests finished");
        }