Пример #1
0
        /// <summary>
        /// 填充报头
        /// </summary>
        /// <param name="pBuffer"></param>
        /// <returns></returns>
        public static SocketBuffer GetPacketBuffer(CompressionType eCompression, ref byte[] pBuffer)
        {
            byte[] pCompressionBuffer = CompressionUtilise.CompressionData(eCompression, pBuffer);

            SocketHead pPacketHead = new SocketHead(eCompression, pCompressionBuffer);

            byte[] pHeadBuffer = pPacketHead.CoalitionInfo();

            byte[] pSendBuffer = new byte[SocketConstant.MAX_SOCKETHEAD_SIZE + pCompressionBuffer.Length];
            Buffer.BlockCopy(pHeadBuffer, 0, pSendBuffer, 0, pHeadBuffer.Length);
            Buffer.BlockCopy(pCompressionBuffer, 0, pSendBuffer, SocketConstant.MAX_SOCKETHEAD_SIZE, pCompressionBuffer.Length);

            return(new SocketBuffer(pBuffer, pSendBuffer));
        }
        /// <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;
        }