void Connect(string username, DataGameViewModel game, string userhost, string userport, string password) { Successful = false; IPAddress host = null; int port = -1; this.ValidateFields(username, game, userhost, userport, password, out host, out port); Program.IsHost = false; Program.GameEngine = new Octgn.GameEngine(game.GetGame(), username, password, true); Program.Client = new ClientSocket(host, port); Program.Client.Connect(); Successful = true; }
void ValidateFields(string username, DataGameViewModel game, string host, string port, string password, out IPAddress ip, out int conPort) { if (string.IsNullOrWhiteSpace(username)) throw new ArgumentException("Username can't be empty"); if (string.IsNullOrWhiteSpace(host)) throw new ArgumentException("You must enter a host name/ip"); if (string.IsNullOrWhiteSpace(port)) throw new ArgumentException("You must enter a port number."); if (game == null) throw new ArgumentException("You must select a game"); // validate host/ip try { ip = Dns.GetHostAddresses(host) .First(x => x.AddressFamily == AddressFamily.InterNetwork); } catch (Exception e) { Debug.WriteLine(e.Message); throw new ArgumentException("Ip/Host name is invalid, or unreachable"); } conPort = -1; if (!int.TryParse(port, out conPort)) throw new ArgumentException("Port number is invalid"); if (conPort <= 0 || conPort >= Int16.MaxValue) throw new ArgumentException("Port number is invalid"); }
void Connect(DataGameViewModel game, string userhost, string userport,string password) { Successful = false; IPAddress host = null; int port = -1; this.ValidateFields(game, userhost,userport,password, out host, out port); Program.Client = new Client(host,port); Program.Client.Connect(); Successful = true; }