Skip to content

Commit

Permalink
MAVFtp: add lock on dir list
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Mar 17, 2023
1 parent c8108d4 commit 77b9c30
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ExtLibs/ArduPilot/Mavlink/MAVFtp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ public class MAVFtp
/// incremented anytime its not a retransmit
private uint16_t seq_no = 0;

static Dictionary<(int, int), object> locker = new Dictionary<(int, int), object>();

public MAVFtp(MAVLinkInterface mavint, byte sysid, byte compid)
{
_mavint = mavint;
_sysid = sysid;
_compid = compid;

locker[(sysid, compid)] = new object();
}

public enum errno
Expand Down Expand Up @@ -1211,7 +1215,9 @@ public List<FtpFileInfo> kCmdListDirectory(string dir, CancellationTokenSource c
Progress?.Invoke(dir + " Listing", 0);
Exception ex = null;
var timeout = new RetryTimeout(5);
var sub = _mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.FILE_TRANSFER_PROTOCOL,
lock (locker[(_sysid, _compid)])
{
var sub = _mavint.SubscribeToPacketType(MAVLink.MAVLINK_MSG_ID.FILE_TRANSFER_PROTOCOL,
message =>
{
if (cancel != null && cancel.IsCancellationRequested)
Expand Down Expand Up @@ -1338,8 +1344,9 @@ public List<FtpFileInfo> kCmdListDirectory(string dir, CancellationTokenSource c

_mavint.sendPacket(fileTransferProtocol, _sysid, _compid);
};
var ans = timeout.DoWork();
_mavint.UnSubscribeToPacketType(sub);
var ans = timeout.DoWork();
_mavint.UnSubscribeToPacketType(sub);
}
Progress?.Invoke(dir + " Ready", 100);
if (ex != null)
throw ex;
Expand Down

0 comments on commit 77b9c30

Please sign in to comment.