void PeekMessage() { lock (_resvMsgQueue) { int handleCount = 0; while (_resvMsgQueue.Count > 0) { WsMsg msg = _resvMsgQueue.Dequeue(); handleCount++; bool handleNextMsg = false; if (msg._isRaw) { handleNextMsg = _handler.OnRawMsg(msg._rawMsg, handleCount); } else { handleNextMsg = _handler.OnTxtMsg(msg._txtMsg, handleCount); } if (!handleNextMsg) { break; } } } }
bool AddSendMsg(WsMsg msg) { bool ret = false; WsMsg thisSend = null; do { if (!this.CanSend()) { break; } lock (_sendQueue) { //发送队列为空,且当前不在发送过程中 if (this._sendQueue.Count == 0 && !this._isSending) { thisSend = msg; } else { this._sendQueue.Enqueue(msg); if (!this._isSending) // 当前没在发送则开始发送 { thisSend = this._sendQueue.Dequeue(); } } DoSendMsgAsync(thisSend); } ret = true; } while (false); return(ret); }
void OnSendComplete(bool completed) { if (this.netWorkState != NetWorkState.Running) { this._isSending = false; return; } if (!completed) { return; } lock (this._sendQueue) { if (this._sendQueue.Count == 0) { this._isSending = false; } else { WsMsg thisSend = this._sendQueue.Dequeue(); DoSendMsgAsync(thisSend); } } }
void DoSendMsgAsync(WsMsg thisSend) { if (thisSend != null) { this._isSending = true; if (thisSend._isRaw) { _webSocket.SendAsync(thisSend._rawMsg, OnSendComplete); } else { _webSocket.SendAsync(thisSend._txtMsg, OnSendComplete); } } }