annotate Util/QueuedPacketStream.cs @ 36:c4a5dbe62513

USBLib: small fix in WinUSB backend bounds checking
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 21 Apr 2013 18:32:43 +0200
parents 9533a87363f3
children 819fb56a56ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2 using System.Collections.Generic;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3 using System.IO;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4 using System.Threading;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5 using SysThreadPool = System.Threading.ThreadPool;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7 namespace UCIS.Util {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 public abstract class QueuedPacketStream : PacketStream {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 Queue<Byte[]> ReceiveQueue = new Queue<byte[]>();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10 Byte[] ReceiveBuffer = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 int ReceiveBufferOffset = 0;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 int ReceiveWaiting = 0;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 AutoResetEvent ReceiveEvent = new AutoResetEvent(false);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 AsyncResult AsyncReceiveOperation = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 protected Boolean Closed { get; private set; }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 public QueuedPacketStream() {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 ReadTimeout = Timeout.Infinite;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19 Closed = false;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
20 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
22 protected void AddReadBufferCopy(Byte[] buffer, int offset, int count) {
9
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
23 Byte[] store = new Byte[count];
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
24 Buffer.BlockCopy(buffer, offset, store, 0, count);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
25 AddReadBufferNoCopy(store);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
26 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
27 protected void AddReadBufferNoCopy(Byte[] store) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
28 if (Closed) return;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 lock (ReceiveQueue) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30 ReceiveQueue.Enqueue(store);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
31 Interlocked.Add(ref ReceiveWaiting, store.Length);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 ReceiveEvent.Set();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
33 if (AsyncReceiveOperation != null && (store.Length > 0 || AsyncReceiveOperation.IsReadPacket)) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
34 AsyncReceiveOperation.SetCompleted(false);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
35 AsyncReceiveOperation = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
36 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
37 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
38 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
39 public override void Close() {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
40 Closed = true;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 base.Close();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 ReceiveEvent.Set();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 lock (ReceiveQueue) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 if (AsyncReceiveOperation != null) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
45 AsyncReceiveOperation.SetCompleted(false);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
46 AsyncReceiveOperation = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
47 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
49 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
50
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 public override bool CanSeek { get { return false; } }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
52 public override bool CanTimeout { get { return true; } }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
53 public override bool CanRead { get { return !Closed; } }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
54 public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
55 public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
56 public override void SetLength(long value) { throw new NotSupportedException(); }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
57
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
58 public override int ReadTimeout { get; set; }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
59 public override long Length { get { return ReceiveWaiting; } }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 public int WaitForPacket() {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62 while (ReceiveBuffer == null) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63 lock (ReceiveQueue) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
64 if (ReceiveQueue.Count > 0) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
65 ReceiveBuffer = ReceiveQueue.Dequeue();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
66 ReceiveBufferOffset = 0;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
67 continue;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
68 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
69 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
70 if (Closed) throw new ObjectDisposedException("QueuedPacketStream", "The connection has been closed");
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
71 if (ReadTimeout == 0 || !ReceiveEvent.WaitOne(ReadTimeout, false)) throw new TimeoutException();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
72 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
73 return ReceiveBuffer.Length - ReceiveBufferOffset;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
74 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
75 public override int Read(byte[] buffer, int offset, int count) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
76 int left = 0;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
77 while (true) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
78 left = WaitForPacket();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 if (left > 0) break;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 ReceiveBuffer = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
82 if (count > left) count = left;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
83 Buffer.BlockCopy(ReceiveBuffer, ReceiveBufferOffset, buffer, offset, count);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
84 ReceiveBufferOffset += count;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
85 if (ReceiveBufferOffset == ReceiveBuffer.Length) ReceiveBuffer = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
86 Interlocked.Add(ref ReceiveWaiting, -count);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
87 return count;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
88 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
89 public override Byte[] ReadPacket() {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
90 WaitForPacket();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
91 Byte[] arr = ReceiveBuffer;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92 if (ReceiveBufferOffset > 0) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 arr = new Byte[ReceiveBuffer.Length - ReceiveBufferOffset];
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
94 Buffer.BlockCopy(ReceiveBuffer, ReceiveBufferOffset, arr, 0, arr.Length - ReceiveBufferOffset);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96 ReceiveBuffer = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 return arr;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
98 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
99 public override ArraySegment<byte> ReadPacketFast() {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
100 WaitForPacket();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
101 ArraySegment<byte> ret = new ArraySegment<byte>(ReceiveBuffer, ReceiveBufferOffset, ReceiveBuffer.Length - ReceiveBufferOffset);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 ReceiveBuffer = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
103 return ret;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
104 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
105
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
106 class AsyncResult : AsyncResultBase {
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
107 public Boolean IsReadPacket { get; private set; }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
108 public Byte[] Buffer = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
109 public int BufferOffset = 0;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
110 public int BufferLength = 0;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
111
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
112 public void SetCompleted(Boolean synchronously) {
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
113 base.SetCompleted(synchronously, null);
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
114 }
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
115 public AsyncResult(AsyncCallback callback, Object state) : base(callback, state) {
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
116 IsReadPacket = true;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
117 }
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
118 public AsyncResult(AsyncCallback callback, Object state, Byte[] buffer, int bufferOffset, int bufferLength) : base(callback, state) {
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
119 this.Buffer = buffer;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
120 this.BufferOffset = bufferOffset;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
121 this.BufferLength = bufferLength;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
122 IsReadPacket = false;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
123 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
124 }
7
4b78cc5f116b Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
125
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
126 private IAsyncResult BeginAsyncReadOperation(AsyncResult ar) {
9
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
127 Boolean synccompleted = false;
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
128 lock (ReceiveQueue) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
129 if (AsyncReceiveOperation != null) throw new InvalidOperationException("Another asynchronous operation is in progress");
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
130 if (ReceiveBuffer != null || ReceiveQueue.Count > 0) {
9
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
131 synccompleted = true;
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
132 } else {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
133 if (Closed) throw new ObjectDisposedException("QueuedPacketStream", "The connection has been closed");
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
134 AsyncReceiveOperation = ar;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
135 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
136 }
9
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 7
diff changeset
137 if (synccompleted) ar.SetCompleted(true);
1
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
138 return ar;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
139 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
140 private void EndAsyncReadOperation(AsyncResult ar) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
141 lock (ReceiveQueue) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
142 if (AsyncReceiveOperation != null && ar != AsyncReceiveOperation) throw new InvalidOperationException("The given AsyncResult object does not match the current pending operation");
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
143 AsyncReceiveOperation = null;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
144 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
145 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
146 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
147 return BeginAsyncReadOperation(new AsyncResult(callback, state, buffer, offset, count));
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
148 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
149 public override IAsyncResult BeginReadPacket(AsyncCallback callback, object state) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
150 return BeginAsyncReadOperation(new AsyncResult(callback, state));
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
151 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
152 public override IAsyncResult BeginReadPacketFast(AsyncCallback callback, object state) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
153 return BeginAsyncReadOperation(new AsyncResult(callback, state));
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
154 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
155 public override int EndRead(IAsyncResult asyncResult) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
156 AsyncResult ar = (AsyncResult)asyncResult;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
157 EndAsyncReadOperation(ar);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
158 return Read(ar.Buffer, ar.BufferOffset, ar.BufferLength);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
159 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
160 public override Byte[] EndReadPacket(IAsyncResult asyncResult) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
161 AsyncResult ar = (AsyncResult)asyncResult;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
162 EndAsyncReadOperation(ar);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
163 return ReadPacket();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
164 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
165 public override ArraySegment<Byte> EndReadPacketFast(IAsyncResult asyncResult) {
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
166 AsyncResult ar = (AsyncResult)asyncResult;
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
167 EndAsyncReadOperation(ar);
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
168 return ReadPacketFast();
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
169 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
170 }
28dc7d535036 Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
171 }