A normal Stream might provide a timeout on a specific read opreation. However, using StreamReader.ReadToEnd() on it can still get stuck for a long time. This class offers a timeout from the moment of it's construction to the read. Every read past the timeout from the stream's construction will fail. If the timeout elapsed while a read is in progress TimeoutStream is not responsible for aborting the read (there is no known good way in .NET to do it) See http://www.dotnet247.com/247reference/msgs/36/182553.aspx and http://www.google.co.il/search?q=cancel+async+Stream+read+.net Stream originalStream = GetStream(); StreamReader reader = new StreamReader(new TimeoutStream(originalStream, 5000)); // assuming the originalStream has a per-operation timeout, then ReadToEnd() // will return in (5000 + THAT_TIMEOUT) string foo = reader.ReadToEnd();
Inheritance: Stream
Esempio n. 1
0
 public void setUp()
 {
     _stream = new PipeStream();
     _writer = new StreamWriter(_stream);
     //timer = new InterruptTimer();
     _timeoutstream = new TimeoutStream(_stream);
     _timeoutstream.setTimeout(timeout);
 }
Esempio n. 2
0
 public void testTimeout_readBuffer_Success2()
 {
     var s = new MemoryStream();
     var t = new TimeoutStream(s);
     t.setTimeout(timeout);
     byte[] exp = new byte[] { (byte)'a', (byte)'b', (byte)'c' };
     byte[] act = new byte[exp.Length];
     s.Write(exp, 0, exp.Length);
     s.Seek(0, SeekOrigin.Begin);
     IO.ReadFully(t, act, 0, 1);
     IO.ReadFully(t, act, 1, 1);
     IO.ReadFully(t, act, 2, 1);
     Assert.AreEqual(exp, act);
 }
Esempio n. 3
0
 public void testTimeout_skip_Success()
 {
     var s = new MemoryStream();
     var t = new TimeoutStream(s);
     byte[] exp = new byte[] { (byte)'a', (byte)'b', (byte)'c' };
     s.Write(exp, 0, exp.Length);
     s.Seek(0, SeekOrigin.Begin);
     Assert.AreEqual(2, t.skip(2));
     Assert.AreEqual((byte)'c', t.ReadByte());
 }
Esempio n. 4
0
        /// <summary>
        /// Execute the upload task on the socket.
        /// </summary>
        /// <param name="input">
        /// raw input to read client commands from. Caller must ensure the
        /// input is buffered, otherwise read performance may suffer.
        /// </param>
        /// <param name="output">
        /// response back to the Git network client, to write the pack
        /// data onto. Caller must ensure the output is buffered,
        /// otherwise write performance may suffer.
        /// </param>
        /// <param name="messages">
        /// secondary "notice" channel to send additional messages out
        /// through. When run over SSH this should be tied back to the
        /// standard error channel of the command execution. For most
        /// other network connections this should be null.
        /// </param>
        /// <exception cref="IOException"></exception>
        public void Upload(Stream input, Stream output, Stream messages)
        {
            _rawIn = input;
            _rawOut = output;

            if (_timeout > 0)
            {
                var i = new TimeoutStream(_rawIn, _timeout * 1000);
                var o = new TimeoutStream(_rawOut, _timeout * 1000);
                _rawIn = i;
                _rawOut = o;
            }

            _pckIn = new PacketLineIn(_rawIn);
            _pckOut = new PacketLineOut(_rawOut);
            Service();
        }