private void ProcessSend()
 {
     if (sendResult != null && sendResult.IsCompleted)
     {
         try {
             OutputStream.EndWrite(sendResult);
             sendBytesCounter.Add((int)sendResult.AsyncState);
         }
         catch (ObjectDisposedException) {
         }
         catch (IOException) {
             OnError();
         }
         sendResult = null;
     }
     if (!HasError && sendResult == null && sendStream.Length > 0)
     {
         var buf = sendStream.ToArray();
         sendStream.SetLength(0);
         sendStream.Position = 0;
         try {
             sendResult = OutputStream.BeginWrite(buf, 0, buf.Length, null, buf.Length);
         }
         catch (ObjectDisposedException) {
         }
         catch (IOException) {
             OnError();
         }
     }
 }
        public void Close(byte[] responseEntity, bool willBlock)
        {
            CheckDisposed();

            if (responseEntity == null)
            {
                throw new ArgumentNullException(nameof(responseEntity));
            }

            if (!SentHeaders && _boundaryType != BoundaryType.Chunked)
            {
                ContentLength64 = responseEntity.Length;
            }

            if (willBlock)
            {
                try
                {
                    OutputStream.Write(responseEntity, 0, responseEntity.Length);
                }
                finally
                {
                    Close(false);
                }
            }
            else
            {
                OutputStream.BeginWrite(responseEntity, 0, responseEntity.Length, iar =>
                {
                    var thisRef = (HttpListenerResponse)iar.AsyncState;
                    try
                    {
                        try
                        {
                            thisRef.OutputStream.EndWrite(iar);
                        }
                        finally
                        {
                            thisRef.Close(false);
                        }
                    }
                    catch (Exception)
                    {
                        // In case response was disposed during this time
                    }
                }, this);
            }
        }
示例#3
0
        IAsyncResult BeginWriteString(string str, Action <IAsyncResult> callback)
        {
            var b = Encoding.ASCII.GetBytes(str);

            return(stream.BeginWrite(b, 0, b.Length, iasr => callback(iasr), null));
        }
示例#4
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 BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            CheckArgument(buffer, offset, count);

            return(OutputStream.BeginWrite(buffer, offset, count, callback, state));
        }