Commit 6836d313 authored by justcoding121's avatar justcoding121 Committed by justcoding121

Replace Thread with Task, Accept TCP Async

parent 0a0ce603
......@@ -32,18 +32,18 @@ namespace Titanium.HTTPProxyServer
private static Dictionary<string, X509Certificate2> certificateCache = new Dictionary<string, X509Certificate2>();
private static X509Store _store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
private TcpListener _listener;
private Thread _listenerThread;
private static TcpListener _listener;
private static Thread _listenerThread;
public event EventHandler<SessionEventArgs> BeforeRequest;
public event EventHandler<SessionEventArgs> BeforeResponse;
public static event EventHandler<SessionEventArgs> BeforeRequest;
public static event EventHandler<SessionEventArgs> BeforeResponse;
public IPAddress ListeningIPInterface { get; set; }
public Int32 ListeningPort
public static Int32 ListeningPort
{
get
{
......@@ -73,7 +73,7 @@ namespace Titanium.HTTPProxyServer
}
public bool Start()
public static bool Start()
{
_listener = new TcpListener(IPAddress.Any, 0);
_listener.Start();
......@@ -81,20 +81,20 @@ namespace Titanium.HTTPProxyServer
_listenerThread.Start(_listener);
_listenerThread.IsBackground = true;
return true;
}
public void Stop()
public static void Stop()
{
_listener.Stop();
_listenerThread.Abort();
_listenerThread.Join();
}
private void Listen(Object obj)
// Thread signal.
public static ManualResetEvent tcpClientConnected =
new ManualResetEvent(false);
private static void Listen(Object obj)
{
TcpListener listener = (TcpListener)obj;
......@@ -102,8 +102,13 @@ namespace Titanium.HTTPProxyServer
{
while (true)
{
TcpClient client = listener.AcceptTcpClient();
while (!ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(ProcessClient), client)) ;
// Set the event to nonsignaled state.
tcpClientConnected.Reset();
listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), listener);
// Wait until a connection is made and processed before
// continuing.
tcpClientConnected.WaitOne();
}
}
catch (ThreadAbortException ex) { Debug.WriteLine(ex.Message); }
......@@ -114,16 +119,35 @@ namespace Titanium.HTTPProxyServer
}
public static void DoAcceptTcpClientCallback(IAsyncResult ar)
{
// Get the listener that handles the client request.
TcpListener listener = (TcpListener)ar.AsyncState;
private void ProcessClient(Object obj)
// End the operation and display the received data on
// the console.
TcpClient client = listener.EndAcceptTcpClient(ar);
while (!ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(ProcessClient), client)) ;
// Signal the calling thread to continue.
tcpClientConnected.Set();
}
private static int pending = 0;
private static void ProcessClient(Object param)
{
try
{
TcpClient client = (TcpClient)obj;
TcpClient client = param as TcpClient;
DoHttpProcessing(client);
client.Close();
}
catch { }
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
......
......@@ -10,7 +10,7 @@ namespace Titanium.HTTPProxyServer
{
public partial class ProxyServer
{
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
var Args = (SessionEventArgs)asynchronousResult.AsyncState;
......
......@@ -19,12 +19,10 @@ namespace Titanium.HTTPProxyServer
partial class ProxyServer
{
private int pending = 0;
private void DoHttpProcessing(TcpClient client)
{
pending++;
private static void DoHttpProcessing(TcpClient client)
{
string ConnectionGroup = null;
......
......@@ -14,7 +14,7 @@ namespace Titanium.HTTPProxyServer
{
partial class ProxyServer
{
private void GetResponseCallback(IAsyncResult asynchronousResult)
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
SessionEventArgs Args = (SessionEventArgs)asynchronousResult.AsyncState;
......
......@@ -9,7 +9,7 @@ namespace Titanium.HTTPProxyServer
public partial class ProxyServer
{
private void SendNormal(Stream inStream, Stream outStream)
private static void SendNormal(Stream inStream, Stream outStream)
{
Byte[] buffer = new Byte[BUFFER_SIZE];
......@@ -23,7 +23,7 @@ namespace Titanium.HTTPProxyServer
}
}
private void SendChunked(Stream inStream, Stream outStream)
private static void SendChunked(Stream inStream, Stream outStream)
{
Byte[] buffer = new Byte[BUFFER_SIZE];
......@@ -45,7 +45,7 @@ namespace Titanium.HTTPProxyServer
outStream.Write(ChunkEnd, 0, ChunkEnd.Length);
}
private void SendChunked(byte[] data, Stream outStream)
private static void SendChunked(byte[] data, Stream outStream)
{
Byte[] buffer = new Byte[BUFFER_SIZE];
......@@ -67,14 +67,14 @@ namespace Titanium.HTTPProxyServer
}
private byte[] EncodeData(string ResponseData, Encoding e)
private static byte[] EncodeData(string ResponseData, Encoding e)
{
return e.GetBytes(ResponseData);
}
private byte[] CompressZlib(string ResponseData, Encoding e)
private static byte[] CompressZlib(string ResponseData, Encoding e)
{
Byte[] bytes = e.GetBytes(ResponseData);
......@@ -95,7 +95,7 @@ namespace Titanium.HTTPProxyServer
}
}
private byte[] CompressDeflate(string ResponseData, Encoding e)
private static byte[] CompressDeflate(string ResponseData, Encoding e)
{
Byte[] bytes = e.GetBytes(ResponseData);
......@@ -112,7 +112,7 @@ namespace Titanium.HTTPProxyServer
}
}
private byte[] CompressGzip(string ResponseData, Encoding e)
private static byte[] CompressGzip(string ResponseData, Encoding e)
{
Byte[] bytes = e.GetBytes(ResponseData);
......@@ -131,7 +131,7 @@ namespace Titanium.HTTPProxyServer
}
}
private void sendData(Stream outStream, byte[] data, bool isChunked)
private static void sendData(Stream outStream, byte[] data, bool isChunked)
{
if (!isChunked)
{
......
......@@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Security;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace Titanium.HTTPProxyServer
{
......@@ -19,14 +19,13 @@ namespace Titanium.HTTPProxyServer
var tunnelStream = tunnelClient.GetStream();
var tunnelReadBuffer = new byte[BUFFER_SIZE];
Thread sendRelay = new Thread(() => StreamUtilities.CopyTo(clientStream, tunnelStream, BUFFER_SIZE));
Thread receiveRelay = new Thread(() => StreamUtilities.CopyTo(tunnelStream, clientStream, BUFFER_SIZE));
Task sendRelay = new Task(() => StreamUtilities.CopyTo(clientStream, tunnelStream, BUFFER_SIZE));
Task receiveRelay = new Task(() => StreamUtilities.CopyTo(tunnelStream, clientStream, BUFFER_SIZE));
sendRelay.Start();
receiveRelay.Start();
sendRelay.Join();
receiveRelay.Join();
Task.WaitAll(sendRelay, receiveRelay);
if (tunnelStream != null)
tunnelStream.Close();
......@@ -81,7 +80,7 @@ namespace Titanium.HTTPProxyServer
}
System.Net.Sockets.TcpClient tunnelClient = new System.Net.Sockets.TcpClient(hostname, tunnelPort);
var tunnelStream = (System.IO.Stream)tunnelClient.GetStream();
var tunnelStream = tunnelClient.GetStream() as System.IO.Stream;
if (isSecure)
{
......@@ -91,14 +90,13 @@ namespace Titanium.HTTPProxyServer
}
Thread sendRelay = new Thread(() => StreamUtilities.CopyTo(sb.ToString(), clientStream, tunnelStream, BUFFER_SIZE));
Thread receiveRelay = new Thread(() => StreamUtilities.CopyTo(tunnelStream, clientStream, BUFFER_SIZE));
var sendRelay = new Task(() => StreamUtilities.CopyTo(sb.ToString(), clientStream, tunnelStream, BUFFER_SIZE));
var receiveRelay = new Task(() => StreamUtilities.CopyTo(tunnelStream, clientStream, BUFFER_SIZE));
sendRelay.Start();
receiveRelay.Start();
sendRelay.Join();
receiveRelay.Join();
Task.WaitAll(sendRelay, receiveRelay);
if (tunnelStream != null)
tunnelStream.Close();
......
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