/// <summary> /// 接收数据处理 -- *** /// </summary> /// <param name="state"></param> private void ReciveProcessing(object state) { if (!this.Disposed) { ConnectionManage pConnection = null; IAsyncResult AsyncResult = (IAsyncResult)state; try { SocketDataCallback pCallbackBuffer = (SocketDataCallback)AsyncResult.AsyncState; pConnection = pCallbackBuffer.Connection; if (pConnection.Active) { int iReadBytes = 0; if (pConnection.CurrentStream != null) { iReadBytes = pConnection.CurrentStream.EndRead(AsyncResult); } else { iReadBytes = pConnection.CurrentSocket.EndReceive(AsyncResult); }//end CurrentStream if...else... if (iReadBytes > 0) { bool bReadSocket = false; this.BufferProcessing(pCallbackBuffer, iReadBytes, ref bReadSocket); if (!bReadSocket) { //检索队列 lock (pConnection.ReciveLock) { pConnection.ReadCount--; if (pConnection.ReadCount > 0) { SocketBuffer pNextBuffer = new SocketBuffer(this.SocketBufferSize); if (pConnection.CurrentStream != null) { //从SSL Stream读取数据 pConnection.CurrentStream.BeginRead(pNextBuffer.ContentBuffer, pNextBuffer.OffSet, pNextBuffer.Remaining, new AsyncCallback(ReciveCallback), new SocketDataCallback(pConnection, pNextBuffer)); } else { //从Socket Stream读取数据 pConnection.CurrentSocket.BeginReceive(pNextBuffer.ContentBuffer, pNextBuffer.OffSet, pNextBuffer.Remaining, SocketFlags.None, new AsyncCallback(ReciveCallback), new SocketDataCallback(pConnection, pNextBuffer)); } //end CurrentStream if...else... } //end ReadCount if...else... } //end lock } } else { pConnection.OnDisconnect(); } //end iReadBytes if...else... } //end Active if...else... } catch (Exception ex) { this.SocketExceptionEvent(pConnection, ex); }//end try...catch... } }
/// <summary> /// 数据处理 - *** /// </summary> /// <param name="pSocketDataCallback"></param> /// <param name="iBytesCount"></param> private void BufferProcessing(SocketDataCallback pSocketDataCallback, int iBytesCount, ref bool bReadSocket) { byte[] pDataBuffer = null; ConnectionManage pConnection = pSocketDataCallback.Connection; SocketBuffer pReciveBuffer = (SocketBuffer)pSocketDataCallback.SocketBuffer; pConnection.LastAction = DateTime.Now; //Console.WriteLine(iBytesCount); pReciveBuffer.OffSet += iBytesCount; //--- 改进的地方 //读取方向开关 bool bFromBuffer = false; bool bFromSocket = false; //确定数据体大小以及读取方向 do { //判断当前Buffer大小 if (pReciveBuffer.OffSet > SocketConstant.MAX_SOCKETHEAD_SIZE) { //判断数据体是否填充 if (pReciveBuffer.BodySize == 0) { SocketHead pPacketHead = new SocketHead(pReciveBuffer.ContentBuffer); pPacketHead.ExtractInfo(); pReciveBuffer.BodySize = pPacketHead.BodyLength; pReciveBuffer.Compression = pPacketHead.Compression; pReciveBuffer.CRCBuffer = pPacketHead.CRCValue; }//end if pReciveBuffer.BodySize //Socket Packet 大小 int iBodySize = pReciveBuffer.BodySize + SocketConstant.MAX_SOCKETHEAD_SIZE; //数据读取结束 if (iBodySize == pReciveBuffer.OffSet) { pDataBuffer = pReciveBuffer.GetRawBuffer(SocketConstant.MAX_SOCKETHEAD_SIZE, iBodySize); bFromBuffer = false; bFromSocket = false; } else { //从Buffer读取数据 if (iBodySize < pReciveBuffer.OffSet) { pDataBuffer = pReciveBuffer.GetRawBuffer(SocketConstant.MAX_SOCKETHEAD_SIZE, iBodySize); bFromBuffer = true; bFromSocket = false; this.SocketReceivedEvent(pConnection, pDataBuffer, false); } else { //从Socket读取数据 if (iBodySize > pReciveBuffer.OffSet) { //更改Buffer大小至Socket Packet大小 if (iBodySize > pReciveBuffer.CurrentSize) { pReciveBuffer.Resize(iBodySize); } bFromBuffer = false; bFromSocket = true; } } }//end if (iBodySize == pReciveBuffer.OffSet) } else { if (pReciveBuffer.Remaining < SocketConstant.MAX_SOCKETHEAD_SIZE) { pReciveBuffer.Resize(pReciveBuffer.CurrentSize + SocketConstant.MAX_SOCKETHEAD_SIZE); } bFromBuffer = false; bFromSocket = true; }//end if (pReciveBuffer.OffSet > SocketConstant.MAX_SOCKETHEAD_SIZE) } while (bFromBuffer); //从Socket读取数据 if (bFromSocket) { if (pConnection.Active) { if (pConnection.CurrentStream != null) { //从SSL Stream读取数据 pConnection.CurrentStream.BeginRead(pReciveBuffer.ContentBuffer, pReciveBuffer.OffSet, pReciveBuffer.Remaining, new AsyncCallback(ReciveCallback), new SocketDataCallback(pConnection, pReciveBuffer)); } else { //从Socket Stream读取数据 pConnection.CurrentSocket.BeginReceive(pReciveBuffer.ContentBuffer, pReciveBuffer.OffSet, pReciveBuffer.Remaining, SocketFlags.None, new AsyncCallback(ReciveCallback), new SocketDataCallback(pConnection, pReciveBuffer)); } } } if (null != pDataBuffer) { pDataBuffer = CompressionUtilise.DeCompressionData(pReciveBuffer.Compression, pDataBuffer); this.SocketReceivedEvent(pConnection, pDataBuffer, true); }//end pDataBuffer if...else... bReadSocket = bFromSocket; pReciveBuffer = null; pSocketDataCallback = null; }
/// <summary> /// 发送数据处理 /// </summary> /// <param name="state"></param> private void SendProcessing(object pSendState) { if (!this.Disposed) { bool bCanReadQueue = false; SocketBuffer pSendBuffer = null; ConnectionManage pConnection = null; IAsyncResult AsyncResult = (IAsyncResult)pSendState; try { SocketDataCallback pCallbackBuffer = (SocketDataCallback)AsyncResult.AsyncState; pConnection = pCallbackBuffer.Connection; pSendBuffer = (SocketBuffer)pCallbackBuffer.SocketBuffer; if (pConnection.Active) { if (pConnection.CurrentStream != null) { pConnection.CurrentStream.EndWrite(AsyncResult); this.SocketSend(pConnection, pSendBuffer.RawBuffer); bCanReadQueue = true; } else { int iWriteBytes = pConnection.CurrentSocket.EndSend(AsyncResult); if (iWriteBytes < pSendBuffer.Remaining) { bCanReadQueue = false; pSendBuffer.OffSet += iWriteBytes; pConnection.CurrentSocket.BeginSend(pSendBuffer.ContentBuffer, pSendBuffer.OffSet, pSendBuffer.Remaining, SocketFlags.None, new AsyncCallback(SendCallback), pCallbackBuffer); } else { this.SocketSentEvent(pConnection, pSendBuffer.RawBuffer); bCanReadQueue = true; } }//end CurrentStream if...else... if (bCanReadQueue) { pSendBuffer = null; pCallbackBuffer = null; lock (pConnection.SendQueue) { if (pConnection.SendQueue.Count > 0) { SocketBuffer pQueueBuffer = pConnection.SendQueue.Dequeue(); //发送数据流 if (pConnection.CurrentStream != null) { pConnection.CurrentStream.BeginWrite(pQueueBuffer.ContentBuffer, pQueueBuffer.OffSet, pQueueBuffer.Remaining, new AsyncCallback(SendCallback), new SocketDataCallback(pConnection, pQueueBuffer)); } else { pConnection.CurrentSocket.BeginSend(pQueueBuffer.ContentBuffer, pQueueBuffer.OffSet, pQueueBuffer.Remaining, SocketFlags.None, new AsyncCallback(SendCallback), new SocketDataCallback(pConnection, pQueueBuffer)); } } else { pConnection.SendState = false; } } //lock SendQueue } //end bCanReadQueue if...else... } //end Active if...else... } catch (Exception ex) { this.SocketExceptionEvent(pConnection, ex); } } }