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