comparison Util/QueuedPacketStream.cs @ 7:4b78cc5f116b

Fixes and improvements (some untested)
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 13 Jan 2013 18:44:17 +0100
parents 28dc7d535036
children 9533a87363f3
comparison
equal deleted inserted replaced
6:5ce7a138fdba 7:4b78cc5f116b
102 ArraySegment<byte> ret = new ArraySegment<byte>(ReceiveBuffer, ReceiveBufferOffset, ReceiveBuffer.Length - ReceiveBufferOffset); 102 ArraySegment<byte> ret = new ArraySegment<byte>(ReceiveBuffer, ReceiveBufferOffset, ReceiveBuffer.Length - ReceiveBufferOffset);
103 ReceiveBuffer = null; 103 ReceiveBuffer = null;
104 return ret; 104 return ret;
105 } 105 }
106 106
107 class AsyncResult : IAsyncResult { 107 class AsyncResult : AsyncResultBase {
108 public Object AsyncState { get; private set; }
109 public WaitHandle AsyncWaitHandle { get { return WaitHandle; } }
110 public Boolean CompletedSynchronously { get; private set; }
111 public Boolean IsCompleted { get; private set; }
112 public Boolean IsReadPacket { get; private set; } 108 public Boolean IsReadPacket { get; private set; }
113
114 public Byte[] Buffer = null; 109 public Byte[] Buffer = null;
115 public int BufferOffset = 0; 110 public int BufferOffset = 0;
116 public int BufferLength = 0; 111 public int BufferLength = 0;
117 112
118 private ManualResetEvent WaitHandle = new ManualResetEvent(false); 113 public void SetCompleted(Boolean synchronously) {
119 private AsyncCallback Callback = null; 114 base.SetCompleted(synchronously, null);
120 private void CallCallback(Object state) {
121 if (Callback != null) Callback(this);
122 } 115 }
123 public void SetCompleted(Boolean synchronously) { 116 public AsyncResult(AsyncCallback callback, Object state) : base(callback, state) {
124 CompletedSynchronously = synchronously;
125 IsCompleted = true;
126 WaitHandle.Set();
127 if (Callback != null) SysThreadPool.QueueUserWorkItem(CallCallback);
128 }
129 public AsyncResult(AsyncCallback callback, Object state) {
130 this.Callback = callback;
131 this.AsyncState = state;
132 CompletedSynchronously = false;
133 IsCompleted = false;
134 IsReadPacket = true; 117 IsReadPacket = true;
135 } 118 }
136 public AsyncResult(AsyncCallback callback, Object state, Byte[] buffer, int bufferOffset, int bufferLength) 119 public AsyncResult(AsyncCallback callback, Object state, Byte[] buffer, int bufferOffset, int bufferLength) : base(callback, state) {
137 : this(callback, state) {
138 this.Buffer = buffer; 120 this.Buffer = buffer;
139 this.BufferOffset = bufferOffset; 121 this.BufferOffset = bufferOffset;
140 this.BufferLength = bufferLength; 122 this.BufferLength = bufferLength;
141 IsReadPacket = false; 123 IsReadPacket = false;
142 } 124 }
143 } 125 }
126
144 private IAsyncResult BeginAsyncReadOperation(AsyncResult ar) { 127 private IAsyncResult BeginAsyncReadOperation(AsyncResult ar) {
145 lock (ReceiveQueue) { 128 lock (ReceiveQueue) {
146 if (AsyncReceiveOperation != null) throw new InvalidOperationException("Another asynchronous operation is in progress"); 129 if (AsyncReceiveOperation != null) throw new InvalidOperationException("Another asynchronous operation is in progress");
147 if (ReceiveBuffer != null || ReceiveQueue.Count > 0) { 130 if (ReceiveBuffer != null || ReceiveQueue.Count > 0) {
148 ar.SetCompleted(true); 131 ar.SetCompleted(true);