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)
		{
			// null continuation, consumer must swallow the data immediately.
			channel.OnData(data, null);
			channel.OnEnd();
			return null;
		}
Example #3
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 #4
0
        public void Render(IDataConsumer consumer, HttpResponseHead head)
        {
            var status = head.Status;
            var headers = head.Headers;

            // XXX don't reallocate every time
            var sb = new StringBuilder();

            sb.AppendFormat("HTTP/1.1 {0}\r\n", status);

            if (headers == null)
                headers = new Dictionary<string, string>();

            if (!headers.ContainsKey("Server"))
                headers["Server"] = "Kayak";

            if (!headers.ContainsKey("Date"))
                headers["Date"] = DateTime.UtcNow.ToString();

            foreach (var pair in headers)
                foreach (var line in pair.Value.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
                    sb.AppendFormat("{0}: {1}\r\n", pair.Key, line);

            sb.Append("\r\n");

            consumer.OnData(new ArraySegment<byte>(Encoding.ASCII.GetBytes(sb.ToString())), null);
        }
Example #5
0
 /// <summary>Записывает данные из буфера в другой буфер</summary>
 /// <param name="DestinationBuffer">Буфер, в который необходимо совершить копирование</param>
 public void CopyTo(IDataConsumer DestinationBuffer)
 {
     foreach (BufferSegment segment in _segments.OrderBy(s => s.StartPosition))
     {
         segment.Seek(0, SeekOrigin.Begin);
         DestinationBuffer.Write(segment.StartPosition, segment.ToArray());
     }
 }
Example #6
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;
 }
 public void Unsubscribe(IDataConsumer dataConsumer)
 {
     Utils.ThrowException(dataConsumer == null ? new ArgumentNullException("dataConsumer") : null);
     if (mDataConsumers.Contains(dataConsumer))
     {
         mDataConsumers.Remove(dataConsumer);
     }
 }
 internal NetworkService(IDataConsumer dataConsumer, Properties props)
 {
     myServerPort = props.NetworkPort;
       myDataConsumer = dataConsumer;
       myServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
       myServerSocket.Bind(new IPEndPoint(IPAddress.Any, myServerPort));
       myServerSocket.Listen(1024);
 }
        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 #10
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);
 }
        public void OnRequest(HttpRequestHead request, bool shouldKeepAlive)
        {
            var responseDelegate = responseDelegateFactory.Create(request, shouldKeepAlive, CloseConnection);

            DataSubject subject = null;
            bool requestBodyConnected = false;

            Debug.WriteLine("[{0}] {1} {2} shouldKeepAlive = {3}",
                DateTime.Now, request.Method, request.Uri, shouldKeepAlive);

            if (request.HasBody())
            {
                subject = new DataSubject(() =>
                {
                    if (requestBodyConnected) 
                        throw new InvalidOperationException("Request body was already connected.");

                    requestBodyConnected = true;

                    if (request.IsContinueExpected())
                        responseDelegate.WriteContinue();

                    return new Disposable(() =>
                    {
                        // XXX what to do?
                        // ideally we stop reading from the socket. 
                        // equivalent to a parse error
                    });
                });
            }

            requestBody = subject;

            if (remoteAddress != null)
            {
                if (request.Headers.ContainsKey("X-Forwarded-For"))
                {
                    request.Headers["X-Forwarded-For"] += "," + remoteAddress.ToString();
                }
                else
                {
                    request.Headers["X-Forwarded-For"] = remoteAddress.ToString();
                }
            }

            requestDelegate.OnRequest(request, subject, responseDelegate);
            observer.OnNext(responseDelegate);
        }
Example #12
0
        public IDisposable Connect(IDataConsumer consumer)
        {
            if (consumer == null)
                throw new ArgumentNullException();

            this.consumer = consumer;

            bool begin = false;

            state.OnConnect(out begin);

            if (begin)
                Begin();

            return new Disposable(Abort);
        }
Example #13
0
        public IDisposable Connect(IDataConsumer consumer)
        {
            if (consumer == null)
                throw new ArgumentNullException();

            this.consumer = consumer;

            bool writeContinue = false;
            bool begin = false;

            state.OnConnect(out begin, out writeContinue);

            if (writeContinue)
                RenderContinue();

            if (begin)
                BeginResponse();

            return new Disposable(Abort);
        }
Example #14
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 #15
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 #16
0
        /// <summary>
        /// Manage data that is dragged over and dropped on the <code>dropTarget</code>.
        /// Supported data is defined as one or more classes that implement IDataConsumer.
        /// </summary>
        /// <param name="dropTarget">FrameworkElement monitored for drag events</param>
        /// <param name="dragDropConsumers">Array of supported data objects</param>
        public DropManager(FrameworkElement dropTarget, IDataConsumer[] dragDropConsumers) {
            this._dropTarget = dropTarget;
            System.Diagnostics.Debug.Assert(dropTarget != null);

            this._dragDropConsumers = dragDropConsumers;
            System.Diagnostics.Debug.Assert(dragDropConsumers != null);

            bool hookDragEnter = false;
            bool hookDragOver = false;
            bool hookDrop = false;
            bool hookDragLeave = false;

            // Determine which events to hook
            foreach(IDataConsumer dragDropConsumer in this._dragDropConsumers) {
                if((dragDropConsumer.DataConsumerActions & DataConsumerActions.DragEnter) != 0)
                    hookDragEnter = true;
                if((dragDropConsumer.DataConsumerActions & DataConsumerActions.DragOver) != 0)
                    hookDragOver = true;
                if((dragDropConsumer.DataConsumerActions & DataConsumerActions.Drop) != 0)
                    hookDrop = true;
                if((dragDropConsumer.DataConsumerActions & DataConsumerActions.DragLeave) != 0)
                    hookDragLeave = true;
            }

            if((hookDragEnter == true) || (hookDragOver == true) || (hookDrop == true) || (hookDragLeave == true))
                this._dropTarget.AllowDrop = true;

            // Hook only the events needed
            if(hookDragEnter == true)
                this._dropTarget.DragEnter += new DragEventHandler(this.DropTarget_DragEnter);
            if(hookDragOver == true)
                this._dropTarget.DragOver += new DragEventHandler(this.DropTarget_DragOver);
            if(hookDrop == true)
                this._dropTarget.Drop += new DragEventHandler(this.DropTarget_Drop);
            if(hookDragLeave == true)
                this._dropTarget.DragLeave += new DragEventHandler(this.DropTarget_DragLeave);
        }
 /// <summary>
 /// Returns the data for the data points of the series.
 /// </summary>
 /// <param name="dataConsumer">Consumer of the data.</param>
 /// <returns>Sequence of data.</returns>
 IEnumerable<object> IDataProvider.GetData(IDataConsumer dataConsumer)
 {
     return IDataProviderGetData(dataConsumer);
 }
 public IDisposable Connect(IDataConsumer channel)
 {
     channel.OnData(data, null);
     channel.OnEnd();
     return null;
 }
Example #19
0
 public IDisposable Connect(IDataConsumer channel)
 {
     this.consumer = channel;
     connect();
     return new Disposable(disconnect);
 }
 /// <summary>
 /// Returns the data for the data points of the series.
 /// </summary>
 /// <param name="dataConsumer">Consumer of the data.</param>
 /// <returns>Sequence of data.</returns>
 protected virtual IEnumerable<object> IDataProviderGetData(IDataConsumer dataConsumer)
 {
     if (dataConsumer == ActualIndependentAxis)
     {
         return IndependentValueGroups.Select(cg => cg.IndependentValue).Distinct();
     }
     throw new NotImplementedException();
 }
Example #21
0
 /// <summary>
 /// Manage data that is dragged over and dropped on the <code>dropTarget</code>.
 /// Supported data is defined as one or more classes that implement IDataConsumer.
 /// </summary>
 /// <param name="dropTarget">FrameworkElement monitored for drag events</param>
 /// <param name="dragDropConsumer">Supported data objects</param>
 public DropManager(FrameworkElement dropTarget, IDataConsumer dragDropConsumer)
     : this(dropTarget, new IDataConsumer[] { dragDropConsumer })
 {
 }
 public void Render(IDataConsumer consumer, HttpResponseHead head)
 {
     if (Rendered) throw new InvalidOperationException("already rendered");
     Rendered = true;
     consumer.OnData(new ArraySegment<byte>(Encoding.ASCII.GetBytes("[headers]")), null);
 }
 void WriteSync(IDataConsumer c, string str)
 {
     c.OnData(new ArraySegment<byte>(Encoding.UTF8.GetBytes(str)), null);
 }
 public void Subscribe(IDataConsumer dataConsumer)
 {
     Utils.ThrowException(dataConsumer == null ? new ArgumentNullException("dataConsumer") : null);
     mDataConsumers.Add(dataConsumer);
 }
 public IDisposable Connect(IDataConsumer channel)
 {
     return new Disposable(subscribe(channel));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DataConsumerMutableCore"/> class.
 /// </summary>
 /// <param name="objTarget">
 /// The obj target. 
 /// </param>
 public DataConsumerMutableCore(IDataConsumer objTarget)
     : base(objTarget)
 {
 }
 /// <summary>
 /// Wraps consumer (MessageConsumer) within a buffered consumer that reads whole message into memory.
 /// Result is that HTTP Headers and Body are sent at same socket.write.
 /// Ios client seems to prefer it this way. Freaks out when payload arrives in different packet.
 /// </summary>
 /// <param name="consumer"></param>
 /// <returns></returns>
 public override IDisposable Connect(IDataConsumer consumer)
 {
     return base.Connect(new BufferedConsumerWrapper(consumer));
 }
Example #28
0
 public IDisposable Connect(IDataConsumer channel)
 {
   channel.OnData(new ArraySegment<byte>(), null);
   channel.OnEnd();
   return null;
 }
Example #29
0
		public IDisposable Connect(IDataConsumer channel) {
			return null;
		}
 public void AttachConsumer(IDataConsumer consumer)
 {
     Consumers.Add(consumer);
 }