Author: alanmc Date: 2008-02-19 16:30:42 -0500 (Tue, 19 Feb 2008) New Revision: 96190
Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/HttpListener.cs trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/ListenerBase.cs trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs trunk/bitsharp/src/TrackerApp/Main.cs trunk/bitsharp/src/TrackerApp/StressTest.cs Log: Renamespaced the listerers. Added null checks for the public methods. Added missing try/catch in HttpListener Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/HttpListener.cs =================================================================== --- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/HttpListener.cs 2008-02-19 21:05:00 UTC (rev 96189) +++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/HttpListener.cs 2008-02-19 21:30:42 UTC (rev 96190) @@ -37,13 +37,13 @@ using MonoTorrent.Common; using MonoTorrent.BEncoding; -namespace MonoTorrent.Tracker +namespace MonoTorrent.Tracker.Listeners { public class HttpListener : ListenerBase { #region Fields - private IPEndPoint endpoint; + private string prefix; private System.Net.HttpListener listener; #endregion Fields @@ -65,18 +65,25 @@ #region Constructors public HttpListener(IPAddress address, int port) - : this(new IPEndPoint(address, port)) + : this(string.Format("http://{0}:{1}/", address, port)) { } public HttpListener(IPEndPoint endpoint) + : this(endpoint.Address, endpoint.Port) { - if (endpoint == null) - throw new ArgumentNullException("endpoint"); + } + + public HttpListener(string httpPrefix) + { + if (string.IsNullOrEmpty(httpPrefix)) + throw new ArgumentNullException("httpPrefix"); + listener = new System.Net.HttpListener(); - this.endpoint = endpoint; + listener.Prefixes.Add(httpPrefix); + this.prefix = httpPrefix; } #endregion Constructors @@ -89,7 +96,6 @@ /// </summary> public override void Start() { - listener.Prefixes.Add(string.Format("http://{0}:{1}/", endpoint.Address.ToString(), endpoint.Port)); listener.Start(); listener.BeginGetContext(EndGetRequest, null); @@ -105,11 +111,24 @@ private void EndGetRequest(IAsyncResult result) { - HttpListenerContext context; - context = listener.EndGetContext(result); - HandleRequest(context); - context.Response.Close(); - listener.BeginGetContext(EndGetRequest, null); + HttpListenerContext context = null; + try + { + context = listener.EndGetContext(result); + HandleRequest(context); + } + catch(Exception ex) + { + Console.Write("Exception in listener: {0}{1}", Environment.NewLine, ex); + } + finally + { + if (context != null) + context.Response.Close(); + + if (listener.IsListening) + listener.BeginGetContext(EndGetRequest, null); + } } private void HandleRequest(HttpListenerContext context) @@ -121,6 +140,7 @@ byte[] response = responseData.Encode(); context.Response.ContentType = "text/plain"; context.Response.StatusCode = 200; + context.Response.ContentLength64 = response.LongLength; context.Response.OutputStream.Write(response, 0, response.Length); } Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/ListenerBase.cs =================================================================== --- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/ListenerBase.cs 2008-02-19 21:05:00 UTC (rev 96189) +++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Listeners/ListenerBase.cs 2008-02-19 21:30:42 UTC (rev 96190) @@ -6,49 +6,8 @@ using System.Net; using System.Threading; -namespace MonoTorrent.Tracker +namespace MonoTorrent.Tracker.Listeners { - internal class RequestAsyncResult : IAsyncResult - { - private object asyncState; - private bool isComplete; - private AsyncCallback callback; - private ManualResetEvent waitHandle; - private BEncodedDictionary response; - - public RequestAsyncResult(AsyncCallback callback, object asyncState) - { - this.asyncState = asyncState; - this.callback = callback; - } - - public object AsyncState - { - get { throw new Exception("The method or operation is not implemented."); } - } - - public WaitHandle AsyncWaitHandle - { - get { return waitHandle; } - } - - public bool CompletedSynchronously - { - get { return false; } - } - - public bool IsCompleted - { - get { return isComplete; } - internal set { isComplete = value; } - } - - public BEncodedDictionary Response - { - get { return response; } - } - } - public abstract class ListenerBase { private delegate void HandleDelegate(NameValueCollection collection, IPAddress remoteAddress); @@ -87,13 +46,21 @@ } */ - protected virtual BEncodedValue Handle(string queryString, IPAddress remoteAddress, bool isScrape) + public virtual BEncodedValue Handle(string queryString, IPAddress remoteAddress, bool isScrape) { + if (queryString == null) + throw new ArgumentNullException("queryString"); + return Handle(ParseQuery(queryString), remoteAddress, isScrape); } - protected virtual BEncodedValue Handle(NameValueCollection collection, IPAddress remoteAddress, bool isScrape) + public virtual BEncodedValue Handle(NameValueCollection collection, IPAddress remoteAddress, bool isScrape) { + if (collection == null) + throw new ArgumentNullException("collection"); + if (remoteAddress == null) + throw new ArgumentNullException("remoteAddress"); + RequestParameters parameters; if (isScrape) parameters = new ScrapeParameters(collection, remoteAddress); @@ -102,17 +69,14 @@ // If the parameters are invalid, the failure reason will be added to the response dictionary if (!parameters.IsValid) - { return parameters.Response; - } + + // Fire the necessary event so the request will be handled and response filled inin + if (isScrape) + RaiseScrapeReceived((ScrapeParameters)parameters); else - { - // Otherwise, fire the necessary event and the request will be handled and the response filled in - if (isScrape) - RaiseScrapeReceived((ScrapeParameters)parameters); - else - RaiseAnnounceReceived((AnnounceParameters)parameters); - } + RaiseAnnounceReceived((AnnounceParameters)parameters); + // Return the response now that the connection has been handled correctly. return parameters.Response; } Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs =================================================================== --- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs 2008-02-19 21:05:00 UTC (rev 96189) +++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs 2008-02-19 21:30:42 UTC (rev 96190) @@ -38,6 +38,7 @@ using MonoTorrent.Common; using MonoTorrent.BEncoding; +using MonoTorrent.Tracker.Listeners; namespace MonoTorrent.Tracker { Modified: trunk/bitsharp/src/TrackerApp/Main.cs =================================================================== --- trunk/bitsharp/src/TrackerApp/Main.cs 2008-02-19 21:05:00 UTC (rev 96189) +++ trunk/bitsharp/src/TrackerApp/Main.cs 2008-02-19 21:30:42 UTC (rev 96190) @@ -38,6 +38,7 @@ using MonoTorrent.Common; using TrackerApp; using MonoTorrent.TorrentWatcher; +using MonoTorrent.Tracker.Listeners; namespace SampleTracker { Modified: trunk/bitsharp/src/TrackerApp/StressTest.cs =================================================================== --- trunk/bitsharp/src/TrackerApp/StressTest.cs 2008-02-19 21:05:00 UTC (rev 96189) +++ trunk/bitsharp/src/TrackerApp/StressTest.cs 2008-02-19 21:30:42 UTC (rev 96190) @@ -7,6 +7,7 @@ using System.Web; using MonoTorrent.BEncoding; using System.Threading; +using MonoTorrent.Tracker.Listeners; namespace TrackerApp { _______________________________________________ Mono-patches maillist - Mono-patches@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-patches