eBus.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // eBus.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "eBus.h"
  4. //#include "Logger.h"
  5. #include "eBusService.h"
  6. #include "P2PModule.h"
  7. /*
  8. void InitEbusEnv(bool EnableLog)
  9. {
  10. return IniteBusService(EnableLog);
  11. }
  12. void QuitEbusEnv()
  13. {
  14. return QuiteBusService();
  15. }
  16. eBus::eBus(void)
  17. {
  18. //BUSC::MessageClient::OpenLogger();
  19. m_pBusService = (HANDLE)new eBusService();
  20. ((eBusService*)m_pBusService)->SetParent(this);
  21. }
  22. eBus::~eBus(void)
  23. {
  24. if (m_pP2PModule)
  25. {
  26. delete m_pP2PModule;
  27. m_pP2PModule = NULL;
  28. }
  29. delete (eBusService*)m_pBusService;
  30. }
  31. bool eBus::Connect(ResDataObject &connection)
  32. {
  33. try {
  34. do {
  35. bool Local;
  36. ResDataObject ret;
  37. //Local
  38. if (TryGetConnectOptions(connection, EBUS_CONNECTION_LOCAL, ret) == false)
  39. {
  40. //ExeptionHappen("connection::Local is not Right");
  41. break;
  42. }
  43. Local = ret;
  44. //BusId
  45. DString SourceID;
  46. if (TryGetConnectOptions(connection, EBUS_CONNECTION_BUSID, ret) == false)
  47. {
  48. //ExeptionHappen("connection::BusId is not Right");
  49. break;
  50. }
  51. SourceID = (const char *)ret;
  52. //Port
  53. DString Port = DString::From(BUSC::Client::GetDefStatusPort());
  54. if (TryGetConnectOptions(connection, EBUS_CONNECTION_PORT, ret))
  55. {
  56. Port = (const char *)ret;
  57. }
  58. //RouterIp
  59. DString Ip = "";
  60. if (TryGetConnectOptions(connection, EBUS_CONNECTION_ROUTERIP, ret))
  61. {
  62. Ip = (const char *)ret;
  63. }
  64. m_Connected = true;
  65. if (m_pP2PModule)
  66. {
  67. m_Connected = ((P2P_Module_Base*)m_pP2PModule)->ConnectP2P();
  68. }
  69. m_Connected &= ((eBusService*)m_pBusService)->Connect(Local, SourceID, Ip, Port);
  70. return m_Connected;
  71. } while (0);
  72. }
  73. catch (ResDataObjectExption &exp)
  74. {
  75. //exp.what()
  76. Logger *p = GetGlobalLogger();
  77. //mLog::FDEBUG( exp.what());
  78. }
  79. catch (...)
  80. {
  81. Logger *p = GetGlobalLogger();
  82. //mLog::FDEBUG( "Unknown Exp Happened\n");
  83. }
  84. m_Connected = false;
  85. return false;
  86. }
  87. bool eBus::IsConnected()
  88. {
  89. if (m_Connected)
  90. {
  91. m_Connected = ((eBusService*)m_pBusService)->IsConnected();
  92. }
  93. return m_Connected;
  94. }
  95. void eBus::InitP2P(const char *pszIp, const char *pszLocalBusId, bool AsServer)
  96. {
  97. if (AsServer)
  98. {
  99. m_pP2PModule = new P2P_Module_Server();
  100. }
  101. else
  102. {
  103. m_pP2PModule = new P2P_Module_Client();
  104. }
  105. ((P2P_Module_Server*)m_pP2PModule)->InitP2P((const char*)pszIp, (const char*)pszLocalBusId, AsServer, (PVOID)this);
  106. }
  107. void eBus::DisConnect()
  108. {
  109. ((eBusService*)m_pBusService)->DisConnect();
  110. if (m_pP2PModule)
  111. {
  112. ((P2P_Module_Base*)m_pP2PModule)->Disconnect();
  113. }
  114. m_Connected = false;
  115. }
  116. void eBus::Quit()
  117. {
  118. ((eBusService*)m_pBusService)->Quit();
  119. if (m_pP2PModule)
  120. {
  121. ((P2P_Module_Base*)m_pP2PModule)->Quit();
  122. }
  123. }
  124. void eBus::SetLogPath(const char *pPath)
  125. {
  126. ((eBusService*)m_pBusService)->SetLogPath(pPath);
  127. }
  128. bool eBus::SendSMPacket(const char *pTargetID, const char *pContext, unsigned long long nShareMemID)
  129. {
  130. if (((eBusService*)m_pBusService)->m_ConnectionStatus)
  131. {
  132. return ((eBusService*)m_pBusService)->SendSMPacket(pTargetID, pContext, nShareMemID, CCOS_HW_CHANNEL + 1);
  133. }
  134. //mLog::FERROR("ConnectionStatus is False");
  135. m_Connected = false;
  136. return false;
  137. }
  138. bool eBus::SendRawPacket(const char *pTargetID, const char *pContext, DWORD ChannelId)
  139. {
  140. if (((eBusService*)m_pBusService)->m_ConnectionStatus)
  141. {
  142. return ((eBusService*)m_pBusService)->SendPacket(pTargetID, pContext, ChannelId);
  143. }
  144. m_Connected = false;
  145. return false;
  146. }
  147. bool eBus::SendPacket(const char *pTargetID, const char *pContext, const char *pBlock, DWORD BlockSize)
  148. {
  149. if (((eBusService*)m_pBusService)->m_ConnectionStatus)
  150. {
  151. if (pBlock)
  152. {
  153. //mLog::FDEBUG("Send BlockData to :{$}",pTargetID);
  154. if (m_pP2PModule != NULL)
  155. {
  156. if (((P2P_Module_Base*)m_pP2PModule)->IsTargetBusId(pTargetID))
  157. {
  158. //going to p2p
  159. return ((P2P_Module_Base*)m_pP2PModule)->SendBlob(pContext, pBlock, BlockSize);
  160. }
  161. }
  162. return ((eBusService*)m_pBusService)->SendPacket(pTargetID, pContext, pBlock, BlockSize, CCOS_HW_CHANNEL + 1);
  163. }
  164. //mLog::FDEBUG("Send MsgData to :{$}", pTargetID);
  165. return ((eBusService*)m_pBusService)->SendPacket(pTargetID, pContext, CCOS_HW_CHANNEL);
  166. }
  167. //mLog::FDEBUG("Send Data to :{$} Failed ", pTargetID);
  168. m_Connected = false;
  169. return false;
  170. }
  171. bool eBus::TryGetConnectOptions(ResDataObject &connection, const char *pKey, ResDataObject &result)
  172. {
  173. bool ret = false;
  174. try {
  175. int Idx = connection.GetFirstOf(pKey);
  176. if (Idx >= 0)
  177. {
  178. result = connection[Idx];
  179. ret = true;
  180. }
  181. }
  182. //catch (ResDataObjectExption &exp)
  183. //{
  184. // //exp.what()
  185. // Logger *p = GetGlobalLogger();
  186. // //mLog::FDEBUG( exp.what());
  187. // result = "";
  188. //}
  189. catch (...)
  190. {
  191. Logger *p = GetGlobalLogger();
  192. //mLog::FDEBUG( "Unknown Exp Happened\n");
  193. result = "";
  194. }
  195. return ret;
  196. }
  197. void eBus::BlobDataArrived(const char *pMsg, unsigned char *pBlockData, DWORD BlockDataLen)
  198. {
  199. if (((eBusService*)m_pBusService)->m_ConnectionStatus)
  200. {
  201. ((eBusService*)m_pBusService)->BlobDataArrived(pMsg, pBlockData, BlockDataLen);
  202. return;
  203. }
  204. m_Connected = false;
  205. }
  206. void eBus::UnRegistThread(DWORD Tid)
  207. {
  208. ((eBusService*)m_pBusService)->UnRegistThread(Tid);
  209. }
  210. */