diff --git a/BrowserStackLocal/Backup/BrowserStackLocal.sln b/BrowserStackLocal/Backup/BrowserStackLocal.sln new file mode 100644 index 0000000..ead4ad6 --- /dev/null +++ b/BrowserStackLocal/Backup/BrowserStackLocal.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocal", "BrowserStackLocal\BrowserStackLocal.csproj", "{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocal Unit Tests", "BrowserStackLocal Unit Tests\BrowserStackLocal Unit Tests.csproj", "{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocalIntegrationTests", "BrowserStackLocalIntegrationTests\BrowserStackLocalIntegrationTests.csproj", "{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Release|Any CPU.Build.0 = Release|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Release|Any CPU.Build.0 = Release|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index 3cd3c35..05f499b 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -24,7 +24,7 @@ public class BrowserStackTunnel : IDisposable public int basePathsIndex = -1; protected string binaryAbsolute = ""; protected string binaryArguments = ""; - + protected StringBuilder output; public LocalState localState; protected string logFilePath = ""; @@ -113,7 +113,7 @@ public virtual void Run(string accessKey, string folder, string logFilePath, str { File.WriteAllText(logFilePath, string.Empty); } - RunProcess(arguments, processType); + RunProcess(arguments, processType); } private void RunProcess(string arguments, string processType) @@ -138,14 +138,40 @@ private void RunProcess(string arguments, string processType) if (e.Data != null) { JObject binaryOutput = null; - try { + try + { binaryOutput = JObject.Parse(e.Data); - } catch (Exception) { + } + catch (Exception) + { + SetTunnelState(LocalState.Error); throw new Exception($"Error while parsing JSON {e.Data}"); } - if(binaryOutput.GetValue("state") != null && !binaryOutput.GetValue("state").ToString().ToLower().Equals("connected")) + + JToken connectionState = binaryOutput.GetValue("state"); + if (connectionState != null) + { + if (connectionState.ToString().ToLower().Equals("connected")) + { + SetTunnelState(LocalState.Connected); + } + else if (connectionState.ToString().ToLower().Equals("disconnected")) + { + SetTunnelState(LocalState.Disconnected); + } + else + { + SetTunnelState(LocalState.Error); + throw new Exception("Error while executing BrowserStackLocal " + processType + " " + e.Data); + } + } + else { - throw new Exception("Eror while executing BrowserStackLocal " + processType + " " + e.Data); + JToken message = binaryOutput.GetValue("message"); + if (message != null && message.Type == JTokenType.String && message.ToString() == "BrowserStackLocal stopped successfully") + { + SetTunnelState(LocalState.Disconnected); + } } } }); @@ -162,13 +188,19 @@ private void RunProcess(string arguments, string processType) process.BeginOutputReadLine(); process.BeginErrorReadLine(); - TunnelStateChanged(LocalState.Idle, LocalState.Connecting); - AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) => Kill()); + SetTunnelState(LocalState.Connecting); + AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) => + { + Kill(); + }); process.WaitForExit(); } - private void TunnelStateChanged(LocalState prevState, LocalState state) { } + private void SetTunnelState(LocalState newState) + { + localState = newState; + } public bool IsConnected() { @@ -182,13 +214,13 @@ public void Kill() process.Close(); process.Kill(); process = null; - localState = LocalState.Disconnected; + SetTunnelState(LocalState.Disconnected); } } public void Dispose() { - if(process != null) + if (process != null) { Kill(); } diff --git a/BrowserStackLocal/BrowserStackLocal/Local.cs b/BrowserStackLocal/BrowserStackLocal/Local.cs index daf27d5..5398c08 100644 --- a/BrowserStackLocal/BrowserStackLocal/Local.cs +++ b/BrowserStackLocal/BrowserStackLocal/Local.cs @@ -99,6 +99,7 @@ private void addArgs(string key, string value) } } } + public static string GetVersionString(string pVersionString) { string tVersion = "Unknown"; @@ -160,7 +161,7 @@ public void start(List> options) accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY"); if (accessKey == null || accessKey.Trim().Length == 0) { - throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. "+ + throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. " + "Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY."); } Regex.Replace(this.accessKey, @"\s+", ""); @@ -175,18 +176,27 @@ public void start(List> options) argumentString += "--source \"c-sharp:" + bindingVersion + "\" "; tunnel.addBinaryPath(customBinaryPath); tunnel.addBinaryArguments(argumentString); - while (true) { + while (true) + { bool except = false; - try { + try + { tunnel.Run(accessKey, folder, customLogPath, "start"); - } catch (Exception) + } + catch (System.ComponentModel.Win32Exception) { except = true; } + catch (Exception e) + { + except = true; + Console.WriteLine(e.ToString()); + } if (except) { tunnel.fallbackPaths(); - } else + } + else { break; }