本文整理汇总了C#中Lidgren.Network.NetPeer.SendUnconnectedMessage方法的典型用法代码示例。如果您正苦于以下问题:C# NetPeer.SendUnconnectedMessage方法的具体用法?C# NetPeer.SendUnconnectedMessage怎么用?C# NetPeer.SendUnconnectedMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lidgren.Network.NetPeer
的用法示例。
在下文中一共展示了NetPeer.SendUnconnectedMessage方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: 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);
}
示例3: GetServerList
/// <summary>
/// Contacts the Master Server on the net and gets a list of available host games
/// </summary>
/// <param name="netPeer"></param>
private static void GetServerList (NetPeer netPeer)
{
m_masterServer = new IPEndPoint (NetUtility.Resolve (masterServer), masterserverport);
NetOutgoingMessage listRequest = netPeer.CreateMessage ();
listRequest.Write ((byte)1);
listRequest.Write (netPeer.Configuration.AppIdentifier);
netPeer.SendUnconnectedMessage (listRequest, m_masterServer);
}
示例4: 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");
}
示例5: Main
static void Main(string[] args)
{
Console.WriteLine("Server Started");
Dictionary<IPEndPoint, AvailableGame> registeredHosts = new Dictionary<IPEndPoint, AvailableGame>();
NetPeerConfiguration config = new NetPeerConfiguration("masterserver");
config.SetMessageTypeEnabled(NetIncomingMessageType.UnconnectedData, true);
config.Port = 6000;
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)
{
System.Threading.Thread.Sleep(10);
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 (msg.ReadByte())
{
case 0: // register a new game
// currently only one game per host per router.
if (!registeredHosts.ContainsKey(msg.SenderEndpoint))
{
AvailableGame game = new AvailableGame();
game.Count = msg.ReadInt32();
game.GamerTag = msg.ReadString();
game.PrivateGamerSlots = msg.ReadInt32();
game.MaxGamers = msg.ReadInt32();
game.IsHost = msg.ReadBoolean();
game.InternalIP = msg.ReadIPEndpoint();
game.ExternalIP = msg.SenderEndpoint;
game.Game = msg.ReadString();
Console.WriteLine("Got registration for host " + game.ToString());
registeredHosts.Add(game.ExternalIP, game);
}
break;
case 1:
// It's a client wanting a list of registered hosts
Console.WriteLine("Sending list of " + registeredHosts.Count + " hosts to client " + msg.SenderEndpoint);
string appid = msg.ReadString();
foreach (AvailableGame g1 in registeredHosts.Values)
{
if (g1.Game == appid)
{
// send registered host to client
NetOutgoingMessage om = peer.CreateMessage();
om.Write(g1.Count);
om.Write(g1.GamerTag);
om.Write(g1.PrivateGamerSlots);
om.Write(g1.MaxGamers);
om.Write(g1.IsHost);
om.Write(g1.InternalIP);
om.Write(g1.ExternalIP);
peer.SendUnconnectedMessage(om, msg.SenderEndpoint);
}
}
break;
case 2:
// 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 (AvailableGame elist in registeredHosts.Values)
{
if (elist.ExternalIP.Equals(hostExternal))
{
// found in list - introduce client and host to eachother
Console.WriteLine("Sending introduction...");
peer.Introduce(
elist.InternalIP, // host internal
elist.ExternalIP, // host external
clientInternal, // client internal
msg.SenderEndpoint, // client external
token // request token
);
break;
}
}
break;
case 3:
//.........这里部分代码省略.........
示例6: Main
static void Main(string[] args)
{
Dictionary<long, ServerData> registeredHosts = new Dictionary<long, ServerData>();
NetPeerConfiguration config = new NetPeerConfiguration("masterserver");
config.SetMessageTypeEnabled(NetIncomingMessageType.UnconnectedData, true);
config.Port = SharedConfiguration.MasterServerEndPoint.Port;
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
var id = msg.ReadInt64(); // server unique identifier
Console.WriteLine("Got registration for host " + id);
ServerData serverData = new ServerData();
serverData.IPEndPoint[0] = msg.ReadIPEndPoint();
serverData.IPEndPoint[1] = msg.SenderEndPoint;
serverData.ServerName = msg.ReadString();
serverData.ScriptName = msg.ReadString();
serverData.Players = msg.ReadInt32();
serverData.MaxPlayers = msg.ReadInt32();
registeredHosts[id] = serverData;
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 (var kvp in registeredHosts) {
// send registered host to client
NetOutgoingMessage om = peer.CreateMessage();
om.Write(kvp.Key);
om.Write(kvp.Value.IPEndPoint[0]);
om.Write(kvp.Value.IPEndPoint[1]);
om.Write(kvp.Value.ServerName);
om.Write(kvp.Value.ScriptName);
om.Write(kvp.Value.Players);
om.Write(kvp.Value.MaxPlayers);
peer.SendUnconnectedMessage(om, msg.SenderEndPoint);
}
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");
}
示例7: 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);
}
示例8: GetServerList
private static void GetServerList(NetPeer netPeer)
{
MonoGamerPeer.m_masterServer = new IPEndPoint(NetUtility.Resolve(MonoGamerPeer.masterServer), MonoGamerPeer.masterserverport);
NetOutgoingMessage message = netPeer.CreateMessage();
message.Write((byte) 1);
message.Write(netPeer.get_Configuration().get_AppIdentifier());
netPeer.SendUnconnectedMessage(message, MonoGamerPeer.m_masterServer);
}