示例#1
0
 private void Listen()
 {
     System.Threading.Thread.CurrentThread.Name = "HttpListener";
     while (_listener.IsListening)
     {
         var result = _listener.BeginGetContext(HandleRequest, null);
         result.AsyncWaitHandle.WaitOne();
     }
 }
示例#2
0
        public void ListenerCallback(IAsyncResult result)
        {
            try
            {
                System.Net.HttpListener        listener = (System.Net.HttpListener)result.AsyncState;
                System.Net.HttpListenerContext context  = listener.EndGetContext(result);
                _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);

                System.Net.HttpListenerRequest  request  = context.Request;
                System.Net.HttpListenerResponse response = context.Response;

                _iprocessRequest.ProcessRequest(context);
            }
            catch (Exception ex)
            {
                ServerComms.LogError($"Http Listener falled {ex.Message}");
                _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
            }
        }
示例#3
0
        public void CreateListener(List <string> prefixes)
        {
            _listener = new System.Net.HttpListener();
            foreach (string s in prefixes)
            {
                _listener.Prefixes.Add(s);
            }
            _listener.Start();

            var result = _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
        }
示例#4
0
        public void CreateListener(Dictionary <String, X509Certificate2> prefixes)
        {
            var certProc = new CertificateProcessor(ServerComms);

            _listener = new System.Net.HttpListener();


            foreach (string s in prefixes.Keys)
            {
                certProc.AddCertificateToHost(s, prefixes[s]);
                _listener.Prefixes.Add(s);
            }
            _listener.Start();

            var result = _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
        }
 private void Listen()
 {
     try
     {
         _listener.BeginGetContext(OnContext, null);
     }
     catch (ObjectDisposedException e)
     {
         // This exception is expected because we don't synchronize dispose with
         // BeginGetContext...
         Log.DebugFormat("Caught expected/unavaoidable exception: {0}", e);
     }
     catch (Exception e)
     {
         Log.Error("Caught unexpected exception: {0}", e);
     }
 }
示例#6
0
 static void _http_beginRecv()
 {
     try
     {
         _http_listener.BeginGetContext(_http_onRequest, null);
     }
     catch (Exception err)
     {
         if (!bRun)
         {
             return;
         }
         else
         {
             Log_Error("err");
         }
     }
 }
示例#7
0
 private static void Listen()
 {
     http.BeginGetContext((iar) =>
     {
         System.Net.HttpListenerContext context = http.EndGetContext(iar);
         string name = "World";
         if (context.Request.QueryString.AllKeys.Contains("name"))
         {
             name = context.Request.QueryString["name"];
         }
         System.IO.Stream stream = context.Response.OutputStream;
         byte[] encodedMessage   = System.Text.Encoding.ASCII.GetBytes("Hello " + name);
         stream.Write(encodedMessage, 0, encodedMessage.Length);
         Console.WriteLine("Request from " + name);
         context.Response.Close();
         Listen();
     }, new object());
 }
示例#8
0
 void _http_beginRecv()
 {
     try
     {
         _http_listener.BeginGetContext(_http_onRequest, null);
     }
     catch (Exception err)
     {
         if (exited)
         {
             return;
         }
         else
         {
             logger.Log_Error("err");
         }
     }
 }
示例#9
0
        public void CreateListener(Dictionary <String, X509Certificate2> prefixes)
        {
            var certProc = new CertificateProcessor(ServerComms);

            _listener = new System.Net.HttpListener();


            foreach (string s in prefixes.Keys)
            {
                certProc.AddCertificateToHost(s, prefixes[s]);
                _listener.Prefixes.Add(s);
            }
            _listener.Start();

            System.Threading.Tasks.Task.Factory.StartNew(() => {
                while (_listener.IsListening)
                {
                    var result = _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
                    result.AsyncWaitHandle.WaitOne();
                }
            });
        }
示例#10
0
        private void _Server()
        {
            Trace.TraceInformation("Enter.");

            System.Net.HttpListener _Listener = null;
            string sResponse = string.Empty;

            try
            {
                _Listener = new System.Net.HttpListener();
                _Listener.Prefixes.Add("http://" + _WebServerIP + ":" + _WebServerPort + "/" + _WebServerURIPrefix + "/");
                _Listener.Start();

                Trace.TraceInformation("Listener started for prefix " + "http://" + _WebServerIP + ":" + _WebServerPort + "/" + _WebServerURIPrefix + "/");

                if (Started != null)
                {
                    Started(this, new EventArgs());
                }

                do
                {
                    Trace.TraceInformation("Listening for request to be processed asyncronously.");

                    IAsyncResult result = _Listener.BeginGetContext(_Callback, _Listener);

                    Trace.TraceInformation("Waiting for request to be processed asyncronously.");

                    result.AsyncWaitHandle.WaitOne();

                    Trace.TraceInformation("Request processed asyncronously.");
                } while (true);
            }
            catch (System.Threading.ThreadAbortException abortEx)
            {
                Trace.TraceError("ThreadAbortException:" + abortEx.Message + Environment.NewLine + "StackTrace:" + abortEx.StackTrace);

                System.Threading.Thread.ResetAbort();

                _Abort.Set();

                if (Aborted != null)
                {
                    Aborted(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Exception:" + ex.Message + Environment.NewLine + "StackTrace:" + ex.StackTrace);

                _Abort.Set();

                if (Error != null)
                {
                    Error(this, new WebServerEventArgs("Exception:" + ex.Message + Environment.NewLine + "StackTrace:" + ex.StackTrace));
                }
            }
            finally
            {
                if (_Listener != null)
                {
                    if (_Listener.IsListening)
                    {
                        _Listener.Stop();
                    }
                }

                _Listener = null;
            }

            if (Stopped != null)
            {
                Stopped(this, new EventArgs());
            }
        }
示例#11
0
 public void StartServer(int port)//启动服务器
 {
     host.Prefixes.Add("http://*:" + port + "/");
     host.Start();
     host.BeginGetContext(onHttpIn, null);
 }