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