当前位置: 首页>>代码示例>>C#>>正文


C# NetPeer.CreateMessage方法代码示例

本文整理汇总了C#中Lidgren.Network.NetPeer.CreateMessage方法的典型用法代码示例。如果您正苦于以下问题:C# NetPeer.CreateMessage方法的具体用法?C# NetPeer.CreateMessage怎么用?C# NetPeer.CreateMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Lidgren.Network.NetPeer的用法示例。


在下文中一共展示了NetPeer.CreateMessage方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ToOutgoing

 internal NetOutgoingMessage ToOutgoing(NetPeer Sender)
 {
     NetOutgoingMessage msg = Sender.CreateMessage("__msg");
     StringBuilder sb = new StringBuilder();
     StringWriter writer = new StringWriter(sb);
     serializer.Serialize(writer, this);
     msg.Write(sb.ToString());
     return msg;
 }
开发者ID:hipolipolopigus,项目名称:SkyNet_Old,代码行数:9,代码来源:Packet.cs

示例2: Main

        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 = peer.WaitMessage(5000);
            while (inc != 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!");
                }

                inc = peer.ReadMessage();
            }

            Console.WriteLine("Done");
            Console.ReadKey();
        }
开发者ID:JamieH,项目名称:lidgren-network-gen3,代码行数:55,代码来源:Program.cs

示例3: GetNetOutgoingMessage

        /// <summary>
        /// Gets a <see cref="NetOutgoingMessage"/>.
        /// </summary>
        /// <param name="netPeer">The <see cref="NetPeer"/> instance that the message is to be used on.</param>
        /// <param name="size">The minimum initial size of the <see cref="NetOutgoingMessage"/> in bytes.</param>
        /// <returns>
        /// The <see cref="NetOutgoingMessage"/> instance to use.
        /// </returns>
        public static NetOutgoingMessage GetNetOutgoingMessage(NetPeer netPeer, int size)
        {
            // Round up to the next power-of-2 size (if not already there) to make allocations a bit more consistency-sized
            // and grabbing from the internal pool will hopefully be faster, even if it does mean over-allocating a bit more.
            // Also, never use less than 16 bytes since there is not much point in cluttering things up with tiny buffers.
            if (size < 16)
                size = 16;
            else
                size = BitOps.NextPowerOf2(size);

            // Grab the NetOutgoingMessage from the internal buffer
            return netPeer.CreateMessage(size);
        }
开发者ID:wtfcolt,项目名称:game,代码行数:21,代码来源:SocketHelper.cs

示例4: Run

		public static void Run(NetPeer peer)
		{
			//
			// Test XTEA
			//
			NetXtea xtea = new NetXtea("TopSecret");

			byte[] original = new byte[16];
			NetRandom.Instance.NextBytes(original);

			byte[] encrypted = new byte[original.Length];
			xtea.EncryptBlock(original, 0, encrypted, 0);
			xtea.EncryptBlock(original, 8, encrypted, 8);

			byte[] decrypted = new byte[original.Length];
			xtea.DecryptBlock(encrypted, 0, decrypted, 0);
			xtea.DecryptBlock(encrypted, 8, decrypted, 8);

			// compare!
			for (int i = 0; i < original.Length; i++)
				if (original[i] != decrypted[i])
					throw new NetException("XTEA fail!");

			Console.WriteLine("XTEA OK");

			NetOutgoingMessage om = peer.CreateMessage();
			om.Write("Hallon");
			om.Write(42);
			om.Write(5, 5);
			om.Write(true);
			om.Write("kokos");
			om.Encrypt(xtea);

			// convert to incoming message
			NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
			im.Decrypt(xtea);

			if (im.ReadString() != "Hallon")
				throw new NetException("fail");
			if (im.ReadInt32() != 42)
				throw new NetException("fail");
			if (im.ReadInt32(5) != 5)
				throw new NetException("fail");
			if (im.ReadBoolean() != true)
				throw new NetException("fail");
			if (im.ReadString() != "kokos")
				throw new NetException("fail");

			Console.WriteLine("Message encryption OK");
		}
开发者ID:AugustoAngeletti,项目名称:blockspaces,代码行数:50,代码来源:EncryptionTests.cs

示例5: RequestNATIntroduction

        public static void RequestNATIntroduction(IPEndPoint host, NetPeer peer)
        {
            if (host == null)
            {
                return;
            }

            if (m_masterServer == null)
                throw new Exception("Must connect to master server first!");

            NetOutgoingMessage om = peer.CreateMessage();
            om.Write((byte)2); // NAT intro request

            // write internal ipendpoint
            IPAddress addr = IPAddress.Parse(GetMyLocalIpAddress());
            om.Write(new IPEndPoint(addr, peer.Port));

            // write external address of host to request introduction to
            IPEndPoint hostEp = new IPEndPoint(host.Address, port);
            om.Write(hostEp);
            om.Write(peer.Configuration.AppIdentifier); // send the app id

            peer.SendUnconnectedMessage(om, m_masterServer);
        }
开发者ID:Clancey,项目名称:MonoGame,代码行数:24,代码来源:MonoGamerPeer.cs

示例6: checkForStatusUpdate

        public void checkForStatusUpdate(NetPeer netPeer, NetConnection connection, int team)
        {
            if (AmountChanged && ((team == Player.Me.Team && timeSinceStatusUpdate >= statusUpdateDelay) || Depleted))
            {
                timeSinceStatusUpdate = 0f;
                AmountChanged = false;

                NetOutgoingMessage msg = netPeer.CreateMessage();
                msg.Write(MessageID.RESOURCE_STATUS_UPDATE);
                msg.Write(ID);
                msg.Write((short)Amount);
                netPeer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
            }
        }
开发者ID:nubington,项目名称:rts,代码行数:14,代码来源:Resource.cs

示例7: CheckForStatusUpdate

        public void CheckForStatusUpdate(GameTime gameTime, NetPeer netPeer, NetConnection connection)
        {
            timeSinceStatusUpdate += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (timeSinceStatusUpdate >= statusUpdateDelay)
            {
                timeSinceStatusUpdate = 0f;

                if (Team == Player.Me.Team)
                {
                    NetOutgoingMessage msg = netPeer.CreateMessage();
                    msg.Write(MessageID.UNIT_STATUS_UPDATE);
                    msg.Write(ID);
                    msg.Write(Team);
                    msg.Write((short)Hp);
                    msg.Write(centerPoint.X);
                    msg.Write(centerPoint.Y);
                    msg.Write(Rotation);
                    msg.Write(IsIdle);

                    // send current command ID, -1 if none
                    if (Commands.Count > 0)
                        msg.Write(Commands[0].ID);
                    else
                        msg.Write((short)-1);

                    // send cargoAmount, 0 if not worker
                    WorkerNublet worker = this as WorkerNublet;
                    if (worker != null)
                        msg.Write(worker.CargoAmount);
                    else
                        msg.Write((short)0);

                    netPeer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
                }
                else if (HpChanged)
                {
                    HpChanged = false;

                    NetOutgoingMessage msg = netPeer.CreateMessage();
                    msg.Write(MessageID.UNIT_HP_UPDATE);
                    msg.Write(ID);
                    msg.Write(Team);
                    msg.Write((short)Hp);
                    netPeer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
                }
            }
        }
开发者ID:nubington,项目名称:rts,代码行数:48,代码来源:Unit.cs

示例8: HandleMessage

        protected override bool HandleMessage(NetPeer peer, NetIncomingMessage msg)
        {
            if (base.HandleMessage(peer, msg))
            {
                return true;
            }

            switch (msg.MessageType)
            {
                case NetIncomingMessageType.DiscoveryRequest:
                {
                    if (m_respondsToDiscovery)
                    {
                        NetOutgoingMessage message = peer.CreateMessage();

                        PostNotificationImmediately(NetworkNotifications.LocalClientDiscovered, message);
                        peer.SendDiscoveryResponse(message, msg.SenderEndPoint);
                        return true;
                    }

                    return false;
                }
            }

            return false;
        }
开发者ID:bbqchickenrobot,项目名称:atomic-bomberman-xna,代码行数:26,代码来源:Server.cs

示例9: RequestNATIntroduction

 public static void RequestNATIntroduction(IPEndPoint host, NetPeer peer)
 {
   if (host == null)
     return;
   if (MonoGamerPeer.m_masterServer == null)
     throw new Exception("Must connect to master server first!");
   NetOutgoingMessage message = peer.CreateMessage();
   message.Write((byte) 2);
   IPAddress address = IPAddress.Parse(MonoGamerPeer.GetMyLocalIpAddress());
   message.Write(new IPEndPoint(address, peer.get_Port()));
   IPEndPoint ipEndPoint = new IPEndPoint(host.Address, MonoGamerPeer.port);
   message.Write(ipEndPoint);
   message.Write(peer.get_Configuration().get_AppIdentifier());
   peer.SendUnconnectedMessage(message, MonoGamerPeer.m_masterServer);
 }
开发者ID:Zeludon,项目名称:FEZ,代码行数:15,代码来源:MonoGamerPeer.cs

示例10: Main

        static void Main(string[] args)
        {
            List<IPEndPoint[]> registeredHosts = new List<IPEndPoint[]>();

            NetPeerConfiguration config = new NetPeerConfiguration("masterserver");
            config.SetMessageTypeEnabled(NetIncomingMessageType.UnconnectedData, true);
            config.Port = CommonConstants.MasterServerPort;

            NetPeer peer = new NetPeer(config);
            peer.Start();

            // keep going until ESCAPE is pressed
            Console.WriteLine("Press ESC to quit");
            while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape)
            {
                NetIncomingMessage msg;
                while((msg = peer.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        case NetIncomingMessageType.UnconnectedData:
                            //
                            // We've received a message from a client or a host
                            //

                            // by design, the first byte always indicates action
                            switch ((MasterServerMessageType)msg.ReadByte())
                            {
                                case MasterServerMessageType.RegisterHost:
                                    // It's a host wanting to register its presence
                                    IPEndPoint[] eps = new IPEndPoint[]
                                    {
                                        msg.ReadIPEndpoint(), // internal
                                        msg.SenderEndpoint // external
                                    };
                                    Console.WriteLine("Got registration for host " + eps[1]);
                                    registeredHosts.Add(eps);
                                    break;

                                case MasterServerMessageType.RequestHostList:
                                    // It's a client wanting a list of registered hosts
                                    Console.WriteLine("Sending list of " + registeredHosts.Count + " hosts to client " + msg.SenderEndpoint);
                                    foreach (IPEndPoint[] ep in registeredHosts)
                                    {
                                        // send registered host to client
                                        NetOutgoingMessage om = peer.CreateMessage();
                                        om.Write(ep[0]);
                                        om.Write(ep[1]);
                                        peer.SendUnconnectedMessage(om, msg.SenderEndpoint);
                                    }

                                    break;
                                case MasterServerMessageType.RequestIntroduction:
                                    // It's a client wanting to connect to a specific (external) host
                                    IPEndPoint clientInternal = msg.ReadIPEndpoint();
                                    IPEndPoint hostExternal = msg.ReadIPEndpoint();
                                    string token = msg.ReadString();

                                    Console.WriteLine(msg.SenderEndpoint + " requesting introduction to " + hostExternal + " (token " + token + ")");

                                    // find in list
                                    foreach (IPEndPoint[] elist in registeredHosts)
                                    {
                                        if (elist[1].Equals(hostExternal))
                                        {
                                            // found in list - introduce client and host to eachother
                                            Console.WriteLine("Sending introduction...");
                                            peer.Introduce(
                                                elist[0], // host internal
                                                elist[1], // host external
                                                clientInternal, // client internal
                                                msg.SenderEndpoint, // client external
                                                token // request token
                                            );
                                            break;
                                        }
                                    }
                                    break;
                            }
                            break;

                        case NetIncomingMessageType.DebugMessage:
                        case NetIncomingMessageType.VerboseDebugMessage:
                        case NetIncomingMessageType.WarningMessage:
                        case NetIncomingMessageType.ErrorMessage:
                            // print diagnostics message
                            Console.WriteLine(msg.ReadString());
                            break;
                    }
                }
            }

            peer.Shutdown("shutting down");
        }
开发者ID:KaptenDavidsson,项目名称:WindowsGame9,代码行数:94,代码来源:Program.cs

示例11: SendMessage

 public static void SendMessage(NetPeer peer, NetworkMessageType networkMessageType, int data, NetConnection recipient)
 {
     NetOutgoingMessage message = peer.CreateMessage();
     message.Write((byte)networkMessageType);
     message.Write(data, 32);
     peer.SendMessage(message, recipient, NetDeliveryMethod.ReliableUnordered);
 }
开发者ID:JOCP9733,项目名称:tank,代码行数:7,代码来源:NetUtil.cs

示例12: SendMCS

        public static void SendMCS()
        {
            Console.WriteLine("Enter Port: ");
            int port = int.Parse(Console.ReadLine());

            NetPeerConfiguration config = new NetPeerConfiguration("InduZtry");
            config.Port = port;
            config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            config.EnableMessageType(NetIncomingMessageType.Data);
            //config.EnableUPnP = true;
            server = new NetPeer(config);
            server.Start();
            //Console.WriteLine(server.UPnP.Status);
            //server.UPnP.ForwardPort(port, "InduZtry");
            //Console.WriteLine(server.UPnP.Status);
            //Console.WriteLine("UPnP IP: " + server.UPnP.GetExternalIP());

            Thread t = new Thread(RecvMCS);
            t.IsBackground = true;
            t.Start();

            message = "NULL";

            while(true) {
                Thread.Sleep(10);
                if(recipient != null) {
                    NetOutgoingMessage sendMsg = server.CreateMessage();
                    sendMsg.Write(message);
                    server.SendMessage(sendMsg, recipient, NetDeliveryMethod.ReliableOrdered);
                }
            }
        }
开发者ID:RegrowthStudios,项目名称:VoxelRTS,代码行数:32,代码来源:Program.cs

示例13: Run

        public static void Run(NetPeer peer)
        {
            //
            // Test XTEA
            //
            NetXtea xtea = new NetXtea("TopSecret");

            byte[] original = new byte[16];
            NetRandom.Instance.NextBytes(original);

            byte[] encrypted = new byte[original.Length];
            xtea.EncryptBlock(original, 0, encrypted, 0);
            xtea.EncryptBlock(original, 8, encrypted, 8);

            byte[] decrypted = new byte[original.Length];
            xtea.DecryptBlock(encrypted, 0, decrypted, 0);
            xtea.DecryptBlock(encrypted, 8, decrypted, 8);

            // compare!
            for (int i = 0; i < original.Length; i++)
                if (original[i] != decrypted[i])
                    throw new NetException("XTEA fail!");

            Console.WriteLine("XTEA OK");

            NetOutgoingMessage om = peer.CreateMessage();
            om.Write("Hallon");
            om.Write(42);
            om.Write(5, 5);
            om.Write(true);
            om.Write("kokos");
            om.Encrypt(xtea);

            // convert to incoming message
            NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
            im.Decrypt(xtea);

            if (im.ReadString() != "Hallon")
                throw new NetException("fail");
            if (im.ReadInt32() != 42)
                throw new NetException("fail");
            if (im.ReadInt32(5) != 5)
                throw new NetException("fail");
            if (im.ReadBoolean() != true)
                throw new NetException("fail");
            if (im.ReadString() != "kokos")
                throw new NetException("fail");

            for (int i = 0; i < 100; i++)
            {
                byte[] salt = NetSRP.CreateRandomSalt();
                byte[] x = NetSRP.ComputePrivateKey("user", "password", salt);

                byte[] v = NetSRP.ComputeServerVerifier(x);
                //Console.WriteLine("v = " + NetUtility.ToHexString(v));

                byte[] a = NetSRP.CreateRandomEphemeral(); //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
                byte[] A = NetSRP.ComputeClientEphemeral(a);
                //Console.WriteLine("A = " + NetUtility.ToHexString(A));

                byte[] b = NetSRP.CreateRandomEphemeral(); // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
                byte[] B = NetSRP.ComputeServerEphemeral(b, v);
                //Console.WriteLine("B = " + NetUtility.ToHexString(B));

                byte[] u = NetSRP.ComputeU(A, B);
                //Console.WriteLine("u = " + NetUtility.ToHexString(u));

                byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
                //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

                byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
                //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

                if (Ss.Length != Sc.Length)
                    throw new NetException("SRP non matching lengths!");

                for (int j = 0; j < Ss.Length; j++)
                {
                    if (Ss[j] != Sc[j])
                        throw new NetException("SRP non matching session values!");
                }

                var test = NetSRP.CreateEncryption(Ss);
            }

            Console.WriteLine("Message encryption OK");
        }
开发者ID:cody82,项目名称:spacewar-arena,代码行数:87,代码来源:EncryptionTests.cs

示例14: CheckForStatusUpdate

        public void CheckForStatusUpdate(GameTime gameTime, NetPeer netPeer, NetConnection connection)
        {
            timeSinceStatusUpdate += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (HpChanged && timeSinceStatusUpdate >= statusUpdateDelay)
            {
                timeSinceStatusUpdate = 0f;
                HpChanged = false;

                NetOutgoingMessage msg = netPeer.CreateMessage();
                msg.Write(MessageID.STRUCTURE_STATUS_UPDATE);
                msg.Write(ID);
                msg.Write(Team);
                msg.Write((short)Hp);
                netPeer.SendMessage(msg, connection, NetDeliveryMethod.ReliableOrdered);
            }
        }
开发者ID:nubington,项目名称:rts,代码行数:17,代码来源:Structure.cs

示例15: Run

		public static void Run(NetPeer peer)
		{
			//
			// Test encryption
			//
			List<NetEncryption> algos = new List<NetEncryption>();

			algos.Add(new NetXorEncryption(peer, "TopSecret"));
			algos.Add(new NetXtea(peer, "TopSecret"));
			algos.Add(new NetAESEncryption(peer, "TopSecret"));
			algos.Add(new NetRC2Encryption(peer, "TopSecret"));
			algos.Add(new NetDESEncryption(peer, "TopSecret"));
			algos.Add(new NetTripleDESEncryption(peer, "TopSecret"));

			foreach (var algo in algos)
			{
				NetOutgoingMessage om = peer.CreateMessage();
				om.Write("Hallon");
				om.Write(42);
				om.Write(5, 5);
				om.Write(true);
				om.Write("kokos");
				int unencLen = om.LengthBits;
				om.Encrypt(algo);

				// convert to incoming message
				NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
				if (im.Data == null || im.Data.Length == 0)
					throw new NetException("bad im!");

				im.Decrypt(algo);

				if (im.Data == null || im.Data.Length == 0 || im.LengthBits != unencLen)
					throw new NetException("Length fail");

				var str = im.ReadString();
				if (str != "Hallon")
					throw new NetException("fail");
				if (im.ReadInt32() != 42)
					throw new NetException("fail");
				if (im.ReadInt32(5) != 5)
					throw new NetException("fail");
				if (im.ReadBoolean() != true)
					throw new NetException("fail");
				if (im.ReadString() != "kokos")
					throw new NetException("fail");

				Console.WriteLine(algo.GetType().Name + " encryption verified");
			}

			for (int i = 0; i < 100; i++)
			{
				byte[] salt = NetSRP.CreateRandomSalt();
				byte[] x = NetSRP.ComputePrivateKey("user", "password", salt);

				byte[] v = NetSRP.ComputeServerVerifier(x);
				//Console.WriteLine("v = " + NetUtility.ToHexString(v));

				byte[] a = NetSRP.CreateRandomEphemeral(); //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
				byte[] A = NetSRP.ComputeClientEphemeral(a);
				//Console.WriteLine("A = " + NetUtility.ToHexString(A));

				byte[] b = NetSRP.CreateRandomEphemeral(); // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
				byte[] B = NetSRP.ComputeServerEphemeral(b, v);
				//Console.WriteLine("B = " + NetUtility.ToHexString(B));

				byte[] u = NetSRP.ComputeU(A, B);
				//Console.WriteLine("u = " + NetUtility.ToHexString(u));

				byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
				//Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

				byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
				//Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

				if (Ss.Length != Sc.Length)
					throw new NetException("SRP non matching lengths!");

				for (int j = 0; j < Ss.Length; j++)
				{
					if (Ss[j] != Sc[j])
						throw new NetException("SRP non matching session values!");
				}

				var test = NetSRP.CreateEncryption(peer, Ss);
			}

			Console.WriteLine("Message encryption OK");
		}
开发者ID:g1111111g,项目名称:lidgren-network-gen3,代码行数:89,代码来源:EncryptionTests.cs


注:本文中的Lidgren.Network.NetPeer.CreateMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。