SpTransactionContext.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpMisc.h"
  4. #include "SpModule.h"
  5. #include "SpEntity.h"
  6. #include "SpTransactionContext.h"
  7. #include "sp_ses.h"
  8. #include <winpr/sysinfo.h>
  9. SpTransactionContext::SpTransactionContext(sp_ses_uas_t *uas, int info, int method_id, int method_sig, int timeout, iobuffer_t **p_pkt, int tsx_id)
  10. : m_pkt(NULL), m_tsx(NULL)
  11. {
  12. int rc;
  13. sp_tsx_uas_callback cb;
  14. cb.on_destroy = NULL;
  15. cb.user_data = this;
  16. rc = sp_tsx_uas_create(uas, tsx_id, &cb, &m_tsx);
  17. if (rc == 0) {
  18. m_info = info;
  19. m_method_id = method_id;
  20. m_method_sig = method_sig;
  21. m_timeout = timeout;
  22. m_expire_time = timeout < 0 ? -1 : ((int)GetTickCount() + timeout);
  23. if (p_pkt) {
  24. m_pkt = *p_pkt;
  25. *p_pkt = NULL;
  26. }
  27. }
  28. }
  29. SpTransactionContext::~SpTransactionContext()
  30. {
  31. if (m_pkt)
  32. iobuffer_dec_ref(m_pkt);
  33. if (m_tsx) {
  34. sp_tsx_uas_close(m_tsx);
  35. sp_tsx_uas_destroy(m_tsx);
  36. }
  37. }
  38. bool SpTransactionContext::IsOneWayCall()
  39. {
  40. return !!m_info;
  41. }
  42. DWORD SpTransactionContext::GetRequestID()
  43. {
  44. if (m_tsx) {
  45. return (DWORD)sp_tsx_uas_get_id(m_tsx);
  46. } else {
  47. return -1;
  48. }
  49. }
  50. ErrorCodeEnum SpTransactionContext::GetReceiveBuffer(DWORD &dwMessageID, DWORD &dwMessageSignature, CAutoBuffer &Buffer)
  51. {
  52. if (m_pkt) {
  53. dwMessageID = (DWORD)m_method_id;
  54. dwMessageSignature = (DWORD)m_method_sig;
  55. int size = iobuffer_get_length(m_pkt);
  56. Buffer.Init(size);
  57. if (size > 0) {
  58. memcpy(&Buffer[0], iobuffer_data(m_pkt, 0), size);
  59. }
  60. return Error_Succeed;
  61. } else {
  62. return Error_Unexpect;
  63. }
  64. }
  65. ErrorCodeEnum SpTransactionContext::SendAnswer(CAutoBuffer Buffer, bool bEnd)
  66. {
  67. if (m_info) {
  68. return Error_Bug;
  69. }
  70. else
  71. {
  72. iobuffer_t *ans_pkt = iobuffer_create(-1, -1);
  73. int rc;
  74. int v;
  75. v = (int)Error_Succeed;
  76. iobuffer_write(ans_pkt, IOBUF_T_I4, &v, 0); // sys error
  77. v = 0;
  78. iobuffer_write(ans_pkt, IOBUF_T_I4, &v, 0); // user error
  79. CSimpleString str = "";
  80. v = Buffer.GetCount();
  81. if (v > 0)
  82. iobuffer_write(ans_pkt, IOBUF_T_BUF, &Buffer[0], v);
  83. rc = sp_tsx_uas_answer(m_tsx, !!bEnd, &ans_pkt);
  84. if (ans_pkt)
  85. iobuffer_dec_ref(ans_pkt);
  86. return SpTranslateError(rc);
  87. }
  88. }
  89. ErrorCodeEnum SpTransactionContext::SendAnswer(ErrorCodeEnum eSysError, DWORD dwUserError, CSimpleString str)
  90. {
  91. if (m_info) {
  92. return Error_Bug;
  93. }
  94. else {
  95. iobuffer_t *ans_pkt = iobuffer_create(-1, -1);
  96. int rc;
  97. int v;
  98. v = (int)eSysError;
  99. iobuffer_write(ans_pkt, IOBUF_T_I4, &v, 0); // sys error
  100. iobuffer_write(ans_pkt, IOBUF_T_I4, &dwUserError, 0); // user error
  101. iobuffer_write(ans_pkt, IOBUF_T_STR, str.GetData(), str.GetLength());
  102. rc = sp_tsx_uas_answer(m_tsx, 1, &ans_pkt);
  103. if (ans_pkt)
  104. iobuffer_dec_ref(ans_pkt);
  105. return SpTranslateError(rc);
  106. }
  107. }
  108. DWORD SpTransactionContext::GetExpireLeftTime()
  109. {
  110. if (m_tsx) { // because the machine will restart every day, so use int for time ticks storage is ok!
  111. if (m_expire_time == -1) {
  112. return m_expire_time;
  113. } else {
  114. int now = (int)GetTickCount();
  115. return m_expire_time > now ? (m_expire_time - now) : 0;
  116. }
  117. } else {
  118. return -1;
  119. }
  120. }
  121. ErrorCodeEnum SpTransactionContext::GetLinkContext(linkContext& curLink)
  122. {
  123. if (m_pkt) {
  124. char bussinessId[LINKINFO_BUSSID_LEN + 1];
  125. char traceId[LINKINFO_TRACEID_LEN + 1];
  126. char spanId[LINKINFO_SPANID_LEN + 1];
  127. char parentSpanId[LINKINFO_PARENTSPANID_LEN + 1];
  128. ZeroMemory(bussinessId, sizeof(bussinessId));
  129. ZeroMemory(traceId, sizeof(traceId));
  130. ZeroMemory(spanId, sizeof(spanId));
  131. ZeroMemory(parentSpanId, sizeof(parentSpanId));
  132. iobuffer_get_linkInfo(m_pkt, bussinessId, traceId, spanId, parentSpanId);
  133. curLink.bussinessId = bussinessId;
  134. curLink.traceId = traceId;
  135. curLink.spanId = spanId;
  136. curLink.parentSpanId = parentSpanId;
  137. }
  138. return Error_Succeed;
  139. }
  140. ErrorCodeEnum SpTransactionContext::GetExpireTime(DWORD &dwWholeTime,DWORD &dwLeftTime)
  141. {
  142. if (m_tsx) {
  143. if (m_expire_time == -1) {
  144. dwWholeTime = dwLeftTime = 0xffffffff;
  145. } else {
  146. int now = (int)GetTickCount();
  147. dwWholeTime = m_timeout;
  148. dwLeftTime = m_expire_time > now ? (m_expire_time - now) : 0;
  149. }
  150. return Error_Succeed;
  151. } else {
  152. return Error_Unexpect;
  153. }
  154. }
  155. ErrorCodeEnum SpTransactionContext::SetExpireTime( DWORD dwMS )
  156. {
  157. return Error_NotImpl;
  158. }
  159. //
  160. // SpMUITransactionContext
  161. //
  162. ErrorCodeEnum SpMUITransactionContext::SendAnswer( ErrorCodeEnum eErrorCode, DWORD dwUserError, CSimpleString str)
  163. {
  164. if (m_op == OP_START) {
  165. m_pEntity->FinishStart(eErrorCode);
  166. } else if (m_op == OP_PAUSE) {
  167. m_pEntity->FinishPause(eErrorCode);
  168. } else if (m_op == OP_SELFTEST) {
  169. m_pEntity->FinishSelfTest(eErrorCode, dwUserError);
  170. } else if (m_op == OP_CONTINUE) {
  171. m_pEntity->FinishContinue(eErrorCode);
  172. } else if (m_op == OP_CLOSE) {
  173. m_pEntity->FinishClose(eErrorCode);
  174. }
  175. //TODO: always return Error_Unexpect ??
  176. return Error_Unexpect;
  177. }