Author: alanmc
Date: 2008-01-12 06:14:50 -0500 (Sat, 12 Jan 2008)
New Revision: 92725
Modified:
trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/HttpListener.cs
trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/ListenerBase.cs
Log:
Simplified the usage of ListenerBase
Modified: trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/HttpListener.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/HttpListener.cs
2008-01-12 09:58:39 UTC (rev 92724)
+++ trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/HttpListener.cs
2008-01-12 11:14:50 UTC (rev 92725)
@@ -114,45 +114,16 @@
private void HandleRequest(HttpListenerContext context)
{
- RequestParameters parameters;
bool isScrape = context.Request.RawUrl.StartsWith("/scrape",
StringComparison.OrdinalIgnoreCase);
- NameValueCollection collection =
ParseQuery(context.Request.RawUrl);
- if (isScrape)
- parameters = new ScrapeParameters(collection,
context.Request.RemoteEndPoint.Address);
- else
- parameters = new AnnounceParameters(collection,
context.Request.RemoteEndPoint.Address);
- if (!parameters.IsValid)
- {
- // The failure reason has already been filled in to the
response
- return;
- }
- else
- {
- if (isScrape)
- RaiseScrapeReceived((ScrapeParameters)parameters);
- else
- RaiseAnnounceReceived((AnnounceParameters)parameters);
- }
+ BEncodedValue responseData = Handle(context.Request.RawUrl,
context.Request.RemoteEndPoint.Address, isScrape);
- byte[] response = parameters.Response.Encode();
+ byte[] response = responseData.Encode();
context.Response.ContentType = "text/plain";
context.Response.StatusCode = 200;
context.Response.OutputStream.Write(response, 0, response.Length);
}
- private NameValueCollection ParseQuery(string url)
- {
- url = url.Substring(url.IndexOf('?') + 1);
- string[] parts = url.Split('&', '=');
- NameValueCollection c = new NameValueCollection(1 + parts.Length /
2);
- for (int i = 0; i < parts.Length; i += 2)
- if (parts.Length > i + 1)
- c.Add(parts[i], parts[i + 1]);
-
- return c;
- }
-
#endregion Methods
}
}
Modified: trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/ListenerBase.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/ListenerBase.cs
2008-01-12 09:58:39 UTC (rev 92724)
+++ trunk/bitsharp/src/MonoTorrent/Tracker/Listeners/ListenerBase.cs
2008-01-12 11:14:50 UTC (rev 92725)
@@ -1,11 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
+using MonoTorrent.BEncoding;
+using System.Collections.Specialized;
+using System.Net;
+using System.Threading;
namespace MonoTorrent.Tracker
{
+ 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);
+
public event EventHandler<ScrapeParameters> ScrapeReceived;
public event EventHandler<AnnounceParameters> AnnounceReceived;
@@ -24,14 +71,76 @@
/// </summary>
public abstract void Stop();
- protected virtual void RaiseAnnounceReceived(AnnounceParameters e)
+ /*
+ public virtual IAsyncResult BeginHandle(string queryString, IPAddress
remoteAddress, AsyncCallback callback, object asyncData)
{
+ return BeginHandle(ParseQuery(queryString), remoteAddress,
callback, asyncData);
+ }
+
+ public virtual IAsyncResult BeginHandle(NameValueCollection
collection, IPAddress remoteAddres, AsyncCallback callback, object asyncData)
+ {
+
+ }
+
+ public virtual BEncodedDictionary EndHandle(IAsyncResult result)
+ {
+
+ }
+ */
+ public virtual BEncodedValue Handle(string queryString, IPAddress
remoteAddress, bool isScrape)
+ {
+ return Handle(ParseQuery(queryString), remoteAddress, isScrape);
+ }
+
+ public virtual BEncodedValue Handle(NameValueCollection collection,
IPAddress remoteAddress, bool isScrape)
+ {
+ RequestParameters parameters;
+ if (isScrape)
+ parameters = new ScrapeParameters(collection, remoteAddress);
+ else
+ parameters = new AnnounceParameters(collection, remoteAddress);
+
+ // If the parameters are invalid, the failure reason will be added
to the response dictionary
+ if (!parameters.IsValid)
+ {
+ return parameters.Response;
+ }
+ 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);
+ }
+ // Return the response now that the connection has been handled
correctly.
+ return parameters.Response;
+ }
+
+ private NameValueCollection ParseQuery(string url)
+ {
+ // The '?' symbol will be there if we received the entire URL as
opposed to
+ // just the query string - we accept both therfore trim out the
excess if we have the entire URL
+ if (url.IndexOf('?') != -1)
+ url = url.Substring(url.IndexOf('?') + 1);
+
+ string[] parts = url.Split('&', '=');
+ NameValueCollection c = new NameValueCollection(1 + parts.Length /
2);
+ for (int i = 0; i < parts.Length; i += 2)
+ if (parts.Length > i + 1)
+ c.Add(parts[i], parts[i + 1]);
+
+ return c;
+ }
+
+ private void RaiseAnnounceReceived(AnnounceParameters e)
+ {
EventHandler<AnnounceParameters> h = AnnounceReceived;
if (h != null)
h(this, e);
}
- protected virtual void RaiseScrapeReceived(ScrapeParameters e)
+ private void RaiseScrapeReceived(ScrapeParameters e)
{
EventHandler<ScrapeParameters> h = ScrapeReceived;
if (h != null)
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches