changeset 9:9533a87363f3

Fixed deadlock in QueuedPacketStream
author Ivo Smits <Ivo@UCIS.nl>
date Fri, 18 Jan 2013 16:29:50 +0100
parents 9525fb2d14ec
children 7269e91c6e26
files Util/ArrayUtil.cs Util/QueuedPacketStream.cs
diffstat 2 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Util/ArrayUtil.cs	Wed Jan 16 23:27:06 2013 +0100
+++ b/Util/ArrayUtil.cs	Fri Jan 18 16:29:50 2013 +0100
@@ -19,6 +19,9 @@
 			input.CopyTo(output, 0);
 			return output;
 		}
+		public static T[] ToArray<T>(ArraySegment<T> input) {
+			return Slice(input.Array, input.Offset, input.Count);
+		}
 		public static IList<T> ToList<T>(IEnumerable<T> input) {
 			return new List<T>(input);
 		}
--- a/Util/QueuedPacketStream.cs	Wed Jan 16 23:27:06 2013 +0100
+++ b/Util/QueuedPacketStream.cs	Fri Jan 18 16:29:50 2013 +0100
@@ -20,8 +20,7 @@
 		}
 
 		protected void AddReadBufferCopy(Byte[] buffer, int offset, int count) {
-			Byte[] store;
-			store = new Byte[count];
+			Byte[] store = new Byte[count];
 			Buffer.BlockCopy(buffer, offset, store, 0, count);
 			AddReadBufferNoCopy(store);
 		}
@@ -125,15 +124,17 @@
 		}
 
 		private IAsyncResult BeginAsyncReadOperation(AsyncResult ar) {
+			Boolean synccompleted = false;
 			lock (ReceiveQueue) {
 				if (AsyncReceiveOperation != null) throw new InvalidOperationException("Another asynchronous operation is in progress");
 				if (ReceiveBuffer != null || ReceiveQueue.Count > 0) {
-					ar.SetCompleted(true);
+					synccompleted = true;
 				} else {
 					if (Closed) throw new ObjectDisposedException("QueuedPacketStream", "The connection has been closed");
 					AsyncReceiveOperation = ar;
 				}
 			}
+			if (synccompleted) ar.SetCompleted(true);
 			return ar;
 		}
 		private void EndAsyncReadOperation(AsyncResult ar) {