コード例 #1
0
        /// <summary>
        /// Creates a new crypto decorator for the <see cref="NetworkClientBase"/>.
        /// Extends the <see cref="ReadAsync"/> and <see cref="WriteAsync"/> implementations to pass
        /// all bytes through the corresponding incoming and outgoing ciphers.
        /// </summary>
        /// <param name="decoratedClient">The client to decorate.</param>
        /// <param name="encryptionServiceProvider">The encryption service.</param>
        /// <param name="decryptionServiceProvider">The decryption service.</param>
        public NetworkClientFixedBlockSizeCryptoDecorator(NetworkClientBase decoratedClient, ICryptoServiceProvider encryptionServiceProvider, ICryptoServiceProvider decryptionServiceProvider, int blockSize, int cryptoBufferSize = 30000)
        {
            if (decoratedClient == null)
            {
                throw new ArgumentNullException(nameof(decoratedClient));
            }
            if (encryptionServiceProvider == null)
            {
                throw new ArgumentNullException(nameof(encryptionServiceProvider));
            }
            if (decryptionServiceProvider == null)
            {
                throw new ArgumentNullException(nameof(decryptionServiceProvider));
            }
            if (blockSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(blockSize));
            }

            DecoratedClient           = decoratedClient;
            EncryptionServiceProvider = encryptionServiceProvider;
            DecryptionServiceProvider = decryptionServiceProvider;
            BlockSize = blockSize;

            //One of the lobby packets is 14,000 bytes. We may even need bigger.
            ReadBuffer  = new AsyncLockableBuffer(cryptoBufferSize);
            WriteBuffer = new AsyncLockableBuffer(cryptoBufferSize);

            CryptoBlockOverflow          = new byte[Math.Max(blockSize - 1, 0)];   //we only need
            CryptoBlockOverflowReadIndex = CryptoBlockOverflow.Length;             //set this to last index to indicate empty initially
        }
コード例 #2
0
        public NetworkClientBufferWriteUntilSizeReachedDecorator(NetworkClientBase decoratedClient, int bufferedWaitSize, int bufferedCombinedSize = 30000)
        {
            if (decoratedClient == null)
            {
                throw new ArgumentNullException(nameof(decoratedClient));
            }
            if (bufferedWaitSize <= 1)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferedWaitSize), "Do not use this decorator if you don't need buffered data.");
            }

            DecoratedClient = decoratedClient;
            BufferedData    = new AsyncLockableBuffer(bufferedWaitSize);
            CombinedBuffer  = new AsyncLockableBuffer(bufferedCombinedSize);
        }