public void Read() { try { while (true) { if (!Connected) { return; } var l = _stream.Read(this._readBuffer, 0, this._readBuffer.Length); if (l <= 0) { this.Disconnect(); return; } FrameHandler.Receive(new ArraySegment <byte>(this._readBuffer, 0, (int)l)); Array.Clear(this._readBuffer, 0, l); } } catch { this.Disconnect(); } }
public void Read() { try { while (true) { if (!Connected) { return; } var op = _reader.LoadAsync(1024 * 320); op.Completed = async(info, status) => { switch (status) { case AsyncStatus.Completed: uint byteCount = _reader.UnconsumedBufferLength; //byte[] buffer = new byte[byteCount]; _reader.ReadBytes(((Communication)this)._readBuffer); if (byteCount <= 0) { await this.Disconnect(); } else { FrameHandler.Receive(new ArraySegment <byte>(((Communication)this)._readBuffer, 0, (int)byteCount)); Array.Clear(((Communication)this)._readBuffer, 0, (int)byteCount); } break; case AsyncStatus.Error: await this.Disconnect(); break; case AsyncStatus.Canceled: await this.Disconnect(); break; } }; } } catch { this.Disconnect(); } }