public void Encrypt(NetworkPayloadWriter npw)
 {
     lock (locky)
     {
         npw.vStream.Position = connection.HEADER_SIZE;
         CryptoStream CryptoStream = new CryptoStream(npw.vStream, Encryptor, CryptoStreamMode.Write);
         CryptoStream.Write(npw.GetBuffer(), connection.HEADER_SIZE, npw.Length - connection.HEADER_SIZE);
         CryptoStream.FlushFinalBlock();
     }
 }
Exemple #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="npw">The input data</param>
        /// <param name="Data">The data that is being cached</param>
        /// <returns>Is data cached</returns>
        public unsafe uint Write(NetworkPayloadWriter npw, ref byte[] Data)
        {
            byte[] payload   = npw.GetBuffer();
            uint   UsedBytes = (uint)connection.HEADER_SIZE;

            Data = new byte[payload.Length + 5000];            //+5kb space
            Array.Copy(payload, Data, connection.HEADER_SIZE); //copy SSP header

            int length = (npw.Length - connection.HEADER_SIZE);
            int loops  = (int)Math.Ceiling((double)length / 4D);

            fixed(byte *tempPtr = &(payload[connection.HEADER_SIZE]), dataPtr = &(Data[connection.HEADER_SIZE + 4]))
            {
                if ((npw.Length - connection.HEADER_SIZE) > 0)
                {
                    uint *payloadPtr = (uint *)tempPtr;
                    byte *DataPtr    = (byte *)dataPtr;
                    for (int i = 0; i < loops; i++)
                    {
                        if (UsedBytes > payload.Length)
                        {
                            //stop caching
                            Flush();
                            return(0);
                        }

                        CacheInfo info      = new CacheInfo(payloadPtr);
                        CacheInfo temp      = null;
                        int       ListIndex = 0;
                        if (TryGetCacheInfo(ref temp, info.Hash, ref ListIndex))
                        {
                            //read from cache
                            temp.UseCount++;

                            //check index size for write
                            if (info.CacheIndex > 254)
                            {
                                UsedBytes += 3;
                                *DataPtr = (byte)CacheType.ReadFromCacheBig;
                                DataPtr[1] = (byte)info.CacheIndex;
                                DataPtr[2] = (byte)(info.CacheIndex >> 8);
                                DataPtr   += 3;
                            }
                            else
                            {
                                UsedBytes += 3;
                                *DataPtr = (byte)CacheType.ReadFromCacheSmall;
                                DataPtr[1] = (byte)info.CacheIndex;
                                DataPtr[2] = (byte)ListIndex;
                                DataPtr   += 3;
                            }
                        }
                        else
                        {
                            //new data
                            info.UseCount = 1;
                            UsedBytes    += 5;
                            cache[info.CacheIndex].Add(info);
                            *DataPtr = (byte)CacheType.NewData;
                            *(uint *)(DataPtr + 1) = info.Hash;
                            UsedMemory            += CacheInfoSize;
                            DataPtr += 5;
                        }
                        payloadPtr++;
                    }
                }
                *(uint *)(dataPtr - 4) = (uint)(npw.Length - connection.HEADER_SIZE);
            }

            Flush();
            return(UsedBytes);
        }