本文整理汇总了C#中Lidgren.Network.NetServer.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# NetServer.Connect方法的具体用法?C# NetServer.Connect怎么用?C# NetServer.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lidgren.Network.NetServer
的用法示例。
在下文中一共展示了NetServer.Connect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MGServer_DoWork
private void MGServer_DoWork (object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
NetPeerConfiguration config = new NetPeerConfiguration (applicationIdentifier);
config.EnableMessageType (NetIncomingMessageType.DiscoveryRequest);
config.EnableMessageType (NetIncomingMessageType.DiscoveryResponse);
config.EnableMessageType (NetIncomingMessageType.NatIntroductionSuccess);
if (availableSession == null)
config.Port = port;
// create and start server
peer = new NetServer (config);
peer.Start ();
myLocalAddress = GetMyLocalIpAddress ();
IPAddress adr = IPAddress.Parse (myLocalAddress);
myLocalEndPoint = new IPEndPoint (adr, port);
// force a little wait until we have a LocalGamer otherwise things
// break. This is the first item in the queue so it shouldnt take long before we
// can continue.
while (session.LocalGamers.Count <= 0)
{
Thread.Sleep(10);
}
if (availableSession != null) {
if (!this.online) {
peer.Connect (availableSession.EndPoint);
} else {
RequestNATIntroduction (availableSession.EndPoint, peer);
}
} else {
if (this.online) {
IPAddress ipaddr = NetUtility.Resolve (masterServer);
if (ipaddr != null) {
m_masterServer = new IPEndPoint (ipaddr, masterserverport);
LocalNetworkGamer localMe = session.LocalGamers [0];
NetOutgoingMessage om = peer.CreateMessage ();
om.Write ((byte)0);
om.Write (session.AllGamers.Count);
om.Write (localMe.Gamertag);
om.Write (session.PrivateGamerSlots);
om.Write (session.MaxGamers);
om.Write (localMe.IsHost);
om.Write (myLocalEndPoint);
om.Write (peer.Configuration.AppIdentifier);
// send up session properties
int[] propertyData = new int[session.SessionProperties.Count * 2];
NetworkSessionProperties.WriteProperties (session.SessionProperties, propertyData);
for (int x = 0; x < propertyData.Length; x++) {
om.Write (propertyData [x]);
}
peer.SendUnconnectedMessage (om, m_masterServer); // send message to peer
} else {
throw new Exception ("Could not resolve live host");
}
}
}
// run until we are done
do {
NetIncomingMessage msg;
while ((msg = peer.ReadMessage ()) != null) {
switch (msg.MessageType) {
case NetIncomingMessageType.UnconnectedData :
break;
case NetIncomingMessageType.NatIntroductionSuccess:
#if !WINDOWS_PHONE
Game.Instance.Log("NAT punch through OK " + msg.SenderEndpoint);
#endif
peer.Connect (msg.SenderEndpoint);
break;
case NetIncomingMessageType.DiscoveryRequest:
//
// Server received a discovery request from a client; send a discovery response (with no extra data attached)
//
// Get the primary local gamer
LocalNetworkGamer localMe = session.LocalGamers [0];
NetOutgoingMessage om = peer.CreateMessage ();
om.Write (session.RemoteGamers.Count);
om.Write (localMe.Gamertag);
om.Write (session.PrivateGamerSlots);
om.Write (session.MaxGamers);
om.Write (localMe.IsHost);
int[] propertyData = new int[session.SessionProperties.Count * 2];
NetworkSessionProperties.WriteProperties (session.SessionProperties, propertyData);
for (int x = 0; x < propertyData.Length; x++) {
om.Write (propertyData [x]);
}
//.........这里部分代码省略.........