Skip to content
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

Serial timeout increase + concurrent queue #604

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ protected override async ValueTask ExecuteCommand()

await WriteEspFiles(connection, deviceInfo, package);

await connection.WaitForMeadowAttach();

// reset device
if (connection != null && connection.Device != null)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.60.0</PackageVersion>
<PackageVersion>2.0.61.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Meadow.CLI;

public static class Constants
{
public const string CLI_VERSION = "2.0.60.0";
public const string CLI_VERSION = "2.0.61.0";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class SerialConnection

public override async Task WaitForMeadowAttach(CancellationToken? cancellationToken)
{
var timeout = 500;
var timeout = 50;

while (timeout-- > 0)
{
Expand All @@ -26,7 +26,7 @@ public override async Task WaitForMeadowAttach(CancellationToken? cancellationTo
return;
}

await Task.Delay(20);
await Task.Delay(100);

if (!_port.IsOpen)
{
Expand Down
20 changes: 9 additions & 11 deletions Source/v2/Meadow.HCom/Connections/SerialConnection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.IO.Ports;
using System.Security.Cryptography;

Expand All @@ -21,7 +22,7 @@ public partial class SerialConnection : ConnectionBase, IDisposable
private bool _isDisposed;
private ConnectionState _state;
private readonly List<IConnectionListener> _listeners = new List<IConnectionListener>();
private readonly Queue<IRequest> _pendingCommands = new Queue<IRequest>();
private readonly ConcurrentQueue<IRequest> _commandQueue = new ConcurrentQueue<IRequest>();
private readonly AutoResetEvent _commandEvent = new AutoResetEvent(false);
private bool _maintainConnection;
private Thread? _connectionManager = null;
Expand Down Expand Up @@ -208,8 +209,7 @@ public override void Detach()
// local function so we can unsubscribe
var count = _messageCount;

_pendingCommands.Enqueue(command);
_commandEvent.Set();
EnqueueRequest(command);

while (timeout-- > 0)
{
Expand Down Expand Up @@ -248,19 +248,17 @@ private void CommandManager()
{
_commandEvent.WaitOne(1000);

while (_pendingCommands.Count > 0)
while (_commandQueue.Count > 0)
{
Debug.WriteLine($"There are {_pendingCommands.Count} pending commands");
Debug.WriteLine($"There are {_commandQueue.Count} pending commands");

_commandQueue.TryDequeue(out var pendingCommand);

var command = _pendingCommands.Dequeue() as Request;
if (command != null)
if (pendingCommand is Request command)
{
// if this is a file write, we need to packetize for progress

var payload = command.Serialize();
EncodeAndSendPacket(payload);

// TODO: re-queue on fail?
}
}
}
Expand Down Expand Up @@ -297,7 +295,7 @@ public void EnqueueRequest(IRequest command)
};
}

_pendingCommands.Enqueue(command);
_commandQueue.Enqueue(command);
_commandEvent.Set();
}

Expand Down
Loading