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