Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit c06b3e0

Browse files
authored
Revert "Enable socket reuse based on framework"
1 parent e255369 commit c06b3e0

File tree

3 files changed

+5
-61
lines changed

3 files changed

+5
-61
lines changed

src/Titanium.Web.Proxy/Helpers/RunTime.cs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Reflection;
1+
using System;
32
using System.Text;
43
using System.Runtime.InteropServices;
5-
using System.Runtime.Versioning;
64

75
namespace Titanium.Web.Proxy.Helpers
86
{
@@ -43,63 +41,7 @@ public static class RunTime
4341
public static bool IsUwpOnWindows => IsWindows && UwpHelper.IsRunningAsUwp();
4442

4543
public static bool IsMac => isRunningOnMac;
46-
47-
/// <summary>
48-
/// Is socket reuse available to use?
49-
/// </summary>
50-
public static bool IsSocketReuseAvailable => isSocketReuseAvailable();
51-
52-
private static bool? _isSocketReuseAvailable;
53-
54-
private static bool isSocketReuseAvailable()
55-
{
56-
// use the cached value if we have one
57-
if (_isSocketReuseAvailable != null)
58-
return _isSocketReuseAvailable.Value;
59-
60-
try
61-
{
62-
if (IsWindows)
63-
{
64-
// since we are on windows just return true
65-
// store the result in our static object so we don't have to be bothered going through all this more than once
66-
_isSocketReuseAvailable = true;
67-
return true;
68-
}
69-
70-
// get the currently running framework name and version (EX: .NETFramework,Version=v4.5.1) (Ex: .NETCoreApp,Version=v2.0)
71-
string ver = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
7244

73-
if (ver == null)
74-
return false; // play it safe if we can not figure out what the framework is
75-
76-
// make sure we are on .NETCoreApp
77-
ver = ver.ToLower(); // make everything lowercase to simplify comparison
78-
if (ver.Contains(".netcoreapp"))
79-
{
80-
var versionString = ver.Replace(".netcoreapp,version=v", "");
81-
var versionArr = versionString.Split('.');
82-
var majorVersion = Convert.ToInt32(versionArr[0]);
83-
84-
var result = majorVersion >= 3; // version 3 and up supports socket reuse
85-
86-
// store the result in our static object so we don't have to be bothered going through all this more than once
87-
_isSocketReuseAvailable = result;
88-
return result;
89-
}
90-
91-
// store the result in our static object so we don't have to be bothered going through all this more than once
92-
_isSocketReuseAvailable = false;
93-
return false;
94-
}
95-
catch
96-
{
97-
// store the result in our static object so we don't have to be bothered going through all this more than once
98-
_isSocketReuseAvailable = false;
99-
return false;
100-
}
101-
}
102-
10345
// https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
10446
private class UwpHelper
10547
{

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
315315
tcpClient.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
316316
tcpClient.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds);
317317

318-
if (proxyServer.ReuseSocket && RunTime.IsSocketReuseAvailable)
318+
// linux has a bug with socket reuse in .net core.
319+
if (proxyServer.ReuseSocket && RunTime.IsWindows)
319320
{
320321
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
321322
}

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ private void listen(ProxyEndPoint endPoint)
654654
{
655655
endPoint.Listener = new TcpListener(endPoint.IpAddress, endPoint.Port);
656656

657-
if (ReuseSocket && RunTime.IsSocketReuseAvailable)
657+
// linux/macOS has a bug with socket reuse in .net core.
658+
if (ReuseSocket && RunTime.IsWindows)
658659
{
659660
endPoint.Listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
660661
}

0 commit comments

Comments
 (0)