-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[iOS] Add basic classes to support the usage of tunnels. #66
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1c28875
[iOS] Add basic classes to support the usage of tunnels.
mandel-macaque a258e57
Fix warning.
mandel-macaque 10bd1a3
Update src/Microsoft.DotNet.XHarness.iOS.Shared/Listeners/SimpleTcpLi…
mandel-macaque a68a6b5
Address reviews. Move TunnelBore and TcpTunnel to by IAsyncDisposable…
mandel-macaque c156aac
Merge branch 'tunnel-bore' of github.com:mandel-macaque/xharness into…
mandel-macaque 4846d61
Remove last CWL.
mandel-macaque d777908
Remove extra 'using'.
mandel-macaque 5cb3812
Merge branch 'master' into tunnel-bore
mandel-macaque 923b1df
Merge branch 'master' into tunnel-bore
mandel-macaque e0571a5
Fix reviews and update the vars to match the project style.
mandel-macaque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/Microsoft.DotNet.XHarness.iOS.Shared/Listeners/TcpTunnel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.DotNet.XHarness.iOS.Shared.Execution; | ||
using Microsoft.DotNet.XHarness.iOS.Shared.Execution.Mlaunch; | ||
using Microsoft.DotNet.XHarness.iOS.Shared.Logging; | ||
|
||
namespace Microsoft.DotNet.XHarness.iOS.Shared.Listeners | ||
{ | ||
|
||
// interface to be implemented by those listeners that can use a tcp tunnel | ||
public interface ITunnelListener : ISimpleListener | ||
{ | ||
TaskCompletionSource<bool> TunnelHoleThrough { get; } | ||
} | ||
|
||
// interface implemented by a tcp tunnel between the host and the device. | ||
public interface ITcpTunnel : IAsyncDisposable | ||
{ | ||
public void Open(string device, ITunnelListener simpleListener, TimeSpan timeout, ILog mainLog); | ||
public Task Close(); | ||
public Task<bool> Started { get; } | ||
} | ||
|
||
// represents a tunnel created between a device and a host. This tunnel allows communication between | ||
// the host and the device via the usb cable. | ||
public class TcpTunnel : ITcpTunnel | ||
{ | ||
readonly object _processExecutionLock = new object(); | ||
readonly IProcessManager _processManager; | ||
|
||
Task<ProcessExecutionResult> _tcpTunnelExecutionTask = null; | ||
CancellationTokenSource _cancellationToken; | ||
|
||
public TaskCompletionSource<bool> startedCompletionSource { get; private set; } = new TaskCompletionSource<bool>(); | ||
public Task<bool> Started => startedCompletionSource.Task; | ||
public int Port { get; private set; } | ||
|
||
public TcpTunnel(IProcessManager processManager) | ||
{ | ||
_processManager = processManager ?? throw new ArgumentNullException(nameof(processManager)); | ||
} | ||
|
||
public void Open(string device, ITunnelListener simpleListener, TimeSpan timeout, ILog mainLog) | ||
{ | ||
if (device == null) | ||
throw new ArgumentNullException(nameof(device)); | ||
if (simpleListener == null) | ||
throw new ArgumentNullException(nameof(simpleListener)); | ||
if (mainLog == null) | ||
throw new ArgumentNullException(nameof(mainLog)); | ||
|
||
lock (_processExecutionLock) | ||
{ | ||
// launch app, but do not await for the result, since we need to create the tunnel | ||
var tcpArgs = new MlaunchArguments { | ||
new TcpTunnelArgument (simpleListener.Port), | ||
new VerbosityArgument (), | ||
new DeviceNameArgument (device), | ||
}; | ||
|
||
// use a cancelation token, later will be used to kill the tcp tunnel process | ||
_cancellationToken = new CancellationTokenSource(); | ||
mainLog.WriteLine($"Starting tcp tunnel between mac port: {simpleListener.Port} and devie port {simpleListener.Port}."); | ||
Port = simpleListener.Port; | ||
var tunnelbackLog = new CallbackLog((line) => | ||
{ | ||
mainLog.WriteLine($"The tcp tunnel output is {line}"); | ||
if (line.Contains("Tcp tunnel started on device")) | ||
{ | ||
mainLog.Write($"Tcp tunnel created on port {simpleListener.Port}"); | ||
startedCompletionSource.TrySetResult(true); | ||
simpleListener.TunnelHoleThrough.TrySetResult(true); | ||
} | ||
}); | ||
// do not await since we are going to be running the process in parallel | ||
_tcpTunnelExecutionTask = _processManager.ExecuteCommandAsync(tcpArgs, tunnelbackLog, timeout, cancellationToken: _cancellationToken.Token); | ||
} | ||
} | ||
|
||
public async Task Close() | ||
{ | ||
if (_cancellationToken == null) | ||
throw new InvalidOperationException("Cannot close tunnel that was not opened."); | ||
// cancel process and wait for it to terminate, else we might want to start a second tunnel to the same device | ||
// which is going to give problems. | ||
_cancellationToken.Cancel(); | ||
await _tcpTunnelExecutionTask; | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await Close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
impossible for this to be null or > 65535?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int cannot be null, if is too large, the exception will be raised later