本文整理汇总了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;
}
示例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();
}
示例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);
}
示例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");
}
示例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);
}
示例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);
}
}
示例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);
}
}
}
示例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;
}
示例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);
}
示例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");
}
示例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);
}
示例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);
}
}
}
示例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");
}
示例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);
}
}
示例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");
}