Commit ee38be01 authored by titanium007's avatar titanium007

Add connection cache

parent a413adc7
...@@ -13,6 +13,10 @@ namespace Titanium.Web.Proxy.Http ...@@ -13,6 +13,10 @@ namespace Titanium.Web.Proxy.Http
{ {
public class TcpConnection public class TcpConnection
{ {
public string HostName { get; set; }
public int port { get; set; }
public bool IsSecure { get; set; }
public TcpClient Client { get; set; } public TcpClient Client { get; set; }
public CustomBinaryReader ServerStreamReader { get; set; } public CustomBinaryReader ServerStreamReader { get; set; }
public Stream Stream { get; set; } public Stream Stream { get; set; }
...@@ -20,9 +24,21 @@ namespace Titanium.Web.Proxy.Http ...@@ -20,9 +24,21 @@ namespace Titanium.Web.Proxy.Http
internal class TcpConnectionManager internal class TcpConnectionManager
{ {
static List<TcpConnection> ConnectionCache = new List<TcpConnection>();
public static TcpConnection GetClient(string Hostname, int port, bool IsSecure) public static TcpConnection GetClient(string Hostname, int port, bool IsSecure)
{ {
lock (ConnectionCache)
{
var cached = ConnectionCache.FirstOrDefault(x => x.HostName == Hostname && x.port == port && x.IsSecure == IsSecure && x.Client.Connected);
if (cached != null)
{
ConnectionCache.Remove(cached);
return cached;
}
}
return CreateClient(Hostname, port, IsSecure); return CreateClient(Hostname, port, IsSecure);
} }
...@@ -48,7 +64,20 @@ namespace Titanium.Web.Proxy.Http ...@@ -48,7 +64,20 @@ namespace Titanium.Web.Proxy.Http
} }
} }
return new TcpConnection() { Client = client, ServerStreamReader = new CustomBinaryReader(stream, Encoding.ASCII), Stream = stream }; return new TcpConnection()
{
HostName = Hostname,
port = port,
IsSecure = IsSecure,
Client = client,
ServerStreamReader = new CustomBinaryReader(stream, Encoding.ASCII),
Stream = stream
};
}
public static void ReleaseClient(TcpConnection Connection)
{
ConnectionCache.Add(Connection);
} }
} }
......
...@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy ...@@ -115,7 +115,7 @@ namespace Titanium.Web.Proxy
private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream, private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream,
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string secureTunnelHostName) CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string secureTunnelHostName)
{ {
TcpConnection connection = null;
while (true) while (true)
{ {
if (string.IsNullOrEmpty(httpCmd)) if (string.IsNullOrEmpty(httpCmd))
...@@ -203,7 +203,7 @@ namespace Titanium.Web.Proxy ...@@ -203,7 +203,7 @@ namespace Titanium.Web.Proxy
//construct the web request that we are going to issue on behalf of the client. //construct the web request that we are going to issue on behalf of the client.
connection = connection == null ? TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps) : connection; var connection = TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps);
args.ProxySession.SetConnection(connection); args.ProxySession.SetConnection(connection);
args.ProxySession.SendRequest(); args.ProxySession.SendRequest();
...@@ -228,9 +228,12 @@ namespace Titanium.Web.Proxy ...@@ -228,9 +228,12 @@ namespace Titanium.Web.Proxy
//if connection is closing exit //if connection is closing exit
if (args.ProxySession.Response.ResponseKeepAlive == false) if (args.ProxySession.Response.ResponseKeepAlive == false)
{ {
connection.Client.Close();
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
return; return;
} }
else
TcpConnectionManager.ReleaseClient(connection);
// read the next request // read the next request
httpCmd = clientStreamReader.ReadLine(); httpCmd = clientStreamReader.ReadLine();
...@@ -243,8 +246,6 @@ namespace Titanium.Web.Proxy ...@@ -243,8 +246,6 @@ namespace Titanium.Web.Proxy
} }
} }
if (connection != null)
connection.Client.Close();
} }
private static void WriteConnectResponse(StreamWriter clientStreamWriter, string httpVersion) private static void WriteConnectResponse(StreamWriter clientStreamWriter, string httpVersion)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment