Exemplo n.º 1
0
        private void ContinuousRead()
        {
            byte[] buffer      = new byte[4096];
            Action kickoffRead = null;

            kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar)
            {
                int count = BaseStream.EndRead(ar);
                byte[] dst = new byte[count];
                Buffer.BlockCopy(buffer, 0, dst, 0, count);
                OnDataReceived(dst);
                kickoffRead();
            }, null)); kickoffRead();
        }
Exemplo n.º 2
0
		public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback,
			object asyncState)
		{
			EnsureDecompressionMode();

			// We use this checking order for compat to earlier versions:
			if (asyncOperations != 0)
				throw new InvalidOperationException(SR.GetString(SR.InvalidBeginCall));

			ValidateParameters(array, offset, count);
			EnsureNotDisposed();

			Interlocked.Increment(ref asyncOperations);

			try
			{
				var userResult = new DeflateStreamAsyncResult(
					this, asyncState, asyncCallback, array, offset, count);
				userResult.isWrite = false;

				// Try to read decompressed data in output buffer
				var bytesRead = inflater.Inflate(array, offset, count);
				if (bytesRead != 0)
				{
					// If decompression output buffer is not empty, return immediately.
					// 'true' means we complete synchronously.
					userResult.InvokeCallback(true, bytesRead);
					return userResult;
				}

				if (inflater.Finished())
				{
					// end of compression stream
					userResult.InvokeCallback(true, 0);
					return userResult;
				}

				// If there is no data on the output buffer and we are not at 
				// the end of the stream, we need to get more data from the base stream
				BaseStream.BeginRead(buffer, 0, buffer.Length, m_CallBack, userResult);
				userResult.m_CompletedSynchronously &= userResult.IsCompleted;

				return userResult;
			}
			catch
			{
				Interlocked.Decrement(ref asyncOperations);
				throw;
			}
		}
 private void ContinuousRead()
 {
     byte[] buffer = new byte[4096];
     Action kickoffRead = null;
     kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
     {
         if (!base.IsOpen) return; //if base is closed exit func, avoid port closed error
         int count = BaseStream.EndRead(ar);
         byte[] dst = new byte[count];
         Buffer.BlockCopy(buffer, 0, dst, 0, count);
         OnDataReceived(dst);
         kickoffRead();
     }, null)); kickoffRead();
 }
Exemplo n.º 4
0
 /// <summary>
 /// Begins an asynchronous read operation.
 /// </summary>
 /// <param name="buffer">The buffer to read the data into.</param>
 /// <param name="offset">The byte offset in <paramref name="buffer"/> at which to begin writing data read from the stream.</param>
 /// <param name="count">The maximum number of bytes to read.</param>
 /// <param name="callback">An optional asynchronous callback, to be called when the read is complete.</param>
 /// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from other requests.</param>
 /// <returns>
 /// An <see cref="T:System.IAsyncResult"/> that represents the asynchronous read, which could still be pending.
 /// </returns>
 /// <exception cref="T:System.IO.IOException">Attempted an asynchronous read past the end of the stream, or a disk error occurs. </exception>
 /// <exception cref="T:System.ArgumentException">One or more of the arguments is invalid. </exception>
 /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
 /// <exception cref="T:System.NotSupportedException">The current Stream implementation does not support the read operation. </exception>
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     if (_proxyAsyncRequests)
     {
         if (CanSeek)
         {
             if (count + Position > Length)
             {
                 count = (int)Math.Max(0, Length - Position);
             }
         }
         return(BaseStream.BeginRead(buffer, offset, count, callback, state));
     }
     else
     {
         return(base.BeginRead(buffer, offset, count, callback, state));
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Reads data from the stream
        /// </summary>
        /// <param name="buffer">Buffer to read into</param>
        /// <param name="offset">Where in the buffer to start</param>
        /// <param name="count">Number of bytes to be read</param>
        /// <returns></returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            IAsyncResult ar = null;

            if (!IsConnected || BaseStream == null)
            {
                return(0);
            }

            ar = BaseStream.BeginRead(buffer, offset, count, null, null);
            if (!ar.AsyncWaitHandle.WaitOne(m_readTimeout, true))
            {
                Close();
                throw new TimeoutException("Timed out trying to read data from the socket stream!");
            }

            return(BaseStream.EndRead(ar));
        }
Exemplo n.º 6
0
        private void ContinuousRead()
        {
            byte[] buffer      = new byte[4096];
            Action kickoffRead = null;

            kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar)
            {
                try
                {
                    int count = BaseStream.EndRead(ar);
                    byte[] dst = new byte[count];
                    Buffer.BlockCopy(buffer, 0, dst, 0, count);
                    OnDataReceived(dst);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("OptimizedSerialPort exception !" + exception.Message);
                }
                kickoffRead();
            }, null)); kickoffRead();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Reads data from the stream
        /// </summary>
        /// <param name="buffer">Buffer to read into</param>
        /// <param name="offset">Where in the buffer to start</param>
        /// <param name="count">Number of bytes to be read</param>
        /// <returns></returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            IAsyncResult ar = null;

            if (BaseStream == null)
            {
                return(0);
            }

            m_lastActivity = DateTime.Now;

            ar = BaseStream.BeginRead(buffer, offset, count, null, null);
            if (!ar.AsyncWaitHandle.WaitOne(m_readTimeout, true))
            {
                Close();
                throw new TimeoutException("Timed out trying to read data from the socket stream!");
            }
            var x = BaseStream.EndRead(ar);

            return(x);
        }
Exemplo n.º 8
0
        /// <summary>异步开始读</summary>
        /// <param name="buffer">缓冲区</param>
        /// <param name="offset">偏移</param>
        /// <param name="count">数量</param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public override IAsyncResult BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state)
        {
            RaiseAction("BeginRead", offset, count);

            return(BaseStream.BeginRead(buffer, offset, count, callback, state));
        }
Exemplo n.º 9
0
 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
 {
     return(BaseStream.BeginRead(buffer, offset, count, callback, state));
 }