Universe.Framework.Servers.HttpServer.BaseHttpServer.Start C# (CSharp) Method

Start() public method

public Start ( ) : void
return void
        public void Start ()
        {
            MainConsole.Instance.InfoFormat ("[Base HTTP server]: Starting {0} server on port {1}", Secure ? "HTTPS" : "HTTP", Port);

            try {
                NotSocketErrors = 0;
                m_internalServer = new HttpListenerManager (m_threadCount, Secure);
                if (OnOverrideRequest != null)
                    m_internalServer.ProcessRequest += OnOverrideRequest;
                else
                    m_internalServer.ProcessRequest += OnRequest;

                m_internalServer.Start (m_port);

                // Long Poll Service Manager with 3 worker threads a 25 second timeout for no events
                m_PollServiceManager = new PollServiceRequestManager (3, 25000);
                m_PollServiceManager.Start ();
                HTTPDRunning = true;
            } catch (Exception e) {
                if (e is HttpListenerException && ((HttpListenerException)e).Message == "Access is denied")
                    MainConsole.Instance.Error ("[Base HTTP server]: You must run this program as an administrator.");
                else {
                    MainConsole.Instance.Error ("[Base HTTP server]: Error - " + e.Message);
                    MainConsole.Instance.Error ("[Base HTTP server]: Tip: Do you have permission to listen on port " + m_port + "?");
                }

                // We want this exception to halt the entire server since in current configurations we aren't too
                // useful without inbound HTTP.
                throw e;
            }
        }

Usage Example

        /// <summary>
        ///     Get an HTTPServer on the given port. It will create one if one does not exist
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public IHttpServer GetHttpServer(uint port)
        {
            if ((port == m_Port || port == 0) && HttpServer != null)
                return HttpServer;

            bool useHTTPS = m_config.Configs["Network"].GetBoolean("use_https", false);
            IHttpServer server;
            if (m_Servers.TryGetValue(port, out server) && server.Secure == useHTTPS)
                return server;

            uint threadCount = m_config.Configs["Network"].GetUInt("HttpThreadCount", 5);

            // find out where we live
            string hostName;

            // been here before?
            if (Utilities.HostName == "")
            {
                hostName = m_config.Configs ["Network"].GetString ("HostName", "0.0.0.0");

                if ((hostName == "") || (hostName == "0.0.0.0"))
                {
                    MainConsole.Instance.Info ("[Network]: Retrieving the external IP address");
                    hostName = "http" + (useHTTPS ? "s" : "") + "://" + Utilities.GetExternalIp ();
                }
            
                //Clean it up a bit
                if (hostName.StartsWith ("http://", StringComparison.OrdinalIgnoreCase) || hostName.StartsWith ("https://", StringComparison.OrdinalIgnoreCase))
                    hostName = hostName.Replace ("https://", "").Replace ("http://", "");
                if (hostName.EndsWith ("/", StringComparison.Ordinal))
                    hostName = hostName.Remove (hostName.Length - 1, 1);
                
                // save this for posterity in case it is needed
                MainConsole.Instance.Info ("[Network]: Network IP address has been set to " + hostName);
                Utilities.HostName = hostName;
            } else
                hostName = Utilities.HostName;

            server = new BaseHttpServer(port, hostName, useHTTPS, threadCount);

            try
            {
                server.Start();
            }
            catch (Exception)
            {
                //Remove the server from the list
                m_Servers.Remove(port);
                //Then pass the exception upwards
                throw;
            }

            if (m_Servers.Count == 0)
                MainServer.Instance = server;

            return (m_Servers[port] = server);
        }
All Usage Examples Of Universe.Framework.Servers.HttpServer.BaseHttpServer::Start