Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TransferContextBase.cs « Internal « LibWinUsb - github.com/ClusterM/clovershell-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5156fc9aefc7b6b5e400e0f1400482541978330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright © 2006-2009 Travis Robinson. All rights reserved.
// 
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail:  libusbdotnet@gmail.com
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or 
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. or 
// visit www.gnu.org.
// 
// 
using System;
using System.Threading;
using LibUsbDotNet.Main;
using Microsoft.Win32.SafeHandles;

namespace LibUsbDotNet.Internal
{
    public abstract class TransferContextBase : IDisposable
    {
        private readonly UsbEndpointBase mEndpointBase;

        private IntPtr mBuffer;
        private int mCurrentOffset;
        private int mCurrentRemaining;
        private int mCurrentTransmitted;

        private int mFailRetries;
        protected int mOriginalCount;
        protected int mOriginalOffset;
        private PinnedHandle mPinnedHandle;

        protected int mTimeout;

        protected bool mHasWaitBeenCalled = true;

        protected ManualResetEvent mTransferCancelEvent = new ManualResetEvent(false);
        protected internal ManualResetEvent mTransferCompleteEvent = new ManualResetEvent(true);

        protected TransferContextBase(UsbEndpointBase endpointBase) { mEndpointBase = endpointBase; }

        public UsbEndpointBase EndpointBase
        {
            get { return mEndpointBase; }
        }

        protected int RequestCount
        {
            get { return (mCurrentRemaining > UsbEndpointBase.MaxReadWrite ? UsbEndpointBase.MaxReadWrite : mCurrentRemaining); }
        }

        protected int FailRetries
        {
            get { return mFailRetries; }
        }

        protected IntPtr NextBufPtr
        {
            get { return new IntPtr(mBuffer.ToInt64() + mCurrentOffset); }
        }

        public bool IsCancelled
        {
            get { return mTransferCancelEvent.WaitOne(0, UsbConstants.EXIT_CONTEXT); }
        }

        public bool IsComplete
        {
            get { return mTransferCompleteEvent.WaitOne(0, UsbConstants.EXIT_CONTEXT); }
        }

        public SafeWaitHandle CancelWaitHandle
        {
            get { return mTransferCancelEvent.SafeWaitHandle; }
        }

        public SafeWaitHandle CompleteWaitHandle
        {
            get { return mTransferCompleteEvent.SafeWaitHandle; }
        }

        #region IDisposable Members

        public virtual void Dispose()
        {
            if (!IsCancelled) Cancel();

            int dummy;
            if (!mHasWaitBeenCalled) Wait(out dummy);
        }

        #endregion

        public virtual ErrorCode Cancel()
        {
            mTransferCancelEvent.Set();
            return ErrorCode.Success;
        }

        public abstract ErrorCode Submit();

        public abstract ErrorCode Wait(out int transferredCount);


        public virtual void Fill(object buffer, int offset, int count, int timeout)
        {
            if (mPinnedHandle != null) mPinnedHandle.Dispose();
            mPinnedHandle = new PinnedHandle(buffer);
            Fill(mPinnedHandle.Handle, offset, count, timeout);
        }

        public virtual void Fill(IntPtr buffer, int offset, int count, int timeout)
        {
            mBuffer = buffer;

            mOriginalOffset = offset;
            mOriginalCount = count;
            mTimeout = timeout;
            Reset();
        }

        internal static ErrorCode SyncTransfer(TransferContextBase transferContext,
                                               IntPtr buffer,
                                               int offset,
                                               int length,
                                               int timeout,
                                               out int transferLength)
        {
            if (ReferenceEquals(transferContext, null)) throw new NullReferenceException("Invalid transfer context.");
            if (offset < 0) throw new ArgumentException("must be >=0", "offset");

            lock (transferContext)
            {
                transferLength = 0;

                int transferred;
                ErrorCode ec;

                transferContext.Fill(buffer, offset, length, timeout);

                while (true)
                {
                    ec = transferContext.Submit();
                    if (ec == ErrorCode.IoEndpointGlobalCancelRedo) continue;
                    if (ec != ErrorCode.Success) return ec;

                    ec = transferContext.Wait(out transferred);
                    if (ec == ErrorCode.IoEndpointGlobalCancelRedo) continue;
                    if (ec != ErrorCode.Success) return ec;

                    transferLength += transferred;

                    if ((ec != ErrorCode.None || transferred != UsbEndpointBase.MaxReadWrite) ||
                        !transferContext.IncrementTransfer(transferred))
                        break;
                }

                return ec;
            }
        }

        public bool IncrementTransfer(int amount)
        {
            mCurrentTransmitted += amount;
            mCurrentOffset += amount;
            mCurrentRemaining -= amount;

            if (mCurrentRemaining <= 0) return false;

            return true;
        }

        protected void IncFailRetries() { mFailRetries++; }

        public void Reset()
        {
            mCurrentOffset = mOriginalOffset;
            mCurrentRemaining = mOriginalCount;
            mCurrentTransmitted = 0;
            mFailRetries = 0;

            mTransferCancelEvent.Reset();
        }
    }
}