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

USBPipeCollection.cs « WinUSBNet « FelLib - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a91d8d58ab2d107bb042373d3b8408ded2173d0e (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
/*  WinUSBNet library
 *  (C) 2010 Thomas Bleeker (www.madwizard.org)
 *  
 *  Licensed under the MIT license, see license.txt or:
 *  http://www.opensource.org/licenses/mit-license.php
 */

using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;

namespace MadWizard.WinUSBNet
{
    /// <summary>
    /// Collection of UsbPipe objects
    /// </summary>
    public class USBPipeCollection : IEnumerable<USBPipe>
    {
        private Dictionary<byte, USBPipe> _pipes;

        internal USBPipeCollection(USBPipe[] pipes)
        {
            _pipes = new Dictionary<byte, USBPipe>(pipes.Length);
            foreach (USBPipe pipe in pipes)
            {
                if (_pipes.ContainsKey(pipe.Address))
                    throw new USBException("Duplicate pipe address in endpoint.");
                _pipes[pipe.Address] = pipe;
            }
        }

        /// <summary>
        /// Returns the pipe from the collection with the given pipe address 
        /// </summary>
        /// <param name="pipeAddress">Address of the pipe to return</param>
        /// <returns>The pipe with the given pipe address</returns>
        /// <exception cref="IndexOutOfRangeException">Thrown if no pipe with the specified address
        /// is available in the collection.</exception>
        public USBPipe this [byte pipeAddress]
        {
            get
            {
                USBPipe pipe;
                if (!_pipes.TryGetValue(pipeAddress, out pipe))
                    throw new IndexOutOfRangeException();
                return pipe;
            }
        }

        private class UsbPipeEnumerator : IEnumerator<USBPipe>
        {
            private int _index;
            private USBPipe[] _pipes;

            public UsbPipeEnumerator(USBPipe[] pipes)
            {
                _pipes = pipes;
                _index = -1;
            }

            public void Dispose()
            {
                // Empty
            }
            private USBPipe GetCurrent()
            {
                try
                {
                    return _pipes[_index];
                }

                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }

            public USBPipe Current
            {
                get
                {
                    return GetCurrent(); 
                }
            }
            
            
            object IEnumerator.Current
            {
                get
                {
                    return GetCurrent();
                }
            }

            public bool MoveNext()
            {
                _index++;
                return _index < _pipes.Length;
            }

            public void Reset()
            {
                _index = -1;
            }

        }

        private USBPipe[] GetPipeList()
        {
            var values = _pipes.Values;
            USBPipe[] pipeList = new USBPipe[values.Count];
            values.CopyTo(pipeList, 0);
            return pipeList;
        }

        /// <summary>
        /// Returns a typed enumerator that iterates through a collection.
        /// </summary>
        /// <returns>The enumerator object that can be used to iterate through the collection.</returns>
        public IEnumerator<USBPipe> GetEnumerator()
        {
            return new UsbPipeEnumerator(GetPipeList());
        }
       
        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>An IEnumerator object that can be used to iterate through the collection.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return new UsbPipeEnumerator(GetPipeList());
        }
    }
}