123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #include "stdafx.h"
- #include "precompile.h"
- #include "SpBase.h"
- #include "SpMisc.h"
- #include "SpModule.h"
- #include "SpEntity.h"
- #include "SpServerSessionFunction.h"
- #include "sp_env.h"
- #include "sp_def.h"
- #include <winpr/wlog.h>
- #define TAG SPBASE_TAG("SpServerSessionFunction")
- std::chrono::steady_clock::time_point SpServerSessionFunction::m_last_Begintime(std::chrono::steady_clock::now());
- std::chrono::steady_clock::time_point SpServerSessionFunction::m_last_Endtime(std::chrono::steady_clock::now());
- SpServerSessionFunction::SpServerSessionFunction(SpEntity *pEntity, CServerSessionBase *pServerSessionBase, int remote_epid, int remote_svc_id, int conn_id)
- : m_uas(NULL), m_remote_ent(NULL), m_pServerSessionBase(pServerSessionBase)
- {
- //LOG_FUNCTION();
- int rc;
- int overlap = pServerSessionBase->IsSessionOverlap();
- sp_ses_uas_callback cb;
- cb.get_method_attr = &__get_method_attr;
- cb.on_req = &__on_req;
- cb.on_info = &__on_info;
- cb.on_close = &__on_close;
- cb.on_destroy = &__on_destroy;
- cb.user_data = this;
- rc = sp_ses_uas_create(pEntity->get_ses_mgr(), remote_epid, remote_svc_id, conn_id, overlap, &cb, &m_uas);
- if (rc != 0) {
- m_uas = NULL;
- } else {
- sp_env_t *env = sp_get_env();
- m_remote_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, remote_svc_id);
- }
- }
- SpServerSessionFunction::~SpServerSessionFunction()
- {
- //LOG_FUNCTION();
- }
- ErrorCodeEnum SpServerSessionFunction::Begin()
- {
- int rc;
- if (m_uas) {
- rc = sp_ses_uas_accept(m_uas);
- } else {
- rc = Error_NotInit;
- }
- return SpTranslateError(rc);
- }
- const char *SpServerSessionFunction::GetRemoteEntityName()
- {
- return m_remote_ent->cfg->name;
- }
- SessionStateEnum SpServerSessionFunction::GetCurrentState()
- {
- if (m_uas) {
- SessionStateEnum State = { SessionState_NotInit };
- int state = sp_ses_uas_get_state(m_uas);
- switch (state) {
- case SP_SES_STATE_INIT:
- case SP_SES_STATE_CONNECTING:
- State = SessionState_NotInit;
- break;
- case SP_SES_STATE_TERM:
- State = SessionState_Close;
- break;
- case SP_SES_STATE_CONNECTED:
- State = SessionState_Live;
- break;
- case SP_SES_STATE_ERROR:
- State = SessionState_Close;
- break;
- default:
- break;
- }
- return State;
- } else {
- return SessionState_NotInit;
- }
- }
- ErrorCodeEnum SpServerSessionFunction::CloseSession()
- {
- int rc;
- if (m_uas) {
- rc = sp_ses_uas_close(m_uas);
- } else {
- rc = Error_NotInit;
- }
- return SpTranslateError(rc);
- }
- int SpServerSessionFunction::__get_method_attr(sp_ses_uas_t *uas, int method_id, int method_sig, int *overlap, void *user_data)
- {
- SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
- return pThis->get_method_attr(method_id, method_sig, overlap);
- }
- 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)
- {
- SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
- pThis->on_req(tsx_id, method_id, method_sig, timeout, req_pkt);
- }
- void SpServerSessionFunction::__on_info(sp_ses_uas_t *uas, int method_id, int method_sig, iobuffer_t **info_pkt, void *user_data)
- {
- SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
- pThis->on_info(method_id, method_sig, info_pkt);
- }
- void SpServerSessionFunction::__on_close(sp_ses_uas_t *uas, int error, void *user_data)
- {
- SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
- pThis->on_close(error);
- }
- void SpServerSessionFunction::__on_destroy(sp_ses_uas_t *uas, void *user_data)
- {
- SpServerSessionFunction *pThis = static_cast<SpServerSessionFunction*>(user_data);
- pThis->on_destroy();
- }
- int SpServerSessionFunction::get_method_attr(int method_id, int method_sig, int *overlap)
- {
- bool bOverlap;
- ErrorCodeEnum Error = m_pServerSessionBase->GetMessageAttr((DWORD)method_id, (DWORD)method_sig, bOverlap);
- if (Error == Error_Succeed) {
- *overlap = bOverlap;
- }
- return Error;
- }
- void SpServerSessionFunction::on_req(int tsx_id, int method_id, int method_sig, int timeout, iobuffer_t **req_pkt)
- {
- auto current = std::chrono::steady_clock::now();
- auto duration = current - m_last_Endtime;
- auto costDuration = m_last_Endtime - m_last_Begintime;
- CSimpleString lastReqInfo = CSimpleString::Format("last req complete at %lldms ago, cost %lldms",
- std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(),
- std::chrono::duration_cast<std::chrono::milliseconds>(costDuration).count());
- if (NULL != *req_pkt)
- {
- /** 目前这里的数据读取后面没有用途 [Gifur@2022624]*/
- char bussinessId[LINKINFO_BUSSID_LEN + 1];
- char traceId[LINKINFO_TRACEID_LEN + 1];
- char spanId[LINKINFO_SPANID_LEN + 1];
- char parentSpanId[LINKINFO_PARENTSPANID_LEN + 1];
- ZeroMemory(bussinessId, sizeof(bussinessId));
- ZeroMemory(traceId, sizeof(traceId));
- ZeroMemory(spanId, sizeof(spanId));
- ZeroMemory(parentSpanId, sizeof(parentSpanId));
- iobuffer_get_linkInfo(*req_pkt, bussinessId, traceId, spanId, parentSpanId);
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
- ("%s, on_req: tsx_id=%d, method_id=%d, method_sig=%d, pkt_size=%d, linkcontext=%s-%s-%s-%s",
- lastReqInfo.GetData(), tsx_id, method_id, method_sig, iobuffer_get_length(*req_pkt), bussinessId, traceId, spanId, parentSpanId);
- }
- else
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
- ("%s, on_req: tsx_id=%d, method_id=%d, method_sig=%d, pkt_size=%d", lastReqInfo.GetData(), tsx_id, method_id, method_sig, 0);
-
- CSmartPointer<ITransactionContext> spTmp;
- SpTransactionContext *pTransactionContext = new SpTransactionContext(m_uas, 0, method_id, method_sig, timeout, req_pkt, tsx_id);
- spTmp.Attach(pTransactionContext);
- m_last_Begintime = std::chrono::steady_clock::now();
- m_pServerSessionBase->OnRequest(spTmp);
- m_last_Endtime = std::chrono::steady_clock::now();
- }
- void SpServerSessionFunction::on_info(int method_id, int method_sig, iobuffer_t **info_pkt)
- {
- auto current = std::chrono::steady_clock::now();
- auto duration = current - m_last_Endtime;
- auto costDuration = m_last_Endtime - m_last_Begintime;
- CSimpleString lastReqInfo = CSimpleString::Format("last req complete at %lldms ago, cost %lldms",
- std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(),
- std::chrono::duration_cast<std::chrono::milliseconds>(costDuration).count());
- if (NULL != *info_pkt)
- {
- char bussinessId[LINKINFO_BUSSID_LEN + 1];
- char traceId[LINKINFO_TRACEID_LEN + 1];
- char spanId[LINKINFO_SPANID_LEN + 1];
- char parentSpanId[LINKINFO_PARENTSPANID_LEN + 1];
- ZeroMemory(bussinessId, sizeof(bussinessId));
- ZeroMemory(traceId, sizeof(traceId));
- ZeroMemory(spanId, sizeof(spanId));
- ZeroMemory(parentSpanId, sizeof(parentSpanId));
- iobuffer_get_linkInfo(*info_pkt, bussinessId, traceId, spanId, parentSpanId);
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
- ("%s, on_info: method_id=%d, method_sig=%d, pkt_size=%d, linkcontext=%s-%s-%s-%s",
- lastReqInfo.GetData(), method_id, method_sig, iobuffer_get_length(*info_pkt), bussinessId, traceId, spanId, parentSpanId);
- }
- else
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)
- ("%s, on_info: method_id=%d, method_sig=%d, pkt_size=%d",lastReqInfo.GetData(), method_id, method_sig, 0);
- CSmartPointer<ITransactionContext> spTmp;
- SpTransactionContext *pTransactionContext = new SpTransactionContext(m_uas, 1, method_id, method_sig, 0, info_pkt, -1);
- spTmp.Attach(pTransactionContext);
- m_last_Begintime = std::chrono::steady_clock::now();
- m_pServerSessionBase->OnRequest(spTmp);
- m_last_Endtime = std::chrono::steady_clock::now();
- }
- void SpServerSessionFunction::on_close(int error)
- {
- m_pServerSessionBase->OnClose(SpTranslateError(error));
- sp_ses_uas_destroy(m_uas);
- }
- void SpServerSessionFunction::on_destroy()
- {
- delete m_pServerSessionBase;
- }
|