static void Main(string[] args) { Console.Title = "KLF Client " + KLFCommon.ProgramVersion; Console.WriteLine("KLF Client Copyright (C) 2013 Alfred Lam"); Console.WriteLine("This program comes with ABSOLUTELY NO WARRANTY; for details type `/show'."); Console.WriteLine("This is free software, and you are welcome to redistribute it"); Console.WriteLine("under certain conditions; type `/show' for details."); Console.WriteLine(); ConsoleClient client = new ConsoleClient(); Configuration = ClientSettings.Load(Path.Combine("./", Filename)); while(true) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.Write("Username: "******"Server Address: "); Console.ResetColor(); Console.WriteLine(((Uri)Configuration.GetDefaultServer()).ToString()); Console.ForegroundColor = ConsoleColor.Green; Console.Write("Auto-Reconnect: "); Console.ResetColor(); Console.WriteLine(Configuration.Reconnect); Console.ResetColor(); Console.WriteLine(); Console.WriteLine("/name change username"); //Console.WriteLine("/auth change token"); Console.WriteLine("/add add server"); Console.WriteLine("/select select default server"); Console.WriteLine("/connect connect to default server"); Console.WriteLine("/auto toggle auto-reconnect"); Console.WriteLine("/quit quit KLFClient"); String[] MenuArgs = Console.ReadLine().Split(' '); switch(MenuArgs[0].ToLowerInvariant()) { case "/quit": case "/q": Console.Write("Closing.\n"); return; case "/name": case "/n": case "/user": case "/u": if(MenuArgs.Length > 1) Configuration.Username = MenuArgs[1]; else { Console.Write("Enter your new username: "******"/auth": //TODO auth token feature break; case "/add": case "/server": String hn; Int32 pn; Console.Write("Host name or IP Address: "); hn = Console.ReadLine(); Console.Write("Port number: "); Int32.TryParse(Console.ReadLine(), out pn); if(Configuration.AddServer(hn, pn)) Console.WriteLine("Server Added."); else Console.WriteLine("Bad input."); Configuration.Save(Filename); break; case "/auto": case "/a": Configuration.Reconnect = !Configuration.Reconnect; Configuration.Save(Filename); break; case "/select": case "/sel": case "/list": case "/default": case "/def": Int32 choice = 0; foreach(ServerProfile server in Configuration.Servers) Console.WriteLine(" {0}{1} {2}", choice++, server.Default?"*":" ", server.Uri.ToString()); Console.Write("Choose default server: "); if(!Int32.TryParse(Console.ReadLine(), out choice)) choice = -1;//invalid if(0 <= choice && choice < Configuration.Servers.Count) { Configuration.SetDefaultServer(choice); Configuration.Save(Filename); Console.WriteLine("Default saved."); } else Console.WriteLine("Invalid index. No changes."); break; case "/connect": case "/co": //TODO validate if(Configuration.ValidServer(Configuration.GetDefaultServer().Host, Configuration.GetDefaultServer().Port)) client.Connect(Configuration); else Console.WriteLine("/add a server then use /def to select default."); break; case "/show": ShowLicense(); break; default: break; } }//end while }
/* Attempt to connect */ public bool ConnectToServer(ClientSettings settings) { if (IsConnected) return false; ClientConfiguration = settings; TcpConnection = new TcpClient(); String host = ClientConfiguration.GetDefaultServer().Host; Int32 port = ClientConfiguration.GetDefaultServer().Port; //attempt to resolve dns IPHostEntry hostEntry = new IPHostEntry(); try { hostEntry = Dns.GetHostEntry(host); } catch (SocketException) { hostEntry = null; } catch (ArgumentException) { hostEntry = null; } //get an IP address IPAddress address = null; if (hostEntry != null && hostEntry.AddressList.Length == 1)//too strict? address = hostEntry.AddressList.First(); else IPAddress.TryParse(host, out address); if (address == null) { Console.WriteLine("Invalid server address."); return false; } //make the connection IPEndPoint endPoint = new IPEndPoint(address, port); Console.WriteLine("Connecting to server..."); try { TcpConnection.Connect(endPoint); if (TcpConnection.Connected) { try { UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); UdpSocket.Connect(endPoint); } catch { if (UdpSocket != null) UdpSocket.Close(); UdpSocket = null; } UdpConnected = false; LastUdpAckReceiveTime = 0; LastUdpMessageSendTime = ClientStopwatch.ElapsedMilliseconds; ConnectionStarted(); return true; } } catch (SocketException e) { Console.WriteLine("Exception: " + e.ToString()); } catch (ObjectDisposedException e) { Console.WriteLine("Exception: " + e.ToString()); } return false; }