fastdo  0.6.16
eiennet_base.hpp
浏览该文件的文档.
1 #ifndef __EIENNET_BASE_HPP__
2 #define __EIENNET_BASE_HPP__
3 
4 #include "winux.hpp"
5 
6 #ifdef EIENNET_DLL_USE
7  #if defined(_MSC_VER) || defined(WIN32)
8  #pragma warning( disable: 4251 )
9  #ifdef EIENNET_DLL_EXPORTS
10  #define EIENNET_DLL __declspec(dllexport)
11  #else
12  #define EIENNET_DLL __declspec(dllimport)
13  #endif
14 
15  #define EIENNET_API __stdcall
16  #else
17  #define EIENNET_DLL
18  #define EIENNET_API
19  #endif
20 #else
21  #define EIENNET_DLL
22  #define EIENNET_API
23 #endif
24 
25 #define EIENNET_FUNC_DECL(ret) EIENNET_DLL ret EIENNET_API
26 #define EIENNET_FUNC_IMPL(ret) ret EIENNET_API
27 
29 namespace eiennet
30 {
31 
32 // 主机序转为网络序
33 template < typename _Ty >
34 inline _Ty htont( _Ty v )
35 {
36  constexpr winux::uint16 judgeHostOrder = 0x0102;
37  if ( *(winux::byte*)&judgeHostOrder == 0x02 ) // 小端主机
38  {
39  for ( size_t i = 0; i < sizeof(_Ty) / 2; ++i )
40  {
41  winux::byte t = reinterpret_cast<winux::byte*>(&v)[i];
42  reinterpret_cast<winux::byte*>(&v)[i] = reinterpret_cast<winux::byte*>(&v)[sizeof(_Ty) - 1 - i];
43  reinterpret_cast<winux::byte*>(&v)[sizeof(_Ty) - 1 - i] = t;
44  }
45  }
46  return v;
47 }
48 
49 // 网络序转为主机序
50 template < typename _Ty >
51 inline _Ty ntoht( _Ty v )
52 {
53  constexpr winux::uint16 judgeHostOrder = 0x0102;
54  if ( *(winux::uint8*)&judgeHostOrder == 0x02 ) // 小端主机
55  {
56  for ( size_t i = 0; i < sizeof(_Ty) / 2; ++i )
57  {
58  winux::byte t = reinterpret_cast<winux::byte*>(&v)[i];
59  reinterpret_cast<winux::byte*>(&v)[i] = reinterpret_cast<winux::byte*>(&v)[sizeof(_Ty) - 1 - i];
60  reinterpret_cast<winux::byte*>(&v)[sizeof(_Ty) - 1 - i] = t;
61  }
62  }
63  return v;
64 }
65 
66 }
67 
68 #endif // __EIENNET_BASE_HPP__
unsigned char byte
Definition: utilities.hpp:249
网络通信库
_Ty ntoht(_Ty v)
unsigned short uint16
Definition: utilities.hpp:218
_Ty htont(_Ty v)
unsigned char uint8
Definition: utilities.hpp:220