BeardedManStudios.Network.ForgeMasterServer.RequestNat C# (CSharp) Метод

RequestNat() публичный статический Метод

This method is used on the client to attempt to connect to the server through the NAT hole punch server
public static RequestNat ( CrossPlatformUDP socket, ushort port, string requestHost, ushort requestPort, string proxyHost, ushort proxyPort = PORT ) : bool
socket CrossPlatformUDP This is the socket that is being used for the communication with the server
port ushort This is the port number that this client is bound to
requestHost string This is the host address of the server that this client is trying to connect to
requestPort ushort This is the host port of the server that this client is trying to connect to
proxyHost string This is the NAT hole punch server host address
proxyPort ushort This is the NAT hole punch server port number
Результат bool
		public static bool RequestNat(CrossPlatformUDP socket, ushort port, string requestHost, ushort requestPort, string proxyHost, ushort proxyPort = PORT)
		{
#if !NETFX_CORE
			IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(proxyHost), proxyPort);

			List<byte> data = new List<byte>(new byte[] { 4, 4, 2 });
			data.AddRange(BitConverter.GetBytes(port));
			data.AddRange(BitConverter.GetBytes(requestPort));
			data.AddRange(Encryptor.Encoding.GetBytes(requestHost));

			try
			{
				int tryCount = 10;
				while (socket.ReadClient.Available == 0)
				{
					socket.ReadClient.Send(data.ToArray(), data.Count, endpoint);
					Thread.Sleep(500);

					if (--tryCount <= 0)
						throw new Exception("Unable to contact proxy host");
				}

				string endpointStr = "";
				BMSByte otherBytes = socket.ReadClient.Receive(ref endpoint, ref endpointStr);

				BMSByte found = new BMSByte();
				found.Clone(otherBytes);

				if (found.byteArr[2] == 0)
					return false;

				ushort targetPort = System.BitConverter.ToUInt16(found.byteArr, 3);
				string targetHost = Encryptor.Encoding.GetString(found.byteArr, 5, found.byteArr.Length - 6);

				IPEndPoint targetEndpoint = new IPEndPoint(IPAddress.Parse(targetHost), targetPort);

				tryCount = 20;
				while (socket.ReadClient.Available == 0)
				{
					socket.ReadClient.Send(new byte[] { 4, 4, 0 }, 3, targetEndpoint);
					Thread.Sleep(500);

					if (--tryCount <= 0)
						throw new Exception("Unable to contact proxy host");
				}

#if UNITY_EDITOR
				Debug.Log("Connected via NAT traversal");
#endif
			}
#if UNITY_EDITOR
			catch (Exception e)
			{
				Debug.LogException(e);
			}
#else
			catch { }
#endif
#endif

			return true;
		}