SpServerSessionFunction.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "stdafx.h"
  2. #include "precompile.h"
  3. #include "SpBase.h"
  4. #include "SpMisc.h"
  5. #include "SpModule.h"
  6. #include "SpEntity.h"
  7. #include "SpServerSessionFunction.h"
  8. #include "sp_env.h"
  9. #include "sp_def.h"
  10. #include <winpr/wlog.h>
  11. #define TAG SPBASE_TAG("SpServerSessionFunction")
  12. std::chrono::steady_clock::time_point SpServerSessionFunction::m_last_Begintime(std::chrono::steady_clock::now());
  13. std::chrono::steady_clock::time_point SpServerSessionFunction::m_last_Endtime(std::chrono::steady_clock::now());
  14. SpServerSessionFunction::SpServerSessionFunction(SpEntity *pEntity, CServerSessionBase *pServerSessionBase, int remote_epid, int remote_svc_id, int conn_id)
  15. : m_uas(NULL), m_remote_ent(NULL), m_pServerSessionBase(pServerSessionBase)
  16. {
  17. //LOG_FUNCTION();
  18. int rc;
  19. int overlap = pServerSessionBase->IsSessionOverlap();
  20. sp_ses_uas_callback cb;
  21. cb.get_method_attr = &__get_method_attr;
  22. cb.on_req = &__on_req;
  23. cb.on_info = &__on_info;
  24. cb.on_close = &__on_close;
  25. cb.on_destroy = &__on_destroy;
  26. cb.user_data = this;
  27. rc = sp_ses_uas_create(pEntity->get_ses_mgr(), remote_epid, remote_svc_id, conn_id, overlap, &cb, &m_uas);
  28. if (rc != 0) {
  29. m_uas = NULL;
  30. } else {
  31. sp_env_t *env = sp_get_env();
  32. m_remote_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, remote_svc_id);
  33. }
  34. }
  35. SpServerSessionFunction::~SpServerSessionFunction()
  36. {
  37. //LOG_FUNCTION();
  38. }
  39. ErrorCodeEnum SpServerSessionFunction::Begin()
  40. {
  41. int rc;
  42. if (m_uas) {
  43. rc = sp_ses_uas_accept(m_uas);
  44. } else {
  45. rc = Error_NotInit;
  46. }
  47. return SpTranslateError(rc);
  48. }
  49. const char *SpServerSessionFunction::GetRemoteEntityName()
  50. {
  51. return m_remote_ent->cfg->name;
  52. }
  53. SessionStateEnum SpServerSessionFunction::GetCurrentState()
  54. {
  55. if (m_uas) {
  56. SessionStateEnum State = { SessionState_NotInit };
  57. int state = sp_ses_uas_get_state(m_uas);
  58. switch (state) {
  59. case SP_SES_STATE_INIT:
  60. case SP_SES_STATE_CONNECTING:
  61. State = SessionState_NotInit;
  62. break;
  63. case SP_SES_STATE_TERM:
  64. State = SessionState_Close;
  65. break;
  66. case SP_SES_STATE_CONNECTED:
  67. State = SessionState_Live;
  68. break;
  69. case SP_SES_STATE_ERROR:
  70. State = SessionState_Close;
  71. break;
  72. default:
  73. break;
  74. }
  75. return State;
  76. } else {
  77. return SessionState_NotInit;
  78. }
  79. }
  80. ErrorCodeEnum SpServerSessionFunction::CloseSession()
  81. {
  82. int rc;
  83. if (m_uas) {
  84. rc = sp_ses_uas_close(m_uas);
  85. } else {
  86. rc = Error_NotInit;
  87. }
  88. return SpTranslateError(rc);
  89. }
  90. int SpServerSessionFunction::__get_method_attr(sp_ses_uas_t *uas, int method_id, int method_sig, int *overlap, void *user_data)
  91. {
  92. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  93. return pThis->get_method_attr(method_id, method_sig, overlap);
  94. }
  95. void SpServerSessionFunction::__on_req(sp_ses_uas_t *uas, int tsx_id, int method_id, int method_sig, int timeout, iobuffer_t **req_pkt, void *user_data)
  96. {
  97. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  98. pThis->on_req(tsx_id, method_id, method_sig, timeout, req_pkt);
  99. }
  100. void SpServerSessionFunction::__on_info(sp_ses_uas_t *uas, int method_id, int method_sig, iobuffer_t **info_pkt, void *user_data)
  101. {
  102. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  103. pThis->on_info(method_id, method_sig, info_pkt);
  104. }
  105. void SpServerSessionFunction::__on_close(sp_ses_uas_t *uas, int error, void *user_data)
  106. {
  107. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  108. pThis->on_close(error);
  109. }
  110. void SpServerSessionFunction::__on_destroy(sp_ses_uas_t *uas, void *user_data)
  111. {
  112. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  113. pThis->on_destroy();
  114. }
  115. int SpServerSessionFunction::get_method_attr(int method_id, int method_sig, int *overlap)
  116. {
  117. bool bOverlap;
  118. ErrorCodeEnum Error = m_pServerSessionBase->GetMessageAttr((DWORD)method_id, (DWORD)method_sig, bOverlap);
  119. if (Error == Error_Succeed) {
  120. *overlap = bOverlap;
  121. }
  122. return Error;
  123. }
  124. void SpServerSessionFunction::on_req(int tsx_id, int method_id, int method_sig, int timeout, iobuffer_t **req_pkt)
  125. {
  126. auto current = std::chrono::steady_clock::now();
  127. auto duration = current - m_last_Endtime;
  128. auto costDuration = m_last_Endtime - m_last_Begintime;
  129. CSimpleString lastReqInfo = CSimpleString::Format("last req complete at %lldms ago, cost %lldms",
  130. std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(),
  131. std::chrono::duration_cast<std::chrono::milliseconds>(costDuration).count());
  132. if (NULL != *req_pkt)
  133. {
  134. /** 目前这里的数据读取后面没有用途 [Gifur@2022624]*/
  135. char bussinessId[LINKINFO_BUSSID_LEN + 1];
  136. char traceId[LINKINFO_TRACEID_LEN + 1];
  137. char spanId[LINKINFO_SPANID_LEN + 1];
  138. char parentSpanId[LINKINFO_PARENTSPANID_LEN + 1];
  139. ZeroMemory(bussinessId, sizeof(bussinessId));
  140. ZeroMemory(traceId, sizeof(traceId));
  141. ZeroMemory(spanId, sizeof(spanId));
  142. ZeroMemory(parentSpanId, sizeof(parentSpanId));
  143. iobuffer_get_linkInfo(*req_pkt, bussinessId, traceId, spanId, parentSpanId);
  144. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  145. ("%s, on_req: tsx_id=%d, method_id=%d, method_sig=%d, pkt_size=%d, linkcontext=%s-%s-%s-%s",
  146. lastReqInfo.GetData(), tsx_id, method_id, method_sig, iobuffer_get_length(*req_pkt), bussinessId, traceId, spanId, parentSpanId);
  147. }
  148. else
  149. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  150. ("%s, on_req: tsx_id=%d, method_id=%d, method_sig=%d, pkt_size=%d", lastReqInfo.GetData(), tsx_id, method_id, method_sig, 0);
  151. CSmartPointer<ITransactionContext> spTmp;
  152. SpTransactionContext *pTransactionContext = new SpTransactionContext(m_uas, 0, method_id, method_sig, timeout, req_pkt, tsx_id);
  153. spTmp.Attach(pTransactionContext);
  154. m_last_Begintime = std::chrono::steady_clock::now();
  155. m_pServerSessionBase->OnRequest(spTmp);
  156. m_last_Endtime = std::chrono::steady_clock::now();
  157. }
  158. void SpServerSessionFunction::on_info(int method_id, int method_sig, iobuffer_t **info_pkt)
  159. {
  160. auto current = std::chrono::steady_clock::now();
  161. auto duration = current - m_last_Endtime;
  162. auto costDuration = m_last_Endtime - m_last_Begintime;
  163. CSimpleString lastReqInfo = CSimpleString::Format("last req complete at %lldms ago, cost %lldms",
  164. std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(),
  165. std::chrono::duration_cast<std::chrono::milliseconds>(costDuration).count());
  166. if (NULL != *info_pkt)
  167. {
  168. char bussinessId[LINKINFO_BUSSID_LEN + 1];
  169. char traceId[LINKINFO_TRACEID_LEN + 1];
  170. char spanId[LINKINFO_SPANID_LEN + 1];
  171. char parentSpanId[LINKINFO_PARENTSPANID_LEN + 1];
  172. ZeroMemory(bussinessId, sizeof(bussinessId));
  173. ZeroMemory(traceId, sizeof(traceId));
  174. ZeroMemory(spanId, sizeof(spanId));
  175. ZeroMemory(parentSpanId, sizeof(parentSpanId));
  176. iobuffer_get_linkInfo(*info_pkt, bussinessId, traceId, spanId, parentSpanId);
  177. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  178. ("%s, on_info: method_id=%d, method_sig=%d, pkt_size=%d, linkcontext=%s-%s-%s-%s",
  179. lastReqInfo.GetData(), method_id, method_sig, iobuffer_get_length(*info_pkt), bussinessId, traceId, spanId, parentSpanId);
  180. }
  181. else
  182. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
  183. ("%s, on_info: method_id=%d, method_sig=%d, pkt_size=%d",lastReqInfo.GetData(), method_id, method_sig, 0);
  184. CSmartPointer<ITransactionContext> spTmp;
  185. SpTransactionContext *pTransactionContext = new SpTransactionContext(m_uas, 1, method_id, method_sig, 0, info_pkt, -1);
  186. spTmp.Attach(pTransactionContext);
  187. m_last_Begintime = std::chrono::steady_clock::now();
  188. m_pServerSessionBase->OnRequest(spTmp);
  189. m_last_Endtime = std::chrono::steady_clock::now();
  190. }
  191. void SpServerSessionFunction::on_close(int error)
  192. {
  193. m_pServerSessionBase->OnClose(SpTranslateError(error));
  194. sp_ses_uas_destroy(m_uas);
  195. }
  196. void SpServerSessionFunction::on_destroy()
  197. {
  198. delete m_pServerSessionBase;
  199. }