comparison Net/ConnectionList.cs @ 0:3ab940a0c7a0

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 11 Sep 2012 16:28:53 +0200
parents
children 819fb56a56ea
comparison
equal deleted inserted replaced
-1:000000000000 0:3ab940a0c7a0
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4
5 namespace UCIS.Net {
6 public class NetworkConnectionList : ICollection<INetworkConnection> {
7 List<INetworkConnection> _list = new List<INetworkConnection>();
8
9 public ulong BytesWritten {
10 get {
11 ulong sum = 0;
12 lock (_list) foreach (INetworkConnection c in _list) sum += c.BytesWritten;
13 return sum;
14 }
15 }
16 public ulong BytesRead {
17 get {
18 ulong sum = 0;
19 lock (_list) foreach (INetworkConnection c in _list) sum += c.BytesRead;
20 return sum;
21 }
22 }
23 public NetworkConnectionList FindByHandler(Object Handler) {
24 NetworkConnectionList l = new NetworkConnectionList();
25 lock (_list) foreach (INetworkConnection c in _list) if (c.Handler == Handler) l.Add(c);
26 return l;
27 }
28 public NetworkConnectionList FindByHandlerType(Type HandlerType) {
29 NetworkConnectionList l = new NetworkConnectionList();
30 lock (_list) foreach (INetworkConnection c in _list) if (c.Handler.GetType() == HandlerType) l.Add(c);
31 return l;
32 }
33 public void CloseAll() {
34 foreach (INetworkConnection c in ToArray()) c.Close();
35 }
36
37 public void Add(INetworkConnection item) {
38 if (item == null) throw new ArgumentNullException("item");
39 lock (_list) _list.Add(item);
40 item.Closed += _ConnectionClosed;
41 if (!item.Connected) Remove(item);
42 }
43 public void Insert(int index, INetworkConnection item) {
44 throw new NotSupportedException();
45 }
46 public bool Remove(INetworkConnection item) {
47 if (item == null) throw new ArgumentNullException("item");
48 item.Closed -= _ConnectionClosed;
49 lock (_list) return _list.Remove(item);
50 }
51 public void RemoveAt(int index) {
52 lock (_list) {
53 INetworkConnection item = _list[index];
54 item.Closed -= _ConnectionClosed;
55 _list.Remove(item);
56 }
57 }
58 public int Count { get { lock (_list) return _list.Count; } }
59 public bool IsReadOnly { get { return false; } }
60 public bool Contains(INetworkConnection item) {
61 lock (_list) return _list.Contains(item);
62 }
63 public void Clear() {
64 lock (_list) {
65 foreach (INetworkConnection c in _list) c.Closed -= _ConnectionClosed;
66 _list.Clear();
67 }
68 }
69 public INetworkConnection this[int index] {
70 get { return _list[index]; }
71 set { throw new NotSupportedException(); }
72 }
73 public int IndexOf(INetworkConnection item) {
74 lock (_list) return _list.IndexOf(item);
75 }
76
77 IEnumerator IEnumerable.GetEnumerator() {
78 return new Enumerator(_list);
79 }
80 public IEnumerator<INetworkConnection> GetEnumerator() {
81 return new Enumerator(_list);
82 }
83
84 public void CopyTo(INetworkConnection[] array, int offset) {
85 lock (_list) _list.CopyTo(array, offset);
86 }
87
88 public INetworkConnection[] ToArray() {
89 lock (_list) return _list.ToArray();
90 }
91
92 private void _ConnectionClosed(Object sender, EventArgs e) {
93 if (sender == null || !(sender is INetworkConnection)) return;
94 Remove((INetworkConnection)sender);
95 }
96
97 private class Enumerator : IEnumerator<INetworkConnection> {
98 int index = 0;
99 INetworkConnection current = null;
100 IList<INetworkConnection> list;
101
102 public Enumerator(IList<INetworkConnection> list) {
103 this.list = list;
104 }
105
106 public void Reset() {
107 index = 0;
108 }
109 public bool MoveNext() {
110 lock (list) {
111 if (index < list.Count) {
112 current = list[index];
113 index++;
114 return true;
115 } else {
116 current = null;
117 return false;
118 }
119 }
120 }
121 public INetworkConnection Current {
122 get { return current; }
123 }
124 object IEnumerator.Current {
125 get { return current; }
126 }
127 public void Dispose() {
128 current = null;
129 list = null;
130 }
131 }
132 }
133 }