private void NonBlockingReadCallback(IAsyncResult asyncResult)
        {
            NonBlockingReadContext context = (NonBlockingReadContext)asyncResult.AsyncState;
            Stream istream = context.istream;

            try {
                if (istream.CanRead)
                {
                    int n = istream.EndRead(asyncResult);
                    if (n > 0)
                    {
                        context.data.Write(context.buffer, 0, n);
                        istream.BeginRead(context.buffer, 0, context.bufferlength,
                                          new AsyncCallback(NonBlockingReadCallback), context);
                        return;
                    }
                }
                else
                {
                    return;
                }
                istream.Close();
            }
            catch (Exception e) {
                FireErrorEvent(e, context.currentContext);
                try {
                    istream.Close();
                    context.currentContext.Response.Close();
                }
                catch (Exception) { }
                return;
            }
            NonBlockingWrite(context);
        }
        private void NonBlockingWrite(NonBlockingReadContext context)
        {
            currentContext = context.currentContext;
            NonBlockingWriteContext writeContext = new NonBlockingWriteContext();

            writeContext.currentContext = context.currentContext;
            try {
                MemoryStream data = Handle(context.data, context.methods, context.currentContext);
                writeContext.ostream = GetOutputStream(context.currentContext);
                writeContext.ostream.BeginWrite(data.GetBuffer(), 0, (int)data.Length,
                                                new AsyncCallback(NonBlockingWriteCallback), writeContext);
            }
            catch (Exception e) {
                FireErrorEvent(e, currentContext);
                try {
                    if (writeContext.ostream != null)
                    {
                        writeContext.ostream.Close();
                    }
                    context.currentContext.Response.Close();
                }
                catch (Exception) { }
                return;
            }
            finally {
                currentContext = null;
            }
        }
        private void NonBlockingHandle(HttpListenerContext currentContext, HproseHttpListenerMethods methods)
        {
            NonBlockingReadContext context = new NonBlockingReadContext();

            context.currentContext = currentContext;
            context.methods        = methods;
            context.istream        = currentContext.Request.InputStream;
            int len = (int)currentContext.Request.ContentLength64;

            context.data         = (len > 0) ? new MemoryStream(len) : new MemoryStream();
            context.bufferlength = (len > 81920 || len < 0) ? 81920 : len;
            context.buffer       = new byte[context.bufferlength];
            context.istream.BeginRead(context.buffer, 0, context.bufferlength, new AsyncCallback(NonBlockingReadCallback), context);
        }
 private void NonBlockingHandle(HproseHttpListenerContext currentContext, HproseHttpListenerMethods methods) {
     NonBlockingReadContext context = new NonBlockingReadContext();
     context.currentContext = currentContext;
     context.methods = methods;
     context.istream = currentContext.Request.InputStream;
     int len = (int)currentContext.Request.ContentLength64;
     context.data = (len > 0) ? new MemoryStream(len) : new MemoryStream();
     context.bufferlength = (len > 81920 || len < 0) ? 81920 : len;
     context.buffer = new byte[context.bufferlength];
     context.istream.BeginRead(context.buffer, 0, context.bufferlength, new AsyncCallback(NonBlockingReadCallback), context);
 }
 private void NonBlockingWrite(NonBlockingReadContext context) {
     currentContext = context.currentContext.Context;
     NonBlockingWriteContext writeContext = new NonBlockingWriteContext();
     writeContext.currentContext = context.currentContext;
     try {
         MemoryStream data = Handle(context.data, context.methods, context.currentContext);
         writeContext.ostream = GetOutputStream(context.currentContext);
         writeContext.ostream.BeginWrite(data.GetBuffer(), 0, (int)data.Length,
                 new AsyncCallback(NonBlockingWriteCallback), writeContext);
     }
     catch (Exception e) {
         FireErrorEvent(e, context.currentContext);
         try {
             if (writeContext.ostream != null) {
                 writeContext.ostream.Close();
             }
             context.currentContext.Response.Close();
         }
         catch (Exception) { }
         return;
     }
     finally {
         currentContext = null;
     }
 }