Exemple #1
0
        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();
        }
Exemple #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();
        }