Exemple #1
0
        private bool CanClose()
        {
            while (MainForm.Instance.LocalAddress == null)
            {
                ;
            }
            if (MainForm.Instance.LocalAddress.ToString() == txtServer.Text)
            {
                txtServer.Text = "127.0.0.1";
            }
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                ConnectionHandler.ConnectTo(s, txtServer.Text, int.Parse(tempPortTxt.Text));
            }
            catch
            {
                MessageBox.Show("Unable to connect to the server.");
                return(false);
            }
            ModUpdaterNetworkStream str = new ModUpdaterNetworkStream(s);

            Packet.Send(new HandshakePacket {
                Type = HandshakePacket.SessionType.ServerList
            }, str);
            Packet           p  = Packet.ReadPacket(str); //The server should only return a ServerList, right?
            ServerListPacket sp = null;

            if (!(p is ServerListPacket)) //But just in case...
            {
                Packet.Send(new DisconnectPacket(), str);
                ConnectTo = new Server {
                    Address = txtServer.Text, Port = int.Parse(tempPortTxt.Text), Name = "Not Yet Defined."
                };
                str.Close();
                s.Disconnect(false);
                return(true);
            }
            sp = (ServerListPacket)p;
            List <Server> servers = new List <Server>();

            for (int i = 0; i < sp.Servers.Length; i++)
            {
                Server srv = new Server {
                    Address = sp.Locations[i], Name = sp.Servers[i], Port = sp.Ports[i]
                };
                servers.Add(srv);
            }
            SelectServerDialog dial = new SelectServerDialog(servers.ToArray());

            dial.ShowDialog();
            if (dial.DialogResult != System.Windows.Forms.DialogResult.OK)
            {
                return(false);
            }
            ConnectTo = dial.SelectedServer;
            return(true);
        }
 private bool CanClose()
 {
     while (MainForm.Instance.LocalAddress == null) ;
     if (MainForm.Instance.LocalAddress.ToString() == txtServer.Text) txtServer.Text = "127.0.0.1";
     Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     try
     {
         ConnectionHandler.ConnectTo(s, txtServer.Text, int.Parse(tempPortTxt.Text));
     }
     catch
     {
         MessageBox.Show("Unable to connect to the server.");
         return false;
     }
     ModUpdaterNetworkStream str = new ModUpdaterNetworkStream(s);
     Packet.Send(new HandshakePacket { Type = HandshakePacket.SessionType.ServerList }, str);
     Packet p = Packet.ReadPacket(str); //The server should only return a ServerList, right?
     ServerListPacket sp = null;
     if (!(p is ServerListPacket)) //But just in case...
     {
         Packet.Send(new DisconnectPacket(), str);
         ConnectTo = new Server { Address = txtServer.Text, Port = int.Parse(tempPortTxt.Text), Name = "Not Yet Defined." };
         str.Close();
         s.Disconnect(false);
         return true;
     }
     sp = (ServerListPacket)p;
     List<Server> servers = new List<Server>();
     for (int i = 0; i < sp.Servers.Length; i++)
     {
         Server srv = new Server { Address = sp.Locations[i], Name = sp.Servers[i], Port = sp.Ports[i] };
         servers.Add(srv);
     }
     SelectServerDialog dial = new SelectServerDialog(servers.ToArray());
     dial.ShowDialog();
     if (dial.DialogResult != System.Windows.Forms.DialogResult.OK)
         return false;
     ConnectTo = dial.SelectedServer;
     return true;
 }