Esempio n. 1
0
        private static void OnRead(IAsyncResult ar)
        {
            int receivedBytes = Stream.EndRead(ar);

            TotalBuffer = Concat(TotalBuffer, Buffer, receivedBytes);

            while (TotalBuffer.Length > 8)
            {
                int encryptedLength = BitConverter.ToInt32(TotalBuffer, 0);
                int decryptedLength = BitConverter.ToInt32(TotalBuffer, 4);

                if (TotalBuffer.Length >= 8 + encryptedLength)
                {
                    byte[] PartialBuffer = TotalBuffer.Skip(8).Take(encryptedLength).ToArray();
                    string Decrypted     = Crypting.DecryptStringFromBytes(PartialBuffer);

                    string[] packetData = Regex.Split(Decrypted, "\r\n");
                    HandleData(packetData);
                    TotalBuffer = TotalBuffer.Skip(encryptedLength + 8).Take(TotalBuffer.Length - encryptedLength - 8).ToArray();
                }
                else
                {
                    break;
                }
            }
            Stream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(OnRead), null);
        }