From f087ae5c83a9e2af67d5239e00cafbf33b540a1e Mon Sep 17 00:00:00 2001 From: high Date: Mon, 5 Sep 2011 01:11:08 -0400 Subject: [PATCH] IP/Port can now be changed before started. RestVerbs added instead of Dictionary --- TShockAPI/Rest.cs | 29 +++++++++++++++++++++++------ TShockAPI/TShock.cs | 4 ++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/TShockAPI/Rest.cs b/TShockAPI/Rest.cs index 51e36d8d..5393dbd7 100644 --- a/TShockAPI/Rest.cs +++ b/TShockAPI/Rest.cs @@ -17,21 +17,33 @@ namespace TShockAPI /// Parameters in the url /// Http request /// Response object or null to not handle request - public delegate object RestCommandD(Dictionary verbs, IParameterCollection parameters, RequestEventArgs request); + public delegate object RestCommandD(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request); public class Rest : IDisposable { readonly List commands = new List(); HttpListener listener; + public IPAddress Ip { get; set; } + public int Port { get; set; } public Rest(IPAddress ip, int port) { - listener = HttpListener.Create(ip, port); - listener.RequestReceived += OnRequest; + Ip = ip; + Port = port; } - public void Start() { - listener.Start(int.MaxValue); + if (listener != null) + { + listener = HttpListener.Create(Ip, Port); + listener.RequestReceived += OnRequest; + listener.Start(int.MaxValue); + } + } + public void Start(IPAddress ip, int port) + { + Ip = ip; + Port = port; + Start(); } public void Stop() { @@ -67,7 +79,7 @@ namespace TShockAPI var matches = Regex.Matches(e.Request.Uri.AbsolutePath, com.UriMatch); if (matches.Count == com.UriNames.Length) { - var verbs = new Dictionary(); + var verbs = new RestVerbs(); for (int i = 0; i < matches.Count; i++) verbs.Add(com.UriNames[i], matches[i].Groups[1].Value); @@ -104,6 +116,11 @@ namespace TShockAPI #endregion } + public class RestVerbs : Dictionary + { + + } + public class RestCommand { public string UriTemplate { get; protected set; } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index ef3effc3..62c9691e 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -203,7 +203,7 @@ namespace TShockAPI Log.ConsoleInfo("Backups " + (Backups.Interval > 0 ? "Enabled" : "Disabled")); if (Initialized != null) - Initialized(); + Initialized(); RestApi.Register(new RestCommand("/HelloWorld/name/{username}", usertest)); } @@ -237,7 +237,7 @@ namespace TShockAPI } //http://127.0.0.1:8080/HelloWorld/name/{username}?type=status - object usertest(Dictionary verbs, IParameterCollection parameters, RequestEventArgs request) + object usertest(RestVerbs verbs, IParameterCollection parameters, RequestEventArgs request) { var ret = new Dictionary(); var type = parameters["type"];