Commit b67ad5d5 authored by Honfika's avatar Honfika

small refactor

parent f4afd161
...@@ -91,7 +91,7 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -91,7 +91,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
//proxyServer.AddEndPoint(transparentEndPoint); //proxyServer.AddEndPoint(transparentEndPoint);
//proxyServer.UpStreamHttpProxy = new ExternalProxy("localhost", 8888); //proxyServer.UpStreamHttpProxy = new ExternalProxy("localhost", 8888);
//proxyServer.UpStreamHttpsProxy = new ExternalProxy("localhost", 8888( }); //proxyServer.UpStreamHttpsProxy = new ExternalProxy("localhost", 8888);
// SOCKS proxy // SOCKS proxy
//proxyServer.UpStreamHttpProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 }; //proxyServer.UpStreamHttpProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 };
......
...@@ -395,55 +395,9 @@ retry: ...@@ -395,55 +395,9 @@ retry:
tcpServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); tcpServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
} }
Task connectTask; var connectTask = socks
if (socks) ? ProxySocketConnectionTaskFactory.CreateTask((ProxySocket.ProxySocket)tcpServerSocket, ipAddress, port)
{ : SocketConnectionTaskFactory.CreateTask(tcpServerSocket, ipAddress, port);
var clientSocket = (ProxySocket.ProxySocket)tcpServerSocket;
IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback,
object state)
{
return clientSocket.BeginConnect(address, port, requestCallback, state);
}
void EndConnect(IAsyncResult asyncResult)
{
var s = clientSocket;
if (s == null)
{
// Dispose nulls out the client socket field.
throw new ObjectDisposedException(GetType().Name);
}
s.EndConnect(asyncResult);
}
connectTask = Task.Factory.FromAsync(BeginConnect, EndConnect, ipAddress, port, state: this);
}
else
{
var clientSocket = tcpServerSocket;
IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback,
object state)
{
return clientSocket.BeginConnect(address, port, requestCallback, state);
}
void EndConnect(IAsyncResult asyncResult)
{
var s = clientSocket;
if (s == null)
{
// Dispose nulls out the client socket field.
throw new ObjectDisposedException(GetType().Name);
}
s.EndConnect(asyncResult);
}
connectTask = Task.Factory.FromAsync(BeginConnect, EndConnect, ipAddress, port, state: this);
}
await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000, cancellationToken)); await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000, cancellationToken));
if (!connectTask.IsCompleted || !tcpServerSocket.Connected) if (!connectTask.IsCompleted || !tcpServerSocket.Connected)
...@@ -767,5 +721,43 @@ retry: ...@@ -767,5 +721,43 @@ retry:
} }
} }
} }
static class SocketConnectionTaskFactory
{
static IAsyncResult beginConnect(IPAddress address, int port, AsyncCallback requestCallback,
object state)
{
return ((Socket)state).BeginConnect(address, port, requestCallback, state);
}
static void endConnect(IAsyncResult asyncResult)
{
((Socket)asyncResult.AsyncState).EndConnect(asyncResult);
}
public static Task CreateTask(Socket socket, IPAddress ipAddress, int port)
{
return Task.Factory.FromAsync(beginConnect, endConnect, ipAddress, port, state: socket);
}
}
static class ProxySocketConnectionTaskFactory
{
static IAsyncResult beginConnect(IPAddress address, int port, AsyncCallback requestCallback,
object state)
{
return ((ProxySocket.ProxySocket)state).BeginConnect(address, port, requestCallback, state);
}
static void endConnect(IAsyncResult asyncResult)
{
((ProxySocket.ProxySocket)asyncResult.AsyncState).EndConnect(asyncResult);
}
public static Task CreateTask(ProxySocket.ProxySocket socket, IPAddress ipAddress, int port)
{
return Task.Factory.FromAsync(beginConnect, endConnect, ipAddress, port, state: socket);
}
}
} }
} }
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