|
|
Visual C++ (MSVC) 环境下,实现 16 字节(128 位)数值
在 Visual C++ (MSVC) 环境下,标准库没有直接提供 128 位整数类型(不像 GCC 有 `__int128`)。
要实现 16 字节(128 位)数值,通常有两种方法:使用 **Boost 库** 或 **自定义结构体**。考虑到您正在开发 MFC 程序且可能不想引入庞大的第三方库,推荐使用 **自定义结构体** 的方式。
以下是一个轻量级的 `UInt128` 实现代码,支持加法运算和格式化输出(非常适合流量统计):
### 方法:自定义 UInt128 类
将此代码添加到您的项目中(例如新建 `UInt128.h` 或直接放在 RDPManagerDlg.h 顶部)。
- #pragma once
- #include <cstring>
- #include <cstdio>
- #include <algorithm>
- struct UInt128
- {
- unsigned __int64 low; // 低64位
- unsigned __int64 high; // 高64位
- // 构造函数
- UInt128() : low(0), high(0) {}
- UInt128(unsigned __int64 l) : low(l), high(0) {}
- UInt128(unsigned __int64 h, unsigned __int64 l) : high(h), low(l) {}
- // 重载加法运算符
- UInt128 operator+(const UInt128& other) const
- {
- UInt128 result;
- result.low = low + other.low;
- // 如果低位溢出(结果小于加数),则高位进1
- result.high = high + other.high + (result.low < low ? 1 : 0);
- return result;
- }
- // 重载减法运算符
- UInt128 operator-(const UInt128& other) const
- {
- UInt128 result;
- result.low = low - other.low;
- // 如果低位不够减(发生借位),高位减1
- result.high = high - other.high - (low < other.low ? 1 : 0);
- return result;
- }
- // 重载赋值加
- UInt128& operator+=(const UInt128& other)
- {
- unsigned __int64 oldLow = low;
- low += other.low;
- high += other.high + (low < oldLow ? 1 : 0);
- return *this;
- }
- // 转换为 double (用于计算 MB/GB 等显示,牺牲精度换取易读性)
- double ToDouble() const
- {
- // 2^64 = 18446744073709551616.0
- const double two_pow_64 = 18446744073709551616.0;
- return (double)high * two_pow_64 + (double)low;
- }
- // 格式化为易读的字符串 (MFC CString)
- CString FormatSize() const
- {
- double size = ToDouble();
- const double KB = 1024.0;
- const double MB = KB * 1024.0;
- const double GB = MB * 1024.0;
- const double TB = GB * 1024.0;
- const double PB = TB * 1024.0;
- const double EB = PB * 1024.0; // 64位存不下的单位
- CString str;
- if (size < KB) str.Format(_T("%.0f Bytes"), size);
- else if (size < MB) str.Format(_T("%.2f KB"), size / KB);
- else if (size < GB) str.Format(_T("%.2f MB"), size / MB);
- else if (size < TB) str.Format(_T("%.2f GB"), size / GB);
- else if (size < PB) str.Format(_T("%.2f TB"), size / TB);
- else if (size < EB) str.Format(_T("%.2f PB"), size / PB);
- else str.Format(_T("%.2f EB"), size / EB);
- return str;
- }
- };
复制代码
### 如何在您的代码中使用
在您的 `CRDPManagerDlg` 类中,将之前的 `ULONG64` 替换为 `UInt128` 即可处理超大流量。
**1. 修改结构体 (RDPManagerDlg.h):**
- struct NetInterfaceState {
- UInt128 u128RxBytes; // 改用 UInt128
- UInt128 u128TxBytes; // 改用 UInt128
- ULONGLONG u64Time;
- };
复制代码
**2. 修改计算逻辑 (RDPManagerDlg.cpp):**
- // 在 UpdateNetworkSpeed 函数中
- // 获取当前流量 (GetIfEntry2 返回的是 ULONG64,直接赋值给 UInt128 即可)
- UInt128 currentRx(ifRow.InOctets);
- UInt128 currentTx(ifRow.OutOctets);
- auto it = m_mapNetState.find(u64Luid);
- if (it != m_mapNetState.end()) {
- // ... 时间计算 ...
- // 处理计数器溢出/重置的情况
- // 如果当前值小于上次值(说明网卡重置或计数器翻转),简单处理为重新计数
- UInt128 diffRx, diffTx;
-
- // 简单的比较逻辑:如果 high 大,或者 high 相等且 low 大
- bool isRxGrown = (currentRx.high > it->second.u128RxBytes.high) ||
- (currentRx.high == it->second.u128RxBytes.high && currentRx.low >= it->second.u128RxBytes.low);
- if (isRxGrown) {
- diffRx = currentRx - it->second.u128RxBytes;
- diffTx = currentTx - it->second.u128TxBytes;
-
- // 计算速度 (转为 double 计算)
- double rxSpeed = diffRx.ToDouble() / timeDiffSec;
- double txSpeed = diffTx.ToDouble() / timeDiffSec;
-
- // 显示...
- }
- }
- // 更新状态
- m_mapNetState[u64Luid].u128RxBytes = currentRx;
- m_mapNetState[u64Luid].u128TxBytes = currentTx;
复制代码
### 为什么需要这个?
标准的 `unsigned __int64` (uint64) 最大值约为 **18 EB (Exabytes)**。
虽然对于目前的单次会话流量很难溢出,但如果是**累加统计**(例如服务器运行数年不重启的累计流量),或者为了代码的健壮性,使用 128 位结构体是更安全的做法。
|
|