Example #1
0
        /// <summary>
        ///     Starts the server, and all request handling threads
        ///     <para />
        ///     Only answers to requests with specified prefixes
        ///     <para />
        ///     Specify in following format:
        ///     <para />
        ///     "+" , "*" , "localhost" , "example.com", "123.12.34.56"
        ///     <para />
        ///     Protocol and port will be added automatically
        /// </summary>
        /// <param name="listeningPrefixes">The prefixes the server will listen for requests with</param>
        public void Start(params string[] listeningPrefixes)
        {
            try
            {
                if (!listeningPrefixes.Any())
                {
                    Console.WriteLine(
                        "You must listen for either http or https (or both) requests for the server to do anything");
                    return;
                }
                InitializeDefaultPlugins();
                foreach (var listeningPrefix in listeningPrefixes)
                {
                    if (HttpEnabled)
                    {
                        _listener.Prefixes.Add($"http://{listeningPrefix}:{Port}/");
                    }
                    if (HttpsEnabled)
                    {
                        _listener.Prefixes.Add($"https://{listeningPrefix}:{HttpsPort}/");
                    }
                }
                _listener.Start();
                _listenerThread.Start();
                OnStart();

                Console.WriteLine("RHttpServer v. {0} started", Version);
                if (_listener.Prefixes.First() == "localhost")
                {
                    Logger.Log("Server visibility", "Listening on localhost only");
                }
                RenderParams.Converter = _rPluginCollection.Use <IJsonConverter>();
            }
            catch (SocketException)
            {
                Console.WriteLine("Unable to bind to port, the port may already be in use.");
                Environment.Exit(0);
            }
            catch (HttpListenerException)
            {
                Console.WriteLine("Could not obtain permission to listen for '{0}' on selected port\n" +
                                  "Please aquire the permission or start the server as local-only",
                                  string.Join(", ", listeningPrefixes));
                Environment.Exit(0);
            }
        }
Example #2
0
 /// <summary>
 ///     Returns the body deserialized or parsed to specified type, if possible, default if not
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public T ParseBody <T>()
 {
     return(_rp.Use <IBodyParser>().ParseBody <T>(UnderlyingRequest));
 }