Skip to content

Commit ae60cae

Browse files
Fixed isRunning
1 parent 78acbb6 commit ae60cae

File tree

5 files changed

+100
-18
lines changed

5 files changed

+100
-18
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
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private void addArgs(string key, string value)
9292
}
9393
}
9494
}
95-
95+
9696
public Local()
9797
{
9898
tunnel = new BrowserStackTunnel();
@@ -111,7 +111,7 @@ public void start(List<KeyValuePair<string, string>> options)
111111
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
112112
if (accessKey == null || accessKey.Trim().Length == 0)
113113
{
114-
throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. "+
114+
throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. " +
115115
"Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
116116
}
117117
Regex.Replace(this.accessKey, @"\s+", "");
@@ -125,18 +125,27 @@ public void start(List<KeyValuePair<string, string>> options)
125125
argumentString += "-logFile \"" + customLogPath + "\" ";
126126
tunnel.addBinaryPath(customBinaryPath);
127127
tunnel.addBinaryArguments(argumentString);
128-
while (true) {
128+
while (true)
129+
{
129130
bool except = false;
130-
try {
131+
try
132+
{
131133
tunnel.Run(accessKey, folder, customLogPath, "start");
132-
} catch (Exception)
134+
}
135+
catch (System.ComponentModel.Win32Exception)
133136
{
134137
except = true;
135138
}
139+
catch (Exception e)
140+
{
141+
except = true;
142+
Console.WriteLine(e.ToString());
143+
}
136144
if (except)
137145
{
138146
tunnel.fallbackPaths();
139-
} else
147+
}
148+
else
140149
{
141150
break;
142151
}

BrowserStackLocalExample/BrowserStackExample/BrowserStackExample.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>BrowserStackExample</RootNamespace>
1111
<AssemblyName>BrowserStackExample</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<PublishUrl>publish\</PublishUrl>
1515
<Install>true</Install>
@@ -26,6 +26,7 @@
2626
<IsWebBootstrapper>false</IsWebBootstrapper>
2727
<UseApplicationTrust>false</UseApplicationTrust>
2828
<BootstrapperEnabled>true</BootstrapperEnabled>
29+
<TargetFrameworkProfile />
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3132
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -83,6 +84,9 @@
8384
<ItemGroup>
8485
<WCFMetadata Include="Service References\" />
8586
</ItemGroup>
87+
<ItemGroup>
88+
<None Include="app.config" />
89+
</ItemGroup>
8690
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8791
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
8892
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>

0 commit comments

Comments
 (0)