/// <summary> /// This method is called when the host server button is clicked /// </summary> private void StartServer() { #if !UNITY_WEBGL // Create a host connection socket = Networking.Host(PORT, Networking.TransportationProtocolType.UDP, playerCount, false) as CrossPlatformUDP; Networking.SetPrimarySocket(socket); #endif }
public ForgeMasterServerPing(CrossPlatformUDP currentSocket) { // TODO: Throw a master server exception if (string.IsNullOrEmpty(ForgeMasterServer.MasterServerIp)) throw new NetworkException("The master server ip has not been assigned with the ForgeMasterServer.SetIp static method."); socket = currentSocket; masterServerEndpoint = new IPEndPoint(IPAddress.Parse(ForgeMasterServer.MasterServerIp), ForgeMasterServer.PORT); masterServerPing = new Thread(PingHostThread); masterServerPing.Start(); }
public ForgeMasterServerPing(CrossPlatformUDP currentSocket) { // TODO: Throw a master server exception if (string.IsNullOrEmpty(ForgeMasterServer.MasterServerIp)) { throw new NetworkException("The master server ip has not been assigned with the ForgeMasterServer.SetIp static method."); } socket = currentSocket; masterServerEndpoint = new IPEndPoint(IPAddress.Parse(ForgeMasterServer.MasterServerIp), ForgeMasterServer.PORT); masterServerPing = new Thread(PingHostThread); masterServerPing.Start(); }
/// <summary> /// This method is used to allow a server to register itself with the NAT hole punching server /// </summary> /// <param name="socket">This is the socket that is being used for the communication with the clients</param> /// <param name="port">The port number that is being used for this server</param> /// <param name="proxyHost">The ip address of the the NAT hole punching (router) server</param> /// <param name="proxyPort">The port number for the NAT hole punch server</param> public static void RegisterNat(CrossPlatformUDP socket, ushort port, 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, 1 }); data.AddRange(BitConverter.GetBytes(port)); 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"); } } #if UNITY_EDITOR Debug.Log("The hole punching registration for this server is complete"); #endif } #if UNITY_EDITOR catch (Exception e) { Debug.LogException(e); } #else catch { } #endif #endif }
public ForgeMasterServerPing(CrossPlatformUDP currentSocket) { }
/// <summary> /// Create and connect a client to the specified server ip and port /// </summary> /// <param name="ip">The host (usually ip address or domain name) to connect to</param> /// <param name="port">The port for the particular server that this connection is attempting</param> /// <param name="comType">The transportation protocol type that is to be used <see cref="Networking.TransportationProtocolType"/></param> /// <param name="winRT">If this is Windows Phone or Windows Store, this should be true, otherwise default to false</param> /// <returns>The NetWorker client that was created (Which may not have established a connection yet <see cref="NetWorker.connected"/></returns> /// <example> /// public string host = "127.0.0.1"; // IP address /// public int port = 15937; // Port number /// public Networking.TransportationProtocolType protocolType = Networking.TransportationProtocolType.UDP; // Communication protocol /// /// #if NETFX_CORE && !UNITY_EDITOR /// private bool isWinRT = true; /// #else /// private bool isWinRT = false; /// #endif /// public void StartServer() /// { /// NetWorker socket = Networking.Connect(host, (ushort)port, protocolType, isWinRT); /// } /// </example> public static NetWorker Connect(string ip, ushort port, TransportationProtocolType comType, bool winRT = false, bool useNat = false, bool standAlone = false) { Threading.ThreadManagement.Initialize(); Unity.MainThreadManager.Create(); if (Sockets == null) Sockets = new Dictionary<ushort, NetWorker>(); if (Sockets.ContainsKey(port)) { #if UNITY_IOS || UNITY_IPHONE if (comType == TransportationProtocolType.UDP) Sockets[port] = new CrossPlatformUDP(false, 0); else Sockets[port] = new DefaultClientTCP(); #else if (Sockets[port].Connected) throw new NetworkException(8, "Socket has already been initialized on that port"); else if (Sockets[port].Disconnected) Sockets.Remove(port); else return Sockets[port]; // It has not finished connecting yet #endif } else if (comType == TransportationProtocolType.UDP) Sockets.Add(port, new CrossPlatformUDP(false, 0)); else { if (winRT) Sockets.Add(port, new WinMobileClient()); else Sockets.Add(port, new DefaultClientTCP()); } Sockets[port].connected += delegate() { if (connectedInvoker != null) connectedInvoker(Sockets[port]); }; Sockets[port].UseNatHolePunch = useNat; Sockets[port].Connect(ip, port); if (!standAlone) SimpleNetworkedMonoBehavior.Initialize(Sockets[port]); return Sockets[port]; }
/// <summary> /// This method is used on the client to attempt to connect to the server through the NAT hole punch server /// </summary> /// <param name="socket">This is the socket that is being used for the communication with the server</param> /// <param name="port">This is the port number that this client is bound to</param> /// <param name="requestHost">This is the host address of the server that this client is trying to connect to</param> /// <param name="requestPort">This is the host port of the server that this client is trying to connect to</param> /// <param name="proxyHost">This is the NAT hole punch server host address</param> /// <param name="proxyPort">This is the NAT hole punch server port number</param> /// <returns></returns> 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); }
/// <summary> /// This method is used to allow a server to register itself with the NAT hole punching server /// </summary> /// <param name="socket">This is the socket that is being used for the communication with the clients</param> /// <param name="port">The port number that is being used for this server</param> /// <param name="proxyHost">The ip address of the the NAT hole punching (router) server</param> /// <param name="proxyPort">The port number for the NAT hole punch server</param> public static void RegisterNat(CrossPlatformUDP socket, ushort port, 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, 1 }); data.AddRange(BitConverter.GetBytes(port)); 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"); } #if UNITY_EDITOR Debug.Log("The hole punching registration for this server is complete"); #endif } #if UNITY_EDITOR catch (Exception e) { Debug.LogException(e); } #else catch { } #endif #endif }
/// <summary> /// This method is used on the client to attempt to connect to the server through the NAT hole punch server /// </summary> /// <param name="socket">This is the socket that is being used for the communication with the server</param> /// <param name="port">This is the port number that this client is bound to</param> /// <param name="requestHost">This is the host address of the server that this client is trying to connect to</param> /// <param name="requestPort">This is the host port of the server that this client is trying to connect to</param> /// <param name="proxyHost">This is the NAT hole punch server host address</param> /// <param name="proxyPort">This is the NAT hole punch server port number</param> /// <returns></returns> 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; }
/// <summary> /// This method is called when the host server button is clicked /// </summary> private void StartServer() { // Create a host connection socket = Networking.Host(PORT, Networking.TransportationProtocolType.UDP, playerCount, false) as CrossPlatformUDP; Networking.SetPrimarySocket(socket); }