// ---------------- Functions ---------------- public void Init(PluginInitor pluginInit) { string configPath = Path.Combine( pluginInit.ChaskisConfigPluginRoot, "HttpServer", "HttpServerConfig.xml" ); this.config = XmlLoader.LoadConfig(configPath); this.log = pluginInit.Log; IChaskisEventCreator eventCreator = pluginInit.ChaskisEventCreator; ChaskisEventHandler coreEvent = eventCreator.CreateCoreEventHandler( ChaskisEventProtocol.IRC, this.OnConnect ); this.handlers.Add(coreEvent); ConnectedEventConfig connectedEventConfig = new ConnectedEventConfig { ConnectedAction = this.OnConnect }; this.handlers.Add( new ConnectedEventHandler(connectedEventConfig) ); }
// ---------------- Functions ---------------- public static HttpServerConfig LoadConfig(string configPath) { if (File.Exists(configPath) == false) { throw new FileNotFoundException("Could not find " + nameof(HttpServerConfig) + " file " + configPath); } XmlDocument doc = new XmlDocument(); doc.Load(configPath); XmlElement rootNode = doc.DocumentElement; if (rootNode.Name != rootNodeName) { throw new XmlException( "Root XML node should be named \"" + rootNodeName + "\". Got: " + rootNode.Name ); } HttpServerConfig config = new HttpServerConfig(); foreach (XmlNode childNode in rootNode.ChildNodes) { switch (childNode.Name) { case "port": config.Port = ushort.Parse(childNode.InnerText); break; } } return(config); }
// ---------------- Constructor ---------------- public HttpServer(HttpServerConfig config, HttpResponseHandler responseHandler) { this.isDisposed = false; this.isListening = false; this.isListeningLock = new object(); ArgumentChecker.IsNotNull(config, nameof(config)); config.Validate(); if (HttpListener.IsSupported == false) { throw new PlatformNotSupportedException( "This platform does not support HTTP Listeners..." ); } this.listener = new HttpListener(); this.listener.Prefixes.Add("http://localhost:" + config.Port + "/"); this.responseHandler = responseHandler; this.listenThread = new Thread(this.HandleRequestThreadEntry); this.listenThread.Name = "Http Server Thread"; }