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

Commit

Permalink
Merge pull request #648 from justcoding121/revert-647-beta
Browse files Browse the repository at this point in the history
Reverts #647
  • Loading branch information
honfika authored Oct 15, 2019
2 parents e255369 + c06b3e0 commit 43e2988
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 61 deletions.
60 changes: 1 addition & 59 deletions src/Titanium.Web.Proxy/Helpers/RunTime.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Reflection;
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

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

public static bool IsMac => isRunningOnMac;

/// <summary>
/// Is socket reuse available to use?
/// </summary>
public static bool IsSocketReuseAvailable => isSocketReuseAvailable();

private static bool? _isSocketReuseAvailable;

private static bool isSocketReuseAvailable()
{
// use the cached value if we have one
if (_isSocketReuseAvailable != null)
return _isSocketReuseAvailable.Value;

try
{
if (IsWindows)
{
// since we are on windows just return true
// store the result in our static object so we don't have to be bothered going through all this more than once
_isSocketReuseAvailable = true;
return true;
}

// get the currently running framework name and version (EX: .NETFramework,Version=v4.5.1) (Ex: .NETCoreApp,Version=v2.0)
string ver = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;

if (ver == null)
return false; // play it safe if we can not figure out what the framework is

// make sure we are on .NETCoreApp
ver = ver.ToLower(); // make everything lowercase to simplify comparison
if (ver.Contains(".netcoreapp"))
{
var versionString = ver.Replace(".netcoreapp,version=v", "");
var versionArr = versionString.Split('.');
var majorVersion = Convert.ToInt32(versionArr[0]);

var result = majorVersion >= 3; // version 3 and up supports socket reuse

// store the result in our static object so we don't have to be bothered going through all this more than once
_isSocketReuseAvailable = result;
return result;
}

// store the result in our static object so we don't have to be bothered going through all this more than once
_isSocketReuseAvailable = false;
return false;
}
catch
{
// store the result in our static object so we don't have to be bothered going through all this more than once
_isSocketReuseAvailable = false;
return false;
}
}

// https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
private class UwpHelper
{
Expand Down
3 changes: 2 additions & 1 deletion src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
tcpClient.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
tcpClient.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds);

if (proxyServer.ReuseSocket && RunTime.IsSocketReuseAvailable)
// linux has a bug with socket reuse in .net core.
if (proxyServer.ReuseSocket && RunTime.IsWindows)
{
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Titanium.Web.Proxy/ProxyServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,8 @@ private void listen(ProxyEndPoint endPoint)
{
endPoint.Listener = new TcpListener(endPoint.IpAddress, endPoint.Port);

if (ReuseSocket && RunTime.IsSocketReuseAvailable)
// linux/macOS has a bug with socket reuse in .net core.
if (ReuseSocket && RunTime.IsWindows)
{
endPoint.Listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
Expand Down

0 comments on commit 43e2988

Please sign in to comment.