private void Listen(CancellationToken cancellationToken)
{
try
{
this.listener = new TcpListener(IPAddress.Loopback, Bootstrapper.GetInstance<IBrowserConfiguration>().Config.PrintSettings.PrintPort);
this.listener.Start();
while (!cancellationToken.IsCancellationRequested)
{
var s = this.listener.AcceptTcpClient();
if (cancellationToken.IsCancellationRequested)
{
return;
}
var httpProcessor = Bootstrapper.TryGetInstance<IHttpProcessor>();
if (httpProcessor == null)
{
continue;
}
httpProcessor.Initialize(s);
var thread = new Thread(httpProcessor.Process); // TODO: Use Threadpool or Task instead?
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
}
}
catch (Exception exception)
{
Logger.Warn(exception);
}
finally
{
if (this.listener != null)
{
try
{
this.listener.Stop();
}
catch (Exception exception)
{
Logger.Warn(exception);
}
}
}
}