SpServerSessionFunction.cpp 6.4 KB

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