Esempio n. 1
0
        internal IMultiPromise <IEnumerable <object> > EventStream()
        {
            var promise = new MultiPromise <IEnumerable <object> >();

            this.connection.InputStream().SingleThen(bytes =>
            {
                var jsonString = Encoding.UTF8.GetString(bytes);
                var jsonObject = JsonConvert.DeserializeObject(jsonString + "");
                if (jsonObject is IEnumerable <object> == false)
                {
                    throw new Exception("invalid request");
                }

                var ev = jsonObject as IEnumerable <object>;
                if (ev.Count() == 0)
                {
                    throw new Exception("invalid request");
                }

                promise.SingleResolve(ev);
            })
            .Then(_ => promise.Resolve(null))
            .Catch(exception =>
            {
                promise.Reject(exception);
            });

            return(promise);
        }
Esempio n. 2
0
        internal IMultiPromise <Message> Stream()
        {
            var promise = new MultiPromise <Message>();

            Log.Debug("Starting event stream for {hash}", this.ConnectionHash);
            this.socket.InputStream().SingleThen(bytes =>
            {
                var originalCount = this.slidingBuffer.Count();
                Array.Resize(ref this.slidingBuffer, this.slidingBuffer.Count() + bytes.Count());
                Array.Copy(bytes, 0, this.slidingBuffer, originalCount, bytes.Count());

                while (this.slidingBuffer.Count() > 0)
                {
                    int seekTo   = 0;
                    int newCount = this.slidingBuffer.Count();

                    Message ev;
                    try
                    {
                        ev = Parser.ParseMessage(this.slidingBuffer, seekTo, out seekTo);
                        this.LastCommunicationDate = DateTime.Now;
                    }
                    catch (Parser.ParseException)
                    {
                        Log.Warning("EventStream {hash} encountered parsing exception, cleaning up the buffer", this.ConnectionHash);
                        this.slidingBuffer = new byte[0];
                        break;
                    }

                    if (ev != null && ev.Type == Message.MessageType.Event)
                    {
                        promise.SingleResolve(ev);
                    }

                    if (seekTo != 0)
                    {
                        newCount = this.slidingBuffer.Count() - seekTo;
                        Array.Copy(this.slidingBuffer, seekTo, this.slidingBuffer, 0, newCount);
                        Array.Resize(ref this.slidingBuffer, newCount);
                    }

                    if (newCount > EventStream.SlidingBufferLimit)
                    {
                        Log.Warning("EventStream {hash} exceeded buffer limit: {total_bytes} total bytes", this.ConnectionHash, newCount);
                        promise.Reject(new Exception("Sliding buffer limit exceeded."));
                        break;
                    }

                    if (seekTo == 0)
                    {
                        Log.Verbose("{hash} failed to parse event, total {total_bytes} in bufffer", this.ConnectionHash, this.slidingBuffer.Count());
                        break;
                    }
                }
            })
            .Then(_ =>
            {
                promise.Resolve(null);
            })
            .Catch(exception => {
                if (exception is CIOSocket.EmptyPacketException)
                {
                    promise.Resolve(null);
                }
                else
                {
                    promise.Reject(exception);
                }
            });

            return(promise);
        }