libuv5_net

1. 网络

1.1. 简介

  libuv中的网络与直接使用BSD套接字接口没什么不同,有些东西更容易,都是非阻塞的,但概念保持不变。此外,libuv还提供实用程序功能来抽象恼人的,重复的和低级别的任务,例如使用BSD套接字结构设置套接字,DNS查找以及调整各种套接字参数。这里网络相关的主要涉及TCP和UDP,由于TCP是Stream流,而PIPE和TTY也是Stream类,所以他们有共同的结构体,libuv将TCP、PIPE、TTY归为Stream流所以这里一并对PIPE和TTY的相关API也做一个简单介绍。

1.2. UDP

UDP handles 为客户端和服务器封装UDP通信。参考
UDP 监听使用 uv_udp_t handle,发送送 uv_udp_send_t request


typedef struct uv_udp_s uv_udp_t;
/* uv_udp_t is a subclass of uv_handle_t. */
struct uv_udp_s {
  UV_HANDLE_FIELDS
  size_t send_queue_size;//待发送块大小
  size_t send_queue_count;//待发送块个数
  UV_UDP_PRIVATE_FIELDS
};

int uv_udp_init(uv_loop_t*, uv_udp_t* handle);//初始化handle
int uv_udp_init_ex(uv_loop_t*, uv_udp_t* handle, unsigned int flags);
int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);//打开init后的udp句柄
int uv_udp_bind(uv_udp_t* handle,const struct sockaddr* addr,unsigned int flags);//绑定一个ip和端口
int uv_udp_send(uv_udp_send_t* req,uv_udp_t* handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr, uv_udp_send_cb send_cb);//发送数据,发到缓存
int uv_udp_try_send(uv_udp_t* handle,const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr* addr);//直接发送数据
size_t uv_udp_get_send_queue_size(const uv_udp_t* handle);//缓存数据大小
size_t uv_udp_get_send_queue_count(const uv_udp_t* handle);//缓存数据个数

int uv_udp_recv_start(uv_udp_t* handle,uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb);//接受数据
int uv_udp_recv_stop(uv_udp_t* handle);//停止接受数据
int uv_udp_getsockname(const uv_udp_t* handle,struct sockaddr* name,int* namelen);//获取本地ip和端口
int uv_udp_set_membership(uv_udp_t* handle,const char* multicast_addr,const char* interface_addr,uv_membership membership);//设置组播角色,接受还是发送

int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);//是否接受组播
int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);//设置足膜的ttl(time to live)值
int uv_udp_set_multicast_interface(uv_udp_t* handle, const char* interface_addr);//设置组播发送或接受地址

int uv_udp_set_broadcast(uv_udp_t* handle, int on);//设置是否接受广播

int uv_udp_set_ttl(uv_udp_t* handle, int ttl);//设置udp的ttl

1.3. stream

  下边是TCP、PIPE、TTY的结构体。他们有相同的结构 UV_HANDLE_FIELDS,UV_STREAM_FIELDS,所以TCP、PIPE、TTY可以强制转换

PowerShell 7 环境已加载 (版本: 7.5.2) PowerShell 7 环境已加载 (版本: 7.5.2) PS C:\Users\Administrator\Desktop> <# >> 终极版 Libuv 部署脚本 - 自动版本检测 + 多源下载 >> #> PS C:\Users\Administrator\Desktop> param([switch]$SkipDownload) PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # === 初始化设置 === PS C:\Users\Administrator\Desktop> $ErrorActionPreference = 'Stop' PS C:\Users\Administrator\Desktop> Set-StrictMode -Version 3 PS C:\Users\Administrator\Desktop> Start-Transcript -Path "$env:TEMP\libuv_install.log" -Append Transcript started, output file is C:\Users\ADMINI~1\AppData\Local\Temp\libuv_install.log PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> function Assert-Admin { >> $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent() >> $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator >> if (-not ([Security.Principal.WindowsPrincipal] $currentUser).IsInRole($adminRole)) { >> throw "请使用管理员权限运行此脚本" >> } >> } PS C:\Users\Administrator\Desktop> Assert-Admin PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> try { >> $workingDir = "E:\PyTorch_Build\pytorch" >> if (-not (Test-Path $workingDir)) { >> New-Item -ItemType Directory -Path $workingDir -Force | Out-Null >> } >> Set-Location -Path $workingDir >> >> # 1. 自动获取最新版本号 >> Write-Host "获取 Libuv 最新版本信息..." -ForegroundColor Cyan >> try { >> $releaseInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/libuv/libuv/releases/latest" -UseBasicParsing >> $libuvVersion = $releaseInfo.tag_name -replace '^v' >> Write-Host "检测到最新版本: v$libuvVersion" -ForegroundColor Green >> } >> catch { >> Write-Host "⚠️ 无法获取最新版本,使用默认版本 1.49.0" -ForegroundColor Yellow >> $libuvVersion = "1.49.0" >> } >> >> # 2. 多源下载链接 >> $downloadSources = @( >> "https://github.com/libuv/libuv/releases/download/v$libuvVersion/libuv-v$libuvVersion-win-x64.zip", >> "https://dist.libuv.org/dist/v$libuvVersion/libuv-v$libuvVersion-win-x64.zip", >> "https://ghproxy.com/https://github.com/libuv/libuv/releases/download/v$libuvVersion/libuv-v$libuvVersion-win-x64.zip" >> ) >> >> $localZip = "libuv_temp_$($libuvVersion.Replace('.','_')).zip" >> $downloadSuccess = $false >> >> if (-not $SkipDownload) { >> Write-Host "尝试从多个源下载 Libuv v$libuvVersion..." -ForegroundColor Cyan >> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 >> >> foreach ($source in $downloadSources) { >> try { >> Write-Host "尝试下载源: $source" -ForegroundColor Cyan >> >> # 使用更可靠的下载方法 >> if ($PSVersionTable.PSVersion.Major -ge 7) { >> # PowerShell 7+ 使用新的下载方法 >> Invoke-WebRequest -Uri $source -OutFile $localZip -SkipCertificateCheck >> } >> else { >> # 兼容旧版 PowerShell >> $webClient = New-Object System.Net.WebClient >> $webClient.DownloadFile($source, $localZip) >> $webClient.Dispose() >> } >> >> if ((Test-Path $localZip) -and (Get-Item $localZip).Length -gt 1MB) { >> $downloadSuccess = $true >> Write-Host "✅ 下载成功!" -ForegroundColor Green >> break >> } >> } >> catch { >> Write-Host "❌ 下载失败: $($_.Exception.Message)" -ForegroundColor Red >> Remove-Item $localZip -Force -ErrorAction SilentlyContinue >> } >> } >> >> if (-not $downloadSuccess) { >> throw "所有下载源均失败。请手动下载并保存为: $localZip" >> } >> } >> >> # 3. 创建目录结构 >> Write-Host "创建 Libuv 目录..." -ForegroundColor Cyan >> $libuvDest = Join-Path $workingDir "third_party\libuv" >> $dirs = @('bin', 'include', 'lib') >> $dirs | ForEach-Object { >> $path = Join-Path $libuvDest $_ >> New-Item -Path $path -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null >> } >> >> # 4. 解压文件 >> Write-Host "解压文件..." -ForegroundColor Cyan >> $tempDir = Join-Path $env:TEMP "libuv_temp_$(Get-Date -Format 'yyyyMMddHHmmss')" >> New-Item -ItemType Directory -Path $tempDir -Force | Out-Null >> >> Expand-Archive -Path $localZip -DestinationPath $tempDir -Force >> >> # 更灵活的文件查找 >> $extractedDir = Get-ChildItem $tempDir -Directory -Recurse -Filter "libuv-*" | >> Select-Object -First 1 >> >> if (-not $extractedDir) { >> # 尝试其他可能的目录名 >> $extractedDir = Get-ChildItem $tempDir -Directory -Recurse | >> Where-Object { $_.Name -match 'uv|libuv' } | >> Select-Object -First 1 >> >> if (-not $extractedDir) { >> throw "无法找到解压后的Libuv目录。解压路径: $tempDir" >> } >> } >> >> # 5. 复制文件 >> Write-Host "复制文件到目标位置..." -ForegroundColor Cyan >> $sourceBase = $extractedDir.FullName >> >> # 使用更通用的文件查找 >> $copyOperations = @( >> @{ Source = "$sourceBase\bin\*.dll"; Dest = "$libuvDest\bin" } >> @{ Source = "$sourceBase\include\*"; Dest = "$libuvDest\include"; Recurse = $true } >> @{ Source = "$sourceBase\lib\*.lib"; Dest = "$libuvDest\lib" } >> ) >> >> foreach ($op in $copyOperations) { >> if ($op.Recurse) { >> Copy-Item -Path $op.Source -Destination $op.Dest -Recurse -Force >> } >> else { >> Copy-Item -Path $op.Source -Destination $op.Dest -Force >> } >> } >> >> # 6. 创建CMake配置文件 >> Write-Host "配置CMake..." -ForegroundColor Cyan >> $cmakeModulesDir = Join-Path $workingDir "cmake\Modules" >> New-Item -Path $cmakeModulesDir -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null >> >> # 查找所有可能的库文件 >> $libFiles = Get-ChildItem "$libuvDest\lib" -Filter "uv*.lib" -Recurse >> if (-not $libFiles) { >> throw "未找到Libuv库文件" >> } >> >> $relativePath = $libuvDest.Replace($workingDir, "").TrimStart('\').Replace('\', '/') >> >> # 动态生成包含所有库文件的配置 >> $libLibraries = $libFiles | ForEach-Object { >> " `"\${CMAKE_CURRENT_SOURCE_DIR}/$relativePath/lib/$($_.Name)`"" >> } -join "`n" >> >> $libuvConfig = @" >> # Libuv手动配置 >> set(LIBUV_FOUND ON) >> set(LIBUV_INCLUDE_DIRS "\${CMAKE_CURRENT_SOURCE_DIR}/$relativePath/include") >> set(LIBUV_LIBRARIES >> $libLibraries >> ) >> "@ >> >> $configPath = Join-Path $cmakeModulesDir "FindLibuv.cmake" >> Set-Content -Path $configPath -Value $libuvConfig -Encoding UTF8 >> >> # 7. 验证安装 >> $requiredFiles = @( >> "$libuvDest\include\uv.h", >> $libFiles[0].FullName, >> "$configPath" >> ) >> >> $missingFiles = $requiredFiles | Where-Object { -not (Test-Path $_) } >> if ($missingFiles) { >> throw "安装不完整,缺少文件: $($missingFiles -join ', ')" >> } >> >> Write-Host "`n✅ Libuv v$libuvVersion 安装成功!" -ForegroundColor Green >> Write-Host "CMake配置文件位置: $configPath" -ForegroundColor Cyan >> Write-Host "Libuv库文件: $($libFiles.Count) 个" -ForegroundColor Cyan >> } >> catch { >> Write-Host "`n❌ 安装失败: $_" -ForegroundColor Red >> Write-Host "错误详细信息: $($_.Exception.Message)" -ForegroundColor Yellow >> Write-Host "错误位置: $($_.InvocationInfo.ScriptName):$($_.InvocationInfo.ScriptLineNumber)" -ForegroundColor Yellow >> Write-Host "请查看完整日志: $env:TEMP\libuv_install.log" -ForegroundColor Yellow >> } 获取 Libuv 最新版本信息... ⚠️ 无法获取最新版本,使用默认版本 1.49.0 尝试从多个源下载 Libuv v1.49.0... 尝试下载源: https://github.com/libuv/libuv/releases/download/v1.49.0/libuv-v1.49.0-win-x64.zip ❌ 下载失败: Response status code does not indicate success: 404 (Not Found). 尝试下载源: https://dist.libuv.org/dist/v1.49.0/libuv-v1.49.0-win-x64.zip ❌ 下载失败: Response status code does not indicate success: 404 (Not Found). 尝试下载源: https://ghproxy.com/https://github.com/libuv/libuv/releases/download/v1.49.0/libuv-v1.49.0-win-x64.zip ❌ 下载失败: The SSL connection could not be established, see inner exception. ❌ 安装失败: 所有下载源均失败。请手动下载并保存为: libuv_temp_1_49_0.zip 错误详细信息: 所有下载源均失败。请手动下载并保存为: libuv_temp_1_49_0.zip 错误位置: :63 请查看完整日志: C:\Users\ADMINI~1\AppData\Local\Temp\libuv_install.log PS E:\PyTorch_Build\pytorch> finally { >> if (Test-Path $tempDir) { >> Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue >> } >> if (Test-Path $localZip) { >> Remove-Item $localZip -Force -ErrorAction SilentlyContinue >> } >> >> Write-Host "`n按任意键退出..." -NoNewline >> $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") >> Stop-Transcript >> } finally: The term 'finally' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS E:\PyTorch_Build\pytorch> 我看不懂那个界面 我下载了一个“libuv-1.51.0.tar.gz”这是你需要的吗
最新发布
09-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值