#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 #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(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(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(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(user_data); pThis->on_close(error); } void SpServerSessionFunction::__on_destroy(sp_ses_uas_t *uas, void *user_data) { SpServerSessionFunction *pThis = static_cast(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(duration).count(), std::chrono::duration_cast(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 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(duration).count(), std::chrono::duration_cast(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 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; }