LibUsbDotNet.Main.UsbTransferQueue.transfer C# (CSharp) Method

transfer() private static method

private static transfer ( UsbTransferQueue transferParam, Handle &handle ) : ErrorCode
transferParam UsbTransferQueue
handle Handle
return ErrorCode
        private static ErrorCode transfer(UsbTransferQueue transferParam, out Handle handle)
        {
            handle = null;
            ErrorCode ret = ErrorCode.Success;

            // Submit transfers until the maximum number of outstanding transfer(s) is reached.
            while (transferParam.mOutstandingTransferCount < transferParam.MaxOutstandingIO)
            {
                if (ReferenceEquals(transferParam.mTransferHandles[transferParam.mTransferHandleNextIndex], null))
                {
                    handle = transferParam.mTransferHandles[transferParam.mTransferHandleNextIndex] =
                        new Handle(transferParam.EndpointBase.NewAsyncTransfer(), transferParam.mBuffer[transferParam.mTransferHandleNextIndex]);

                    // Get the next available benchmark transfer handle.
                    handle.Context.Fill(handle.Data, 0, handle.Data.Length, transferParam.Timeout, transferParam.IsoPacketSize);
                }
                else
                {
                    // Get the next available benchmark transfer handle.
                    handle = transferParam.mTransferHandles[transferParam.mTransferHandleNextIndex];

                }

                handle.Transferred = 0;

                // Submit this transfer now.
                handle.Context.Reset();
                ret = handle.Context.Submit();
                if (ret != ErrorCode.Success) goto Done;

                // Mark this handle has InUse.
                handle.InUse = true;

                // When transfers ir successfully submitted, OutstandingTransferCount goes up; when
                // they are completed it goes down.
                //
                transferParam.mOutstandingTransferCount++;

                // Move TransferHandleNextIndex to the next available transfer.
                IncWithRoll(ref transferParam.mTransferHandleNextIndex, transferParam.MaxOutstandingIO);
            }

            // If the number of outstanding transfers has reached the limit, wait for the 
            // oldest outstanding transfer to complete.
            //
            if (transferParam.mOutstandingTransferCount == transferParam.MaxOutstandingIO)
            {
                // TransferHandleWaitIndex is the index of the oldest outstanding transfer.
                handle = transferParam.mTransferHandles[transferParam.mTransferHandleWaitIndex];
                ret = handle.Context.Wait(out handle.Transferred, false);
                if (ret != ErrorCode.Success)
                    goto Done;

                // Mark this handle has no longer InUse.
                handle.InUse = false;

                // When transfers ir successfully submitted, OutstandingTransferCount goes up; when
                // they are completed it goes down.
                //
                transferParam.mOutstandingTransferCount--;

                // Move TransferHandleWaitIndex to the oldest outstanding transfer.
                IncWithRoll(ref transferParam.mTransferHandleWaitIndex, transferParam.MaxOutstandingIO);

                return ErrorCode.Success;
            }

        Done:
            return ret;
        }