public void Start() { using (ILock @lock = new SocketLock(this.profile.Port - 1)) { @lock.LockObject(this.process.Timeout); try { int num = FirefoxDriverServer.DetermineNextFreePort(this.host, this.profile.Port); this.profile.Port = num; this.profile.WriteToDisk(); this.process.StartProfile(this.profile, new string[] { "-foreground" }); this.SetAddress(num); this.extensionUri = new Uri(string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}/hub/", new object[] { this.host, num })); this.ConnectToBrowser(this.process.Timeout); } finally { @lock.UnlockObject(); } } }
private void SetAddress(int port) { if (string.Compare("localhost", this.host, StringComparison.OrdinalIgnoreCase) == 0) { this.addresses = FirefoxDriverServer.ObtainLoopbackAddresses(port); } else { IPHostEntry hostEntry = Dns.GetHostEntry(this.host); IPAddress address = IPAddress.Parse("127.0.0.1"); IPAddress[] addressList = hostEntry.AddressList; for (int i = 0; i < addressList.Length; i++) { IPAddress iPAddress = addressList[i]; if (iPAddress.AddressFamily == AddressFamily.InterNetwork) { address = iPAddress; break; } } IPEndPoint item = new IPEndPoint(address, port); this.addresses.Add(item); } if (this.addresses.Count == 0) { throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Could not find any IPv4 addresses for host '{0}'", new object[] { this.host })); } }
/// <summary> /// Initializes a new instance of the <see cref="FirefoxDriverCommandExecutor"/> class. /// </summary> /// <param name="binary">The <see cref="FirefoxBinary"/> on which to make the connection.</param> /// <param name="profile">The <see cref="FirefoxProfile"/> creating the connection.</param> /// <param name="host">The name of the host on which to connect to the Firefox extension (usually "localhost").</param> /// <param name="commandTimeout">The maximum amount of time to wait for each command.</param> public FirefoxDriverCommandExecutor(FirefoxBinary binary, FirefoxProfile profile, string host, TimeSpan commandTimeout) { this.server = new FirefoxDriverServer(binary, profile, host); this.commandTimeout = commandTimeout; }
private void ConnectToBrowser(TimeSpan timeToWait) { Socket socket = null; DateTime t = DateTime.Now.AddMilliseconds(timeToWait.TotalMilliseconds); try { while (!FirefoxDriverServer.IsSocketConnected(socket) && t > DateTime.Now) { foreach (IPEndPoint current in this.addresses) { try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(current); break; } catch (SocketException) { Thread.Sleep(250); } } } if (!FirefoxDriverServer.IsSocketConnected(socket)) { if (socket == null || socket.RemoteEndPoint == null) { StringBuilder stringBuilder = new StringBuilder(); foreach (IPEndPoint current2 in this.addresses) { if (stringBuilder.Length > 0) { stringBuilder.Append(", "); } stringBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}:{1}", new object[] { current2.Address.ToString(), current2.Port.ToString(CultureInfo.InvariantCulture) }); } throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Failed to start up socket within {0} milliseconds. Attempted to connect to the following addresses: {1}", new object[] { timeToWait.TotalMilliseconds, stringBuilder.ToString() })); } IPEndPoint iPEndPoint = (IPEndPoint)socket.RemoteEndPoint; string message = string.Format(CultureInfo.InvariantCulture, "Unable to connect to host {0} on port {1} after {2} milliseconds", new object[] { iPEndPoint.Address.ToString(), iPEndPoint.Port.ToString(CultureInfo.InvariantCulture), timeToWait.TotalMilliseconds }); throw new WebDriverException(message); } } finally { socket.Close(); } }