public virtual void Send(RawStreamBuffer sb) { if (m_SocketState == SocketState.Connected) { m_sendBufferQueue.Enqueue(sb); } }
protected bool SendBatchBuffer(RawStreamBuffer toSendBuffer) { byte[] tmpBytes = toSendBuffer.GetCopyOfContent(); if (tmpBytes != null) { RawStreamBuffer tempBuffer = new RawStreamBuffer(tmpBytes); m_peerSession.Send(tempBuffer); return(true); } return(false); }
private void ProcessPacket(ref RawStreamBuffer cReceiveBuffer) { while (cReceiveBuffer.WriteIndex - cReceiveBuffer.ReadIndex >= ProtocolHeader.HeadLength) { StreamBuffer sb = Packing.GetPacketBufferWithHeader(ref cReceiveBuffer); if (sb != null) { m_peerListener.OnPktReceive(sb); } else { break; } } //cReceiveBuffer.ClearBuffer(); }
protected void SendLoop() { while (!m_sendBufferQueue.Closed) { try { RawStreamBuffer sb = m_sendBufferQueue.Dequeue(); if (sb == null) { continue; } byte[] sendBuf = null; int sendLength = sb.GetSendBuffer(out sendBuf); if (sendLength == 0) { continue; } if (this.m_SocketState == SocketState.Connected) { int sendBufferSize = 0; do { int bytesSend = this.m_cSocket.Send(sendBuf, sendBufferSize, sendLength - sendBufferSize, SocketFlags.DontRoute); sendBufferSize += bytesSend; if (bytesSend <= 0) { WriteFiles.WritFile.Log(LogerType.INFO, string.Format("the packet is not be send")); HandleException(SocketState.Disconnecting); } } while (sendBufferSize < sendLength); } else { WriteFiles.WritFile.Log(LogerType.DEBUG, string.Format("socket state: {0} ", this.m_SocketState)); } } catch (Exception e) { WriteFiles.WritFile.Log(e); HandleException(SocketState.Disconnecting); } } }
protected virtual void OnDispatch() { if (m_batch) { if (m_sendBufferQueue.Count > 0) { RawStreamBuffer rsb = new RawStreamBuffer(m_sendBufferLenght); while (m_sendBufferQueue.Count > 0) { StreamBuffer buffer = m_sendBufferQueue.Dequeue(); rsb.WriteBuffer(buffer); } // assert m_sendBufferLenght == rsb.ContentSize; m_peerSession.Send(rsb); m_sendBufferLenght = 0; } } }
protected override void OnReceivePacket(StreamBuffer sb) { if (sb.m_protocolHeader.ProtocolID == -2) { if (sb.m_protocolHeader.ErrorCode == m_reliableSendBuffer.m_protocolHeader.ErrorCode) { m_reliableSendBuffer.ClearBuffer(); } } else if (sb.m_protocolHeader.ProtocolID == -1) { StreamBuffer sendbuffer = new StreamBuffer(-2, 0); sendbuffer.m_protocolHeader.ErrorCode = sb.m_protocolHeader.ErrorCode; m_peerSession.Send(sendbuffer); RawStreamBuffer cReceiveBuffer = new RawStreamBuffer(sb.ByteBuffer); ProcessPacket(ref cReceiveBuffer); } else { m_peerListener.OnPktReceive(sb); } }