Skip to content

Commit

Permalink
Retry on disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
HeHang0 committed Mar 27, 2024
1 parent 7bf74f9 commit cee7570
Showing 1 changed file with 57 additions and 41 deletions.
98 changes: 57 additions & 41 deletions windows/Speaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static Speaker()
private readonly string _name = string.Empty;
private string _id = string.Empty;
private bool _disposed = false;
private bool _retried = false;
private readonly Dispatcher _dispatcher;

public string Id => _id;
Expand Down Expand Up @@ -161,54 +162,57 @@ private void Connect(object sender)
_ = Connect();
}

public async Task Connect()
public async Task Connect(bool retry=false)
{
if (Connecting) return;
_disposed = false;
await DisConnect();
await DisConnect(false, retry);
SetConnectStatus(ConnectStatus.Connecting);
Logger.Info("connect start");
try
{
tcpClient = new TcpClient();
tcpClient.NoDelay = true;
if (_isUSB)
if (!retry)
{
if (!await EnsureDevice(_id))
if (_isUSB)
{
throw new Exception("device not ready");
}
int port = await Utils.GetFreePort();
var device = adbClient.GetDevice(_id);
adbClient.RemoveRemoteForward(device, REMOTE_SOCKET);
adbClient.CreateForward(device, "tcp:" + port, REMOTE_SOCKET, true);
_remoteIP = "127.0.0.1";
_remotePort = port;
}
else
{
string pattern = @"^[\[]?(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|([0-9A-Fa-f]{1,4}:){1,7}([0-9A-Fa-f]{1,4}|:))[\]]?(:(?<port>\d+))?$";
var match = Regex.Match(_id, pattern);
if (!match.Success)
{
MessageBox.Show(Application.Current.MainWindow,
Languages.Language.GetLanguageText("ipParseError"),
Application.Current.MainWindow.Title);
throw new Exception("address error");
}
var groupIP = match.Groups["ip"];
var groupPort = match.Groups["port"];
string ip = groupIP.Value;
if(!int.TryParse(groupPort?.Value ?? string.Empty, out int port))
{
port = 80;
if (!await EnsureDevice(_id))
{
throw new Exception("device not ready");
}
int port = await Utils.GetFreePort();
var device = adbClient.GetDevice(_id);
adbClient.RemoveRemoteForward(device, REMOTE_SOCKET);
adbClient.CreateForward(device, "tcp:" + port, REMOTE_SOCKET, true);
_remoteIP = "127.0.0.1";
_remotePort = port;
}
if (!await EnsureDevice(ip, port))
else
{
throw new Exception("device not ready");
string pattern = @"^[\[]?(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|([0-9A-Fa-f]{1,4}:){1,7}([0-9A-Fa-f]{1,4}|:))[\]]?(:(?<port>\d+))?$";
var match = Regex.Match(_id, pattern);
if (!match.Success)
{
MessageBox.Show(Application.Current.MainWindow,
Languages.Language.GetLanguageText("ipParseError"),
Application.Current.MainWindow.Title);
throw new Exception("address error");
}
var groupIP = match.Groups["ip"];
var groupPort = match.Groups["port"];
string ip = groupIP.Value;
if (!int.TryParse(groupPort?.Value ?? string.Empty, out int port))
{
port = 80;
}
if (!await EnsureDevice(ip, port))
{
throw new Exception("device not ready");
}
_remoteIP = ip;
_remotePort = port;
}
_remoteIP = ip;
_remotePort = port;
}
await RequestTcp(Command.Stop, force: true);
IPAddress ipAddress = IPAddress.Parse(_remoteIP);
Expand Down Expand Up @@ -244,6 +248,7 @@ public async Task Connect()
ReadAsync(tcpClient.GetStream());
}
SetConnectStatus(ConnectStatus.Connected);
_retried = false;
}
catch (Exception ex)
{
Expand All @@ -257,14 +262,14 @@ private async void ReadAsync(NetworkStream stream)
{
try
{
if (stream.CanRead)
while (stream.CanRead)
{
await stream.ReadAsync(_receiveBuffer, 0, _receiveBuffer.Length);
ReadAsync(stream);
}
}
catch (Exception)
catch (Exception e)
{
// 处理异常
}
}

Expand All @@ -284,7 +289,15 @@ private async void SendAudioData(object sender, WaveInEventArgs e)
}
if (!(await WriteTcp(e.Buffer, e.BytesRecorded, true)))
{
await DisConnect(true);
if (_retried)
{
await DisConnect(true);
}
else
{
_retried = true;
await Connect(true);
}
}
lock (writeLock)
{
Expand Down Expand Up @@ -372,13 +385,16 @@ await Utils.RunAdbShellCommandAsync(adbClient,
return false;
}

private async Task DisConnect(bool toast=false)
private async Task DisConnect(bool toast=false, bool retry = false)
{
if (_connectStatus == ConnectStatus.UnConnected) return;
SetConnectStatus(ConnectStatus.UnConnected, toast);
Logger.Info("disconnect start");
_remoteIP = string.Empty;
_remotePort = -1;
if (!retry)
{
_remoteIP = string.Empty;
_remotePort = -1;
}
try
{
AudioManager.Stoped -= OnAudioStoped;
Expand Down

0 comments on commit cee7570

Please sign in to comment.