コード例 #1
0
        private void ErrorPage(IRequest request, IResponse response, HttpException exception)
        {
            response.Status = exception.Code;
            response.Reason = exception.Message;
            response.Body.SetLength(0);

            var args = new ErrorPageEventArgs(HttpContext.Current, request, response);

            args.Exception = exception;
            ErrorPageRequested(this, args);

            // Add default error body.
            if (!args.IsHandled)
            {
                string htmlTemplate = "<html><head><title>{1}</title></head><body>Error {0}: {1}</body></html>";
                byte[] body         = Encoding.UTF8.GetBytes(string.Format(htmlTemplate, (int)response.Status, response.Reason));
                response.Body.Write(body, 0, body.Length);
            }

            try
            {
                var generator = new ResponseWriter();
                generator.Send(HttpContext.Current, response);
            }
            catch (Exception err)
            {
#if DEBUG
                if (ExceptionThrown.GetInvocationList().Length == 1)
                {
                    throw;
                }
#endif
                ExceptionThrown(this, new ExceptionEventArgs(err));
            }
        }
コード例 #2
0
        private void BeginAccept()
        {
            if (_shuttingDown)
            {
                return;
            }


            Interlocked.Increment(ref _pendingAccepts);
            try
            {
                _listener.BeginAcceptSocket(OnSocketAccepted, null);
            }
            catch (Exception err)
            {
#if DEBUG
                if (ExceptionThrown.GetInvocationList().Length == 1)
                {
                    throw;
                }
#endif
                ExceptionThrown(this, new ExceptionEventArgs(err));
                _logger.Error("Unhandled exception in BeginAccept.\r\n" + err);
            }
        }
コード例 #3
0
        /// <exception cref="Exception">Throwing exception if in debug mode and not exception handler have been specified.</exception>
        private void OnRequest(object sender, RequestEventArgs e)
        {
            var context = (HttpContext)sender;

            HttpFactory.Current = Factory;
            HttpContext.Current = context;

            try
            {
                var args = new RequestEventArgs(context, e.Request, e.Response);
                RequestReceived(this, args);
                if (!args.IsHandled)
                {
                    // need to respond to the context.
                    var generator = new ResponseWriter();
                    generator.Send(context, args.Response);
                }

                // Disconnect when done.
                if (e.Response.HttpVersion == "HTTP/1.0" || e.Response.Connection.Type == ConnectionType.Close)
                {
                    context.Disconnect();
                }
            }
            catch (Exception err)
            {
                if (err is HttpException)
                {
                    var exception = (HttpException)err;
                    ErrorPage(e.Request, e.Response, exception);
                }
                else
                {
                    _logger.Debug("Request failed.", err);
#if DEBUG
                    if (ExceptionThrown.GetInvocationList().Length == 1)
                    {
                        throw;
                    }
#endif
                    ExceptionThrown(this, new ExceptionEventArgs(err));
                    SendInternalServerError(context, err);
                }
            }
        }
コード例 #4
0
        private void WorkingLoop()
        {
            try
            {
                IntPtr ptrPacket = IntPtr.Zero;
                IntPtr ptrHeader = IntPtr.Zero;

                while (bRun)
                {
                    int iReturn = pcap_next_ex(iptrOpenDevice, ref ptrHeader, ref ptrPacket);
                    if (iReturn == 1)
                    {
                        OnPacketCaptured(IntPtr.Zero, (PcapPacketHeader)Marshal.PtrToStructure(ptrHeader, typeof(PcapPacketHeader)), ptrPacket);
                    }
                    else if (iReturn == -1)
                    {
                        throw new Exception("An error occurred while reading from a WinPcap interface. (Error number: " + iReturn + ")");
                    }
                }

                ptrPacket = IntPtr.Zero;
                ptrHeader = IntPtr.Zero;
            }
            catch (Exception ex)
            {
                if (ExceptionThrown != null)
                {
                    foreach (Delegate dDelgate in ExceptionThrown.GetInvocationList())
                    {
                        if (dDelgate.Target != null && dDelgate.Target is System.ComponentModel.ISynchronizeInvoke &&
                            ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).InvokeRequired)
                        {
                            ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).BeginInvoke(dDelgate, new object[] { this, new ExceptionEventArgs(ex, DateTime.Now) });
                        }
                        else
                        {
                            ((eExNetworkLibrary.TrafficHandler.ExceptionEventHandler)dDelgate)(this, new ExceptionEventArgs(ex, DateTime.Now));
                        }
                    }
                }
            }
        }
コード例 #5
0
        private void OnSocketAccepted(IAsyncResult ar)
        {
            HttpFactory.Current = Factory;
            Socket socket = null;

            try
            {
                socket = _listener.EndAcceptSocket(ar);
                Interlocked.Decrement(ref _pendingAccepts);
                if (_shuttingDown && _pendingAccepts == 0)
                {
                    _shutdownEvent.Set();
                }

                if (!CanAcceptSocket(socket))
                {
                    _logger.Debug("Socket was rejected: " + socket.RemoteEndPoint);
                    socket.Disconnect(true);
                    BeginAccept();
                    return;
                }
            }
            catch (Exception err)
            {
                _logger.Warning("Failed to end accept: " + err.Message);
                BeginAccept();
                if (socket != null)
                {
                    socket.Disconnect(true);
                }
                return;
            }

            if (!_shuttingDown)
            {
                BeginAccept();
            }

            _logger.Trace("Accepted connection from: " + socket.RemoteEndPoint);

            // Got a new context.
            try
            {
                HttpContext context = CreateContext(socket);
                HttpContext.Current                = context;
                context.HttpFactory                = _factory;
                context.RequestReceived           += OnRequest;
                context.Disconnected              += OnDisconnect;
                context.ContinueResponseRequested += On100Continue;
                context.Start();
            }
            catch (Exception err)
            {
                _logger.Error("ContextReceived raised an exception: " + err.Message);
#if DEBUG
                if (ExceptionThrown.GetInvocationList().Length == 1)
                {
                    throw;
                }
#endif
                ExceptionThrown(this, new ExceptionEventArgs(err));
                socket.Disconnect(true);
            }
        }