Refactor the TorWebClient

This commit is contained in:
2024-03-12 12:20:53 -04:00
parent 076ab7b887
commit bcc56a1e73
34 changed files with 351 additions and 139 deletions

View File

@@ -6,6 +6,8 @@ using System.Net.Sockets;
using System.IO;
using System.Threading;
using MarketData;
using Tor.Extensions;
using System.Diagnostics;
namespace Tor.Proxy
{
@@ -15,8 +17,8 @@ namespace Tor.Proxy
internal sealed class Connection : IDisposable
{
private readonly Client client;
private volatile bool disposed;
private Profiler profiler = new Profiler();
private ConnectionDisposedCallback disposedCallback;
private Dictionary<string, string> headers;
private string host;
@@ -26,6 +28,10 @@ namespace Tor.Proxy
private byte[] post;
private Socket socket;
private Connection()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Connection"/> class.
/// </summary>
@@ -47,6 +53,65 @@ namespace Tor.Proxy
this.GetHeaderData();
}
#region System.IDisposable
public bool IsSocketClosed()
{
if(null==socket)return true;
return socket.IsConnected()?false:true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
if (socket != null)
{
try
{
socket.Shutdown(SocketShutdown.Both);
}
catch { }
socket.Dispose();
socket = null;
}
disposed = true;
if (disposedCallback != null)
disposedCallback(this);
}
}
public Profiler Profiler
{
get{return profiler;}
}
public bool IsDisposed()
{
return disposed;
}
#endregion
#region Properties
/// <summary>
@@ -107,48 +172,7 @@ namespace Tor.Proxy
#endregion
#region System.IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
private void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
if (socket != null)
{
try
{
socket.Shutdown(SocketShutdown.Both);
}
catch { }
socket.Dispose();
socket = null;
}
disposed = true;
if (disposedCallback != null)
disposedCallback(this);
}
}
#endregion
/// <summary>
/// Gets the header block which was dispatched with the original socket request.
@@ -180,9 +204,6 @@ namespace Tor.Proxy
{
try
{
// MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Reading headers from LocalEndPoint:{0}/RemoteEndPoint:{1}",Socket.LocalEndPoint,Socket.RemoteEndPoint));
StringBuilder builder = new StringBuilder();
using (StreamReader reader = new StreamReader(new NetworkStream(socket, false)))