中航光电的中间件仓库

FsProxy.cpp 34KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. #include "StdAfx.h"
  2. #include "FsProxy.h"
  3. #include "ChanExten.h"
  4. #include "ChanTrunk.h"
  5. #include "Session.h"
  6. #include "OperationReactor.h"
  7. #include "../IVR/IvrSysInc.h"
  8. SINGLETON_IMPLEMENT(CFsProxy)
  9. CFsProxy::CFsProxy(void) : m_Gateway(this), m_pEventHandler(NULL)
  10. {
  11. }
  12. CFsProxy::~CFsProxy(void)
  13. {
  14. }
  15. /*****************************************************************
  16. **【函数名称】 __transLogicState2CtiState
  17. **【函数功能】 将逻辑状态转换为CTI识别的状态
  18. **【参数】
  19. **【返回值】
  20. ****************************************************************/
  21. UINT CFsProxy::__transLogicState2CtiState(DEV_RES_TYPE ChanType, CHAN_LOGIC_STATE State)
  22. {
  23. if (ChanType == DEV_RES_TYPE_EXT)
  24. {
  25. UINT HoldMask = State & HELD_STATE_IND_MASK;
  26. switch (State & HELD_STATE_FILTER_MASK)
  27. {
  28. case CHAN_LOGIC_STATE_DISABLED: return INNER_STATE_DISABLED; // 不可用
  29. case CHAN_LOGIC_STATE_FREE: return INNER_STATE_FREE | HoldMask; // 空闲
  30. case CHAN_LOGIC_STATE_INIT: return INNER_STATE_INIT | HoldMask; // 摘机等待拨号
  31. case CHAN_LOGIC_STATE_DIALING: return INNER_STATE_DIALING | HoldMask; // 拨号
  32. case CHAN_LOGIC_STATE_RING_BACK: return INNER_STATE_RING_BACK | HoldMask; // 呼出振铃
  33. case CHAN_LOGIC_STATE_ALERTING: return INNER_STATE_ALERTING | HoldMask; // 来电振铃
  34. case CHAN_LOGIC_STATE_TALKING: return INNER_STATE_TALKING | HoldMask; // 通话中
  35. default:
  36. ASSERT(FALSE);
  37. return INNER_STATE_DISABLED;
  38. }
  39. }
  40. else
  41. {
  42. switch (State)
  43. {
  44. case CHAN_LOGIC_STATE_DISABLED: return TRUNK_STATE_DISABLED; // 不可用
  45. case CHAN_LOGIC_STATE_FREE: return TRUNK_STATE_FREE; // 空闲
  46. case CHAN_LOGIC_STATE_DIALING: return TRUNK_STATE_DIALING; // 拨号
  47. case CHAN_LOGIC_STATE_RING_BACK: return TRUNK_STATE_RING_BACK; // 呼出振铃
  48. case CHAN_LOGIC_STATE_ALERTING: return TRUNK_STATE_ALERTING; // 来电振铃
  49. case CHAN_LOGIC_STATE_STANDBY:
  50. case CHAN_LOGIC_STATE_TALKING: return TRUNK_STATE_TALKING; // 通话中
  51. default:
  52. ASSERT(FALSE);
  53. return TRUNK_STATE_UNKNOWN;
  54. }
  55. }
  56. }
  57. /*****************************************************************
  58. **【函数名称】 __addExten
  59. **【函数功能】 添加分机
  60. **【参数】
  61. **【返回值】
  62. ****************************************************************/
  63. void CFsProxy::__addExten(UINT ExtenNo)
  64. {
  65. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  66. CChanExten* pExten = new CChanExten(this, ExtenNo);
  67. m_MapChanExt.SetAt(ExtenNo, pExten);
  68. pExten->regist();
  69. }
  70. /*****************************************************************
  71. **【函数名称】 delExten
  72. **【函数功能】 删除分机
  73. **【参数】
  74. **【返回值】
  75. ****************************************************************/
  76. void CFsProxy::__delExten(UINT ExtenNo)
  77. {
  78. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  79. CChanExten* pExten = NULL;
  80. if (m_MapChanExt.Lookup(ExtenNo, pExten))
  81. {
  82. if (pExten->isFree())
  83. {
  84. m_MapChanExt.RemoveKey(ExtenNo);
  85. FS_LINK_DELETE(pExten);
  86. }
  87. else pExten->discard(true);
  88. }
  89. }
  90. /*****************************************************************
  91. **【函数名称】 __freeExten
  92. **【函数功能】 清空分机
  93. **【参数】
  94. **【返回值】
  95. ****************************************************************/
  96. void CFsProxy::__freeExten(void)
  97. {
  98. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  99. UINT ExtenNo = 0;
  100. CChanExten* pExten = NULL;
  101. POSITION Pos = m_MapChanExt.GetStartPosition();
  102. while (Pos != NULL)
  103. {
  104. m_MapChanExt.GetNextAssoc(Pos, ExtenNo, pExten);
  105. FS_LINK_DELETE(pExten);
  106. }
  107. m_MapChanExt.RemoveAll();
  108. }
  109. /*****************************************************************
  110. **【函数名称】 __initTrunkChan
  111. **【函数功能】 初始化中继通道
  112. **【参数】
  113. **【返回值】
  114. ****************************************************************/
  115. void CFsProxy::__initTrunkChan(void)
  116. {
  117. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  118. for (int i = 1; i <= CConfig::trunkCount(); ++i)
  119. {
  120. CChanTrunk* pTrunk = new CChanTrunk(this, i);
  121. m_ArrayTrunk.Add(pTrunk);
  122. pTrunk->regist();
  123. }
  124. }
  125. /*****************************************************************
  126. **【函数名称】 __freeTrunkChan
  127. **【函数功能】 清空中继通道
  128. **【参数】
  129. **【返回值】
  130. ****************************************************************/
  131. void CFsProxy::__freeTrunkChan(void)
  132. {
  133. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  134. CChanTrunk* pTrunk = NULL;
  135. for (int i = 0; i < m_ArrayTrunk.GetCount(); ++i)
  136. {
  137. pTrunk = m_ArrayTrunk[i];
  138. FS_LINK_DELETE(pTrunk);
  139. }
  140. m_ArrayTrunk.RemoveAll();
  141. }
  142. /*****************************************************************
  143. **【函数名称】 __getSession
  144. **【函数功能】 查找会话
  145. **【参数】
  146. **【返回值】
  147. ****************************************************************/
  148. CSession* CFsProxy::__getSession(PCHAN_EVENT_NOTIFY pNotify, bool NewWhenNull /*= false*/)
  149. {
  150. CSession* pSession = __getSession(pNotify->CallId);
  151. if (pSession == NULL && NewWhenNull)
  152. {
  153. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  154. pSession = new CSession(this, pNotify->CallId);
  155. pSession->prepare(pNotify);
  156. m_MapSession.SetAt(pNotify->CallId, pSession);
  157. LOGGER(LOG_LEVEL_NORMAL, _T("{CFsProxy}:Add Session[%s],ChanId[%s],EventId[%d]"), pSession->id(), pNotify->ChanId, pNotify->EventId);
  158. }
  159. return pSession;
  160. }
  161. /*****************************************************************
  162. **【函数名称】 __getSession
  163. **【函数功能】 查找会话
  164. **【参数】
  165. **【返回值】
  166. ****************************************************************/
  167. CSession* CFsProxy::__getSession(LPCTSTR SessionId)
  168. {
  169. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  170. CSession* pSession = NULL;
  171. m_MapSession.Lookup(SessionId, pSession);
  172. return pSession;
  173. }
  174. /*****************************************************************
  175. **【函数名称】 __delSession
  176. **【函数功能】 删除会话
  177. **【参数】
  178. **【返回值】
  179. ****************************************************************/
  180. void CFsProxy::__delSession(LPCTSTR SessionId)
  181. {
  182. if (SessionId == NULL)
  183. return;
  184. CSession* pSession = NULL;
  185. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  186. if (m_MapSession.Lookup(SessionId, pSession))
  187. {
  188. m_MapSession.RemoveKey(SessionId);
  189. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: Delete Session[%s]"), pSession->id());
  190. FS_LINK_DELETE(pSession);
  191. }
  192. }
  193. /*****************************************************************
  194. **【函数名称】 __freeSession
  195. **【函数功能】 清空会话
  196. **【参数】
  197. **【返回值】
  198. ****************************************************************/
  199. void CFsProxy::__freeSession(void)
  200. {
  201. CString SessionId = 0;
  202. CSession* pSession = NULL;
  203. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  204. POSITION Pos = m_MapSession.GetStartPosition();
  205. while (Pos != NULL)
  206. {
  207. m_MapSession.GetNextAssoc(Pos, SessionId, pSession);
  208. FS_LINK_DELETE(pSession);
  209. }
  210. m_MapSession.RemoveAll();
  211. }
  212. /*****************************************************************
  213. **【函数名称】 __kill
  214. **【函数功能】 挂断通道
  215. **【参数】
  216. **【返回值】
  217. ****************************************************************/
  218. bool CFsProxy::__kill(LONG JobId, LPCTSTR ChanId)
  219. {
  220. CString EslCmd;
  221. if (JobId == FS_LINK_JOBID_INVALID)
  222. EslCmd.Format(_T("bgapi uuid_kill %s"), ChanId);
  223. else
  224. EslCmd.Format(_T("bgapi uuid_kill %s\r\n%s: %ld"), ChanId, ESL_HEADER_JOB_UUID, JobId);
  225. return m_Gateway.sendCmd(EslCmd);
  226. }
  227. /*****************************************************************
  228. **【函数名称】 init
  229. **【函数功能】 初始化
  230. **【参数】
  231. **【返回值】
  232. ****************************************************************/
  233. bool CFsProxy::init(IEslEventHandler* pEventHandler)
  234. {
  235. ASSERT(pEventHandler != NULL);
  236. m_pEventHandler = pEventHandler;
  237. __initTrunkChan();
  238. if (m_Gateway.start())
  239. {
  240. m_Gateway.hangupAll();
  241. if (m_Gateway.scanExten())
  242. {
  243. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: FreeSWITCH代理初始化成功"));
  244. return true;
  245. }
  246. else
  247. {
  248. LOGGER(LOG_LEVEL_ERROR, _T("{FsProxy}: FreeSWITCH代理初始化失败, 扫描分机资源失败"));
  249. return false;
  250. }
  251. }
  252. else
  253. {
  254. LOGGER(LOG_LEVEL_ERROR, _T("{FsProxy}: FreeSWITCH代理初始化失败, ESL网关启动失败"));
  255. m_pEventHandler = NULL;
  256. return false;
  257. }
  258. }
  259. /*****************************************************************
  260. **【函数名称】 release
  261. **【函数功能】 释放资源
  262. **【参数】
  263. **【返回值】
  264. ****************************************************************/
  265. void CFsProxy::release(void)
  266. {
  267. esl_disconnect(&m_EslHandle);
  268. m_Gateway.stop();
  269. __freeSession();
  270. __freeExten();
  271. __freeTrunkChan();
  272. }
  273. /*****************************************************************
  274. **【函数名称】 onChanRegist
  275. **【函数功能】 通道注册的处理函数
  276. **【参数】
  277. **【返回值】
  278. ****************************************************************/
  279. void CFsProxy::onChanRegist(DEV_RES_TYPE ChanType, UINT ChanNo, CHAN_LOGIC_STATE ChanState)
  280. {
  281. CDevControl::GetInstance().onEventResDetail(ChanType, ChanNo);
  282. CDevControl::GetInstance().onEventResState(ChanType, ChanNo, __transLogicState2CtiState(ChanType, ChanState));
  283. }
  284. /*****************************************************************
  285. **【函数名称】 onExtenDestroy
  286. **【函数功能】 分机销毁的处理函数
  287. **【参数】
  288. **【返回值】
  289. ****************************************************************/
  290. void CFsProxy::onExtenDestroy(UINT ExtenNo)
  291. {
  292. CDevControl::GetInstance().onEventResState(DEV_RES_TYPE_EXT, ExtenNo, INNER_STATE_REMOVE);
  293. }
  294. /*****************************************************************
  295. **【函数名称】 onExtenDirectOp
  296. **【函数功能】 分机直接操作启动
  297. **【参数】 OpType 启动的操作类型
  298. pHostChan 触发事件的通道
  299. pNotify 触发操作事件内容
  300. **【返回值】
  301. ****************************************************************/
  302. void CFsProxy::onExtenDirectOp(DEV_OP OpType, CVirtualChan* pHostChan, PCHAN_EVENT_NOTIFY pNotify)
  303. {
  304. switch (OpType)
  305. {
  306. case DEV_OP_CALL_OUT:
  307. {
  308. if (!COperationReactor::GetInstance().onExtenCallFromDev(pHostChan, pNotify))
  309. __kill(FS_LINK_JOBID_INVALID, pHostChan->chanId());
  310. }
  311. break;
  312. } // end switch
  313. }
  314. /*****************************************************************
  315. **【函数名称】 onChanStateUpdate
  316. **【函数功能】 通道状态更新处理
  317. **【参数】
  318. **【返回值】
  319. *****************************************************************/
  320. void CFsProxy::onChanStateUpdate(LONG OpInstance, CVirtualChan* pChan)
  321. {
  322. if (OpInstance != FS_LINK_INSTANCE_INVALID)
  323. m_pEventHandler->onEslEvtChanState(OpInstance, pChan);
  324. CDevControl::GetInstance().onEventResState(pChan->type(), pChan->no(), __transLogicState2CtiState(pChan->type(), pChan->state()), pChan->callerNum(), pChan->calleeNum());
  325. }
  326. /*****************************************************************
  327. **【函数名称】 onChanPoor
  328. **【函数功能】 空闲通道枯竭处理
  329. **【参数】
  330. **【返回值】
  331. *****************************************************************/
  332. void CFsProxy::onChanPoor(CSession* pSession, PCHAN_EVENT_NOTIFY pNotify)
  333. {
  334. __kill(FS_LINK_JOBID_INVALID, pNotify->ChanId);
  335. }
  336. /*****************************************************************
  337. **【函数名称】 onTrunkCallIn
  338. **【函数功能】 中继呼入
  339. **【参数】
  340. **【返回值】
  341. *****************************************************************/
  342. void CFsProxy::onTrunkCallIn(CChanTrunk* pTrunk)
  343. {
  344. CDevControl::GetInstance().onEventDevOperation(pTrunk->no(), DEV_OP_CALL_IN, pTrunk->callerNum(), pTrunk->calleeNum());
  345. }
  346. /*****************************************************************
  347. **【函数名称】 onEslDisconnect
  348. **【函数功能】 ESL连接中断处理
  349. **【参数】
  350. **【返回值】
  351. *****************************************************************/
  352. void CFsProxy::onEslDisconnect(void)
  353. {
  354. CDevControl::GetInstance().onEventDevDown();
  355. }
  356. /*****************************************************************
  357. **【函数名称】 onEslSipReg
  358. **【函数功能】 分机注册的处理函数
  359. **【参数】
  360. **【返回值】
  361. ****************************************************************/
  362. void CFsProxy::onEslExtenReg(UINT ExtenNo)
  363. {
  364. CChanExten* pExten = getExten(ExtenNo);
  365. if (pExten == NULL)
  366. {
  367. __addExten(ExtenNo);
  368. }
  369. else
  370. {
  371. if (pExten->isVoid()) // 若被丢弃,则改变丢弃状态
  372. pExten->discard(false);
  373. }
  374. #ifdef _DEBUG
  375. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 分机[%u]注册"), ExtenNo);
  376. #endif
  377. }
  378. /*****************************************************************
  379. **【函数名称】 onEslExtenUnreg
  380. **【函数功能】 分机注销的处理函数
  381. **【参数】
  382. **【返回值】
  383. ****************************************************************/
  384. void CFsProxy::onEslExtenUnreg(UINT ExtenNo)
  385. {
  386. __delExten(ExtenNo);
  387. #ifdef _DEBUG
  388. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 分机[%u]注销"), ExtenNo);
  389. #endif
  390. }
  391. /*****************************************************************
  392. **【函数名称】 onEslGwExten
  393. **【函数功能】 网关分机的处理函数
  394. **【参数】
  395. **【返回值】
  396. ****************************************************************/
  397. void CFsProxy::onEslGwExtenReg(LPCTSTR ExtenNo, LPCTSTR Ip)
  398. {
  399. m_MapAddrGwExt.SetAt(ExtenNo, Ip);
  400. return;
  401. }
  402. void CFsProxy::onEslGwExtenUnreg(LPCTSTR ExtenNo)
  403. {
  404. CString t_ip;
  405. if (m_MapAddrGwExt.Lookup(ExtenNo, t_ip))
  406. {
  407. t_ip.Empty();
  408. m_MapAddrGwExt.RemoveKey(ExtenNo);
  409. }
  410. }
  411. /*****************************************************************
  412. **【函数名称】 onEslEvtBgJobDone
  413. **【函数功能】 后台任务执行结束事件处理
  414. **【参数】
  415. **【返回值】
  416. *****************************************************************/
  417. void CFsProxy::onEslEvtBgJobDone(PBG_JOB_NOTIFY pNotify)
  418. {
  419. ASSERT(m_pEventHandler != NULL);
  420. m_pEventHandler->onEslEvtBgJobDone(pNotify);
  421. }
  422. /*****************************************************************
  423. **【函数名称】 onEslEvtChannel
  424. **【函数功能】 通道事件处理
  425. **【参数】
  426. **【返回值】
  427. *****************************************************************/
  428. void CFsProxy::onEslEvtChannel(PCHAN_EVENT_NOTIFY pNotify)
  429. {
  430. ASSERT(pNotify != NULL);
  431. if (pNotify == NULL)
  432. return;
  433. CSession* pSession = __getSession(pNotify, true);
  434. ASSERT(pSession != NULL);
  435. if (pSession == NULL)
  436. {
  437. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 通道事件,没有找到会话,EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s]"), pNotify->EventId, pNotify->ChanId, pNotify->CallId, pNotify->Caller, pNotify->Callee);
  438. return;
  439. }
  440. __try {
  441. pSession->onChanEvent(pNotify);
  442. if (pSession->isVoid())
  443. __delSession(pSession->id());
  444. }
  445. __except (EXCEPTION_EXECUTE_HANDLER)
  446. {
  447. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 处理通道事件时异常[%u],EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s]"), GetExceptionCode(), pNotify->EventId, pNotify->ChanId, pNotify->CallId, pNotify->Caller, pNotify->Callee);
  448. __delSession(pSession->id());
  449. }
  450. }
  451. /*****************************************************************
  452. **【函数名称】 onEslEvtDtmf
  453. **【函数功能】 DTMF事件处理
  454. **【参数】
  455. **【返回值】
  456. *****************************************************************/
  457. void CFsProxy::onEslEvtDtmf(PDTMF_NOTIFY pNotify)
  458. {
  459. ASSERT(pNotify != NULL);
  460. CSession* pSession = __getSession(pNotify->CallId);
  461. ASSERT(pSession != NULL);
  462. if (pSession != NULL)
  463. pSession->onChanDtmf(pNotify);
  464. }
  465. /*****************************************************************
  466. **【函数名称】 onEslEvtHold
  467. **【函数功能】 保持事件处理
  468. **【参数】
  469. **【返回值】
  470. *****************************************************************/
  471. void CFsProxy::onEslEvtHold(PHOLD_NOTIFY pNotify)
  472. {
  473. ASSERT(pNotify != NULL);
  474. CSession* pSession = __getSession(pNotify->CallId);
  475. ASSERT(pSession != NULL);
  476. if (pSession != NULL)
  477. pSession->onChanHold(pNotify);
  478. }
  479. /*****************************************************************
  480. **【函数名称】 getExten
  481. **【函数功能】 查找分机
  482. **【参数】
  483. **【返回值】
  484. ****************************************************************/
  485. CChanExten* CFsProxy::getExten(UINT ExtenNo)
  486. {
  487. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  488. CChanExten* pExten = NULL;
  489. m_MapChanExt.Lookup(ExtenNo, pExten);
  490. return pExten;
  491. }
  492. /*****************************************************************
  493. **【函数名称】 getTrunk
  494. **【函数功能】 查找中继
  495. **【参数】
  496. **【返回值】
  497. ****************************************************************/
  498. CChanTrunk* CFsProxy::getTrunk(UINT TrunkNo)
  499. {
  500. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  501. if (TrunkNo <= 0 || TrunkNo > (UINT)m_ArrayTrunk.GetCount())
  502. return NULL;
  503. --TrunkNo;
  504. return m_ArrayTrunk[TrunkNo];
  505. }
  506. /*****************************************************************
  507. **【函数名称】 delChan
  508. **【函数功能】 删除通道
  509. **【参数】
  510. **【返回值】
  511. ****************************************************************/
  512. void CFsProxy::delChan(CVirtualChan* pChan)
  513. {
  514. if (pChan->type() == DEV_RES_TYPE_EXT)
  515. __delExten(pChan->no());
  516. }
  517. /*****************************************************************
  518. **【函数名称】 getFreeTrunk
  519. **【函数功能】 查找空闲中继
  520. **【参数】
  521. **【返回值】
  522. ****************************************************************/
  523. CChanTrunk* CFsProxy::getFreeTrunk(void)
  524. {
  525. // 当前轮循到的索引
  526. static int PosStatic = 0;
  527. int CurPos = PosStatic;
  528. int BusyTruckCount = 0;
  529. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  530. // 保证遍历一轮
  531. for (int i = 0; i < m_ArrayTrunk.GetCount(); ++i)
  532. {
  533. // 保证POS是有效的
  534. if (PosStatic >= m_ArrayTrunk.GetCount())
  535. PosStatic = 0;
  536. CChanTrunk* pTrunk = m_ArrayTrunk[PosStatic++];
  537. ASSERT(pTrunk != NULL);
  538. // 当前外线是否空闲
  539. if (pTrunk->isFree())
  540. {
  541. return pTrunk;
  542. }
  543. else
  544. {
  545. BusyTruckCount++;
  546. }
  547. }
  548. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 没有空闲中继通道,起始索引=%d,当前索引=%d,中继总数量=%d,忙通道数量=%d"), CurPos, PosStatic, m_ArrayTrunk.GetCount(), BusyTruckCount);
  549. //2019.3.7
  550. PosStatic = CurPos;
  551. for (int i = 0; i < m_ArrayTrunk.GetCount(); ++i)
  552. {
  553. // 保证POS是有效的
  554. if (PosStatic >= m_ArrayTrunk.GetCount())
  555. PosStatic = 0;
  556. CChanTrunk* pTrunk = m_ArrayTrunk[PosStatic++];
  557. ASSERT(pTrunk != NULL);
  558. // 当前外线是否空闲
  559. if (pTrunk->state() == CHAN_LOGIC_STATE_FREE)
  560. {
  561. if (pTrunk->currOp() != NULL) {
  562. COperationReactor::GetInstance().releaseOpResult(pTrunk->currOp());
  563. pTrunk->releaseOp(pTrunk->currOp());
  564. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}:没有空闲中继通道,通道[%u],释放操作."), pTrunk->no());
  565. }
  566. return pTrunk;
  567. }
  568. }
  569. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 没有空闲中继通道,起始索引=%d,当前索引=%d"), CurPos, PosStatic);
  570. return NULL;
  571. }
  572. /*****************************************************************
  573. **【函数名称】 getAssoChanInSession
  574. **【函数功能】 获取会话中关联通道
  575. **【参数】
  576. **【返回值】
  577. ****************************************************************/
  578. CVirtualChan* CFsProxy::getAssoChanInSession(CVirtualChan* pChan)
  579. {
  580. CSession* pSession = __getSession(pChan->sessionId());
  581. if (pSession != NULL)
  582. return pSession->getAssoChan(pChan);
  583. else
  584. return NULL;
  585. }
  586. /*****************************************************************
  587. **【函数名称】 getBusyChan
  588. **【函数功能】 获取忙通道
  589. **【参数】
  590. **【返回值】
  591. *****************************************************************/
  592. CVirtualChan* CFsProxy::getBusyChan(LPCTSTR ChanId)
  593. {
  594. std::lock_guard<std::mutex> lock(m_LockMapBusyChan);
  595. CVirtualChan* pBusyChan = NULL;
  596. m_MapBusyChan.Lookup(ChanId, pBusyChan);
  597. return pBusyChan;
  598. }
  599. /*****************************************************************
  600. **【函数名称】 regBusyChan
  601. **【函数功能】 登记忙通道
  602. **【参数】
  603. **【返回值】
  604. *****************************************************************/
  605. void CFsProxy::regBusyChan(CVirtualChan* pChan)
  606. {
  607. std::lock_guard<std::mutex> lock(m_LockMapBusyChan);
  608. m_MapBusyChan.SetAt(pChan->chanId(), pChan);
  609. }
  610. /*****************************************************************
  611. **【函数名称】 unregBusyChan
  612. **【函数功能】 取消登记忙通道
  613. **【参数】
  614. **【返回值】
  615. *****************************************************************/
  616. void CFsProxy::unregBusyChan(CVirtualChan* pChan)
  617. {
  618. std::lock_guard<std::mutex> lock(m_LockMapBusyChan);
  619. m_MapBusyChan.RemoveKey(pChan->chanId());
  620. }
  621. /*****************************************************************
  622. **【函数名称】 ExtenCall
  623. **【函数功能】 分机呼叫
  624. **【参数】
  625. **【返回值】
  626. ****************************************************************/
  627. bool CFsProxy::ExtenCall(LONG JobId, CVirtualChan* pChan, LPCTSTR CallerNum, LPCTSTR CalleeNum)
  628. {
  629. CString EslCmd;
  630. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu,absolute_codec_string='PCMA,PCMU'}user/%lu %s XML %s\r\n%s: %ld"),
  631. pChan->no(), pChan->no(), CalleeNum, CConfig::extContext(), ESL_HEADER_JOB_UUID, JobId);
  632. return m_Gateway.sendCmd(EslCmd);
  633. }
  634. /*****************************************************************
  635. **【函数名称】 PredictionCall
  636. **【函数功能】 预测外呼
  637. **【参数】
  638. **【返回值】
  639. ****************************************************************/
  640. bool CFsProxy::PredictionCall(LONG JobId, CString CallerNum, CString CalleeNum)
  641. {
  642. CString CallString;
  643. if (!m_CallStringMaker.makeCallStringWithDefault(CallerNum, CalleeNum, CallString))
  644. return false;
  645. CString EslCmd;
  646. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld"),
  647. CallerNum, ESL_VAR_OP_INSTANCE, JobId, CallString, CalleeNum, CConfig::pCallContext(), ESL_HEADER_JOB_UUID, JobId);
  648. return m_Gateway.sendCmd(EslCmd);
  649. }
  650. /*****************************************************************
  651. **【函数名称】 answer
  652. **【函数功能】 应答通道
  653. **【参数】
  654. **【返回值】
  655. ****************************************************************/
  656. bool CFsProxy::answer(LONG JobId, CVirtualChan* pChan)
  657. {
  658. return false;
  659. }
  660. /*****************************************************************
  661. **【函数名称】 kill
  662. **【函数功能】 挂断通道
  663. **【参数】
  664. **【返回值】
  665. ****************************************************************/
  666. bool CFsProxy::kill(LONG JobId, CVirtualChan* pChan)
  667. {
  668. return __kill(JobId, pChan->chanId());
  669. }
  670. /*****************************************************************
  671. **【函数名称】 consult
  672. **【函数功能】 协商呼叫
  673. **【参数】
  674. **【返回值】
  675. ****************************************************************/
  676. bool CFsProxy::consult(LONG JobId, CVirtualChan* pChan, CString DestNum)
  677. {
  678. CString CallerNum;
  679. CString CallString;
  680. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
  681. return false;
  682. CString EslCmd;
  683. EslCmd.Format(_T("bgapi uuid_broadcast %s att_xfer::%s\r\n%s: %ld"), pChan->chanId(), CallString, ESL_HEADER_JOB_UUID, JobId);
  684. return m_Gateway.sendCmd(EslCmd);
  685. }
  686. /*****************************************************************
  687. **【函数名称】 insert
  688. **【函数功能】 强插
  689. **【参数】
  690. **【返回值】
  691. ****************************************************************/
  692. bool CFsProxy::insert(LONG JobId, CVirtualChan* pChan, LPCTSTR DestSessionId)
  693. {
  694. CString EslCmd;
  695. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu}user/%lu &three_way(%s)\r\n%s: %ld"),
  696. pChan->no(), pChan->no(), DestSessionId, ESL_HEADER_JOB_UUID, JobId);
  697. return m_Gateway.sendCmd(EslCmd);
  698. }
  699. /*****************************************************************
  700. **【函数名称】 intercept
  701. **【函数功能】 强截
  702. **【参数】
  703. **【返回值】
  704. ****************************************************************/
  705. bool CFsProxy::intercept(LONG JobId, CVirtualChan* pChan, LPCTSTR DestChanId)
  706. {
  707. CString EslCmd;
  708. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu}user/%lu &intercept(%s)\r\n%s: %ld"),
  709. pChan->no(), pChan->no(), DestChanId, ESL_HEADER_JOB_UUID, JobId);
  710. return m_Gateway.sendCmd(EslCmd);
  711. }
  712. /*****************************************************************
  713. **【函数名称】 listen
  714. **【函数功能】 监听
  715. **【参数】
  716. **【返回值】
  717. ****************************************************************/
  718. bool CFsProxy::listen(LONG JobId, CVirtualChan* pChan, LPCTSTR DestChanId)
  719. {
  720. CString EslCmd;
  721. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu}user/%lu &eavesdrop(%s)\r\n%s: %ld"),
  722. pChan->no(), pChan->no(), DestChanId, ESL_HEADER_JOB_UUID, JobId);
  723. return m_Gateway.sendCmd(EslCmd);
  724. }
  725. // ych
  726. std::string CFsProxy::EslCommand(std::string strFsIp, int FsPort, std::string strName, std::string strPsWd, std::string strCommand)
  727. {
  728. if (!m_EslHandle.connected)
  729. {
  730. esl_connect(&m_EslHandle, strFsIp.c_str(), FsPort, strName.c_str(), strPsWd.c_str());
  731. }
  732. if (m_EslHandle.connected)
  733. {
  734. esl_send_recv_timed(&m_EslHandle, strCommand.c_str(), 2000);
  735. if (m_EslHandle.last_sr_event && m_EslHandle.last_sr_event->body)
  736. {
  737. std::string strRet = m_EslHandle.last_sr_event->body;
  738. return strRet;
  739. }
  740. }
  741. return "";
  742. }
  743. /*****************************************************************
  744. **【函数名称】 meeting
  745. **【函数功能】 会议
  746. **【参数】
  747. **【返回值】
  748. ****************************************************************/
  749. bool CFsProxy::meeting(LONG JobId, CString CallerNum, CString DestNum, LPCTSTR MeetingId)
  750. {
  751. bool bMatchPrefix = m_CallStringMaker.isMatchPrefix(DestNum);
  752. TRUNK_MATCH* pMatch = NULL;
  753. CString CallString;
  754. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString, pMatch))
  755. return false;
  756. if (pMatch != NULL)
  757. {
  758. SIP_ACCOUNT* pAccount = CConfig::sipAccount().getAccount(pMatch->TrunkItemId);
  759. if (bMatchPrefix && pAccount != NULL && pAccount->IsDynamicGw) // 外线且动态网关
  760. {
  761. std::string strSipAccount = pAccount->Account;
  762. std::string strGwIp = EslCommand(CConfig::fsAddr(), CConfig::fsPort(), "", CConfig::fsPwd(), "api sofia_contact " + strSipAccount);
  763. if (!strGwIp.empty())
  764. {
  765. size_t Index = strGwIp.find_first_of('@');
  766. if (Index != std::string::npos)
  767. {
  768. strGwIp = strGwIp.substr(Index);
  769. CallString = "sofia/internal/";
  770. CallString += DestNum;
  771. CallString += strGwIp.c_str();
  772. }
  773. }
  774. }
  775. }
  776. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 三方会议呼叫字符串: %s"), CallString);
  777. CString EslCmd;
  778. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld"),
  779. CallerNum, ESL_VAR_OP_INSTANCE, JobId, CallString, MeetingId, CConfig::meetingContext(), ESL_HEADER_JOB_UUID, JobId);
  780. return m_Gateway.sendCmd(EslCmd);
  781. }
  782. /*****************************************************************
  783. **【函数名称】 muteOn
  784. **【函数功能】 静音
  785. **【参数】
  786. **【返回值】
  787. ****************************************************************/
  788. bool CFsProxy::muteOn(LONG JobId, CVirtualChan* pChan)
  789. {
  790. CString EslCmd;
  791. EslCmd.Format(_T("bgapi uuid_audio %s start write mute -4\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  792. return m_Gateway.sendCmd(EslCmd);
  793. }
  794. /*****************************************************************
  795. **【函数名称】 muteOff
  796. **【函数功能】 取消静音
  797. **【参数】
  798. **【返回值】
  799. ****************************************************************/
  800. bool CFsProxy::muteOff(LONG JobId, CVirtualChan* pChan)
  801. {
  802. CString EslCmd;
  803. EslCmd.Format(_T("bgapi uuid_audio %s stop write mute -4\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  804. return m_Gateway.sendCmd(EslCmd);
  805. }
  806. /*****************************************************************
  807. **【函数名称】 holdon
  808. **【函数功能】 保持
  809. **【参数】
  810. **【返回值】
  811. ****************************************************************/
  812. bool CFsProxy::holdon(LONG JobId, CVirtualChan* pChan)
  813. {
  814. CString EslCmd;
  815. EslCmd.Format(_T("bgapi uuid_hold %s\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  816. return m_Gateway.sendCmd(EslCmd);
  817. }
  818. /*****************************************************************
  819. **【函数名称】 takeBack
  820. **【函数功能】 接回
  821. **【参数】
  822. **【返回值】
  823. ****************************************************************/
  824. bool CFsProxy::takeBack(LONG JobId, CVirtualChan* pChan)
  825. {
  826. CString EslCmd;
  827. EslCmd.Format(_T("bgapi uuid_hold off %s\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  828. return m_Gateway.sendCmd(EslCmd);
  829. }
  830. /*****************************************************************
  831. **【函数名称】 record
  832. **【函数功能】 录音
  833. **【参数】
  834. **【返回值】
  835. ****************************************************************/
  836. bool CFsProxy::record(LONG JobId, CVirtualChan* pChan, LPCTSTR RcdFile)
  837. {
  838. CString EslCmd;
  839. EslCmd.Format(_T("bgapi uuid_record %s start %s\r\n%s: %ld"), pChan->chanId(), RcdFile, ESL_HEADER_JOB_UUID, JobId);
  840. return m_Gateway.sendCmd(EslCmd);
  841. }
  842. /*****************************************************************
  843. **【函数名称】 transfer
  844. **【函数功能】 呼叫转移
  845. **【参数】
  846. **【返回值】
  847. ****************************************************************/
  848. bool CFsProxy::transfer(LONG JobId, CVirtualChan* pChan, LPCTSTR DestNum)
  849. {
  850. CString EslCmd;
  851. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld"), pChan->chanId(), DestNum, CConfig::extContext(), ESL_HEADER_JOB_UUID, JobId);
  852. return m_Gateway.sendCmd(EslCmd);
  853. }
  854. /*****************************************************************
  855. **【函数名称】 transfer2Context
  856. **【函数功能】 转移通道至其它Context
  857. **【参数】
  858. **【返回值】
  859. ****************************************************************/
  860. bool CFsProxy::transfer2Context(LONG JobId, LPCTSTR DestChanId, LPCTSTR Exten, LPCTSTR Context, bool BothSide /*= false*/)
  861. {
  862. CString EslCmd;
  863. if (BothSide)
  864. EslCmd.Format(_T("bgapi uuid_transfer %s -both %s xml %s\r\n%s: %ld"), DestChanId, Exten, Context, ESL_HEADER_JOB_UUID, JobId);
  865. else
  866. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld"), DestChanId, Exten, Context, ESL_HEADER_JOB_UUID, JobId);
  867. return m_Gateway.sendCmd(EslCmd);
  868. }
  869. /*****************************************************************
  870. **【函数名称】 cancel
  871. **【函数功能】 取消当前执行的应用
  872. **【参数】
  873. **【返回值】
  874. ****************************************************************/
  875. bool CFsProxy::cancel(CChanTrunk* pChan)
  876. {
  877. ASSERT(pChan != NULL);
  878. return m_Gateway.Execte2(ESL_APP_BREAK, NULL, pChan->chanId());
  879. }
  880. /*****************************************************************
  881. **【函数名称】 hangup
  882. **【函数功能】 挂机
  883. **【参数】
  884. **【返回值】
  885. ****************************************************************/
  886. bool CFsProxy::hangup(CChanTrunk* pChan)
  887. {
  888. ASSERT(pChan != NULL);
  889. return m_Gateway.execute(pChan->m_pEslHandle, ESL_APP_HANGUP, NULL);
  890. }
  891. /*****************************************************************
  892. **【函数名称】 leaveWord
  893. **【函数功能】 留言
  894. **【参数】
  895. **【返回值】
  896. ****************************************************************/
  897. bool CFsProxy::leaveWord(CChanTrunk* pChan, LPCTSTR FileName, UINT LimitTime, TCHAR FinishKey)
  898. {
  899. ASSERT(pChan != NULL);
  900. CString Param;
  901. Param.Format(_T("playback_terminators=%c"), FinishKey);
  902. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  903. Param.Format(_T("%s %lu"), FileName, LimitTime);
  904. return m_Gateway.Execte2(ESL_APP_LEAVEWORD, Param, pChan->chanId());
  905. }
  906. /*****************************************************************
  907. **【函数名称】 playAndDtmf
  908. **【函数功能】 放音收按键
  909. **【参数】
  910. **【返回值】
  911. ****************************************************************/
  912. bool CFsProxy::playAndDtmf(CChanTrunk* pChan, PlayVoiceContent* pPlayContent)
  913. {
  914. ASSERT(pChan != NULL);
  915. ASSERT(pPlayContent != NULL);
  916. if (pChan == NULL || pPlayContent == NULL) return false;
  917. TCHAR AudioFile[MAX_PATH] = { 0 };
  918. // 如果使用了TTS,进行TTS转换
  919. switch (pPlayContent->nTts)
  920. {
  921. default:
  922. case PLAY_CONTENT_AUDIO: // 不使用TTS
  923. lstrcpy(AudioFile, pPlayContent->szFileName);
  924. break;
  925. case PLAY_CONTENT_TTS_STR: // TTS文本
  926. {
  927. ITtsInterface::getInstance().setTTSParam(pPlayContent->nTtsDigitMode, pPlayContent->nTtsSpeed, pPlayContent->nTtsVolume);
  928. if (!ITtsInterface::getInstance().string2Audio(pPlayContent->szFileName, AudioFile, MAX_PATH))
  929. {
  930. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败"), pChan->no());
  931. return false;
  932. }
  933. }
  934. break;
  935. case PLAY_CONTENT_TTS_FILE: // TTS文件
  936. {
  937. ITtsInterface::getInstance().setTTSParam(pPlayContent->nTtsDigitMode, pPlayContent->nTtsSpeed, pPlayContent->nTtsVolume);
  938. if (!ITtsInterface::getInstance().file2Audio(pPlayContent->szFileName, AudioFile, MAX_PATH))
  939. {
  940. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 中继通道[%lu]放音时TTS文件转换失败, File = %s"), pChan->no(), pPlayContent->szFileName);
  941. return false;
  942. }
  943. }
  944. break;
  945. }
  946. CString Param;
  947. if (pPlayContent->nModel == PVM_PLAY_ONLY && pPlayContent->cDtmfEnd == 0)
  948. {
  949. Param.Format(_T("playback_terminators=none"));
  950. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  951. //m_Gateway.execute(pChan->m_pEslHandle,ESL_APP_SET,Param);
  952. Param.Format(_T("%s"), AudioFile);
  953. return m_Gateway.Execte2(ESL_APP_PLAY, Param, pChan->chanId());
  954. //return m_Gateway.execute(pChan->m_pEslHandle,ESL_APP_PLAY,Param);
  955. }
  956. else
  957. {
  958. Param.Format(_T("%lu %lu 1 %lu %c %s silence_stream://250 %s"), pPlayContent->nDtmfCount, pPlayContent->nDtmfCount,
  959. pPlayContent->nDtmfCount * pPlayContent->nDtmfPeriod * 1000, pPlayContent->cDtmfEnd, AudioFile, ESL_VAR_DTMF_KEY);
  960. return m_Gateway.Execte2(ESL_APP_PLAY_DTMF, Param, pChan->chanId());
  961. //return m_Gateway.execute(pChan->m_pEslHandle,ESL_APP_PLAY_DTMF,Param);
  962. }
  963. }
  964. /*****************************************************************
  965. **【函数名称】 bridge
  966. **【函数功能】 桥接通道
  967. **【参数】
  968. **【返回值】
  969. ****************************************************************/
  970. bool CFsProxy::bridge(CChanTrunk* pChan, CString CallerNum, CString CalleeNum)
  971. {
  972. CString CallString;
  973. if (!m_CallStringMaker.makeCallString(CallerNum, CalleeNum, CallString))
  974. return false;
  975. CString Param;
  976. Param.Format(_T("ringback=${us-ring}"));
  977. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  978. return m_Gateway.Execte2(ESL_APP_BRIDGE, CallString, pChan->chanId());
  979. }
  980. /*****************************************************************
  981. **【函数名称】 bridge
  982. **【函数功能】 桥接通道 动态网关方法
  983. **【参数】
  984. **【返回值】
  985. ****************************************************************/
  986. bool CFsProxy::bridge(CChanTrunk* pChan, CString CalleeNum)
  987. {
  988. CString EslCmd;
  989. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s"), pChan->chanId(), CalleeNum, "ForExten");
  990. return m_Gateway.sendCmd(EslCmd);
  991. }