0
votes

After I pass a reference of filestream from the client to the service, and the service start downloading the stream to him, how can I determine, from the client side, how much bytes were read until now (while Im using the filestream object)?

My goal is to calculate client's upload speed only for this file and the only way I can think of is this.

1
That doesn't make any sense. How can you serialize a filestream? - Jonathan Allen
Im passing it as stream object + some information about it as a massage. - Stav Alfi
@Jonathan Allen: WCF can work in streaming mode, where is just sends the data as bytes. see msdn.microsoft.com/en-us/library/ms731913.aspx - MaLio
I'm also surprised this is possible. The FileStream object is neither [Serializable] or [DataContract]. Does this work because it is a subclass of Stream, which is [Serializable]? - Dan Ling
Must be, cant find other reason. - Stav Alfi

1 Answers

4
votes

Extend FileStream or create a wrapper for it. Override the read methods and have a counter count the bytes read.

extending (not properly implement, but should be more than enough to explain)

   public class CountingStream : System.IO.FileStream {

      // provide appropriate constructors

      // may want to override BeginRead too

      // not thread safe

      private long _Counter = 0;

      public override int ReadByte() {
         _Counter++;
         return base.ReadByte();            
      }

      public override int Read(byte[] array, int offset, int count) {
         // check if not going over the end of the stream
         _Counter += count;
         return base.Read(array, offset, count);             
      }

      public long BytesReadSoFar {
         get {
            return _Counter;
         }
      }
   }