Skip to content

Commit 534031c

Browse files
authored
Merge pull request #33 from browserstack/LOC_1340_fix_local_isRunning
LOC-1340 Fixed isRunning
2 parents 7fb062d + 07e99df commit 534031c

File tree

3 files changed

+92
-16
lines changed

3 files changed

+92
-16
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocal", "BrowserStackLocal\BrowserStackLocal.csproj", "{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocal Unit Tests", "BrowserStackLocal Unit Tests\BrowserStackLocal Unit Tests.csproj", "{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStackLocalIntegrationTests", "BrowserStackLocalIntegrationTests\BrowserStackLocalIntegrationTests.csproj", "{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{F58E1C9A-5910-4DA8-B531-9F4A6B0AE8C8}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{FF1ABB6F-4A2C-48F4-B4DB-47DAD566D2F9}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{01FFB287-C79A-4476-AEDB-EE8DE80EB3B3}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
EndGlobal

BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class BrowserStackTunnel : IDisposable
2424
public int basePathsIndex = -1;
2525
protected string binaryAbsolute = "";
2626
protected string binaryArguments = "";
27-
27+
2828
protected StringBuilder output;
2929
public LocalState localState;
3030
protected string logFilePath = "";
@@ -113,7 +113,7 @@ public virtual void Run(string accessKey, string folder, string logFilePath, str
113113
{
114114
File.WriteAllText(logFilePath, string.Empty);
115115
}
116-
RunProcess(arguments, processType);
116+
RunProcess(arguments, processType);
117117
}
118118

119119
private void RunProcess(string arguments, string processType)
@@ -138,14 +138,40 @@ private void RunProcess(string arguments, string processType)
138138
if (e.Data != null)
139139
{
140140
JObject binaryOutput = null;
141-
try {
141+
try
142+
{
142143
binaryOutput = JObject.Parse(e.Data);
143-
} catch (Exception) {
144+
}
145+
catch (Exception)
146+
{
147+
SetTunnelState(LocalState.Error);
144148
throw new Exception($"Error while parsing JSON {e.Data}");
145149
}
146-
if(binaryOutput.GetValue("state") != null && !binaryOutput.GetValue("state").ToString().ToLower().Equals("connected"))
150+
151+
JToken connectionState = binaryOutput.GetValue("state");
152+
if (connectionState != null)
153+
{
154+
if (connectionState.ToString().ToLower().Equals("connected"))
155+
{
156+
SetTunnelState(LocalState.Connected);
157+
}
158+
else if (connectionState.ToString().ToLower().Equals("disconnected"))
159+
{
160+
SetTunnelState(LocalState.Disconnected);
161+
}
162+
else
163+
{
164+
SetTunnelState(LocalState.Error);
165+
throw new Exception("Error while executing BrowserStackLocal " + processType + " " + e.Data);
166+
}
167+
}
168+
else
147169
{
148-
throw new Exception("Eror while executing BrowserStackLocal " + processType + " " + e.Data);
170+
JToken message = binaryOutput.GetValue("message");
171+
if (message != null && message.Type == JTokenType.String && message.ToString() == "BrowserStackLocal stopped successfully")
172+
{
173+
SetTunnelState(LocalState.Disconnected);
174+
}
149175
}
150176
}
151177
});
@@ -162,13 +188,19 @@ private void RunProcess(string arguments, string processType)
162188
process.BeginOutputReadLine();
163189
process.BeginErrorReadLine();
164190

165-
TunnelStateChanged(LocalState.Idle, LocalState.Connecting);
166-
AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) => Kill());
191+
SetTunnelState(LocalState.Connecting);
192+
AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) =>
193+
{
194+
Kill();
195+
});
167196

168197
process.WaitForExit();
169198
}
170199

171-
private void TunnelStateChanged(LocalState prevState, LocalState state) { }
200+
private void SetTunnelState(LocalState newState)
201+
{
202+
localState = newState;
203+
}
172204

173205
public bool IsConnected()
174206
{
@@ -182,13 +214,13 @@ public void Kill()
182214
process.Close();
183215
process.Kill();
184216
process = null;
185-
localState = LocalState.Disconnected;
217+
SetTunnelState(LocalState.Disconnected);
186218
}
187219
}
188220

189221
public void Dispose()
190222
{
191-
if(process != null)
223+
if (process != null)
192224
{
193225
Kill();
194226
}

BrowserStackLocal/BrowserStackLocal/Local.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private void addArgs(string key, string value)
9999
}
100100
}
101101
}
102+
102103
public static string GetVersionString(string pVersionString)
103104
{
104105
string tVersion = "Unknown";
@@ -160,7 +161,7 @@ public void start(List<KeyValuePair<string, string>> options)
160161
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
161162
if (accessKey == null || accessKey.Trim().Length == 0)
162163
{
163-
throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. "+
164+
throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. " +
164165
"Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
165166
}
166167
Regex.Replace(this.accessKey, @"\s+", "");
@@ -175,18 +176,27 @@ public void start(List<KeyValuePair<string, string>> options)
175176
argumentString += "--source \"c-sharp:" + bindingVersion + "\" ";
176177
tunnel.addBinaryPath(customBinaryPath);
177178
tunnel.addBinaryArguments(argumentString);
178-
while (true) {
179+
while (true)
180+
{
179181
bool except = false;
180-
try {
182+
try
183+
{
181184
tunnel.Run(accessKey, folder, customLogPath, "start");
182-
} catch (Exception)
185+
}
186+
catch (System.ComponentModel.Win32Exception)
183187
{
184188
except = true;
185189
}
190+
catch (Exception e)
191+
{
192+
except = true;
193+
Console.WriteLine(e.ToString());
194+
}
186195
if (except)
187196
{
188197
tunnel.fallbackPaths();
189-
} else
198+
}
199+
else
190200
{
191201
break;
192202
}

0 commit comments

Comments
 (0)