IO.Profile.BUS.Detail.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #pragma once
  2. #include <list>
  3. #include <vector>
  4. #include <functional>
  5. #include <chrono>
  6. #include "String.DString.hpp"
  7. #include "IO.Profile.hpp"
  8. #include "IO.Profile.BUS.Define.hpp"
  9. namespace ECOM::IO
  10. {
  11. class Link;
  12. }
  13. namespace ECOM::ServiceBus::Controller
  14. {
  15. namespace Role
  16. {
  17. class Sender;
  18. class Receiver;
  19. }
  20. }
  21. namespace ECOM::IO::Profile
  22. {
  23. namespace ServiceBus
  24. {
  25. DWORD constexpr WireVersion_510 = 0x0510;
  26. DWORD constexpr WireVersion_801 = 0x0801;
  27. }
  28. }
  29. namespace ECOM::IO::Profile::ServiceBus::Detail
  30. {
  31. using IOLink = ECOM::IO::Link;
  32. //-----------------------------------------------------------------------------
  33. // Manager
  34. //-----------------------------------------------------------------------------
  35. class _IO_Profile_BUS_API Manager
  36. {
  37. Manager () = delete;
  38. public:
  39. static void OpenLogger ();
  40. static void Prepare ();
  41. static void Quit ();
  42. public:
  43. using tfnCallback = std::function <bool (std::unique_ptr <IOLink> link)>;
  44. static void SetKeepWarm (tfnCallback fun);
  45. };
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Send::Profile 和 Receive::Profile 都无需继承自 Profile_IP
  49. // 因为 Create_Profile () 函数体中, 才创建 Profile_IP
  50. //
  51. //-----------------------------------------------------------------------------
  52. namespace ECOM::IO::Profile::ServiceBus::Detail
  53. {
  54. namespace Send
  55. {
  56. //-----------------------------------------------------------------------------
  57. // Send::Profile
  58. //-----------------------------------------------------------------------------
  59. class _IO_Profile_BUS_API Profile : public BaseProfile
  60. {
  61. using base = BaseProfile;
  62. protected:
  63. // 传入的 strProfile 中, 如果指定了端口号, 必须是 Sender 的端口号 (比如 6630)
  64. Profile (int codec, CV_String & strProfile);
  65. public:
  66. virtual ~Profile ();
  67. Profile (Profile && from) = delete;
  68. Profile (const Profile & from) = delete;
  69. Profile & operator = (Profile && from) = delete;
  70. Profile & operator = (const Profile & from) = delete;
  71. protected:
  72. void Parse () override;
  73. private:
  74. void DoParse ();
  75. void Create_Profile ();
  76. private:
  77. eSTR::DString m_IPAddress;
  78. eSTR::DString m_NamedPipe; // 命名管道的名字
  79. int m_CommandPort = 0;
  80. int m_NbOfChannel = 4;
  81. int m_NbOfRetry = 4;
  82. mutable DWORD m_WireVersion = 0;
  83. bool m_bPool = true;
  84. bool m_bTcpNoDelay = false;
  85. using tTimeOut = std::chrono::milliseconds;
  86. tTimeOut m_TimeOut;
  87. std::vector <std::shared_ptr <Profile_IP>> m_arProfile_IP;
  88. std::vector <std::shared_ptr <Profile_NamedPipe>> m_arProfile_Pipe;
  89. private:
  90. auto GetNext_IP () const->std::shared_ptr <Profile_IP>;
  91. auto GetNext_Pipe () const->std::shared_ptr <Profile_NamedPipe>;
  92. public:
  93. bool IsTcpNoDelay () const;
  94. void SetTcpNoDelay (bool bSet);
  95. DWORD GetWireVersion () const;
  96. const Profile & SetWireVersion (DWORD version) const;
  97. public:
  98. auto GetProvider () const -> std::unique_ptr <ECOM::ServiceBus::Controller::Role::Sender>;
  99. public:
  100. eSTR::DString GetIP () const; // 返回 IP 地址
  101. eSTR::DString GetNamedPipe () const; // 返回命名管道名称
  102. // 如果 strProfile 中未指定, 就返回 0. 注意单位 !
  103. auto GetTimeOut () const->std::chrono::milliseconds;
  104. public:
  105. // 返回值一定非空
  106. // 传入的 strProfile 中, 如果指定了端口号, 必须是 Sender 的端口号 (比如 6630)
  107. static auto From (int codec, CV_String strProfile) -> std::unique_ptr <Profile>;
  108. static auto FromIPv4 (CV_String IP, int Port = 0) -> std::unique_ptr <Profile>;
  109. static auto FromIPv6 (CV_String IP, int Port = 0) -> std::unique_ptr <Profile>;
  110. static auto FromPort (int Port) -> std::unique_ptr <Profile>;
  111. static auto Default () -> std::unique_ptr <Profile>;
  112. };
  113. }
  114. namespace Receive
  115. {
  116. //-----------------------------------------------------------------------------
  117. // Receive::Profile
  118. //-----------------------------------------------------------------------------
  119. class _IO_Profile_BUS_API Profile : public BaseProfile
  120. {
  121. using base = BaseProfile;
  122. protected:
  123. // 传入的 strProfile 中, 如果指定了端口号, 必须是 Receiver 的端口号 (比如 6631)
  124. Profile (int codec, CV_String & strProfile);
  125. public:
  126. virtual ~Profile ();
  127. public:
  128. auto GetProvider () const -> std::list < std::unique_ptr <ECOM::ServiceBus::Controller::Role::Receiver> >;
  129. public:
  130. // 返回一个简单的 Link, 用于 SetUniqueSourceID 等功能. 一般是连接到第一个通道.
  131. auto EasyLink () -> std::unique_ptr <IOLink>;
  132. public:
  133. eSTR::DString GetIP () const;
  134. eSTR::DString GetNamedPipe () const; // 返回命名管道名称
  135. // 如果 strProfile 中未指定, 就返回 0. 注意单位 !
  136. auto GetHeartbeat () const -> std::chrono::milliseconds;
  137. public:
  138. auto ToProfileOfSend () const -> std::unique_ptr <Send::Profile>; // 返回 Send::Profile
  139. protected:
  140. void Parse () override;
  141. private:
  142. void DoParse ();
  143. protected:
  144. eSTR::DString m_IPAddress;
  145. int m_NbOfChannel = 4;
  146. int m_StatusPort = 0;
  147. std::chrono::milliseconds m_Heartbeat;
  148. public:
  149. // 返回值一定非空
  150. // 传入的 strProfile 中, 如果指定了端口号, 必须是 Receiver 的端口号 (比如 6631)
  151. static auto From (int codec, CV_String strProfile) -> std::unique_ptr <Profile>;
  152. static auto FromIPv4 (CV_String IP, int Port = 0) -> std::unique_ptr <Profile>;
  153. static auto FromIPv6 (CV_String IP, int Port = 0) -> std::unique_ptr <Profile>;
  154. static auto FromPort (int Port) -> std::unique_ptr <Profile>;
  155. static auto Default () -> std::unique_ptr <Profile>;
  156. };
  157. }
  158. }
  159. //-----------------------------------------------------------------------------
  160. // AccessControl::Profile & AccessControl::AccessToken
  161. //-----------------------------------------------------------------------------
  162. namespace ECOM::IO::Profile::ServiceBus::Detail
  163. {
  164. namespace AccessControl
  165. {
  166. class _IO_Profile_BUS_API Passport
  167. {
  168. class Password
  169. {
  170. union HASH64
  171. {
  172. static constexpr auto NbOf8 = 8;
  173. static constexpr auto NbOf32 = 2;
  174. static constexpr auto NbOf64 = 1;
  175. BYTE Hash8 [NbOf8]; // LOWLOW, LOW, HIGH, HIGHHIGH
  176. DWORD Hash32 [NbOf32]; // LOW, HIGH
  177. UINT64 Hash64;
  178. HASH64 (CV_String str);
  179. };
  180. union SHA256
  181. {
  182. static constexpr auto NbOf8 = 32;
  183. static constexpr auto NbOf32 = 8;
  184. static constexpr auto NbOf64 = 4;
  185. BYTE Hash8 [NbOf8];
  186. DWORD Hash32 [NbOf32];
  187. UINT64 Hash64 [NbOf64];
  188. SHA256 (CV_String str);
  189. };
  190. union SHA512
  191. {
  192. static constexpr auto NbOf8 = 32 * 2;
  193. static constexpr auto NbOf32 = 8 * 2;
  194. static constexpr auto NbOf64 = 4 * 2;
  195. BYTE Hash8 [NbOf8];
  196. DWORD Hash32 [NbOf32];
  197. UINT64 Hash64 [NbOf64];
  198. SHA512 (CV_String str);
  199. };
  200. std::unique_ptr <HASH64> m_Hash64;
  201. std::unique_ptr <SHA256> m_SHA256;
  202. std::unique_ptr <SHA512> m_SHA512;
  203. public:
  204. const HASH64 * GetHash64 () const { return m_Hash64.get (); };
  205. const SHA256 * GetSHA256 () const { return m_SHA256.get (); };
  206. const SHA512 * GetSHA512 () const { return m_SHA512.get (); };
  207. friend class Profile;
  208. }; // password
  209. private:
  210. eSTR::DString m_UserName;
  211. Password m_Password;
  212. friend class Profile;
  213. public:
  214. const eSTR::DString & GetUser () const { return m_UserName; }
  215. const Password & GetPassword () const { return m_Password; }
  216. };
  217. //-----------------------------------------------------------------------------
  218. // AccessControl::Profile
  219. //-----------------------------------------------------------------------------
  220. class _IO_Profile_BUS_API Profile : public BaseProfile
  221. {
  222. using base = BaseProfile;
  223. protected:
  224. Profile (int codec, CV_String & strProfile);
  225. public:
  226. virtual ~Profile ();
  227. Profile (Profile && from) = delete;
  228. Profile (const Profile & from) = delete;
  229. Profile & operator = (Profile && from) = delete;
  230. Profile & operator = (const Profile & from) = delete;
  231. public:
  232. auto GetPassport () const
  233. {
  234. return m_Passport;
  235. }
  236. protected:
  237. void Parse () override;
  238. private:
  239. void DoParse ();
  240. private:
  241. std::shared_ptr <Passport> m_Passport;
  242. public:
  243. // 返回值一定非空
  244. static auto From (int codec, CV_String strProfile) -> std::unique_ptr <Profile>;
  245. public:
  246. // 返回值一定非空
  247. static auto LoadFile (int codec, CV_String FileName, CV_String encKey) -> std::unique_ptr <Profile>;
  248. // 返回: 写入的字节数
  249. static int RenewACL (int codec, CV_String FileName, CV_String encKey);
  250. };
  251. }
  252. }
  253. //namespace IOPBUS = ECOM::IO::Profile::ServiceBus;