Wrapper class for writing to streams with time-outs. Used primarily to prevent stream write deadlocks.
Exemplo n.º 1
0
 /// <summary>
 /// Return the MD5 hash of part of the current <see cref="ThreadSafeStream"/> as a string
 /// </summary>
 /// <param name="start">The start position in the stream</param>
 /// <param name="length">The length of stream to MD5</param>
 /// <returns></returns>
 public string MD5(long start, int length)
 {
     using (MemoryStream partialStream = new MemoryStream(length))
     {
         lock (streamLocker)
         {
             StreamTools.Write(_innerStream, start, length, partialStream, 8000, 1000, 500);
             return(StreamTools.MD5(partialStream));
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Return the MD5 hash of the provided memory stream as a string. Stream position will be equal to the length of stream on
        /// return, this ensures the MD5 is consistent.
        /// </summary>
        /// <param name="streamToMD5">The bytes which will be checksummed</param>
        /// <param name="start">The start position in the stream</param>
        /// <param name="length">The length in the stream to MD5</param>
        /// <returns>The MD5 checksum as a string</returns>
        public static string MD5(Stream streamToMD5, long start, int length)
        {
            if (streamToMD5 == null)
            {
                throw new ArgumentNullException("streamToMD5", "Provided Stream cannot be null.");
            }

            using (MemoryStream stream = new MemoryStream(length))
            {
                StreamTools.Write(streamToMD5, start, length, stream, 8000, 100, 2000);
                return(MD5(stream));
            }
        }
        /// <inheritdoc />
        protected override double[] SendStreams(StreamTools.StreamSendWrapper[] streamsToSend, double maxSendTimePerKB, long totalBytesToSend)
        {
            double[] timings = new double[streamsToSend.Length];

            Stream sendingStream = btClientNetworkStream;

            for (int i = 0; i < streamsToSend.Length; i++)
            {
                if (streamsToSend[i].Length > 0)
                {
                    //Write each stream
                    timings[i] = streamsToSend[i].ThreadSafeStream.CopyTo(sendingStream, streamsToSend[i].Start, streamsToSend[i].Length, NetworkComms.SendBufferSizeBytes, maxSendTimePerKB, MinSendTimeoutMS);

                    streamsToSend[i].ThreadSafeStream.Dispose();
                }
                else
                    timings[i] = 0;
            }

            if (!btClient.Connected)
            {
                if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Error("BTClient is not marked as connected after write to networkStream. Possibly indicates a dropped connection.");

                throw new CommunicationException("BTClient is not marked as connected after write to networkStream. Possibly indicates a dropped connection.");
            }

            return timings;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Copies data specified by start and length properties from internal stream to the provided stream.
 /// </summary>
 /// <param name="destinationStream">The destination stream to write to</param>
 /// <param name="startPosition"></param>
 /// <param name="length"></param>
 /// <param name="writeBufferSize">The buffer size to use for copying stream contents</param>
 /// <param name="minTimeoutMS">The minimum time allowed for any sized copy</param>
 /// <param name="timeoutMSPerKBWrite">The timouts in milliseconds per KB to write</param>
 /// <returns>The average time in milliseconds per byte written</returns>
 public double CopyTo(Stream destinationStream, long startPosition, long length, int writeBufferSize, double timeoutMSPerKBWrite = 1000, int minTimeoutMS = 500)
 {
     lock (streamLocker)
         return(StreamTools.Write(_innerStream, startPosition, length, destinationStream, writeBufferSize, timeoutMSPerKBWrite, minTimeoutMS));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Return the MD5 hash of the current <see cref="ThreadSafeStream"/> as a string
 /// </summary>
 /// <returns></returns>
 public string MD5()
 {
     lock (streamLocker)
         return(StreamTools.MD5(_innerStream));
 }
        /// <inheritdoc />
        protected override double[] SendStreams(StreamTools.StreamSendWrapper[] streamsToSend, double maxSendTimePerKB, long totalBytesToSend)
        {
            double[] timings = new double[streamsToSend.Length];

            Stream sendingStream;
#if WINDOWS_PHONE || NETFX_CORE
            sendingStream = socket.OutputStream.AsStreamForWrite();
#else
            sendingStream = connectionStream;
#endif

            for(int i=0; i<streamsToSend.Length; i++)
            {
                if (streamsToSend[i].Length > 0)
                {
                    //Write each stream
                    timings[i] = streamsToSend[i].ThreadSafeStream.CopyTo(sendingStream, streamsToSend[i].Start, streamsToSend[i].Length, NetworkComms.SendBufferSizeBytes, maxSendTimePerKB, MinSendTimeoutMS);
                    streamsToSend[i].ThreadSafeStream.Dispose();
                }
                else
                    timings[i] = 0;
            }

#if WINDOWS_PHONE || NETFX_CORE
            sendingStream.Flush();
#endif

#if !WINDOWS_PHONE && !NETFX_CORE
            if (!tcpClient.Connected)
            {
                if (NetworkComms.LoggingEnabled) NetworkComms.Logger.Error("TCPClient is not marked as connected after write to networkStream. Possibly indicates a dropped connection.");
                
                throw new CommunicationException("TCPClient is not marked as connected after write to networkStream. Possibly indicates a dropped connection.");
            }
#endif

            return timings;
        }