mod_assistantchannel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #include "stdafx.h"
  2. #include "mod_assistantchannel.h"
  3. #include "EventCode.h"
  4. static const char *__states[] = {
  5. "Idle", "Connecting", "Connected", "Closing"
  6. };
  7. inline const char *state2str(int state)
  8. {
  9. return __states[state];
  10. }
  11. static __inline unsigned int hash32_buf(const void *bf, size_t len, unsigned int hash)
  12. {
  13. const unsigned char *s = (const unsigned char*)bf;
  14. while (len-- != 0) /* "nemesi": k=257, r=r*257 */
  15. hash = hash * 257 + *s++;
  16. return (hash * 257);
  17. }
  18. static int MakeDesc(DWORD eScreen,DeviceTypeEnum eDevicetype)
  19. {
  20. if (eScreen == 1)
  21. {
  22. return media_desc_encode(
  23. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE,
  24. ACM_VIDEO_MODE_QVGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE);
  25. }
  26. else if (eScreen == 2)
  27. {
  28. return media_desc_encode(
  29. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE,
  30. ACM_VIDEO_MODE_VGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE);
  31. }
  32. else
  33. {
  34. assert(0);
  35. }
  36. return 0;
  37. }
  38. static void __on_recv_pkt(bizchan_t *chan, int type, int sub_type, int id, const char *pkt, int pkt_size, void *user_data)
  39. {
  40. CBizChannelEntity *pThis = static_cast<CBizChannelEntity *>(user_data);
  41. pThis->_on_recv_pkt(type, sub_type, id, pkt, pkt_size);
  42. }
  43. static void __on_connect(bizchan_t *chan, int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc, const char *remote_client_id, void *user_data)
  44. {
  45. CBizChannelEntity *pThis = static_cast<CBizChannelEntity *>(user_data);
  46. pThis->_on_connect(error, remote_ip, remote_video_rtp, remote_video_desc);
  47. }
  48. static void __on_close(bizchan_t *chan, void *user_data)
  49. {
  50. CBizChannelEntity *pThis = static_cast<CBizChannelEntity *>(user_data);
  51. pThis->_on_close();
  52. }
  53. static void __on_destroy(bizchan_t *chan, void *user_data)
  54. {
  55. }
  56. static void __dbg(void *user_data, const char *fmt, va_list arg)
  57. {
  58. //vDbg(fmt, arg);
  59. #if defined(RVC_OS_WIN)
  60. int n = _vscprintf(fmt, arg);
  61. #else
  62. int n = _scprintf(fmt, arg);
  63. #endif //RVC_OS_WIN
  64. if (n >= MAX_PATH) {
  65. char* buf = (char*)malloc((size_t)(n + 1));
  66. vsnprintf(buf, n + 1, fmt, arg);
  67. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  68. free(buf);
  69. }
  70. else {
  71. char strlog[MAX_PATH] = { 0 };
  72. #if defined(RVC_OS_WIN)
  73. _vsnprintf(strlog, MAX_PATH, fmt, arg);
  74. #else
  75. vsnprintf(strlog, MAX_PATH, fmt, arg);
  76. #endif //RVC_OS_WIN
  77. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  78. }
  79. }
  80. void CBizChannelEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  81. {
  82. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  83. pTransactionContext->SendAnswer(Error);
  84. }
  85. void CBizChannelEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  86. {
  87. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  88. pTransactionContext->SendAnswer(Error);
  89. }
  90. ErrorCodeEnum CBizChannelEntity::__OnStart(ErrorCodeEnum preOperationError)
  91. {
  92. //MessageBoxA(0,0,0,0);
  93. bizchan_lib_init();
  94. m_eState = eChannelState_Idle;
  95. m_pChan = NULL;
  96. m_eDeviceType = RvcGetDeviceType();
  97. ListEntry_InitHead(&m_stateList);
  98. ListEntry_InitHead(&m_rxpktList);
  99. return Error_Succeed;
  100. }
  101. void CBizChannelEntity::OnStarted()
  102. {
  103. LogEvent(Severity_Middle, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS, "assistant channel started successfully.");
  104. }
  105. DeviceTypeEnum CBizChannelEntity::RvcGetDeviceType()
  106. {
  107. DeviceTypeEnum eType = eStand2sType;
  108. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  109. CSystemStaticInfo stStaticinfo;
  110. spFunction->GetSystemStaticInfo(stStaticinfo);
  111. if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) {
  112. eType = eStand1SPlusType;
  113. }
  114. else if (stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) {
  115. eType = eCardStore;
  116. }
  117. else{
  118. eType = eStand2sType;
  119. }
  120. if (eType >= 0 && eType < sizeof(Device_Type_Table)/sizeof(char*)){
  121. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  122. }
  123. return eType;
  124. }
  125. ErrorCodeEnum CBizChannelEntity::__OnClose(ErrorCodeEnum preOperationError)
  126. {
  127. //.....
  128. bizchan_lib_term();
  129. return Error_Succeed;
  130. }
  131. CServerSessionBase* CBizChannelEntity::OnNewSession(const char* pszRemoteEntityName, const char * pszClass)
  132. {
  133. return new ChannelServiceSession(this, m_id_seq++);
  134. }
  135. ErrorCodeEnum CBizChannelEntity::Connect(const char *ip, int port, const char *callno, CallingTypeEnum eType)
  136. {
  137. ErrorCodeEnum Error = Error_Succeed;
  138. if (m_eState == eChannelState_Idle)
  139. {
  140. CSystemStaticInfo Info;
  141. bizchan_config_t config = {0};
  142. bizchan_callback_t cb = {0};
  143. GetFunction()->GetSystemStaticInfo(Info);
  144. config.proxy_server = const_cast<char*>(ip);
  145. config.proxy_server_port = port;
  146. config.session_id = const_cast<char*>(((const char*)Info.strTerminalID));
  147. config.call_no = const_cast<char*>(callno);
  148. config.client_id = "";
  149. config.video.desc = MakeDesc(Info.eScreen,m_eDeviceType);
  150. config.video.rtp_port = REC_COMMON_VIDEO_PORT;
  151. cb.user_data = this;
  152. cb.on_close = &__on_close;
  153. cb.on_connect = &__on_connect;
  154. cb.on_destroy = &__on_destroy;
  155. cb.on_recv_pkt = &__on_recv_pkt;
  156. cb.dbg = &__dbg;
  157. int rc = bizchan_create(&config, &cb, &m_pChan);
  158. if (rc == 0) {
  159. rc = bizchan_start_connect(m_pChan);
  160. }
  161. if (rc != 0) {
  162. Error = Error_Unexpect;
  163. }
  164. else {
  165. ChangeState(eChannelState_Connecting);
  166. }
  167. }
  168. else
  169. {
  170. Error = Error_InvalidState;
  171. }
  172. return Error;
  173. }
  174. ErrorCodeEnum CBizChannelEntity::Close()
  175. {
  176. ErrorCodeEnum Error = Error_Succeed;
  177. if (m_eState == eChannelState_Connecting || m_eState == eChannelState_Connected) {
  178. int rc = bizchan_start_close(m_pChan);
  179. if (rc == 0) {
  180. ChangeState(eChannelState_Closing);
  181. } else {
  182. Error = Error_Unexpect;
  183. }
  184. } else {
  185. Error = Error_InvalidState;
  186. }
  187. return Error_Succeed;
  188. }
  189. ErrorCodeEnum CBizChannelEntity::RegisterState(int id, SpSubscribeContext<ChannelService_BeginState_Sub, ChannelService_State_Info>::Pointer ctx)
  190. {
  191. state_entry *pos;
  192. ListEntry_ForEach(pos, &m_stateList, state_entry, entry)
  193. {
  194. if (pos->id == id)
  195. {
  196. return Error_AlreadyExist;
  197. }
  198. }
  199. pos = new state_entry();
  200. pos->ctx = ctx;
  201. pos->id = id;
  202. ListEntry_AddTail(&m_stateList, &pos->entry);
  203. return Error_Succeed;
  204. }
  205. ErrorCodeEnum CBizChannelEntity::RegisterRxPkt(int id, SpSubscribeContext<ChannelService_BeginRecv_Sub, ChannelService_Packet_Info>::Pointer ctx)
  206. {
  207. rxpkt_entry *pos;
  208. ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  209. {
  210. if ((pos->id == id)&&(pos->ctx->Req.type == ctx->Req.type))
  211. {
  212. return Error_AlreadyExist;
  213. }
  214. }
  215. pos = new rxpkt_entry();
  216. pos->id = id;
  217. pos->ctx = ctx;
  218. ListEntry_AddTail(&m_rxpktList, &pos->entry);
  219. return Error_Succeed;
  220. }
  221. void CBizChannelEntity::UnregisterState(int id)
  222. {
  223. state_entry *pos,*n;
  224. ListEntry_ForEachSafe(pos,n, &m_stateList, state_entry, entry) {
  225. if (pos->id == id) {
  226. ListEntry_DeleteNode(&pos->entry);
  227. delete pos;
  228. }
  229. }
  230. }
  231. void CBizChannelEntity::UnregisterRxpkt(int id)
  232. {
  233. rxpkt_entry *pos,*n;
  234. //ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  235. ListEntry_ForEachSafe(pos, n, &m_rxpktList, rxpkt_entry, entry)
  236. {
  237. if((pos->id == id))
  238. {
  239. ListEntry_DeleteNode(&pos->entry);
  240. delete pos;
  241. }
  242. }
  243. }
  244. ErrorCodeEnum CBizChannelEntity::Send(int type, bool compress, bool encrypt, int sub_type, int id, CBlob &data)
  245. {
  246. if (m_eState == eChannelState_Connected) {
  247. //LOG_TRACE("tx pkt, %d bytes, type = %d, compress = %d, encrypt = %d, sub_type = %d, id = %d, hash=%d", data.m_iLength, type, !!compress, !!encrypt, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  248. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("tx pkt, %d bytes, type = %d, compress = %d, encrypt = %d, sub_type = %d, id = %d, hash=%d", data.m_iLength, type, !!compress, !!encrypt, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  249. int rc = bizchan_post_pkt(m_pChan, type, compress, encrypt, sub_type, id, (const char*)data.m_pData, data.m_iLength);
  250. return rc == 0 ? Error_Succeed : Error_NetBroken;
  251. }
  252. else
  253. {
  254. return Error_NetBroken;
  255. }
  256. }
  257. void CBizChannelEntity::GetIpByDomain(char* poutput, size_t ulen, const char *pinput)
  258. {
  259. #if defined(RVC_OS_WIN)
  260. bizchan_getip_bydomain(m_pChan, poutput, ulen, pinput);
  261. #endif //RVC_OS_WIN
  262. }
  263. void CBizChannelEntity::ChangeState(int new_state, const char *param)
  264. {
  265. if (m_eState != new_state) {
  266. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("change state from %s to %s", state2str(m_eState), state2str(new_state));
  267. m_eState = new_state;
  268. state_entry *pos;
  269. ListEntry_ForEach(pos, &m_stateList, state_entry, entry) {
  270. ChannelService_State_Info State;
  271. State.state = new_state;
  272. State.status = state2str(new_state);
  273. if (param) {
  274. State.param = param;
  275. }
  276. pos->ctx->SendMessage(State);
  277. }
  278. }
  279. }
  280. //void on_recv_pkt(int type, int sub_type, const char *pkt, int pkt_size)
  281. void CBizChannelEntity::on_recv_pkt(int type, int sub_type, int id, CBlob &data)
  282. {
  283. //LOG_TRACE("rx pkt, %d bytes, type = %d, sub_type = %d, id = %d, hash = %d", data.m_iLength, type, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  284. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("rx pkt, %d bytes, type = %d, sub_type = %d, id = %d, hash = %d", data.m_iLength, type, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  285. rxpkt_entry *pos;
  286. ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  287. {
  288. if (pos->ctx->Req.type == type)
  289. {
  290. ChannelService_Packet_Info pkt;
  291. pkt.type = type;
  292. pkt.sub_type = sub_type;
  293. pkt.id = id;
  294. //pkt.data.m_bManaged = true;
  295. pkt.data = data;
  296. //data.m_bManaged = false;
  297. pos->ctx->SendMessage(pkt);
  298. //break;
  299. }
  300. }
  301. }
  302. void CBizChannelEntity::on_connect(int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc)
  303. {
  304. if (!error) {
  305. if (m_eState == eChannelState_Connecting) {
  306. CSimpleStringA strValue;
  307. ErrorCodeEnum Error = GetFunction()->GetSysVar("VideoWindowInitializeParam", strValue);
  308. if (Error == Error_Succeed) {
  309. int local_view_x;
  310. int local_view_y;
  311. int local_view_cx;
  312. int local_view_cy;
  313. int remote_view_x;
  314. int remote_view_y;
  315. int remote_view_cx;
  316. int remote_view_cy;
  317. int remote_video_width = 0;
  318. int remote_video_height = 0;
  319. {
  320. CSystemStaticInfo Info;
  321. GetFunction()->GetSystemStaticInfo(Info);
  322. if (Info.eScreen == 1)
  323. {
  324. remote_video_width = REC_COMMON_VIDEO_SSM_AGENT_WIDTH;
  325. remote_video_height = REC_COMMON_VIDEO_SSM_AGENT_HEIGHT;
  326. }
  327. else
  328. {
  329. remote_video_width = REC_COMMON_VIDEO_DSM_AGENT_WIDTH;
  330. remote_video_height = REC_COMMON_VIDEO_DSM_AGENT_HEIGHT;
  331. }
  332. }
  333. ParseVideoViewParam((LPCSTR)strValue, local_view_x, local_view_y, local_view_cx, local_view_cy,
  334. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  335. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get Video Window Initialize Param:%d,%d,%d,%d %d,%d,%d,%d",local_view_x, local_view_y, local_view_cx, local_view_cy,
  336. //remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  337. CSimpleStringA param;
  338. param = BuildVideoDesc(remote_ip, remote_video_rtp, remote_video_width, remote_video_height,
  339. REC_COMMON_VIDEO_FPS, local_view_x, local_view_y, local_view_cx, local_view_cy,
  340. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  341. ChangeState(eChannelState_Connected, (LPCSTR)param);
  342. }
  343. else {
  344. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get VideoWindowInitializeParam failed!");
  345. bizchan_start_close(m_pChan);
  346. }
  347. }
  348. }
  349. else {
  350. if (m_pChan) {
  351. bizchan_close(m_pChan);
  352. bizchan_destroy(m_pChan);
  353. m_pChan = NULL;
  354. }
  355. ChangeState(eChannelState_Idle);
  356. }
  357. }
  358. void CBizChannelEntity::on_close()
  359. {
  360. if (m_pChan) {
  361. bizchan_close(m_pChan);
  362. bizchan_destroy(m_pChan);
  363. m_pChan = NULL;
  364. }
  365. ChangeState(eChannelState_Idle);
  366. }
  367. void CBizChannelEntity::_on_recv_pkt(int type, int sub_type, int id, const char *pkt, int pkt_size)
  368. {
  369. //LOG_TRACE("_on rx pkt, %d bytes, type = %d, sub_type = %d, id = %d, hash = %d", pkt_size, type, sub_type, id, hash32_buf(pkt, pkt_size, 0));
  370. NotifyOnRecvPkt *task = new NotifyOnRecvPkt();
  371. task->m_pEntity = this;
  372. task->type = type;
  373. task->sub_type = sub_type;
  374. task->id = id;
  375. task->data.m_bManaged = true;
  376. task->data.m_iLength = pkt_size;
  377. if (pkt_size)
  378. {
  379. task->data.m_pData = new char[pkt_size];
  380. memcpy(task->data.m_pData, pkt, pkt_size);
  381. } else {
  382. task->data.m_pData = NULL;
  383. }
  384. GetFunction()->PostEntityTaskFIFO(task);
  385. }
  386. void CBizChannelEntity::_on_connect(int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc)
  387. {
  388. NotifyOnConnect *task = new NotifyOnConnect();
  389. task->m_pEntity = this;
  390. task->error = error;
  391. task->remote_rtp_ip = remote_ip;
  392. task->remote_video_port = remote_video_rtp;
  393. task->remote_video_desc = remote_video_desc;
  394. GetFunction()->PostEntityTaskFIFO(task);
  395. }
  396. void CBizChannelEntity::_on_close()
  397. {
  398. NotifyOnClose *task = new NotifyOnClose();
  399. task->m_pEntity = this;
  400. GetFunction()->PostEntityTaskFIFO(task);
  401. }
  402. void ChannelServiceSession::Handle_Connect( SpReqAnsContext<ChannelService_Connect_Req, ChannelService_Connect_Ans>::Pointer ctx )
  403. {
  404. DbgToBeidou(ctx->link, __FUNCTION__)();
  405. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("start connect, %s:%d [%s]", (LPCSTR)ctx->Req.ip, ctx->Req.port, (LPCSTR)ctx->Req.callno);
  406. ErrorCodeEnum Error = m_pEntity->Connect(ctx->Req.ip, ctx->Req.port, ctx->Req.callno, (CallingTypeEnum)ctx->Req.etype);
  407. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("connect Error = %d", Error);
  408. ctx->Answer(Error);
  409. }
  410. void ChannelServiceSession::Handle_Close( SpReqAnsContext<ChannelService_Close_Req, ChannelService_Close_Ans>::Pointer ctx )
  411. {
  412. DbgToBeidou(ctx->link, __FUNCTION__)();
  413. ErrorCodeEnum Error = m_pEntity->Close();
  414. ctx->Answer(Error);
  415. }
  416. void ChannelServiceSession::Handle_GetState( SpReqAnsContext<ChannelService_GetState_Req, ChannelService_GetState_Ans>::Pointer ctx )
  417. {
  418. DbgToBeidou(ctx->link, __FUNCTION__)();
  419. ctx->Ans.status = state2str(m_pEntity->GetState());
  420. ctx->Answer(Error_Succeed);
  421. }
  422. void ChannelServiceSession::Handle_BeginState( SpSubscribeContext<ChannelService_BeginState_Sub, ChannelService_State_Info>::Pointer ctx )
  423. {
  424. m_pEntity->RegisterState(m_id, ctx);
  425. }
  426. void ChannelServiceSession::Handle_EndState( SpOnewayCallContext<ChannelService_EndState_Info>::Pointer ctx )
  427. {
  428. DbgToBeidou(ctx->link, __FUNCTION__)();
  429. m_pEntity->UnregisterState(m_id);
  430. }
  431. void ChannelServiceSession::Handle_Send( SpOnewayCallContext<ChannelService_Send_Info>::Pointer ctx )
  432. {
  433. DbgToBeidou(ctx->link, __FUNCTION__)();
  434. m_pEntity->Send(ctx->Info.type, ctx->Info.compress, ctx->Info.encrypt, ctx->Info.sub_type, ctx->Info.id, ctx->Info.data);
  435. }
  436. void ChannelServiceSession::Handle_BeginRecv( SpSubscribeContext<ChannelService_BeginRecv_Sub, ChannelService_Packet_Info>::Pointer ctx )
  437. {
  438. m_pEntity->RegisterRxPkt(m_id, ctx);
  439. }
  440. void ChannelServiceSession::Handle_EndRecv( SpOnewayCallContext<ChannelService_EndRecv_Info>::Pointer ctx )
  441. {
  442. DbgToBeidou(ctx->link, __FUNCTION__)();
  443. m_pEntity->UnregisterRxpkt(m_id);
  444. }
  445. void ChannelServiceSession::Handle_GetIpByDomain(SpReqAnsContext<ChannelService_GetIpByDomain_Req, ChannelService_GetIpByDomain_Ans>::Pointer ctx)
  446. {
  447. DbgToBeidou(ctx->link, __FUNCTION__)();
  448. char strip[MAX_PATH] = {0};
  449. m_pEntity->GetIpByDomain(strip, MAX_PATH, ctx->Req.strdomain);
  450. ctx->Ans.strip = strip;
  451. ctx->Answer(Error_Succeed);
  452. }
  453. void ChannelServiceSession::OnClose( ErrorCodeEnum eErrorCode )
  454. {
  455. m_pEntity->UnregisterRxpkt(m_id);
  456. m_pEntity->UnregisterState(m_id);
  457. }
  458. SP_BEGIN_ENTITY_MAP()
  459. SP_ENTITY(CBizChannelEntity)
  460. SP_END_ENTITY_MAP()