85 lines
1.6 KiB
C#
85 lines
1.6 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using appsrv.connector;
|
|
using appsrv.server;
|
|
using System.Threading;
|
|
|
|
namespace appsrv.protocol
|
|
{
|
|
public class Http : Connector
|
|
{
|
|
public static int backlog = 5;
|
|
public static int defaultPort = 8080;
|
|
public static bool exclusivePortListener = false;
|
|
|
|
IPEndPoint endPoint;
|
|
TcpListener tcpListener;
|
|
Thread tAccept;
|
|
|
|
public Http(ApplicationServer appServer)
|
|
:this(appServer,new IPEndPoint(IPAddress.Any, defaultPort))
|
|
{
|
|
}
|
|
|
|
public Http(ApplicationServer appServer,IPEndPoint endPoint)
|
|
: base(appServer)
|
|
{
|
|
this.endPoint = endPoint;
|
|
this.tcpListener = new TcpListener(this.endPoint);
|
|
this.tcpListener.ExclusiveAddressUse = exclusivePortListener;
|
|
}
|
|
|
|
public override void Start()
|
|
{
|
|
if (
|
|
(tAccept == null) ||
|
|
(!tAccept.IsAlive))
|
|
{
|
|
this.tcpListener.Start(backlog);
|
|
tAccept = new Thread(new ThreadStart(() => acceptRequests()));
|
|
tAccept.Start();
|
|
}
|
|
}
|
|
|
|
public override void Stop()
|
|
{
|
|
this.tcpListener.Stop();
|
|
}
|
|
|
|
private void acceptRequests(){
|
|
try
|
|
{
|
|
|
|
|
|
while (true)
|
|
{
|
|
TcpClient client = this.tcpListener.AcceptTcpClient();
|
|
|
|
try
|
|
{
|
|
HttpRequest request = new HttpRequest(this.ApplicationServer, client);
|
|
|
|
Console.WriteLine("new request: {0}",request);
|
|
|
|
Thread t = new Thread(() => request.Handle());
|
|
t.Start();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("Exception: {0}", e);
|
|
}
|
|
|
|
}
|
|
} catch (SocketException e){
|
|
Console.WriteLine("Http connector interupted");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|