Mercurial > hg > ucis.core
annotate Net/TCPStream.cs @ 49:7e4dae99f919
USBLib: fix unhandled exception in WinUSB shutdown after failure to open device
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 19 Aug 2013 16:56:50 +0200 |
parents | 4b78cc5f116b |
children | 4ba4fd48e1da |
rev | line source |
---|---|
0 | 1 ???using System; |
2 using System.IO; | |
3 using System.Net; | |
4 using System.Net.Sockets; | |
5 using System.Threading; | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
6 using UCIS.Util; |
2
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
7 using SysThreadPool = System.Threading.ThreadPool; |
0 | 8 |
9 namespace UCIS.Net { | |
10 public class TCPStream : Stream, INetworkConnection { | |
11 private static long _totalBytesRead = 0; | |
12 private static long _totalBytesWritten = 0; | |
13 public static ulong TotalBytesWritten { get { return (ulong)_totalBytesWritten; } set { _totalBytesWritten = (long)value; } } | |
14 public static ulong TotalBytesRead { get { return (ulong)_totalBytesRead; } set { _totalBytesRead = (long)value; } } | |
15 | |
16 private Socket _Socket; | |
17 private byte _PeekByte; | |
18 private bool _HasPeekByte; | |
19 private ulong _BytesWritten; | |
20 private ulong _BytesRead; | |
21 private DateTime _StartTime; | |
22 private bool _Blocking; | |
23 | |
24 public event EventHandler Closed; | |
25 | |
26 public TCPStream(Socket Socket) { | |
27 _Socket = Socket; | |
28 _HasPeekByte = false; | |
29 _StartTime = DateTime.Now; | |
30 _Blocking = _Socket.Blocking; | |
31 } | |
32 | |
33 public Socket Socket { | |
34 get { return _Socket; } | |
35 } | |
36 | |
37 public DateTime CreationTime { | |
38 get { return _StartTime; } | |
39 } | |
40 | |
41 public bool Blocking { | |
42 get { return _Blocking; } | |
43 set { | |
44 Socket.Blocking = value; | |
45 _Blocking = value; | |
46 } | |
47 } | |
48 | |
49 public bool NoDelay { | |
50 get { return Socket.NoDelay; } | |
51 set { Socket.NoDelay = value; } | |
52 } | |
53 | |
54 public override bool CanTimeout { | |
55 get { return true; } | |
56 } | |
57 | |
58 public override int ReadTimeout { | |
59 get { return Socket.ReceiveTimeout; } | |
60 set { Socket.ReceiveTimeout = value; } | |
61 } | |
62 | |
63 public override int WriteTimeout { | |
64 get { return Socket.SendTimeout; } | |
65 set { Socket.SendTimeout = value; } | |
66 } | |
67 | |
68 public override int ReadByte() { | |
69 if (_HasPeekByte) { | |
70 _HasPeekByte = false; | |
71 return _PeekByte; | |
72 } else { | |
73 byte[] Buffer = new byte[1]; | |
74 if (Read(Buffer, 0, 1) != 1) return -1; | |
75 return Buffer[0]; | |
76 } | |
77 } | |
78 | |
79 public override int Read(byte[] buffer, int offset, int size) { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
80 if (size < 1) return 0; |
0 | 81 int Count = 0; |
82 if (_HasPeekByte) { | |
83 buffer[offset] = _PeekByte; | |
84 _HasPeekByte = false; | |
85 Count = 1; | |
86 offset += 1; | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
87 size = 0; |
0 | 88 } |
89 try { | |
90 if (size > 0) Count += Socket.Receive(buffer, offset, size, SocketFlags.None); | |
91 } catch (SocketException ex) { | |
92 switch (ex.SocketErrorCode) { | |
93 case SocketError.WouldBlock: | |
94 _Socket.Blocking = _Blocking; | |
95 throw new TimeoutException("The receive operation would block", ex); | |
96 case SocketError.TimedOut: | |
97 throw new TimeoutException("The receive operation timed out", ex); | |
98 case SocketError.ConnectionReset: | |
99 case SocketError.Disconnecting: | |
100 case SocketError.NetworkDown: | |
101 case SocketError.NetworkReset: | |
102 case SocketError.NetworkUnreachable: | |
103 case SocketError.NotConnected: | |
104 Close(); | |
105 throw new SocketException((int)ex.SocketErrorCode); | |
106 default: | |
107 throw new SocketException((int)ex.SocketErrorCode); | |
108 } | |
109 } | |
110 | |
111 _BytesRead += (ulong)Count; | |
112 Interlocked.Add(ref _totalBytesRead, (long)Count); | |
113 return Count; | |
114 } | |
115 | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
116 class AsyncResult : AsyncResultBase { |
2
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
117 public int Count { get; private set; } |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
118 public AsyncResult(AsyncCallback callback, Object state) : base(callback, state) { } |
2
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
119 public void SetCompleted(Boolean synchronously, int cnt) { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
120 Count = cnt; |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
121 base.SetCompleted(synchronously, null); |
2
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
122 } |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
123 } |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
124 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
125 if (count < 0) { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
126 AsyncResult ar = new AsyncResult(callback, state); |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
127 ar.SetCompleted(true, 0); |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
128 return ar; |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
129 } else if (_HasPeekByte) { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
130 buffer[offset] = _PeekByte; |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
131 _HasPeekByte = false; |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
132 AsyncResult ar = new AsyncResult(callback, state); |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
133 ar.SetCompleted(true, 1); |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
134 return ar; |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
135 } else { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
136 return Socket.BeginReceive(buffer, offset, count, SocketFlags.None, callback, state); |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
137 } |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
138 } |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
139 public override int EndRead(IAsyncResult asyncResult) { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
140 if (asyncResult is AsyncResult) { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
141 return ((AsyncResult)asyncResult).Count; |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
142 } else { |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
143 return Socket.EndReceive(asyncResult); |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
144 } |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
145 } |
d0117dc37c34
Added asynchronous read function to TCPStream (untested!)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
146 |
0 | 147 public int PeekByte() { |
148 if (_HasPeekByte) { | |
149 return _PeekByte; | |
150 } else { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
151 int Result = ReadByte(); |
0 | 152 if (Result >= 0 && Result <= 255) { |
153 _PeekByte = (byte)Result; | |
154 _HasPeekByte = true; | |
155 } | |
156 return Result; | |
157 } | |
158 } | |
159 | |
160 public int WriteBufferSize { | |
161 get { return Socket.SendBufferSize; } | |
162 set { Socket.SendBufferSize = value; } | |
163 } | |
164 | |
165 public int ReadBufferSize { | |
166 get { return Socket.ReceiveBufferSize; } | |
167 set { Socket.ReceiveBufferSize = value; } | |
168 } | |
169 | |
170 public override bool CanRead { | |
171 get { return Socket.Connected && (Blocking || (Socket.Available > 0)); } | |
172 } | |
173 | |
174 public override bool CanSeek { | |
175 get { return false; } | |
176 } | |
177 | |
178 public override bool CanWrite { | |
179 get { return Socket.Connected; } | |
180 } | |
181 | |
182 public override void Flush() { | |
183 //Do nothing | |
184 //_Socket.NoDelay = true; | |
185 } | |
186 | |
187 public override long Length { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
188 get { throw new NotSupportedException(); } |
0 | 189 } |
190 public override long Position { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
191 get { throw new NotSupportedException(); } |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
192 set { throw new NotSupportedException(); } |
0 | 193 } |
194 | |
195 public override long Seek(long offset, SeekOrigin origin) { | |
196 throw new NotSupportedException(); | |
197 } | |
198 | |
199 public override void SetLength(long value) { | |
200 throw new NotSupportedException(); | |
201 } | |
202 | |
203 public override void Write(byte[] buffer, int offset, int size) { | |
204 int left = size; | |
205 if (_Socket == null) throw new ObjectDisposedException("socket"); | |
206 try { | |
207 while (left > 0) { | |
208 int sent = _Socket.Send(buffer, offset, left, 0); | |
209 left -= sent; | |
210 offset += sent; | |
211 } | |
212 } catch (SocketException ex) { | |
213 switch (ex.SocketErrorCode) { | |
214 case SocketError.WouldBlock: | |
215 _Socket.Blocking = _Blocking; | |
216 throw new TimeoutException("The send operation would block", ex); | |
217 case SocketError.TimedOut: | |
218 throw new TimeoutException("The send operation timed out", ex); | |
219 case SocketError.ConnectionReset: | |
220 case SocketError.Disconnecting: | |
221 case SocketError.NetworkDown: | |
222 case SocketError.NetworkReset: | |
223 case SocketError.NetworkUnreachable: | |
224 case SocketError.NotConnected: | |
225 Close(); | |
226 throw new SocketException((int)ex.SocketErrorCode); | |
227 default: | |
228 throw new SocketException((int)ex.SocketErrorCode); | |
229 } | |
230 } | |
231 _BytesWritten += (ulong)size; | |
232 Interlocked.Add(ref _totalBytesWritten, (long)size); | |
233 } | |
234 | |
235 public override void Close() { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
2
diff
changeset
|
236 Socket s = Interlocked.Exchange(ref _Socket, null); |
0 | 237 try { |
238 if (s != null) { | |
239 try { | |
240 if (s.Connected) s.Shutdown(SocketShutdown.Both); | |
241 } catch { } | |
242 s.Close(); | |
243 } | |
244 } finally { | |
245 base.Close(); | |
246 if (Closed != null) Closed(this, new EventArgs()); | |
247 } | |
248 } | |
249 | |
250 public bool Connected { | |
251 get { | |
252 if (Socket == null) return false; | |
253 if (Socket.Connected) return true; | |
254 Close(); | |
255 return false; | |
256 } | |
257 } | |
258 | |
259 public object Tag { get; set; } | |
260 | |
261 public ulong BytesWritten { get { return _BytesWritten; } } | |
262 public ulong BytesRead { get { return _BytesRead; } } | |
263 public TimeSpan Age { get { return DateTime.Now.Subtract(_StartTime); } } | |
264 public EndPoint RemoteEndPoint { | |
265 get { | |
266 if (_Socket == null || !_Socket.Connected) return null; | |
267 try { | |
268 return _Socket.RemoteEndPoint; | |
269 } catch (SocketException) { | |
270 return null; | |
271 } | |
272 } | |
273 } | |
274 //public Object Proxy { get { return null; } } | |
275 Object INetworkConnection.Handler { get { return Tag; } } | |
276 } | |
277 } |