序列化浮点型数据流程
bitwise_cast<unsigned __int64,double>(double from) 行 74 C++
apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TTransport>::writeDouble(const double dub) 行 171 C++
apache::thrift::protocol::TVirtualProtocol<apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TTransport>,apache::thrift::protocol::TProtocolDefaults>::writeDouble_virt(const double dub) 行 414 C++
apache::thrift::protocol::TProtocol::writeDouble(const double dub) 行 458 C++
ucrtbased.dll!60dfa298() 未知
[下面的框架可能不正确和/或缺失,没有为 ucrtbased.dll 加载符号] 未知
ucrtbased.dll!60df9fd9() 未知
kernel32.dll!74a86359() 未知
ntdll.dll!76ee8944() 未知
ntdll.dll!76ee8914() 未知
template <class Transport_>
uint32_t TBinaryProtocolT<Transport_>::writeDouble(const double dub) {
BOOST_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t));
BOOST_STATIC_ASSERT(std::numeric_limits<double>::is_iec559);
uint64_t bits = bitwise_cast<uint64_t>(dub);
bits = htonll(bits);
this->trans_->write((uint8_t*)&bits, 8);
return 8;
}
xfer += oprot->writeFieldBegin("fFhorizontalValue", ::apache::thrift::protocol::T_DOUBLE, 12);
xfer += oprot->writeDouble(this->fFhorizontalValue);
xfer += oprot->writeFieldEnd();
uint32_t writeDouble(const double dub) {
T_VIRTUAL_CALL();
return writeDouble_virt(dub);
}
virtual uint32_t writeDouble_virt(const double dub) {
return static_cast<Protocol_*>(this)->writeDouble(dub);
}
double类型保存成整型
通过联合体将double类型转换成整型
// Use this to get around strict aliasing rules.
// For example, uint64_t i = bitwise_cast<uint64_t>(returns_double());
// The most obvious implementation is to just cast a pointer,
// but that doesn't work.
// For a pretty in-depth explanation of the problem, see
// http://www.cellperformance.com/mike_acton/2006/06/ (...)
// understanding_strict_aliasing.html
template <typename To, typename From>
static inline To bitwise_cast(From from) {
BOOST_STATIC_ASSERT(sizeof(From) == sizeof(To));
// BAD!!! These are all broken with -O2.
//return *reinterpret_cast<To*>(&from); // BAD!!!
//return *static_cast<To*>(static_cast<void*>(&from)); // BAD!!!
//return *(To*)(void*)&from; // BAD!!!
// Super clean and paritally blessed by section 3.9 of the standard.
//unsigned char c[sizeof(from)];
//memcpy(c, &from, sizeof(from));
//To to;
//memcpy(&to, c, sizeof(c));
//return to;
// Slightly more questionable.
// Same code emitted by GCC.
//To to;
//memcpy(&to, &from, sizeof(from));
//return to;
// Technically undefined, but almost universally supported,
// and the most efficient implementation.
union {
From f;
To t;
} u;
u.f = from;
return u.t;
}
大小端数据传输
htonll函数将会对主机字节序转换成网络字节序,然后发送
当前主机的字节序,主要通过包含
include <boost/detail/endian.hpp>
确定的
小端系统代码
#ifndef __THRIFT_BYTE_ORDER
# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# define __THRIFT_BYTE_ORDER BYTE_ORDER
# define __THRIFT_LITTLE_ENDIAN LITTLE_ENDIAN
# define __THRIFT_BIG_ENDIAN BIG_ENDIAN
# else
# include <boost/config.hpp>
# include <boost/detail/endian.hpp>
大端系统代码
#ifndef __THRIFT_BYTE_ORDER
# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# define __THRIFT_BYTE_ORDER BYTE_ORDER
# define __THRIFT_LITTLE_ENDIAN LITTLE_ENDIAN
# define __THRIFT_BIG_ENDIAN BIG_ENDIAN
# else
# include <boost/config.hpp>
//# include <boost/detail/endian.hpp>
问题总结
目前项目上发现传输给客户端的浮点型数据全部变成类似3.40788366E-312等异常数据,其他版本代码都是正常的,发现# include <boost/detail/endian.hpp>被注释掉,导致htonll被宏定义为
#define htonll(n) (n)
没有转换为大端字节序