Example #1
0
        BeginWrite(byte [] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (!CanWrite)
            {
                throw new NotSupportedException("This stream does not support writing");
            }

            // Creating a class derived from Stream that doesn't override BeginWrite
            // shows that it actually calls Write and does everything synchronously except
            // when invoking the callback, which is done from the ThreadPool.
            // Just put this in the Write override:
            //  Console.WriteLine ("Write");
            //  Console.WriteLine (Environment.StackTrace);
            //	Thread.Sleep (10000);

            StreamAsyncResult result = new StreamAsyncResult(state);

            try {
                Write(buffer, offset, count);
                result.SetComplete(null);
            } catch (Exception e) {
                result.SetComplete(e);
            }

            if (callback != null)
            {
                callback.BeginInvoke(result, null, null);
            }

            return(result);
        }
Example #2
0
        public virtual void EndWrite(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }

            StreamAsyncResult result = asyncResult as StreamAsyncResult;

            if (result == null || result.NBytes != -1)
            {
                throw new ArgumentException("Invalid IAsyncResult", "asyncResult");
            }

            if (result.Done)
            {
                throw new InvalidOperationException("EndWrite already called.");
            }

            result.Done = true;
            if (result.Exception != null)
            {
                throw result.Exception;
            }
        }
Example #3
0
        BeginRead(byte [] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (!CanRead)
            {
                throw new NotSupportedException("This stream does not support reading");
            }

            // Creating a class derived from Stream that doesn't override BeginRead
            // shows that it actually calls Read and does everything synchronously.
            // Just put this in the Read override:
            //	Console.WriteLine ("Read");
            //  Console.WriteLine (Environment.StackTrace);
            //	Thread.Sleep (10000);
            //	return 10;

            StreamAsyncResult result = new StreamAsyncResult(state);

            try {
                int nbytes = Read(buffer, offset, count);
                result.SetComplete(null, nbytes);
            } catch (Exception e) {
                result.SetComplete(e, 0);
            }

            if (callback != null)
            {
                callback(result);
            }

            return(result);
        }
Example #4
0
        /// <summary>Waits for the pending asynchronous read to complete.</summary>
        /// <returns>The number of bytes read from the stream, between zero (0) and the number of bytes you requested. Streams return zero (0) only at the end of the stream, otherwise, they should block until at least one byte is available.</returns>
        /// <param name="asyncResult">The reference to the pending asynchronous request to finish. </param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="asyncResult" /> is null. </exception>
        /// <exception cref="T:System.ArgumentException">
        ///   <paramref name="asyncResult" /> did not originate from a <see cref="M:System.IO.Stream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)" /> method on the current stream. </exception>
        /// <exception cref="T:System.IO.IOException">The stream is closed or an internal error has occurred.</exception>
        /// <filterpriority>2</filterpriority>
        public virtual int EndRead(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException("asyncResult");
            }
            StreamAsyncResult streamAsyncResult = asyncResult as StreamAsyncResult;

            if (streamAsyncResult == null || streamAsyncResult.NBytes == -1)
            {
                throw new ArgumentException("Invalid IAsyncResult", "asyncResult");
            }
            if (streamAsyncResult.Done)
            {
                throw new InvalidOperationException("EndRead already called.");
            }
            streamAsyncResult.Done = true;
            if (streamAsyncResult.Exception != null)
            {
                throw streamAsyncResult.Exception;
            }
            return(streamAsyncResult.NBytes);
        }
Example #5
0
        /// <summary>Begins an asynchronous write operation.</summary>
        /// <returns>An IAsyncResult that represents the asynchronous write, which could still be pending.</returns>
        /// <param name="buffer">The buffer to write data from. </param>
        /// <param name="offset">The byte offset in <paramref name="buffer" /> from which to begin writing. </param>
        /// <param name="count">The maximum number of bytes to write. </param>
        /// <param name="callback">An optional asynchronous callback, to be called when the write is complete. </param>
        /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests. </param>
        /// <exception cref="T:System.IO.IOException">Attempted an asynchronous write 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 write operation. </exception>
        /// <filterpriority>2</filterpriority>
        public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (!this.CanWrite)
            {
                throw new NotSupportedException("This stream does not support writing");
            }
            StreamAsyncResult streamAsyncResult = new StreamAsyncResult(state);

            try
            {
                this.Write(buffer, offset, count);
                streamAsyncResult.SetComplete(null);
            }
            catch (Exception complete)
            {
                streamAsyncResult.SetComplete(complete);
            }
            if (callback != null)
            {
                callback.BeginInvoke(streamAsyncResult, null, null);
            }
            return(streamAsyncResult);
        }
Example #6
0
        /// <summary>Begins an asynchronous read operation.</summary>
        /// <returns>An <see cref="T:System.IAsyncResult" /> that represents the asynchronous read, which could still be pending.</returns>
        /// <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>
        /// <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>
        /// <filterpriority>2</filterpriority>
        public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (!this.CanRead)
            {
                throw new NotSupportedException("This stream does not support reading");
            }
            StreamAsyncResult streamAsyncResult = new StreamAsyncResult(state);

            try
            {
                int nbytes = this.Read(buffer, offset, count);
                streamAsyncResult.SetComplete(null, nbytes);
            }
            catch (Exception e)
            {
                streamAsyncResult.SetComplete(e, 0);
            }
            if (callback != null)
            {
                callback(streamAsyncResult);
            }
            return(streamAsyncResult);
        }
        public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            var e = m_SendEventArgs;

            if (e == null)
            {
                e = new SocketAsyncEventArgs();
                e.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendEventCompleted);
                m_SendEventArgs = e;
            }

            e.SetBuffer(buffer, offset, count);

            StreamAsyncResult result = new StreamAsyncResult(e, callback);
            result.AsyncState = state;

            var async = m_Socket.SendAsync(e);

            if (!async)
            {
                result.CompletedSynchronously = true;
                ProcessSend(e);
            }

            return result;
        }
Example #8
0
		BeginWrite (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			if (!CanWrite)
				throw new NotSupportedException ("This stream does not support writing");
	
			// Creating a class derived from Stream that doesn't override BeginWrite
			// shows that it actually calls Write and does everything synchronously except
			// when invoking the callback, which is done from the ThreadPool.
			// Just put this in the Write override:
			// 	Console.WriteLine ("Write");
			// 	Console.WriteLine (Environment.StackTrace);
			//	Thread.Sleep (10000);

			StreamAsyncResult result = new StreamAsyncResult (state);
			try {
				Write (buffer, offset, count);
				result.SetComplete (null);
			} catch (Exception e) {
				result.SetComplete (e);
			}

			if (callback != null)
				callback.BeginInvoke (result, null, null);

			return result;
		}
Example #9
0
		BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			if (!CanRead)
				throw new NotSupportedException ("This stream does not support reading");

			// Creating a class derived from Stream that doesn't override BeginRead
			// shows that it actually calls Read and does everything synchronously.
			// Just put this in the Read override:
			//	Console.WriteLine ("Read");
			// 	Console.WriteLine (Environment.StackTrace);
			//	Thread.Sleep (10000);
			//	return 10;

			StreamAsyncResult result = new StreamAsyncResult (state);
			try {
				int nbytes = Read (buffer, offset, count);
				result.SetComplete (null, nbytes);
			} catch (Exception e) {
				result.SetComplete (e, 0);
			}

			if (callback != null)
				callback (result);

			return result;
		}