Example #1
0
 public IDisposable Connect(IDataConsumer channel)
 {
     return new Disposable(del(
         (data, continuation) => channel.OnData(data, continuation),
         error => channel.OnError(error),
         () => channel.OnEnd()));
 }
Example #2
0
        public IDisposable Connect(IDataConsumer channel)
        {
            var fileInfo = new FileInfo(_filepath);
            using(FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read)) {
                var buffer = new byte[fileInfo.Length];
                fileStream.Read(buffer, 0, (int) fileInfo.Length);
                int length = (int) fileInfo.Length;
                int offset = 0;

                if(_requestHeaders.ContainsKey(HttpRequestHeader.Range.ToString())) {
                    string range = _requestHeaders[HttpRequestHeader.Range.ToString()];
                    Regex rangeEx = new Regex(@"bytes=([\d]*)-([\d]*)");
                    if(rangeEx.IsMatch(range)) {
                        int from = Convert.ToInt32(rangeEx.Match(range).Groups[1].Value);
                        int to = Convert.ToInt32(rangeEx.Match(range).Groups[2].Value);
                        offset = from;
                        length = (to - from) +1;
                    }
                }
                ArraySegment<byte> data = new ArraySegment<byte>(buffer, offset, length);
                channel.OnData(data, null);

                _log.DebugFormat("Wrote {0} bytes to buffer", data.Array.Length);
                channel.OnEnd();
                return null;
            }
        }
Example #3
0
 public void OnRequestEnd()
 {
     if (requestBody != null)
     {
         requestBody.OnEnd();
     }
 }
Example #4
0
 public void OnEnd()
 {
     byte[] allbytes = ConvertToByteArray(_buffer);
     _buffer.Clear();
     _wrappedConsumer.OnData(new ArraySegment <byte>(allbytes), null);
     _wrappedConsumer.OnEnd();
 }
Example #5
0
        public IDisposable Connect(IDataConsumer channel)
        {
            var fileInfo = new FileInfo(_filepath);

            using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read)) {
                var buffer = new byte[fileInfo.Length];
                fileStream.Read(buffer, 0, (int)fileInfo.Length);
                int length = (int)fileInfo.Length;
                int offset = 0;

                if (_requestHeaders.ContainsKey(HttpRequestHeader.Range.ToString()))
                {
                    string range   = _requestHeaders[HttpRequestHeader.Range.ToString()];
                    Regex  rangeEx = new Regex(@"bytes=([\d]*)-([\d]*)");
                    if (rangeEx.IsMatch(range))
                    {
                        int from = Convert.ToInt32(rangeEx.Match(range).Groups[1].Value);
                        int to   = Convert.ToInt32(rangeEx.Match(range).Groups[2].Value);
                        offset = from;
                        length = (to - from) + 1;
                    }
                }
                ArraySegment <byte> data = new ArraySegment <byte>(buffer, offset, length);
                channel.OnData(data, null);

                Log.DebugFormat("Wrote {0} bytes to buffer", data.Array.Length);
                channel.OnEnd();
                return(null);
            }
        }
Example #6
0
	    public IDisposable Connect(IDataConsumer channel)
		{
			// null continuation, consumer must swallow the data immediately.
			channel.OnData(data, null);
			channel.OnEnd();
			return null;
		}
Example #7
0
        static void WriteNext(IEnumerator <string> data, IDataConsumer c, bool sync)
        {
            while (true)
            {
                if (!data.MoveNext())
                {
                    c.OnEnd();
                    data.Dispose();
                    break;
                }

                var seg = new ArraySegment <byte>(Encoding.UTF8.GetBytes(data.Current));
                if (sync)
                {
                    if (c.OnData(seg, null))
                    {
                        throw new Exception("sync write should have returned false");
                    }
                }
                else
                if (c.OnData(
                        seg,
                        () => WriteNext(data, c, sync)))
                {
                    break;
                }
            }
        }
Example #8
0
 public IDisposable Connect(IDataConsumer channel)
 {
     // null continuation, consumer must swallow the data immediately.
     channel.OnData(data, null);
     channel.OnEnd();
     return(null);
 }
Example #9
0
        public IDisposable Connect(IDataConsumer channel)
        {
            this.channel = channel;

            if (buffer != null)
            {
                buffer.Each(d => channel.OnData(new ArraySegment <byte>(d), null));

                // XXX this maybe is kinda wrong.
                if (continuation != null)
                {
                    continuation();
                }
            }

            if (error != null)
            {
                channel.OnError(error);
            }

            if (gotEnd)
            {
                channel.OnEnd();
            }

            return(disposable());
        }
Example #10
0
 public IDisposable Connect(IDataConsumer channel)
 {
     // null continuation, consumer must swallow the data immediately.
     var bytes = new ArraySegment<byte>(dataFunc());
     channel.OnData(bytes, null);
     channel.OnEnd();
     return null;
 }
Example #11
0
        public IDisposable Connect(IDataConsumer channel)
        {
            // null continuation, consumer must swallow the data immediately.
            var bytes = new ArraySegment <byte>(dataFunc());

            channel.OnData(bytes, null);
            channel.OnEnd();
            return(null);
        }
Example #12
0
        public void OnEnd()
        {
            if (channel == null)
            {
                return;
            }

            channel.OnEnd();
        }
Example #13
0
 public void OnEnd()
 {
     if (channel == null)
     {
         gotEnd = true;
     }
     else
     {
         channel.OnEnd();
     }
 }
        void BeginResponse()
        {
            RenderHeaders();

            if (body == null)
            {
                consumer.OnEnd();
            }
            else
            {
                abortBody = body.Connect(consumer);
            }
        }
        public IDisposable Connect(IDataConsumer channel)
        {
            using(Stream source = File.OpenRead(_fileName))
            {
                var buffer = new byte[_bufferSize];
                while(source.Read(buffer, 0, buffer.Length) > 0)
                {
                    channel.OnData(new ArraySegment<byte>(buffer), null);
                }
            }

            channel.OnEnd();
            return null;
        }
Example #16
0
        public IDisposable Connect(IDataConsumer channel)
        {
            try
            {
                channel?.OnData(data, null);
                channel?.OnEnd();
            }

            catch (Exception)
            {
            }

            return(null);
        }
Example #17
0
 public IDisposable Connect(IDataConsumer channel)
 {
     // null continuation, consumer must swallow the data immediately.
     byte[] storage = new byte[4096];
     using (FileStream stream = new FileStream(this.m_Path, FileMode.Open, FileAccess.Read))
     {
         while (stream.Position < stream.Length)
         {
             int count = stream.Read(storage, 0, 4096);
             channel.OnData(new ArraySegment <byte>(storage, 0, count), null);
         }
         channel.OnEnd();
     }
     return(null);
 }
Example #18
0
 public IDisposable Connect(IDataConsumer channel)
 {
     var cts = new CancellationTokenSource();
     del(
         channel.OnData,
         error =>
         {
             if (error == null)
                 channel.OnEnd();
             else
                 channel.OnError(error);
         },
         cts.Token);
     return new Disposable(cts.Cancel);
 }
Example #19
0
 public IDisposable Connect(IDataConsumer channel)
 {
     var cts = new CancellationTokenSource();
     del(
         (data, continuation)=>
         {
             if (channel.OnData(data, () => continuation(null)) == true)
             {
                 return OwinConstants.CompletingAsynchronously;
             }
             return OwinConstants.CompletedSynchronously;
         },
         error =>
         {
             if (error == null)
                 channel.OnEnd();
             else
                 channel.OnError(error);
         },
         cts.Token);
     return new Disposable(cts.Cancel);
 }
Example #20
0
 public IDisposable Connect(IDataConsumer channel)
 {
     channel.OnData(new ArraySegment <byte>(), null);
     channel.OnEnd();
     return(null);
 }
Example #21
0
 public IDisposable Connect(IDataConsumer channel)
 {
     channel.OnData(_rawData, null);
     channel.OnEnd();
     return(null);
 }
Example #22
0
        void RunAsyncProd(IDataConsumer consumer, IEnumerator<ArraySegment<byte>> data)
        {
            while (true)
            {
                if (!data.MoveNext())
                {
                    consumer.OnEnd();
                    data.Dispose();
                    break;
                }

                if (consumer.OnData(data.Current, () => RunAsyncProd(consumer, data)))
                    break;
            }
        }
 public IDisposable Connect(IDataConsumer channel)
 {
     channel.OnData(data, null);
     channel.OnEnd();
     return null;
 }
Example #24
0
        public IDisposable Connect(IDataConsumer channel)
        {
            this.channel = channel;

            if (buffer != null)
            {
                buffer.Each(d => channel.OnData(new ArraySegment<byte>(d), null));

                // XXX this maybe is kinda wrong.
                if (continuation != null)
                    continuation();
            }

            if (error != null)
                channel.OnError(error);

            if (gotEnd)
                channel.OnEnd();

            return disposable();
        }
Example #25
0
 public IDisposable Connect(IDataConsumer channel)
 {
   channel.OnData(new ArraySegment<byte>(), null);
   channel.OnEnd();
   return null;
 }
Example #26
0
 public void OnEnd()
 {
     consumer.OnEnd();
 }
Example #27
-1
        static void WriteNext(IEnumerator<string> data, IDataConsumer c, bool sync)
        {
            while (true)
            {
                if (!data.MoveNext())
                {
                    c.OnEnd();
                    data.Dispose();
                    break;
                }

                var seg = new ArraySegment<byte>(Encoding.UTF8.GetBytes(data.Current));
                if (sync)
                {
                    if (c.OnData(seg, null))
                        throw new Exception("sync write should have returned false");
                }
                else
                    if (c.OnData(
                        seg,
                        () => WriteNext(data, c, sync)))
                        break;
            }
        }