Skip to content

Commit

Permalink
fix(command): command multiple call protection
Browse files Browse the repository at this point in the history
  • Loading branch information
asvol committed Dec 26, 2024
1 parent a85f69e commit ad5d8ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Asv.Mavlink;

public abstract class CommandServerEx<TArgPacket> : MavlinkMicroserviceServer, ICommandServerEx<TArgPacket>, IDisposable, IAsyncDisposable
public abstract class CommandServerEx<TArgPacket> : MavlinkMicroserviceServer, ICommandServerEx<TArgPacket>
where TArgPacket : MavlinkMessage
{
private readonly Func<TArgPacket,ushort> _cmdGetter;
Expand All @@ -37,10 +37,7 @@ protected CommandServerEx(
_logger = server.Core.LoggerFactory.CreateLogger<CommandServerEx<TArgPacket>>();
_cmdGetter = cmdGetter;
_confirmationGetter = confirmationGetter;
_subscribe = commandsPipe.SubscribeAwait(
async (pkt, ct) =>
await OnRequest(pkt, ct).ConfigureAwait(false)
);
_subscribe = commandsPipe.SubscribeAwait(OnRequest, AwaitOperation.Parallel); // ! Important parrallel
}

public ICommandServer Base { get; }
Expand All @@ -60,7 +57,7 @@ public CommandDelegate<TArgPacket>? this[MavCmd cmd]

public IEnumerable<MavCmd> SupportedCommands =>_registry.Keys.Select(x=>(MavCmd)x);

private async Task OnRequest(TArgPacket pkt, CancellationToken cancellationToken)
private async ValueTask OnRequest(TArgPacket pkt, CancellationToken cancellationToken)
{
var requester = new DeviceIdentity() { ComponentId = pkt.ComponentId, SystemId = pkt.SystemId };
var cmd = _cmdGetter(pkt);
Expand All @@ -74,10 +71,12 @@ private async Task OnRequest(TArgPacket pkt, CancellationToken cancellationToken
if (confirmation != 0 && _lastCommand == cmd)
{
// do nothing, we already doing this task

_logger.ZLogWarning($"Duplicate command {pkt}): already executin {_lastCommand} command)");
await Base.SendCommandAck((MavCmd)cmd, requester,
CommandResult.FromResult(MavResult.MavResultInProgress), DisposeCancel).ConfigureAwait(false);
return;
}
_logger.ZLogWarning($"Reject command {pkt}): too busy now");
_logger.ZLogWarning($"Reject command {pkt}): too busy now (executing {_lastCommand} command)");
await Base.SendCommandAck((MavCmd)cmd, requester,
CommandResult.FromResult(MavResult.MavResultTemporarilyRejected), DisposeCancel).ConfigureAwait(false);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public ValueTask SendReached(ushort seq)
public ValueTask SendMissionCurrent(ushort current, CancellationToken cancel = default)
{
_currentMissionIndex = current;
return InternalSend<MissionCurrentPacket>(x => x.Payload.Seq = current, cancel);
return InternalSend<MissionCurrentPacket>(x =>
{
x.Payload.Seq = current;
}, cancel);
}

public ValueTask SendMissionItemInt(ServerMissionItem item,byte targetSystemId = 0, byte targetComponentId = 0,CancellationToken cancel = default)
Expand Down

0 comments on commit ad5d8ff

Please sign in to comment.