eBus.cpp 4.9 KB

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