Пример #1
0
        protected void readDataPacket(SocketAsyncEventArgs args)
        {
            ReceiveUserToken token             = (ReceiveUserToken)args.UserToken;
            bool             bDataPacketReaded = false;
            int nProcessedDataCount            = 0;
            IDataPacketHeader dph = _dataProcessor.CreateDataPacketHeader();
            IDataPacket       dp  = _dataProcessor.CreateDataPacket();

            if (!token.IsHeaderReaded)
            {
                if (args.BytesTransferred >= _nPacketHeaderSize)
                {
                    if (args.Buffer != null)
                    {
                        byte[] newdata = new byte[args.Count];
                        Buffer.BlockCopy(args.Buffer, args.Offset, newdata, 0, args.Count);

                        dph.Deserialize(newdata);
                        if (_dataProcessor.IsValidHeader(dph))
                        {
                            token.IsHeaderReaded      = true;
                            token.ProcessedDataCount += _nPacketHeaderSize;
                            nProcessedDataCount      += _nPacketHeaderSize;
                            token.DataPacketHeader    = dph;
                        }
                    }
                }
            }
            else
            {
                dph = token.DataPacketHeader;
            }

            if (token.IsHeaderReaded)
            {
                if (args.BytesTransferred >= token.ProcessedDataCount + dph.DataPacketSize)
                {
                    if (args.Buffer != null)
                    {
                        byte[] buffer = new byte[dph.DataPacketSize];
                        Buffer.BlockCopy(args.Buffer, args.Offset + _dataProcessor.DataPacketHeaderSize, buffer, 0,
                                         dph.DataPacketSize);

                        dp.Deserialize(buffer);
                        bDataPacketReaded = true;
                    }
                }
                else
                {
                    if (args.Buffer != null)
                    {
                        if (token.ReadData == null)
                        {
                            byte[] buffer = new byte[dph.DataPacketSize];
                            token.ReadData = buffer;
                        }

                        int nCount = args.BytesTransferred - nProcessedDataCount;
                        nCount = nCount > (dph.DataPacketSize - token.ReadDataOffset)
                            ? dph.DataPacketSize - token.ReadDataOffset
                            : nCount;
                        Buffer.BlockCopy(args.Buffer, args.Offset + nProcessedDataCount, token.ReadData,
                                         token.ReadDataOffset, nCount);

                        token.ReadDataOffset     += args.BytesTransferred - nProcessedDataCount;
                        token.ProcessedDataCount += args.BytesTransferred - nProcessedDataCount;
                        if (token.ProcessedDataCount >= dph.DataPacketSize + _nPacketHeaderSize)
                        {
                            dp.Deserialize(token.ReadData);
                            bDataPacketReaded = true;
                        }
                    }
                }
            }
            if (bDataPacketReaded)
            {
                token.Reset();
                onDataPacketReaded(args, dp);
            }
        }