当前位置:网站首页>.NET 设置Socket 对象的“TFO”快速打开连接选项(跨平台兼容的)
.NET 设置Socket 对象的“TFO”快速打开连接选项(跨平台兼容的)
2022-07-19 23:12:00 【liulilittle】
调用示例:
Socket socket = new Socket(AF_INET, SOCK_STREAM, 0);
1: socket.SetFastOpen();
2: SetFastOpen(socket);
函数实现
#if NETCOREAPP
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static bool SetFastOpen(this Socket socket, bool fastOpen = true)
{
if (SocketExtension.CleanedUp(socket) || socket.ProtocolType != ProtocolType.Tcp)
{
return false; // https://github.com/dotnet/runtime/issues/1476#issue-547173361
}
PlatformID platform = Environments.Platform;
if (platform == PlatformID.Win32NT)
{
// #define TCP_FASTOPEN 15
try
{
socket.SetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)15, fastOpen);
return true;
}
catch
{
return false;
}
}
else if (platform == PlatformID.Unix)
{
// #define TCP_FASTOPEN 23 /* Enable FastOpen on listeners */
// #define SOL_TCP 6 /* TCP level */
int v = fastOpen ? 1 : 0;
int r = NativeMethods.linux_setsockopt(socket.Handle.ToInt32(), 6, 23,
(IntPtr)(&v), sizeof(int));
return r == 0;
}
else
{
return false;
}
}
外部函数:
[DllImport("libc", EntryPoint = "setsockopt", CallingConvention = CallingConvention.Cdecl)]
public static extern int linux_setsockopt(int socket, int level, int option_name, IntPtr option_value, int option_len);
边栏推荐
- 微信和QQ都能正常上网,但网页不能上网的3种解决办法。
- Skywalking分布式链路跟踪,相关图形,dljd,cat
- print-is网页打印
- Target segmentation for 10000 frames of video, less than 1.4GB of video memory, open source code | ECCV 2022
- [swintransformer source code reading 1] build_ Loader part code
- 【1】 Harbor online installation: experience harbor in 3 minutes
- 【Flink】提交jar包到Flink集群并运行
- What is CPU virtualization? Open or close?
- 自定义View处理不当的内存泄漏
- 改善用户体验的404页面最佳实践
猜你喜欢
随机推荐
mysql之select使用篇1
王者荣耀商城异地多活架构
微信小程序授权登录(含获取基本信息及绑定的手机号)
js截取字符串(3种方法)
matplotlib的savefig必须在show之前
Camtasia 2022新版本发布CS喀秋莎2022功能亮点
【SwinTransformer源码阅读一】build_loader部分代码
【Flink】富函数
301. 删除无效的括号
JMeter single scenario usage example of performance test learning
mysql之select查询篇3
MySQL 19: database and table splitting practice
Success factors of software R & D effectiveness measurement
[ROS] topic based use
JS intercept string (3 methods)
01_教育2
【ROS】roslaunch的使用
MySQL表的增删查改(二)
洛谷P1918 保龄球
Adding, deleting, querying and modifying MySQL tables (II)