intercomm.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #include "intercomm.h"
  2. #include <QMutex>
  3. #include <QWaitCondition>
  4. #include <iostream>
  5. namespace iv {
  6. struct InterListenUnit
  7. {
  8. QMutex mWaitMutex;
  9. SMCallBack mpCall;
  10. ModuleFun mFun;
  11. QWaitCondition * mpwc;
  12. bool mbFunPlus = false;
  13. };
  14. struct interunit
  15. {
  16. char strintername[256];
  17. char * strdatabuf;
  18. int nbufsize = 0;
  19. int nPacCount;
  20. QMutex mMutexUnit;
  21. std::vector<InterListenUnit *> mvectorlisten;
  22. QWaitCondition mwc;
  23. bool mbHaveWriter = false;
  24. };
  25. std::vector<interunit *> gvectorinter;
  26. QMutex gMutexInter;
  27. static interunit * FindInterUnitByName(const char * strname)
  28. {
  29. interunit * p = 0;
  30. int i;
  31. int nsize;
  32. nsize = gvectorinter.size();
  33. for(i=0;i<nsize;i++)
  34. {
  35. if(strncmp(strname,gvectorinter.at(i)->strintername,256) == 0)
  36. {
  37. return (gvectorinter.at(i));
  38. }
  39. }
  40. return p;
  41. }
  42. intercomm::intercomm(const char * strsmname,const unsigned int nBufSize,const unsigned int nMaxPacCount,const int nMode)
  43. {
  44. strncpy(mstrsmname,strsmname,256);
  45. mnMode = nMode;
  46. if(nMode == ModeWrite)
  47. {
  48. gMutexInter.lock();
  49. interunit * p = FindInterUnitByName(strsmname);
  50. if(p == 0)
  51. {
  52. interunit * pnewinter = new interunit;
  53. strncpy(pnewinter->strintername,strsmname,256);
  54. pnewinter->strdatabuf = new char[sizeof(procinter_info)+nMaxPacCount*sizeof(procinter_head) + nBufSize];
  55. pnewinter->nPacCount = nMaxPacCount;
  56. pnewinter->nbufsize = nBufSize;
  57. gvectorinter.push_back(pnewinter);
  58. mpa = pnewinter;
  59. }
  60. else
  61. {
  62. p->mMutexUnit.lock();
  63. delete p->strdatabuf;
  64. p->strdatabuf = new char[sizeof(procinter_info)+nMaxPacCount*sizeof(procinter_head) + nBufSize];
  65. p->nPacCount = nMaxPacCount;
  66. p->nbufsize = nBufSize;
  67. p->mMutexUnit.unlock();
  68. mpa = p;
  69. }
  70. interunit * pinter = (interunit * )mpa;
  71. char * pdata = (char *)pinter->strdatabuf;
  72. mpinfo = (procinter_info *)pdata;
  73. mphead = (procinter_head *)(pdata+sizeof(procinter_info));
  74. mpinfo->mCap = nMaxPacCount;
  75. mpinfo->mnBufSize = nBufSize;
  76. mpinfo->mFirst = 0;
  77. mpinfo->mNext = 0;
  78. mpinfo->mLock = 0;
  79. pinter->mbHaveWriter = true;
  80. gMutexInter.unlock();
  81. }
  82. else
  83. {
  84. gMutexInter.lock();
  85. interunit * p = FindInterUnitByName(strsmname);
  86. if(p == 0)
  87. {
  88. interunit * pnewinter = new interunit;
  89. strncpy(pnewinter->strintername,strsmname,256);
  90. gvectorinter.push_back(pnewinter);
  91. mpa = pnewinter;
  92. }
  93. else
  94. {
  95. mpa = p;
  96. }
  97. interunit * pinter = (interunit * )mpa;
  98. char * pdata = (char *)pinter->strdatabuf;
  99. mpinfo = (procinter_info *)pdata;
  100. mphead = (procinter_head *)(pdata+sizeof(procinter_info));
  101. gMutexInter.unlock();
  102. }
  103. }
  104. intercomm::~intercomm()
  105. {
  106. if(mnMode == ModeRead)
  107. {
  108. stoplisten();
  109. interunit * p = (interunit *)mpa;
  110. p->mMutexUnit.lock();
  111. InterListenUnit * plisten = (InterListenUnit *)mplistenunit;
  112. int i;
  113. for(i=0;i<p->mvectorlisten.size();i++)
  114. {
  115. if(plisten == p->mvectorlisten.at(i))
  116. {
  117. p->mvectorlisten.erase(p->mvectorlisten.begin() + i);
  118. delete plisten;
  119. break;
  120. }
  121. }
  122. p->mMutexUnit.unlock();
  123. }
  124. }
  125. int intercomm::listenmsg(ModuleFun xFun)
  126. {
  127. if(mnMode == ModeWrite)
  128. {
  129. std::cout<<"intercomm::listenmsg this is Write. Can't Listen."<<std::endl;
  130. return - 1;
  131. }
  132. interunit * p = (interunit *)mpa;
  133. p->mMutexUnit.lock();
  134. InterListenUnit * pnewlisten = new InterListenUnit;
  135. pnewlisten->mbFunPlus = true;
  136. pnewlisten->mFun = xFun;
  137. pnewlisten->mpwc = &p->mwc;
  138. p->mvectorlisten.push_back(pnewlisten);
  139. p->mMutexUnit.unlock();
  140. mplistenunit = (void *)pnewlisten;
  141. mplistenthread = new std::thread(&intercomm::listernrun,this);
  142. return 0;
  143. }
  144. int intercomm::listenmsg(SMCallBack pCall)
  145. {
  146. if(mnMode == ModeWrite)
  147. {
  148. std::cout<<"intercomm::listenmsg this is Write. Can't Listen."<<std::endl;
  149. return - 1;
  150. }
  151. interunit * p = (interunit *)mpa;
  152. p->mMutexUnit.lock();
  153. InterListenUnit * pnewlisten = new InterListenUnit;
  154. pnewlisten->mbFunPlus = false;
  155. pnewlisten->mpCall = pCall;
  156. pnewlisten->mpwc = &p->mwc;
  157. p->mvectorlisten.push_back(pnewlisten);
  158. p->mMutexUnit.unlock();
  159. mplistenunit = (void *)pnewlisten;
  160. mplistenthread = new std::thread(&intercomm::listernrun,this);
  161. return 0;
  162. }
  163. void intercomm::stoplisten()
  164. {
  165. mblistenrun = false;
  166. if(mplistenthread != 0)
  167. {
  168. mplistenthread->join();
  169. mplistenthread = 0;
  170. }
  171. }
  172. void intercomm::pausecomm()
  173. {
  174. mbPause = true;
  175. }
  176. void intercomm::continuecomm()
  177. {
  178. mbPause = false;
  179. }
  180. void intercomm::listernrun()
  181. {
  182. InterListenUnit * pILU = (InterListenUnit * )mplistenunit;
  183. QTime xTime;
  184. xTime.start();
  185. unsigned int nBufLen = 1;
  186. unsigned int nRead;
  187. char * str = new char[nBufLen];
  188. unsigned int index =0;
  189. QDateTime *pdt = new QDateTime();
  190. interunit * pinter = (interunit *)mpa;
  191. while(mblistenrun)
  192. {
  193. if(mbPause)
  194. {
  195. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  196. continue;
  197. }
  198. if(pinter->mbHaveWriter == false)
  199. {
  200. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  201. continue;
  202. }
  203. pILU->mWaitMutex.lock();
  204. pILU->mpwc->wait(&pILU->mWaitMutex,100);
  205. pILU->mWaitMutex.unlock();
  206. int nRtn = readmsg(index,str,nBufLen,&nRead,pdt);
  207. while ((nRtn != 0)&&(mblistenrun))
  208. {
  209. if(nRtn == -1)
  210. {
  211. nBufLen = nRead;
  212. delete str;
  213. if(nBufLen < 1)nBufLen = 1;
  214. str = new char[nBufLen];
  215. }
  216. else
  217. {
  218. if(nRtn == -2)
  219. {
  220. index = getcurrentnext();
  221. }
  222. else
  223. {
  224. if(nRtn >0)
  225. {
  226. if(pILU->mbFunPlus)
  227. {
  228. pILU->mFun(str,nRtn,index,pdt,mstrsmname);
  229. }
  230. else
  231. {
  232. (*pILU->mpCall)(str,nRtn,index,pdt,mstrsmname);
  233. }
  234. index++;
  235. }
  236. else
  237. {
  238. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  239. }
  240. }
  241. }
  242. nRtn = readmsg(index,str,nBufLen,&nRead,pdt);
  243. }
  244. }
  245. delete str;
  246. delete pdt;
  247. }
  248. int intercomm::writemsg(const char *str, const unsigned int nSize)
  249. {
  250. if(mbPause)return -2;
  251. if(mnMode == ModeRead)
  252. {
  253. std::cout<<"Register read. can't write."<<std::endl;
  254. return -1;
  255. }
  256. interunit * pinter = (interunit *)mpa;
  257. pinter->mMutexUnit.lock();
  258. if(nSize > pinter->nbufsize)
  259. {
  260. // qDebug("procsm::writemsg message size is very big");
  261. int nnewsize = std::max(nSize*11/10,nSize+100);
  262. char * strdatabuf = new char[sizeof(procinter_info)+ pinter->nPacCount*sizeof(procinter_head) + nnewsize];
  263. memcpy(strdatabuf,pinter->strdatabuf,sizeof(procinter_info)+ pinter->nPacCount*sizeof(procinter_head) + pinter->nbufsize);
  264. pinter->nbufsize = nnewsize;
  265. delete pinter->strdatabuf;
  266. pinter->strdatabuf = strdatabuf;
  267. qDebug("inter resize buf,new buffer size is %d",nnewsize);
  268. }
  269. char * pdata = (char *)pinter->strdatabuf;
  270. mpinfo = (procinter_info *)pdata;
  271. mphead = (procinter_head *)(pdata+sizeof(procinter_info));
  272. WRITEMSG:
  273. char * pH,*pD;
  274. QDateTime dt;
  275. pH = (char *)pinter->strdatabuf;pH = pH + sizeof(procinter_info);
  276. pD = (char *)pinter->strdatabuf;pD = pD + sizeof(procinter_info) + pinter->nPacCount * sizeof(procinter_head);
  277. procinter_head * phh = (procinter_head *)pH;
  278. unsigned int nPac = mpinfo->mNext - mpinfo->mFirst;
  279. if(nPac>=pinter->nPacCount)
  280. {
  281. unsigned int nRemove = pinter->nPacCount/3;
  282. if(nRemove == 0)nRemove = 1;
  283. MoveMem(nRemove);
  284. goto WRITEMSG;
  285. }
  286. if(nPac == 0)
  287. {
  288. memcpy(pD,str,nSize);
  289. dt = QDateTime::currentDateTime();
  290. phh->SetDate(dt);
  291. phh->mindex = mpinfo->mNext;
  292. phh->mnPos = 0;
  293. phh->mnLen = nSize;
  294. mpinfo->mNext = mpinfo->mNext+1;
  295. }
  296. else
  297. {
  298. if(((phh+nPac-1)->mnPos+(phh+nPac-1)->mnLen + nSize)>=pinter->nbufsize)
  299. {
  300. unsigned int nRemove = pinter->nPacCount/2;
  301. if(nRemove == 0)nRemove = 1;
  302. MoveMem(nRemove);
  303. goto WRITEMSG;
  304. }
  305. else
  306. {
  307. unsigned int nPos = (phh+nPac-1)->mnPos + (phh+nPac-1)->mnLen;
  308. memcpy(pD+nPos,str,nSize);
  309. dt = QDateTime::currentDateTime();
  310. (phh+nPac)->SetDate(dt);
  311. (phh+nPac)->mindex = mpinfo->mNext;
  312. (phh+nPac)->mnPos = nPos;
  313. (phh+nPac)->mnLen = nSize;
  314. mpinfo->mNext = mpinfo->mNext+1;
  315. }
  316. }
  317. const unsigned int nTM = 0x6fffffff;
  318. if((mpinfo->mNext >nTM)&&(mpinfo->mFirst>nTM))
  319. {
  320. nPac = mpinfo->mNext - mpinfo->mFirst;
  321. unsigned int i;
  322. for(i=0;i<nPac;i++)
  323. {
  324. (phh+i)->mindex = (phh+i)->mindex-nTM;
  325. }
  326. mpinfo->mFirst = mpinfo->mFirst-nTM;
  327. mpinfo->mNext = mpinfo->mNext - nTM;
  328. }
  329. pinter->mMutexUnit.unlock();
  330. pinter->mwc.wakeAll();
  331. return 0;
  332. }
  333. int intercomm::MoveMem(const unsigned int nSize)
  334. {
  335. unsigned int nRemove = nSize;
  336. if(nRemove == 0)return -1;
  337. interunit * pinter = (interunit *)mpa;
  338. char * pH,*pD;
  339. pH = (char *)pinter->strdatabuf;pH = pH + sizeof(procinter_info);
  340. pD = (char *)pinter->strdatabuf;pD = pD + sizeof(procinter_info) + pinter->nPacCount * sizeof(procinter_head);
  341. procinter_head * phh = (procinter_head *)pH;
  342. unsigned int nPac = mpinfo->mNext - mpinfo->mFirst;
  343. if(nRemove >nPac)
  344. {
  345. nRemove = nPac;
  346. }
  347. if(nRemove == nPac)
  348. {
  349. mpinfo->mFirst = mpinfo->mFirst + (unsigned int)nRemove;
  350. return 0;
  351. }
  352. unsigned int i;
  353. int nDataMove = 0;
  354. for(i=0;i<nRemove;i++)
  355. {
  356. procinter_head * phd = phh+i;
  357. nDataMove = nDataMove + phd->mnLen;
  358. }
  359. unsigned int nDataTotal;
  360. for(i=0;i<(nPac - nRemove);i++)
  361. {
  362. memcpy(phh+i,phh+i+nRemove,sizeof(procinter_head));
  363. (phh+i)->mnPos = (phh+i)->mnPos - nDataMove;
  364. }
  365. nDataTotal = (phh + nPac-nRemove-1)->mnPos + (phh+nPac-nRemove-1)->mnLen;
  366. char * strtem = new char[pinter->nbufsize];
  367. memcpy(strtem,pD+nDataMove,nDataTotal);
  368. memcpy(pD,strtem,nDataTotal);
  369. delete strtem;
  370. mpinfo->mFirst = mpinfo->mFirst + (unsigned int)nRemove;
  371. return 0;
  372. }
  373. int intercomm::readmsg(unsigned int index, char *str, unsigned int nMaxSize, unsigned int *nRead, QDateTime *pdt)
  374. {
  375. int nRtn = 0;
  376. interunit * pinter = (interunit *)mpa;
  377. if(pinter->nbufsize == 0)return 0;
  378. pinter->mMutexUnit.lock();
  379. char * pdata = (char *)pinter->strdatabuf;
  380. mpinfo = (procinter_info *)pdata;
  381. mphead = (procinter_head *)(pdata+sizeof(procinter_info));
  382. if((index< mpinfo->mFirst)||(index > mpinfo->mNext))
  383. {
  384. nRtn = -2;
  385. }
  386. if(nRtn != (-2))
  387. {
  388. if(index == mpinfo->mNext)
  389. {
  390. nRtn = 0;
  391. }
  392. else
  393. {
  394. char * pH,*pD;
  395. // pH = (char *)mpASM->data();pH = pH + 2*sizeof(unsigned int);
  396. // pD = (char *)mpASM->data();pD = pD + 2*sizeof(unsigned int) + mnMaxPacCount * sizeof(procsm_head);
  397. pD = (char *)pinter->strdatabuf;pD = pD+ sizeof(procinter_info) + mpinfo->mCap*sizeof(procinter_head);
  398. pH = (char *)pinter->strdatabuf;pH = pH+sizeof(procinter_info);
  399. procinter_head * phh = (procinter_head *)pH;
  400. unsigned int nPac = mpinfo->mNext - mpinfo->mFirst;
  401. if(nPac == 0)
  402. {
  403. nRtn = 0;
  404. }
  405. else
  406. {
  407. unsigned int nPos = index - mpinfo->mFirst;
  408. *nRead = (phh+nPos)->mnLen;
  409. if((phh+nPos)->mnLen > nMaxSize)
  410. {
  411. nRtn = -1;
  412. }
  413. else
  414. {
  415. // qDebug("read pos = %d",(phh+nPos)->mnPos);
  416. memcpy(str,pD + (phh+nPos)->mnPos,(phh+nPos)->mnLen);
  417. // qDebug("read pos = %d",(phh+nPos)->mnPos);
  418. nRtn = (phh+nPos)->mnLen;
  419. (phh+nPos)->GetDate(pdt);
  420. // memcpy(pdt,&((phh+nPos)->mdt),sizeof(QDateTime));
  421. }
  422. }
  423. }
  424. }
  425. pinter->mMutexUnit.unlock();
  426. return nRtn;
  427. }
  428. unsigned int intercomm::getcurrentnext()
  429. {
  430. unsigned int nNext;
  431. interunit * pinter = (interunit *)mpa;
  432. char * pdata = (char *)pinter->strdatabuf;
  433. mpinfo = (procinter_info *)pdata;
  434. mphead = (procinter_head *)(pdata+sizeof(procinter_info));
  435. nNext = mpinfo->mNext;
  436. return nNext;
  437. }
  438. }