Exemplo n.º 1
0
        /// <summary>
        /// Reads an EDI item from the stream.
        /// </summary>
        /// <returns>Indication if an item was read.</returns>
        public override bool Read()
        {
            if (Item is ReaderErrorContext && !ContinueOnError)
            {
                return(false);
            }

            Item = null;

            try
            {
                while ((!StreamReader.EndOfStream || InternalBuffer.Any()) && Item == null)
                {
                    var segment = ReadSegment();

                    if (Separators == null)
                    {
                        throw new ReaderException("No valid separator set was found.", ReaderErrorCode.InvalidControlStructure);
                    }

                    if (string.IsNullOrEmpty(segment))
                    {
                        continue;
                    }

                    Item = Split(segment) ?? Process(segment);
                }
            }
            catch (ReaderException ex)
            {
                Item = new ReaderErrorContext(ex, ex.ErrorCode);
            }
            catch (ParserMessageException ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.InvalidSpecOrAssembly, ex.MessageErrorContext);
            }
            catch (Exception ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.Unknown);
            }

            if (StreamReader.EndOfStream && CurrentSegments.Any())
            {
                Item = new ReaderErrorContext(new Exception("Improper end of file."), ReaderErrorCode.ImproperEndOfFile);
            }

            var readerErrorContext = Item as ReaderErrorContext;

            if (readerErrorContext != null)
            {
                CurrentSegments.Clear();
                if (readerErrorContext.MessageErrorContext == null)
                {
                    Separators = null;
                }
            }

            return(Item != null);
        }
 public virtual void Enqueue(TEntity item)
 {
     if (InternalBuffer == null || InternalBuffer.Count >= Settings.WorkerBatchSize)
     {
         _itemAvailableEvent.Reset();
         _itemAvailableEvent.WaitOne();
     }
     InternalBuffer.TryAdd(item);
 }
Exemplo n.º 3
0
 public override void Clear()
 {
     /* Don't bother re-creating the stream if it's already empty. */
     if (InternalBuffer.Length != 0)
     {
         InternalBuffer.Close();
         InternalBuffer = new MemoryStream();
     }
 }
Exemplo n.º 4
0
 protected virtual async Task <T> Enqueue(T item)
 {
     LoadItem(item);
     if (InternalBuffer.Count >= BufferSize)
     {
         _semaphore.WaitOne();
     }
     InternalBuffer.TryAdd(item);
     return(item);
 }
    void SwapBuffers()
    {
        //Put into temporary variable so we dont loose the reference when we repoint.
        InternalBuffer temp = this.currentBuffer;

        //This make our current buffers next pointer become our front buffer, this will cycle infinite.
        this.currentBuffer = temp.GetNextBuffer();

        //Always clear frontbuffer when it becomes active.
        this.currentBuffer.Clear();
    }
Exemplo n.º 6
0
 public void copyInto(InternalBuffer ano)
 {
     ano.last_pic_num = this.last_pic_num;
     for (int i = 0; i < 4; i++)
     {
         ano.@base[i] = this.@base[i];
         ano.data_offset[i] = this.data_offset[i];
         ano.linesize[i] = this.linesize[i];
     } // for i
     ano.width = this.width;
     ano.height = this.height;
     ano.pix_fmt = this.pix_fmt;
 }
Exemplo n.º 7
0
 public void copyInto(InternalBuffer ano)
 {
     ano.last_pic_num = this.last_pic_num;
     for (int i = 0; i < 4; i++)
     {
         ano.@base[i]       = this.@base[i];
         ano.data_offset[i] = this.data_offset[i];
         ano.linesize[i]    = this.linesize[i];
     }             // for i
     ano.width   = this.width;
     ano.height  = this.height;
     ano.pix_fmt = this.pix_fmt;
 }
Exemplo n.º 8
0
        /// <summary>
        /// Reads an item from the stream.
        /// </summary>
        /// <returns>Indication if an item was read.</returns>
        public override bool Read()
        {
            if (Item is ReaderErrorContext && !ContinueOnError)
            {
                return(false);
            }

            Item = null;

            try
            {
                while ((!StreamReader.EndOfStream || InternalBuffer.Any()) && Item == null)
                {
                    var segment = ReadSegment();

                    if (string.IsNullOrEmpty(segment))
                    {
                        continue;
                    }

                    Item = Process(segment);
                }
            }
            catch (ReaderException ex)
            {
                Item = new ReaderErrorContext(ex, ex.ErrorCode);
            }
            catch (ParserMessageException ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.InvalidSpecOrAssembly, ex.MessageErrorContext);
            }
            catch (Exception ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.Unknown);
            }

            if (StreamReader.EndOfStream && CurrentSegments.Any())
            {
                Item = Flush(null);
            }

            var readerErrorContext = Item as ReaderErrorContext;

            if (readerErrorContext != null)
            {
                CurrentSegments.Clear();
            }

            return(Item != null);
        }
Exemplo n.º 9
0
        private void CheckForSpace(ushort count)
        {
            if (IndexPosition + count < InternalBuffer.Length)
            {
                return;
            }

            int newSize = (int)Math.Ceiling(InternalBuffer.Length * _growFactor);

            while (IndexPosition + count >= newSize)
            {
                newSize = (int)Math.Ceiling(newSize * _growFactor);
            }

            byte[] tmp = new byte[newSize];
            InternalBuffer.CopyTo(tmp, 0);
            InternalBuffer = tmp;
        }
Exemplo n.º 10
0
        public ServerWebSocket(Stream innerStream,
                               string subProtocol,
                               int receiveBufferSize,
                               TimeSpan keepAliveInterval,
                               ArraySegment <byte> internalBuffer)
            : base(innerStream, subProtocol, keepAliveInterval,
                   WebSocketBuffer.CreateServerBuffer(internalBuffer, receiveBufferSize))
        {
            _properties    = InternalBuffer.CreateProperties(false);
            _sessionHandle = CreateWebSocketHandle();

            if (_sessionHandle == null || _sessionHandle.IsInvalid)
            {
                WebSocketValidate.ThrowPlatformNotSupportedException_WSPC();
            }

            StartKeepAliveTimer();
        }
Exemplo n.º 11
0
        public float CalculateONPExpresion(string onpExpression)
        {
            int id = 0;
            OutputOperationBuffer = new InternalBuffer<OutputOperation>();

            InternalStack<string> stack = new InternalStack<string>();

            List<string> tokenList = onpExpression.Trim().Split(' ').ToList();
            string input = onpExpression.Trim();

            OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Empty));

            foreach (string token in tokenList)
            {
                if (!string.IsNullOrEmpty(input.Trim()))
                    input = input.Trim().Remove(0, 1);

                if (token.IsOperator())
                {
                    string secondToken = stack.Pop();
                    string firstToken = stack.Pop();
                    string result = Calculate(token, firstToken, secondToken);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Format("{0} {1} {2}", firstToken, token, secondToken)));

                    stack.Push(result);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Empty));
                }
                else
                {
                    stack.Push(token);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Empty));
                }
            }

            string resultToken = stack.Pop();
            float resultValue = float.Parse(resultToken);
            return resultValue;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Reads from the stream until a non-escaped segment terminator was reached.
        /// Breaks if no segment terminator was encountered after 5000 symbols were read.
        /// This is to avoid loading large and corrupt files.
        /// </summary>
        /// <returns>
        /// The segment.
        /// </returns>
        protected override string ReadSegment()
        {
            var line    = "";
            var postFix = "";

            while ((!StreamReader.EndOfStream || InternalBuffer.Any()) && line.Length < MaxSegmentLength)
            {
                line = line + Read(1);
            }

            while (!string.IsNullOrEmpty(_postFix) && !StreamReader.EndOfStream && postFix.Length < _postFix.Length)
            {
                postFix = postFix + Read(1);
            }

            if (!string.IsNullOrEmpty(_postFix) && postFix != _postFix)
            {
                throw new Exception(string.Format("Postfix {0} is different than the expected {1}", postFix, _postFix));
            }

            return(line);
        }
    public PhonemeBuffer(float bufferLength, int numberOfBuffers)
    {
        this.phonemeBuffers = new List <InternalBuffer>(numberOfBuffers);

        //Create buffers;
        for (int i = 0; i < numberOfBuffers; i++)
        {
            InternalBuffer buffer = new InternalBuffer(bufferLength, 4);
            phonemeBuffers.Add(buffer);
        }

        //First buffer added get lowest possible starttime. Then we work ourself up to 0 from there.
        int timeScale = -numberOfBuffers;

        //Create cyclic buffer pointers.
        for (int i = 0; i < numberOfBuffers; i++)
        {
            InternalBuffer buffer = phonemeBuffers[i];

            //loop back to first index when at the end.
            buffer.SetNextBuffer(phonemeBuffers[i < numberOfBuffers - 1 ? i + 1 : 0]);

            //The time at which this buffer will start.
            float startTime = timeScale * bufferLength;

            //All buffers are filled with silent phonemes.
            buffer.SetDefaultValues(startTime, bufferLength, 4);

            //Make sure all buffers have different default times stretching back.
            timeScale += 1;
        }

        //Clear this index since its going to be our frontbuffer.
        this.phonemeBuffers[0].Clear();
        this.currentBuffer = this.phonemeBuffers[0];

        //This buffer will eat all incoming phonemes even when it gets full.
        this.completeBuffer = new InternalBuffer(bufferLength, 100);
    }
        // return value indicates sync vs async completion
        // false: sync completion
        // true: async completion or error
        private unsafe bool ReadAsyncFast(HttpListenerAsyncEventArgs eventArgs)
        {
            eventArgs.StartOperationCommon(this, _inputStream.InternalHttpContext.RequestQueueBoundHandle);
            eventArgs.StartOperationReceive();

            bool completedAsynchronouslyOrWithError;

            try
            {
                Debug.Assert(eventArgs.Buffer != null, "'BufferList' is not supported for read operations.");
                if (eventArgs.Count == 0 || _inputStream.Closed)
                {
                    eventArgs.FinishOperationSuccess(0, true);
                    return(false);
                }

                uint dataRead       = 0;
                int  offset         = eventArgs.Offset;
                int  remainingCount = eventArgs.Count;

                if (_inputStream.BufferedDataChunksAvailable)
                {
                    dataRead = _inputStream.GetChunks(eventArgs.Buffer, eventArgs.Offset, eventArgs.Count);
                    if (_inputStream.BufferedDataChunksAvailable && dataRead == eventArgs.Count)
                    {
                        eventArgs.FinishOperationSuccess(eventArgs.Count, true);
                        return(false);
                    }
                }

                Debug.Assert(!_inputStream.BufferedDataChunksAvailable, "'m_InputStream.BufferedDataChunksAvailable' MUST BE 'FALSE' at this point.");
                Debug.Assert(dataRead <= eventArgs.Count, "'dataRead' MUST NOT be bigger than 'eventArgs.Count'.");

                if (dataRead != 0)
                {
                    offset         += (int)dataRead;
                    remainingCount -= (int)dataRead;
                    //the http.sys team recommends that we limit the size to 128kb
                    if (remainingCount > HttpRequestStream.MaxReadSize)
                    {
                        remainingCount = HttpRequestStream.MaxReadSize;
                    }

                    eventArgs.SetBuffer(eventArgs.Buffer, offset, remainingCount);
                }
                else if (remainingCount > HttpRequestStream.MaxReadSize)
                {
                    remainingCount = HttpRequestStream.MaxReadSize;
                    eventArgs.SetBuffer(eventArgs.Buffer, offset, remainingCount);
                }

                uint flags         = 0;
                uint bytesReturned = 0;
                uint statusCode    =
                    Interop.HttpApi.HttpReceiveRequestEntityBody(
                        _inputStream.InternalHttpContext.RequestQueueHandle,
                        _inputStream.InternalHttpContext.RequestId,
                        flags,
                        (byte *)_webSocket !.InternalBuffer.ToIntPtr(eventArgs.Offset),
                        (uint)eventArgs.Count,
                        out bytesReturned,
                        eventArgs.NativeOverlapped);

                if (statusCode != Interop.HttpApi.ERROR_SUCCESS &&
                    statusCode != Interop.HttpApi.ERROR_IO_PENDING &&
                    statusCode != Interop.HttpApi.ERROR_HANDLE_EOF)
                {
                    throw new HttpListenerException((int)statusCode);
                }
                else if (statusCode == Interop.HttpApi.ERROR_SUCCESS &&
                         HttpListener.SkipIOCPCallbackOnSuccess)
                {
                    // IO operation completed synchronously. No IO completion port callback is used because
                    // it was disabled in SwitchToOpaqueMode()
                    eventArgs.FinishOperationSuccess((int)bytesReturned, true);
                    completedAsynchronouslyOrWithError = false;
                }
                else if (statusCode == Interop.HttpApi.ERROR_HANDLE_EOF)
                {
                    eventArgs.FinishOperationSuccess(0, true);
                    completedAsynchronouslyOrWithError = false;
                }
                else
                {
                    completedAsynchronouslyOrWithError = true;
                }
            }
            catch (Exception e)
            {
                _readEventArgs !.FinishOperationFailure(e, true);
                _outputStream.SetClosedFlag();
                _outputStream.InternalHttpContext.Abort();

                completedAsynchronouslyOrWithError = true;
            }

            return(completedAsynchronouslyOrWithError);
        }
Exemplo n.º 15
0
        public async Task <T> ReadMessage <T>(int size, CancellationToken ct = default, UInt32?checksum = null) where T : IStreamable, new()
        {
top:
            if (BufferedData >= size)
            {
                //we have enough data buffered
                if (checksum.HasValue)
                {
                    var chk = BitConverter.ToUInt32(InternalBuffer.Span.Slice(0, size).SHA256d());
                    if (chk != checksum.Value)
                    {
                        //skip this payload because checksum failed
                        if (BufferedData > size)
                        {
                            InternalBuffer.Span.Slice(size, BufferedData - size).CopyTo(InternalBuffer.Span);
                            BufferedData -= size;
                        }

                        throw new Exception("Checksum failed");
                    }
                }

                var obj = new T();
                obj.ReadFromPayload(InternalBuffer.Span);
                if (BufferedData > size)
                {
                    //copy extra data to start of the buffer
                    InternalBuffer.Span.Slice(size, BufferedData - size).CopyTo(InternalBuffer.Span);
                }

                BufferedData -= size;

                return(obj);
            }
            else
            {
                //resize the buffer
                if (size > InternalBuffer.Length)
                {
                    if (size > MemoryPool <byte> .Shared.MaxBufferSize)
                    {
                        throw new Exception($"Buffer size is larger than the max size {MemoryPool<byte>.Shared.MaxBufferSize}");
                    }

                    var newowner = MemoryPool <byte> .Shared.Rent((int)(Math.Ceiling(size / (decimal)InternalBufferSize) * InternalBufferSize));

                    var newbuf = newowner.Memory;
                    if (BufferedData > 0)
                    {
                        InternalBuffer.Slice(0, BufferedData).CopyTo(newbuf);
                    }

                    MemoryOwner.Dispose();

                    MemoryOwner    = newowner;
                    InternalBuffer = newbuf;
                }

read_more:
                try
                {
                    var rlen = await Stream.ReadAsync(InternalBuffer.Slice(BufferedData), ct);

                    BufferedData += rlen;
                    if (rlen == 0)
                    {
                        return(default);
Exemplo n.º 16
0
 public override void Write(byte[] buffer, int offset, int count)
 {
     InternalBuffer.Write(buffer, offset, count);
 }
Exemplo n.º 17
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(InternalBuffer.Read(buffer, offset, count));
 }
Exemplo n.º 18
0
 public override void SetLength(long value)
 {
     InternalBuffer.SetLength(value);
 }
Exemplo n.º 19
0
 public override long Seek(long offset, SeekOrigin origin)
 {
     return(InternalBuffer.Seek(offset, origin));
 }
Exemplo n.º 20
0
        /// <summary>
        /// Reads from the stream until a non-escaped segment terminator was reached.
        /// Breaks if no segment terminator was encountered after 5000 symbols were read.
        /// This is to avoid loading large and corrupt files.
        /// </summary>
        /// <returns>
        /// An EDI segment.
        /// </returns>
        protected override string ReadSegment()
        {
            var line = "";

            while (!StreamReader.EndOfStream || InternalBuffer.Any())
            {
                line = line + Read(1);
                if (line.Length > 2)
                {
                    var        last3 = line.Substring(line.Length - 3);
                    Separators separators;
                    string     probed;
                    if (TryReadHeader(last3, out probed, out separators))
                    {
                        Separators = separators;
                        Trims      = Trims.Except(new[] { Separators.Segment }).ToArray();
                        line       = probed;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(probed))
                        {
                            Buffer(probed.Skip(3));
                        }
                    }
                }

                // Segment terminator may never be reached
                if (line.Length > MaxSegmentLength)
                {
                    //  Reset and continue or break
                    if (ContinueOnError)
                    {
                        line = "";
                    }
                    else
                    {
                        throw new ReaderException(
                                  string.Format("No segment was found before the buffer reached the allowed maximum of {0}.", MaxSegmentLength),
                                  ReaderErrorCode.InvalidInterchangeContent);
                    }
                }

                if (Separators == null)
                {
                    continue;
                }

                if (!line.EndsWith(Separators.Segment.ToString(), StringComparison.Ordinal))
                {
                    continue;
                }

                if (Separators.Escape.HasValue &&
                    line.EndsWith(string.Concat(Separators.Escape.Value, Separators.Segment),
                                  StringComparison.Ordinal))
                {
                    continue;
                }

                var index = line.LastIndexOf(Separators.Segment.ToString(), StringComparison.Ordinal);
                if (index > 0)
                {
                    line = line.Remove(index);
                }

                if (!string.IsNullOrEmpty(line))
                {
                    break;
                }
            }

            return(line.Trim(Trims));
        }
Exemplo n.º 21
0
 public byte[] ToBytes()
 {
     return(InternalBuffer.ToArray());
 }
 public void SetNextBuffer(InternalBuffer nextBuffer)
 {
     this.Next = nextBuffer;
 }
Exemplo n.º 23
0
 public override void Flush()
 {
     InternalBuffer.Flush();
 }
Exemplo n.º 24
0
        public string ConvertToONP(string infix)
        {
            int id = 0;
            OutputOperationBuffer = new InternalBuffer<OutputOperation>();
            InternalStack<Operator> stack = new InternalStack<Operator>();

            string output = string.Empty;
            char[] infixArray = infix.Trim().ToArray();
            string input = infix;
            OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));

            foreach (char infixChar in infixArray)
            {
                if(!string.IsNullOrEmpty(input))
                    input = input.Remove(0, 1);

                if (!infixChar.IsOperator())
                {
                    AddToOutput(ref output, infixChar);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
                else if (infixChar == Operators.OpenBracket)
                {
                    AddToOutput(ref output);

                    Operator newOperator = new Operator(infixChar);
                    stack.Push(newOperator);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
                else if (infixChar == Operators.CloseBracket)
                {
                    AddToOutput(ref output);

                    Operator newOperator = new Operator(infixChar);
                    while (stack.Any() && stack.Peek().OperatorType != Operators.OpenBracket)
                    {
                        AddToOutput(ref output, stack.Pop());
                        OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                    }
                    stack.Pop();

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
                else
                {
                    AddToOutput(ref output);

                    Operator newOperator = new Operator(infixChar);
                    while (stack.Any() && stack.Peek().Priority >= newOperator.Priority)
                    {
                        AddToOutput(ref output, stack.Pop());
                    }
                    stack.Push(newOperator);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
            }
            while (stack.Any())
            {
                AddToOutput(ref output, stack.Pop());
            }

            return output;
        }