Friends,
I posted earlier my problem but even after trying out a billion things, its not working for me. I'm developing an asynchronous http server (using HttpListener) which needs to receive/process requests simultaneously.
Here is my "synchronous" model - working just fine - which I need to change to asynchronous mode...All suggestions / tips / brickbats are welcome.
Thanks n Regards,
Kunal
namespace HttpListenerLibrary
{
.
. Extra supporting stuff here....
.
public class HttpListenerController
{
private Thread _pump;
private bool _listening = false;
private string _virtualDir;
private string _physicalDir;
private string[] _prefixes;
private HttpListenerWrapper _listener;
public event LogEventHandler _LogEvent;
public LogCallback _logCallback;
internal PersistentXmlEventBuffer _pxeb;
public HttpListenerController(string[] prefixes, string vdir,
string pdir, LogEventHandler leh)
{
_prefixes = prefixes;
_virtualDir = vdir;
_physicalDir = pdir;
_LogEvent += leh;
_logCallback = new LogCallback(LogThis);
_pxeb = new PersistentXmlEventBuffer();
}
public void Start()
{
_listening = true;
_pump = new Thread(new ThreadStart(Pump));
_pump.Start();
}
public void Stop()
{
// some stuff
}
private void Pump()
{
try
{
_listener = (HttpListenerWrapper)ApplicationHost.CreateApplicationHost(
typeof(HttpListenerWrapper), _virtualDir, _physicalDir);
_listener.Configure(_prefixes, _virtualDir, _physicalDir, _LogEvent, _pxeb);
_listener.Start();
while (_listening)
_listener.ProcessRequest();
}
catch (Exception ex)
{
// some stuff
}
}
}
public class HttpListenerWrapper : MarshalByRefObject
{
private HttpListener _listener;
private string _virtualDir;
private string _physicalDir;
public event LogEventHandler _LogEvent;
public LogCallback _logCallback;
internal PersistentXmlEventBuffer _pxeb;
public void Configure(string[] prefixes, string vdir, string pdir,
LogEventHandler leh, PersistentXmlEventBuffer pxeb)
{
_virtualDir = vdir;
_physicalDir = pdir;
_listener = new HttpListener();
_logCallback = new LogCallback(LogThis);
_pxeb = pxeb;
foreach (string prefix in prefixes)
_listener.Prefixes.Add(prefix);
}
public void Start()
{
_listener.Start();
//some more stuff
}
public void Stop()
{
_listener.Stop();
}
public void ProcessRequest()
{
try
{
HttpListenerContext ctx = _listener.GetContext();
string str = ctx.Request.HttpMethod;
HttpListenerWorkerRequest workerRequest =
new HttpListenerWorkerRequest(
ctx,_virtualDir, _physicalDir, _logCallback, _pxeb);
HttpRuntime.ProcessRequest(workerRequest);
}
catch (XmlException xE)
{
LogThis(xE.Message, ColorCode.RcvColor);
}
}
}
public class HttpListenerWorkerRequest : HttpWorkerRequest public HttpListenerWorkerRequest(HttpListenerContext context,
{
private HttpListenerContext _context;
private string _virtualDir;
private string _physicalDir;
public LogCallback _logCallback;
PersistentXmlEventBuffer _pxeb;
string vdir, string pdir,
LogCallback lcb, PersistentXmlEventBuffer pxeb)
{
_context = context;
_virtualDir = vdir;
_physicalDir = pdir;
_logCallback = lcb;
_pxeb = pxeb;
}
// required overrides (abstract)
public override void EndOfRequest()
{
_context.Response.OutputStream.Close();
_context.Response.Close();
}
public override void SendStatus(int statusCode, string statusDescription)
{
_context.Response.StatusCode = statusCode;
_context.Response.StatusDescription = statusDescription;
}
// other overrided functions follow.....
}
}