-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
049e2af
commit 0b361e9
Showing
14 changed files
with
353 additions
and
281 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Drone.CommModules; | ||
|
||
public class ExtCommModule : P2PCommModule | ||
{ | ||
public override ModuleType Type => ModuleType.P2P; | ||
public override ModuleMode Mode { get; } | ||
|
||
public override bool Running { get; protected set; } | ||
|
||
public override event Func<C2Frame, Task> FrameReceived; | ||
public override event Action OnException; | ||
|
||
private readonly CancellationTokenSource _tokenSource = new(); | ||
|
||
// this will be queued by the client controller via reflection | ||
public static ConcurrentQueue<byte[]> Inbound { get; set; } = new(); | ||
|
||
// this will be dequeued by the client controller via reflection | ||
public static ConcurrentQueue<byte[]> Outbound { get; set; } = new(); | ||
|
||
public override void Init(Metadata metadata) | ||
{ | ||
// nothing | ||
} | ||
|
||
public override Task SendFrame(C2Frame frame) | ||
{ | ||
Outbound.Enqueue(frame.Serialize()); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public override Task Start() | ||
{ | ||
Running = true; | ||
return Task.CompletedTask; | ||
} | ||
|
||
public override async Task Run() | ||
{ | ||
while (!_tokenSource.IsCancellationRequested) | ||
{ | ||
while (Inbound.TryDequeue(out var data)) | ||
{ | ||
try | ||
{ | ||
var frame = data.Deserialize<C2Frame>(); | ||
FrameReceived?.Invoke(frame); | ||
} | ||
catch | ||
{ | ||
OnException?.Invoke(); | ||
return; | ||
} | ||
} | ||
|
||
await Task.Delay(100); | ||
} | ||
|
||
_tokenSource.Dispose(); | ||
} | ||
|
||
public override void Stop() | ||
{ | ||
Running = false; | ||
_tokenSource.Cancel(); | ||
} | ||
} |
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
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
Oops, something went wrong.