当前位置 : 主页 > 编程语言 > c语言 >

Thrift 消息序列化分析过程

来源:互联网 收集:自由互联 发布时间:2023-09-03
序列化浮点型数据流程 bitwise_castunsigned __int64,double(double from) 行 74C++apache::thrift::protocol::TBinaryProtocolTapache::thrift::transport::TTransport::writeDouble(const double dub) 行 171C++ apache::thrift::protocol::TVi

序列化浮点型数据流程

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;
}



上一篇:C++ STL中 next_permutation 函数的用法
下一篇:没有了
网友评论