SpServerSessionFunction.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpMisc.h"
  4. #include "SpModule.h"
  5. #include "SpEntity.h"
  6. #include "SpServerSessionFunction.h"
  7. #include "sp_env.h"
  8. #include "sp_def.h"
  9. SpServerSessionFunction::SpServerSessionFunction(SpEntity *pEntity, CServerSessionBase *pServerSessionBase, int remote_epid, int remote_svc_id, int conn_id)
  10. : m_uas(NULL), m_remote_ent(NULL), m_pServerSessionBase(pServerSessionBase)
  11. {
  12. //LOG_FUNCTION();
  13. int rc;
  14. int overlap = pServerSessionBase->IsSessionOverlap();
  15. sp_ses_uas_callback cb;
  16. cb.get_method_attr = &__get_method_attr;
  17. cb.on_req = &__on_req;
  18. cb.on_info = &__on_info;
  19. cb.on_close = &__on_close;
  20. cb.on_destroy = &__on_destroy;
  21. cb.user_data = this;
  22. rc = sp_ses_uas_create(pEntity->get_ses_mgr(), remote_epid, remote_svc_id, conn_id, overlap, &cb, &m_uas);
  23. if (rc != 0) {
  24. m_uas = NULL;
  25. } else {
  26. sp_env_t *env = sp_get_env();
  27. m_remote_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, remote_svc_id);
  28. }
  29. }
  30. SpServerSessionFunction::~SpServerSessionFunction()
  31. {
  32. //LOG_FUNCTION();
  33. }
  34. ErrorCodeEnum SpServerSessionFunction::Begin()
  35. {
  36. int rc;
  37. if (m_uas) {
  38. rc = sp_ses_uas_accept(m_uas);
  39. } else {
  40. rc = Error_NotInit;
  41. }
  42. return SpTranslateError(rc);
  43. }
  44. const char *SpServerSessionFunction::GetRemoteEntityName()
  45. {
  46. return m_remote_ent->cfg->name;
  47. }
  48. SessionStateEnum SpServerSessionFunction::GetCurrentState()
  49. {
  50. if (m_uas) {
  51. SessionStateEnum State = { SessionState_NotInit };
  52. int state = sp_ses_uas_get_state(m_uas);
  53. switch (state) {
  54. case SP_SES_STATE_INIT:
  55. case SP_SES_STATE_CONNECTING:
  56. State = SessionState_NotInit;
  57. break;
  58. case SP_SES_STATE_TERM:
  59. State = SessionState_Close;
  60. break;
  61. case SP_SES_STATE_CONNECTED:
  62. State = SessionState_Live;
  63. break;
  64. case SP_SES_STATE_ERROR:
  65. State = SessionState_Close;
  66. break;
  67. default:
  68. break;
  69. }
  70. return State;
  71. } else {
  72. return SessionState_NotInit;
  73. }
  74. }
  75. ErrorCodeEnum SpServerSessionFunction::CloseSession()
  76. {
  77. int rc;
  78. if (m_uas) {
  79. rc = sp_ses_uas_close(m_uas);
  80. } else {
  81. rc = Error_NotInit;
  82. }
  83. return SpTranslateError(rc);
  84. }
  85. int SpServerSessionFunction::__get_method_attr(sp_ses_uas_t *uas, int method_id, int method_sig, int *overlap, void *user_data)
  86. {
  87. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  88. return pThis->get_method_attr(method_id, method_sig, overlap);
  89. }
  90. 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)
  91. {
  92. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  93. pThis->on_req(tsx_id, method_id, method_sig, timeout, req_pkt);
  94. }
  95. void SpServerSessionFunction::__on_info(sp_ses_uas_t *uas, int method_id, int method_sig, iobuffer_t **info_pkt, void *user_data)
  96. {
  97. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  98. pThis->on_info(method_id, method_sig, info_pkt);
  99. }
  100. void SpServerSessionFunction::__on_close(sp_ses_uas_t *uas, int error, void *user_data)
  101. {
  102. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  103. pThis->on_close(error);
  104. }
  105. void SpServerSessionFunction::__on_destroy(sp_ses_uas_t *uas, void *user_data)
  106. {
  107. SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
  108. pThis->on_destroy();
  109. }
  110. int SpServerSessionFunction::get_method_attr(int method_id, int method_sig, int *overlap)
  111. {
  112. bool bOverlap;
  113. ErrorCodeEnum Error = m_pServerSessionBase->GetMessageAttr((DWORD)method_id, (DWORD)method_sig, bOverlap);
  114. if (Error == Error_Succeed) {
  115. *overlap = bOverlap;
  116. }
  117. return Error;
  118. }
  119. void SpServerSessionFunction::on_req(int tsx_id, int method_id, int method_sig, int timeout, iobuffer_t **req_pkt)
  120. {
  121. Dbg("on_req: tsx_id=%d, method_id=%d, method_sig=%d, pkt_size=%d", tsx_id, method_id, method_sig,
  122. *req_pkt == NULL ? 0 : iobuffer_get_length(*req_pkt));
  123. CSmartPointer<ITransactionContext> spTmp;
  124. SpTransactionContext *pTransactionContext = new SpTransactionContext(m_uas, 0, method_id, method_sig, timeout, req_pkt, tsx_id);
  125. spTmp.Attach(pTransactionContext);
  126. m_pServerSessionBase->OnRequest(spTmp);
  127. }
  128. void SpServerSessionFunction::on_info(int method_id, int method_sig, iobuffer_t **info_pkt)
  129. {
  130. Dbg("on_info: method_id=%d, method_sig=%d, pkt_size=%d", method_id, method_sig,
  131. *info_pkt == NULL ? 0 : iobuffer_get_length(*info_pkt));
  132. CSmartPointer<ITransactionContext> spTmp;
  133. SpTransactionContext *pTransactionContext = new SpTransactionContext(m_uas, 1, method_id, method_sig, 0, info_pkt, -1);
  134. spTmp.Attach(pTransactionContext);
  135. m_pServerSessionBase->OnRequest(spTmp);
  136. }
  137. void SpServerSessionFunction::on_close(int error)
  138. {
  139. m_pServerSessionBase->OnClose(SpTranslateError(error));
  140. sp_ses_uas_destroy(m_uas);
  141. }
  142. void SpServerSessionFunction::on_destroy()
  143. {
  144. delete m_pServerSessionBase;
  145. }