Skip to content

Commit

Permalink
Added KeepConnection option
Browse files Browse the repository at this point in the history
Added KeepConnection option to fastcgi asp.net server command. This
option can be found and set to true/false in the app.config file
  • Loading branch information
gigi81 committed Nov 17, 2014
1 parent 1a397bd commit 728ba69
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
19 changes: 12 additions & 7 deletions FastCgi.AspNet/FastCgiAspNetServer.cs
Expand Up @@ -19,11 +19,12 @@ public class FastCgiAspNetServer : MarshalByRefObject
/// <param name="port">Tcp/ip port where the server will be listening for fastcgi requests</param>
/// <param name="virtualPath">Virtual path of your ASP.NET application</param>
/// <param name="physicalPath">Physical path of your ASP.NET application</param>
/// <param name="keepConnection">When enabling this option, it will be responsability of the web server to close the connection. Default is false.</param>
/// <returns></returns>
public static FastCgiAspNetServer CreateApplicationHost(int port, string virtualPath, string physicalPath)
public static FastCgiAspNetServer CreateApplicationHost(int port, string virtualPath, string physicalPath, bool keepConnection)
{
var ret = (FastCgiAspNetServer)ApplicationHost.CreateApplicationHost(typeof(FastCgiAspNetServer), virtualPath, physicalPath);
ret.Initialize(port, virtualPath, physicalPath);
ret.Initialize(port, virtualPath, physicalPath, keepConnection);
return ret;
}

Expand All @@ -32,9 +33,9 @@ public override object InitializeLifetimeService()
return null; //never expire lease
}

internal void Initialize(int port, string virtualPath, string physicalPath)
internal void Initialize(int port, string virtualPath, string physicalPath, bool keepConnection)
{
_server = new ServerInternal(port, virtualPath, physicalPath);
_server = new ServerInternal(port, virtualPath, physicalPath, keepConnection);
}

/// <summary>
Expand All @@ -56,17 +57,20 @@ public void Stop()

internal class ServerInternal : TcpServer
{
internal ServerInternal(int port, string virtualPath, string physicalPath)
internal ServerInternal(int port, string virtualPath, string physicalPath, bool keepConnection)
: base(port)
{
this.VirtualPath = virtualPath;
this.PhysicalPath = physicalPath;
this.KeepConnection = keepConnection;
}

public string VirtualPath { get; private set; }

public string PhysicalPath { get; private set; }

public bool KeepConnection { get; private set; }

protected override IChannel CreateChannel(TcpLayer tcpLayer)
{
return new CustomAspNetChannelStack(tcpLayer, this);
Expand All @@ -77,7 +81,7 @@ private class CustomAspNetChannelStack : IChannel
private readonly ServerInternal _server;
private readonly TcpLayer _tcpLayer;

public CustomAspNetChannelStack(TcpLayer tcpLayer, ServerInternal server)
public CustomAspNetChannelStack(TcpLayer tcpLayer, ServerInternal server)
{
_server = server;
_tcpLayer = tcpLayer;
Expand All @@ -94,7 +98,8 @@ private CustomAspNetChannel CreateUpperLayer(TcpLayer tcpLayer)

private void RequestEnded(object sender, EventArgs e)
{
_tcpLayer.Close();
if(!_server.KeepConnection)
_tcpLayer.Close();
}

public void Run()
Expand Down
14 changes: 13 additions & 1 deletion FastCgi.Server/Program.cs
Expand Up @@ -40,9 +40,21 @@ private static int Port
}
}

private static bool KeepConnection
{
get
{
bool ret;
if (Boolean.TryParse(ConfigurationManager.AppSettings["KeepConnection"], out ret))
return ret;

return false;
}
}

static void Main(string[] args)
{
var server = FastCgiAspNetServer.CreateApplicationHost(Port, VirtualPath, PhysicalPath);
var server = FastCgiAspNetServer.CreateApplicationHost(Port, VirtualPath, PhysicalPath, KeepConnection);
server.Start();

Console.WriteLine ("Listening on port {0}", Port);
Expand Down
1 change: 1 addition & 0 deletions FastCgi.Server/app.config
Expand Up @@ -4,6 +4,7 @@
<add key="VirtualPath" value="/"/>
<add key="PhysicalPath" value="Root"/>
<add key="Port" value="9000"/>
<add key="KeepConnection" value="false"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
Expand Down

0 comments on commit 728ba69

Please sign in to comment.