Hello everyone,
Here is my code for an HttpService. My question is, if there is no coming client request, both (1) and (2) wil be blocked and when there is a coming request,
1. the the callback function provided as the 1st parameter of the BeginGetContext function will be called;
2. and also for the handle wait on (result.AsyncWaitHandle.WaitOne()) will be signalled.
Are there any sequences for (1) and (2)? i.e. for asynchronous call, signal is set before callback is called or vice versa?
[Code]
public class TestHttpServer
{
// listening HTTP port
private int _Port = 0;
// internal wrapped HTTP listener
private HttpListener _Server = new HttpListener();
private Service1 _manager;
public TestHttpServer (Service1 manager)
{
_manager = manager;
}
public int ListenPort
{
get
{
return _Port;
}
set
{
_Port = value;
}
}
public void StartListen()
{
try
{
IAsyncResult result;
_Server.Prefixes.Add(String.Format("http://+:{0}/", 9099));
_Server.Start();
while (true)
{
result = _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);
result.AsyncWaitHandle.WaitOne();
}
}
// any exceptions are not expected
// catch InvalidOperationException during service stop
// [System.InvalidOperationException] = {"Please call the Start() method before calling this method."}
catch (Exception ex)
{
throw ex;
}
}
public void Stop(bool isTerminate)
{
_Server.Stop();
}
// callback function when there is HTTP request received
private void HttpCallback(IAsyncResult result)
{
HttpListenerContext context = _Server.EndGetContext(result);
HandleRequest(context);
}
// find matched URL HTTP request handler and invoke related handler
private void HandleRequest(HttpListenerContext context)
{
string matchUrl = context.Request.Url.AbsolutePath.Trim().ToLower();
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Close();
}
}
[/Code]
thanks in advance,
George