|
1 |
| -using System; |
| 1 | +using System; |
| 2 | +using System.Reflection; |
2 | 3 | using System.Text;
|
3 | 4 | using System.Runtime.InteropServices;
|
| 5 | +using System.Runtime.Versioning; |
4 | 6 |
|
5 | 7 | namespace Titanium.Web.Proxy.Helpers
|
6 | 8 | {
|
@@ -41,7 +43,63 @@ public static class RunTime
|
41 | 43 | public static bool IsUwpOnWindows => IsWindows && UwpHelper.IsRunningAsUwp();
|
42 | 44 |
|
43 | 45 | 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; |
44 | 72 |
|
| 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 | + |
45 | 103 | // https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
|
46 | 104 | private class UwpHelper
|
47 | 105 | {
|
|
0 commit comments