Esempio n. 1
0
        private async Task <T> SendPacketTaskAsync <T>(IdpPacket packet, int timeout) where T : Transaction
        {
            T          result = null;
            List <int> x      = new List <int>();

            var command           = commandManager.GetCommand <T>();
            var packetReceivedTcs = new TaskCompletionSource <T>();

            EventHandler <T> commandReceivedHandler = (sender, e) =>
            {
                if (!packetReceivedTcs.Task.IsCompleted)
                {
                    packetReceivedTcs.SetResult(e);
                }
            };

            command.CommandReceived += commandReceivedHandler;

            configurationCommsThread.InvokeAsync(() =>
            {
                packet.Insert(0, (byte)packet.Count);
                packet.Insert(0, 0x01);
                configurationDevice.WriteFeatureData(packet.ToArray());
            });

            if (await Task.WhenAny(packetReceivedTcs.Task, Task.Delay(timeout)) == packetReceivedTcs.Task)
            {
                result = packetReceivedTcs.Task.Result;
            }
            else
            {
                result = null;
            }

            command.CommandReceived -= commandReceivedHandler;

            return(result);
        }
 public override bool Encode(IdpPacket packet)
 {
     return(true);
 }