Esempio n. 1
0
            /// <summary>
            /// Is called when asynchrounous line reading has completed.
            /// </summary>
            /// <param name="e">Callback data.</param>
            private void OnReadHeader_ReadLine_Completed(ReadLine_EventArgs e)
            {
                try
                {
                    m_TotalReadedCount += e.ReadedCount;

                    // We got error.
                    if (e.Exception != null)
                    {
                        try
                        {
                            m_pBufferedStream.Flush();
                            m_pStoreStream.Flush();
                        }
                        catch
                        {
                            // Just skip excpetions here, otherwise we may hide original exception, 
                            // if exceptions is thrown by flush.
                        }

                        // Maximum line size exceeded, but junk data wanted.
                        if (m_ExceededAction == SizeExceededAction.JunkAndThrowException &&
                            e.Exception is LineSizeExceededException)
                        {
                            m_IsLineSizeExceeded = true;

                            // Start reading next data buffer block.
                            m_pStreamHelper.BeginReadLineInternal(m_pLineBuffer,
                                                                  m_ExceededAction,
                                                                  null,
                                                                  OnReadHeader_ReadLine_Completed,
                                                                  false,
                                                                  false);
                        }
                            // Unknown exception or ThrowException, so we are done.
                        else
                        {
                            ReadHeaderCompleted(e.Exception);
                        }
                    }
                        // We got terminator, so we are done now.
                    else if (e.Count == 0 || e.ReadedCount == 0)
                    {
                        m_pBufferedStream.Flush();
                        m_pStoreStream.Flush();

                        // LineSizeExceeded.
                        if (m_IsLineSizeExceeded)
                        {
                            ReadHeaderCompleted(new LineSizeExceededException());
                        }
                            // DataSizeExceeded.
                        else if (m_TotalReadedCount > m_MaxSize)
                        {
                            ReadHeaderCompleted(new DataSizeExceededException());
                        }
                            // Completed successfuly.
                        else
                        {
                            ReadHeaderCompleted(null);
                        }
                    }
                        // Just append line to store stream and get next line.
                    else
                    {
                        // Maximum allowed data size exceeded.
                        if (m_TotalReadedCount > m_MaxSize)
                        {
                            if (m_ExceededAction == SizeExceededAction.ThrowException)
                            {
                                ReadHeaderCompleted(new DataSizeExceededException());
                                return;
                            }
                            // Junk data.
                            //else{
                            //}
                        }
                        else
                        {
                            m_pBufferedStream.Write(e.LineBuffer, 0, e.Count);
                            m_pBufferedStream.Write(m_pStreamHelper.m_LineBreak,
                                                    0,
                                                    m_pStreamHelper.m_LineBreak.Length);
                        }

                        // Start reading new line.
                        m_pStreamHelper.BeginReadLineInternal(m_pLineBuffer,
                                                              m_ExceededAction,
                                                              null,
                                                              OnReadHeader_ReadLine_Completed,
                                                              false,
                                                              false);
                    }
                }
                catch (Exception x)
                {
                    ReadHeaderCompleted(x);
                }
            }
Esempio n. 2
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="stream">Source stream.</param>
        /// <param name="maxLineSize">Specifies maximum line size in bytes.</param>
        /// <param name="bufferRead">Specifies if source stream reads are buffered..</param>
        public StreamHelper(Stream stream, int maxLineSize, bool bufferRead)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (maxLineSize < 1 || maxLineSize > Workaround.Definitions.MaxStreamLineLength)
            {
                throw new ArgumentException("Parameter maxLineSize value must be >= 1 and <= Workaround.Definitions.MaxStreamLineLength !");
            }

            m_IsReadBuffered = bufferRead;
            if (m_IsReadBuffered)
            {
                m_pReadBuffer = new byte[Workaround.Definitions.MaxStreamLineLength];
            }

            m_pStream = stream;
            m_pLineBuffer = new byte[maxLineSize];
            m_MaxLineSize = maxLineSize;

            m_pRLine_EventArgs = new ReadLine_EventArgs();
            m_pRLine_ByteBuffer = new byte[1];
        }
Esempio n. 3
0
        /// <summary>
        /// Is called when BeginWritePeriodTerminated stream.BeginReadLine has completed.
        /// </summary>
        /// <param name="e">Callback data.</param>
        private void InternalBeginWritePeriodTerminatedReadLineCompleted(ReadLine_EventArgs e)
        {
            try
            {
                // Error happened during read.
                if (e.Exception != null)
                {
                    InternalBeginWritePeriodTerminatedCompleted(e.Exception);
                }
                    // We reached end of stream, no more data.
                else if (e.ReadedCount == 0)
                {
                    InternalBeginWritePeriodTerminatedCompleted(null);
                }
                else
                {
                    m_BeginWritePeriodTerminated_Readed += e.ReadedCount;

                    // Maximum allowed data size exceeded.
                    if (m_BeginWritePeriodTerminated_Readed > m_BeginWritePeriodTerminated_MaxSize)
                    {
                        throw new DataSizeExceededException();
                    }

                    // If line starts with period, addtional period is added.
                    if (e.LineBuffer[0] == '.')
                    {
                        m_pBeginWritePeriodTerminated_BufferedStream.WriteByte((int) '.');
                        m_BeginWritePeriodTerminated_Written++;
                    }

                    // Write readed line to buffered stream.
                    m_pBeginWritePeriodTerminated_BufferedStream.Write(e.LineBuffer, 0, e.Count);
                    m_pBeginWritePeriodTerminated_BufferedStream.Write(m_LineBreak, 0, m_LineBreak.Length);
                    m_BeginWritePeriodTerminated_Written += e.Count + m_LineBreak.Length;
                }
            }
            catch (Exception x)
            {
                InternalBeginWritePeriodTerminatedCompleted(x);
            }
        }