private void InternalRealRead(byte[] buffer) { if (_isClosed) { return; } var arraySeg = new ArraySegment <byte>(buffer); //开始异步接收数据 _receiveAsync(arraySeg, CancellationToken.None).ContinueWith(_t => { //如果出现了异常 if (_t.IsFaulted || _t.IsCanceled) { _isClosed = true; OnClose?.Invoke(this); OnClose = null; if (_t.IsFaulted) { if (_t.Exception != null) { _t.Exception.Handle(_ => true); } } ; _webSocketCompSource.TrySetResult(1); return; } //处理接收到的数据 InternalReadComplte(arraySeg, _t.Result.Item1, _t.Result.Item2, _t.Result.Item3); }); }
/// <summary> /// 发送完成的回调函数 /// </summary> /// <param name="task"></param> private void InternalWriteComplete(Task task) { _writting = false; if (task.IsCanceled || task.IsFaulted) { _isClosed = true; if (task.IsFaulted && task.Exception != null) { task.Exception.Handle(_ => true); } OnClose?.Invoke(this); OnClose = null; _webSocketCompSource.TrySetResult(1); return; } if (OnSend == null) { return; } try { OnSend(this); } catch (Exception e) { _webSocketCompSource.TrySetException(e); throw e; } }
/// <summary> /// 发送完成的回调函数 /// </summary> /// <param name="task"></param> private void InternalWriteComplete(Task task) { var err = task.IsFaulted || task.IsCanceled; if (err) { _isClosed = true; if (task.IsFaulted) { var tmp = task.Exception; } if (OnClose != null) { OnClose(this); OnClose = null; } return; } if (OnSend == null) { return; } OnSend(this); }
/// <summary> /// 内部调用的,用于数据接受成功的回调函数 /// </summary> /// <param name="buffer"></param> /// <param name="type"></param> /// <param name="endOfMessage"></param> /// <param name="size"></param> /// <param name="error"></param> private void InternalReadComplte(ArraySegment <byte> buffer, int type, bool endOfMessage, int size) { // 只接受客户端文本数据,否则关闭,如果需要接收二进制数据,请自行添加处理方法 // type==8:对端已关闭; type=2:二进制数据 if (type == 8 || type == 2) { _isClosed = true; try { _closeAsync(1000, null, CancellationToken.None); } catch { } if (OnClose != null) { OnClose(this); OnClose = null; } _webSocketCompSource.TrySetResult(1); return; } // 如果一帧数据已经完成 if (endOfMessage) { var lastSize = _lastReadData == null ? 0 : _lastReadData.Length; var data = new byte[size + lastSize]; if (lastSize != 0) { Buffer.BlockCopy(_lastReadData, 0, data, 0, lastSize); } Buffer.BlockCopy(buffer.Array, 0, data, lastSize, size); _lastReadData = null; if (OnRead != null) { var s = Encoding.UTF8.GetString(data); OnRead(this, s); } //继续接收 InternalRealRead(buffer.Array); return; } //不完整数据帧,保存起来 var oldSize = _lastReadData == null ? 0 : _lastReadData.Length; var tmpData = new byte[oldSize + size]; if (oldSize > 0) { Buffer.BlockCopy(_lastReadData, 0, tmpData, 0, oldSize); } Buffer.BlockCopy(buffer.Array, 0, tmpData, oldSize, size); _lastReadData = tmpData; //继续接收 InternalRealRead(buffer.Array); }
/// <summary> /// 内部调用的,用于数据接受成功的回调函数 /// </summary> /// <param name="buffer"></param> /// <param name="type"></param> /// <param name="endOfMessage"></param> /// <param name="size"></param> /// <param name="error"></param> private void InternalReadComplte(ArraySegment <byte> buffer, int type, bool endOfMessage, int size, bool error) { //只接受文本数据,否则关闭 if (type == 8 || type == 2 || error) { _isClosed = true; _closeAsync(1000, null, CancellationToken.None); if (OnClose != null) { OnClose(this); OnClose = null; } return; } // 如果一帧数据已经完成 if (endOfMessage) { var lastSize = _lastReadData == null ? 0 : _lastReadData.Length; var data = new byte[size + lastSize]; if (lastSize != 0) { Buffer.BlockCopy(_lastReadData, 0, data, 0, lastSize); } Buffer.BlockCopy(buffer.Array, 0, data, lastSize, size); _lastReadData = null; if (OnRead != null) { var s = Encoding.UTF8.GetString(data); if (OnRead != null) { OnRead(this, s); } } //继续接收 InternalRealRead(buffer.Array); return; } //不完整数据帧,保存起来 var oldSize = _lastReadData == null ? 0 : _lastReadData.Length; var tmpData = new byte[oldSize + size]; if (oldSize > 0) { Buffer.BlockCopy(_lastReadData, 0, tmpData, 0, oldSize); } Buffer.BlockCopy(buffer.Array, 0, tmpData, oldSize, size); _lastReadData = tmpData; //继续接收 InternalRealRead(buffer.Array); }