Esempio n. 1
0
 void QuitWorld()
 {
     Debug.LogError("QuitWorld");
     state = WorldState.Closed;
     if (rc != null)
     {
         rc.evtHandler = null;
         rc.msgHandler = null;
         rc.Disconnect();
         rc = null;
     }
     if (udpClient != null)
     {
         udpClient.Quit();
         udpClient = null;
     }
     if (kcpClient != null)
     {
         kcpClient.Close();
         kcpClient = null;
     }
 }
Esempio n. 2
0
        private bool TryConnect()
        {
            if (Client != null)
            {
                if (Client.IsConnected)
                {
                    return(false);
                }
                else
                {
                    Client.Dispose();
                    Client = null;
                }
            }

            IPAddress IP;
            int       port = 0;

            bool OK = true;

            if (IsServer)
            {
                Console.WriteLine("Cannot connect while running a server");
                OK = false;
            }

            if (string.IsNullOrWhiteSpace(txtServer.Text))
            {
                Console.WriteLine("Server is required");
                OK = false;
            }

            if (string.IsNullOrWhiteSpace(txtPort.Text))
            {
                Console.WriteLine("Port is required");
                OK = false;
            }
            else
            {
                if (!int.TryParse(txtPort.Text, out port))
                {
                    Console.WriteLine("Invalid Port: {0}", txtPort.Text);
                    OK = false;
                }
            }

            if (!OK)
            {
                return(false);
            }

            string servername = txtServer.Text.Trim();

            if (!IPAddress.TryParse(servername, out IP))
            {
                IPAddress[] ips = null;
                try
                {
                    ips = Dns.GetHostAddresses(servername);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not resolve {0}: ", servername, ex.Message);
                    return(false);
                }

                //IP = ips.Where(m => m.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6).FirstOrDefault();
                //if (IP == null)
                {
                    IP = ips.Where(m => m.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault();
                }
                if (IP == null)
                {
                    Console.WriteLine("No valid addresses for {0}", servername);
                    return(false);
                }
            }

            RemoteClient remClient;

            if (comboProt.Text == "UDP")
            {
                remClient = new RemoteUDPClient(User, IP, port);
            }
            else
            {
                remClient = new RemoteTCPClient(User, IP, port);
            }

            Client = remClient;

            invalidLoginAttempts = 0;
            Client.Password      = Password;

            EventSource.InvokeOnUserList(null, null);
            ControlsEnabled = false;
            remClient.Connect();
            btnConnect.Content = "Disconnect";

            return(true);
        }