protected override void InterruptRead() { lock (UsbLock) { if (DriverAPI != null) { DriverAPI.AbortPipe(ReadPipeId); } } }
public async Task <byte[]> ReadAsync(int timeout) { return(await Task.Run(async() => { Task <byte[]> readTask = new Task <byte[]>(() => Read()); readTask.Start(); Task runTimeout = Task.Run(async() => { await Task.Delay(timeout); while (!readTask.IsCompleted) { Device.AbortPipe(0x81); } }); return await readTask; })); }
public static void SendIni(FileInfo info) { if (!WaitingForIniInject) { IniTask = Task.Run(() => { bool attemptInject() { Device.AbortPipe(1); Device.AbortPipe(0x81); DirectoryInfo root = info.Directory; MemloaderIniData iniData = new MemloaderIniData(info); Task <bool> task; foreach (LoadData currData in iniData.LoadData) { task = DeviceWrapper.WriteAsync("RECV".ToBytes(), 2000); task.Wait(); if (!task.Result) { return(false); } FileInfo file = root.GetFile(currData.SourceFile); IEnumerable <byte> data = File.ReadAllBytes(file.FullName); int skip = (int)currData.Skip; int dataLength = data.Count() - skip; if (currData.Count > 0) { dataLength = Math.Min(dataLength, (int)currData.Count); } IEnumerable <byte> address = currData.Dest.ToBytes(4).Reverse(); IEnumerable <byte> size = dataLength.ToBytes(4).Reverse(); byte[] bytesToSend = address.Concat(size).ToArray(); task = DeviceWrapper.WriteAsync(bytesToSend, 2000); task.Wait(); if (!task.Result) { return(false); } task = DeviceWrapper.WriteAsync(data.Skip(skip).Take(dataLength).ToArray(), 2000); task.Wait(); if (!task.Result) { return(false); } } foreach (BootData currData in iniData.BootData) { Device.WritePipe(1, "BOOT".ToBytes(), 4, out int lengthTransfered, IntPtr.Zero); if (lengthTransfered != 4) { return(false); } IEnumerable <byte> pc = currData.PC.ToBytes(4).Reverse(); task = DeviceWrapper.WriteAsync(pc.ToArray(), 2000); task.Wait(); if (!task.Result) { return(false); } } return(true); }; WaitForReady(); while (!attemptInject()) { WaitForReady(); } }) .ContinueWith(t => { IniTask = null; // task is complete, discard IniInjectFinished?.Invoke(); }) .ContinueWith(t => ErrorOccurred?.Invoke(), TaskContinuationOptions.OnlyOnFaulted); // catch exceptions and inform delegate } }