Esempio n. 1
0
        protected void sendDataPacket(SocketAsyncEventArgs sendArgs)
        {
            SendUserToken token = (SendUserToken)sendArgs.UserToken;

            if (token.DataToSend == null)
            {
                IDataPacket dp         = token.DataPacket;
                byte[]      dataBuffer = dp.Serialize();

                IDataPacketHeader dph = _dataProcessor.CreateDataPacketHeader();
                dph.DataPacketSize = dataBuffer.Length;

                byte[] headerBuffer = dph.Serialize();
                int    nSize        = dataBuffer.Length + headerBuffer.Length;
                byte[] packetBuffer = new byte[nSize];
                Buffer.BlockCopy(headerBuffer, 0, packetBuffer, 0, headerBuffer.Length);
                Buffer.BlockCopy(dataBuffer, 0, packetBuffer, headerBuffer.Length, dataBuffer.Length);
                token.DataToSend           = packetBuffer;
                token.SentDataOffset       = 0;
                token.ProcessedDataRemains = packetBuffer.Length;
            }
            if (token.ProcessedDataRemains <= nBufferSize && token.SentDataOffset == 0) //data packet fully fit in send buffer
            {
                Buffer.BlockCopy(token.DataToSend, 0, sendArgs.Buffer, sendArgs.Offset, token.DataToSend.Length);
                token.Reset();
            }
            else
            {// need to separate data packet into several buffers
                int nLength = token.ProcessedDataRemains <= nBufferSize ? token.ProcessedDataRemains : nBufferSize;
                Buffer.BlockCopy(token.DataToSend, token.SentDataOffset, sendArgs.Buffer,
                                 sendArgs.Offset, nLength);

                token.SentDataOffset       += nLength;
                token.ProcessedDataRemains -= nLength;
                if (token.ProcessedDataRemains == 0)
                {
                    token.Reset();
                }
            }
            byte[] testByte2 = new byte[nBufferSize];
            Buffer.BlockCopy(sendArgs.Buffer, sendArgs.Offset, testByte2, 0, nBufferSize);
            string strOut = Encoding.UTF8.GetString(testByte2);

            Debug.WriteLine("Send = {0}", strOut);

            if (!sendArgs.AcceptSocket.SendAsync(sendArgs))
            {
                sendArgs_Completed(sendArgs.AcceptSocket, sendArgs);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        public bool IsValidHeader(IDataPacketHeader header)
        {
            DataPacketHeader dph = (DataPacketHeader)header;

            return(dph.MagicKey == NMagicKey && dph.Version == NVersion);
        }