libcm is a C development framework with an emphasis on audio signal processing applications.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

cmRtNet.c 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. #include "cmGlobal.h"
  2. #include "cmRpt.h"
  3. #include "cmErr.h"
  4. #include "cmCtx.h"
  5. #include "cmMem.h"
  6. #include "cmMallocDebug.h"
  7. #include "cmLinkedHeap.h"
  8. #include "cmUdpPort.h"
  9. #include "cmRtSysMsg.h"
  10. #include "cmRtNet.h"
  11. #include "cmTime.h"
  12. #include "cmText.h"
  13. // flags for cmRtNetNode_t.flags;
  14. enum
  15. {
  16. kLocalNodeNetFl = 0x01,
  17. kValidNodeNetFl = 0x02
  18. };
  19. // flags for cmRtNet_t.flags
  20. enum
  21. {
  22. kReportSyncNetFl = 0x01
  23. };
  24. struct cmRtNetNode_str;
  25. typedef struct cmRtNetEnd_str
  26. {
  27. cmChar_t* label;
  28. unsigned id;
  29. struct cmRtNetNode_str* np; // Owner node.
  30. struct cmRtNetEnd_str* link;
  31. } cmRtNetEnd_t;
  32. struct cmRtNet_str;
  33. typedef struct cmRtNetNode_str
  34. {
  35. unsigned rtSubIdx; // rtSubIdx of the sub-system which owns this node
  36. cmChar_t* label; // Node label.
  37. struct sockaddr_in sockaddr; // Socket address
  38. cmChar_t* addr; // IP Address (human readable)
  39. cmUdpPort_t port; // Socket port
  40. unsigned flags; // See kXXXNodeNetFl flags above.
  41. unsigned endPtIdx; // tracks the next endpoint to send during sync-mode
  42. unsigned endPtCnt; // local-node=actual cnt of endpt's remote-node:expected cnt of endpt's
  43. cmTimeSpec_t lastSendTime; // Time of last message sent
  44. cmRtNetEnd_t* ends; // End point list for this node
  45. struct cmRtNetNode_str* link;
  46. } cmRtNetNode_t;
  47. typedef struct cmRtNet_str
  48. {
  49. cmErr_t err; // Error state object
  50. unsigned flags; // See kXXXNetFl above.
  51. unsigned rtSubIdx; // rtSubIdx of the owning sub-system
  52. cmUdpH_t udpH; // UDP port handle
  53. cmUdpCallback_t cbFunc; // Client callback to receive incoming messages from network.
  54. void* cbArg; //
  55. cmRtNetNode_t* nodes; // Node list.
  56. cmRtNetNode_t* localNode; // Pointer to local node (which is also in node list)
  57. unsigned udpRecvBufByteCnt; // UDP port receive buffer size.
  58. unsigned udpTimeOutMs; // UDP time-out period
  59. cmChar_t* bcastAddr; // Network broadcast address
  60. } cmRtNet_t;
  61. cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
  62. cmRtNetEndptH_t cmRtNetEndptNullHandle = cmSTATIC_NULL_HANDLE;
  63. cmRtNet_t* _cmRtNetHandleToPtr( cmRtNetH_t h )
  64. {
  65. cmRtNet_t* p = (cmRtNet_t*)h.h;
  66. assert( p != NULL );
  67. return p;
  68. }
  69. void _cmRtNetVRpt( cmRtNet_t* p, const cmChar_t* fmt, va_list vl )
  70. {
  71. if( cmIsFlag(p->flags,kReportSyncNetFl) )
  72. cmRptVPrintf(p->err.rpt,fmt,vl);
  73. }
  74. void _cmRtNetRpt( cmRtNet_t* p, const cmChar_t* fmt, ... )
  75. {
  76. va_list vl;
  77. va_start(vl,fmt);
  78. _cmRtNetVRpt(p,fmt,vl);
  79. va_end(vl);
  80. }
  81. cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label )
  82. {
  83. if( label == NULL )
  84. return NULL;
  85. cmRtNetNode_t* np = p->nodes;
  86. for(; np!=NULL; np=np->link)
  87. if( strcmp(label,np->label)==0)
  88. return np;
  89. return NULL;
  90. }
  91. cmRtNetNode_t* _cmRtNetFindNodeFromSockAddr( cmRtNet_t* p, const struct sockaddr_in* saddr )
  92. {
  93. if( saddr == NULL )
  94. return NULL;
  95. cmRtNetNode_t* np = p->nodes;
  96. for(; np!=NULL; np=np->link)
  97. if( np->sockaddr.sin_addr.s_addr == saddr->sin_addr.s_addr && np->sockaddr.sin_port == saddr->sin_port )
  98. return np;
  99. return NULL;
  100. }
  101. void _cmRtNetFreeNode( cmRtNetNode_t* np )
  102. {
  103. cmRtNetEnd_t* ep = np->ends;
  104. while( ep != NULL )
  105. {
  106. cmRtNetEnd_t* nep = ep->link;
  107. cmMemFree(ep->label);
  108. cmMemFree(ep);
  109. ep = nep;
  110. }
  111. cmMemFree(np->label);
  112. cmMemFree(np->addr);
  113. cmMemFree(np);
  114. }
  115. void _cmRtNetReleaseNodes( cmRtNet_t* p )
  116. {
  117. cmRtNetNode_t* np = p->nodes;
  118. while( np != NULL )
  119. {
  120. cmRtNetNode_t* nnp = np->link;
  121. _cmRtNetFreeNode(np);
  122. np = nnp;
  123. }
  124. p->nodes = NULL;
  125. p->localNode = NULL;
  126. }
  127. cmRtNetRC_t _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
  128. {
  129. cmRtNetNode_t* cnp = p->nodes;
  130. cmRtNetNode_t* pnp = NULL;
  131. while( cnp != NULL )
  132. {
  133. cmRtNetNode_t* nnp = cnp->link;
  134. if( np == cnp )
  135. {
  136. if( pnp == NULL )
  137. p->nodes = np->link;
  138. else
  139. pnp->link = np->link;
  140. _cmRtNetFreeNode(np);
  141. return kOkNetRC;
  142. }
  143. pnp = np;
  144. cnp = nnp;
  145. }
  146. assert(0);
  147. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"Node to release not found.");
  148. }
  149. cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, unsigned rtSubIdx, const cmChar_t* addr, cmUdpPort_t port, const struct sockaddr_in* saddr, unsigned flags, unsigned endPtCnt )
  150. {
  151. cmRtNetRC_t rc = kOkNetRC;
  152. cmRtNetNode_t* np;
  153. if( cmTextIsEmpty(label) )
  154. return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
  155. if((np = _cmRtNetFindNode(p,label)) != NULL )
  156. return cmErrMsg(&p->err,kDuplLabelNetRC,"The node label '%s' is already in use.",cmStringNullGuard(label));
  157. np = cmMemAllocZ(cmRtNetNode_t,1);
  158. np->label = cmMemAllocStr(label);
  159. if( saddr != NULL )
  160. np->sockaddr = *saddr;
  161. np->rtSubIdx = rtSubIdx;
  162. np->addr = addr==NULL ? NULL : cmMemAllocStr(addr);
  163. np->port = port;
  164. np->flags = flags;
  165. np->endPtCnt = endPtCnt;
  166. np->link = p->nodes;
  167. p->nodes = np;
  168. return rc;
  169. }
  170. cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, const cmChar_t* endPtLabel )
  171. {
  172. cmRtNetEnd_t* ep = np->ends;
  173. for(; ep!=NULL; ep=ep->link)
  174. if( strcmp(ep->label,endPtLabel)==0 )
  175. return ep;
  176. return NULL;
  177. }
  178. cmRtNetEnd_t* _cmRtNetIndexToEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned endIndex )
  179. {
  180. cmRtNetEnd_t* ep = np->ends;
  181. unsigned i;
  182. for(i=0; ep!=NULL; ep=ep->link)
  183. {
  184. if( i == endIndex )
  185. return ep;
  186. ++i;
  187. }
  188. return NULL;
  189. }
  190. cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmChar_t* endPtLabel, unsigned endPtId )
  191. {
  192. if( endPtLabel == NULL )
  193. return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
  194. if( _cmRtNetFindNodeEnd( np, endPtLabel) != NULL)
  195. return cmErrMsg(&p->err,kDuplEndNetRC,"A duplicate endpoint ('%s') was encountered on node '%s'.",endPtLabel,np->label);
  196. cmRtNetRC_t rc = kOkNetRC;
  197. cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1);
  198. ep->label = cmMemAllocStr(endPtLabel);
  199. ep->id = endPtId;
  200. ep->np = np;
  201. ep->link = np->ends;
  202. np->ends = ep;
  203. return rc;
  204. }
  205. unsigned _cmRtNetNodeEndpointCount( cmRtNetNode_t* np )
  206. {
  207. cmRtNetEnd_t* ep = np->ends;
  208. unsigned n = 0;
  209. for(; ep!=NULL; ep=ep->link)
  210. ++n;
  211. return n;
  212. }
  213. unsigned _cmRtNetSyncMsgSerialByteCount( const cmRtNetSyncMsg_t* m )
  214. { return sizeof(cmRtNetSyncMsg_t) + (m->label==NULL ? 1 : strlen(m->label) + 1); }
  215. cmRtNetRC_t _cmRtNetSerializeSyncMsg( cmRtNet_t* p, const cmRtNetSyncMsg_t* m, void* buf, unsigned n )
  216. {
  217. unsigned bn = _cmRtNetSyncMsgSerialByteCount(m);
  218. char* b = (char*)buf;
  219. if( bn > n )
  220. return cmErrMsg(&p->err,kBufToSmallNetRC,"Serialize buffer too small.");
  221. memcpy(b,m,sizeof(*m));
  222. strcpy(b + sizeof(*m),m->label==NULL ? "" : m->label);
  223. return kOkNetRC;
  224. }
  225. cmRtNetRC_t _cmRtNetDeserializeSyncMsg( const void* buf, unsigned n, cmRtNetSyncMsg_t* m )
  226. {
  227. assert( n > sizeof(*m));
  228. memcpy(m,buf,sizeof(*m));
  229. const cmRtNetSyncMsg_t* mp = (const cmRtNetSyncMsg_t*)buf;
  230. const cmChar_t* s = (const cmChar_t*)(mp+1);
  231. m->label = cmMemAllocStr(s);
  232. return kOkNetRC;
  233. }
  234. cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* msgLabel, unsigned msgId, unsigned msgRtSubIdx )
  235. {
  236. cmRtNetSyncMsg_t m;
  237. cmRtNetRC_t rc = kOkNetRC;
  238. cmUdpRC_t udpRC = kOkUdpRC;
  239. m.hdr.rtSubIdx = cmInvalidIdx;
  240. m.hdr.selId = kNetSyncSelRtId;
  241. m.selId = selId;
  242. m.label = msgLabel;
  243. m.id = msgId;
  244. m.rtSubIdx = msgRtSubIdx;
  245. // determine size of msg to send
  246. unsigned n = _cmRtNetSyncMsgSerialByteCount(&m);
  247. cmChar_t buf[n];
  248. // serialize msg into buf[]
  249. if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
  250. return rc;
  251. // send the msg
  252. if( selId == kHelloSelNetId )
  253. udpRC = cmUdpSend2(p->udpH, buf, n, p->bcastAddr, np->port );
  254. else
  255. udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
  256. // check for send errors
  257. if( udpRC != kOkUdpRC )
  258. {
  259. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"Sync msg. send on UDP port failed.");
  260. }
  261. else
  262. {
  263. // record the last send time
  264. cmTimeGet(&np->lastSendTime);
  265. }
  266. return rc;
  267. }
  268. cmRtNetRC_t _cmRtNetFree( cmRtNet_t* p )
  269. {
  270. cmRtNetRC_t rc = kOkNetRC;
  271. if( cmUdpFree(&p->udpH) != kOkUdpRC )
  272. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port free failed.");
  273. _cmRtNetReleaseNodes(p);
  274. cmMemFree(p->bcastAddr);
  275. cmMemFree(p);
  276. return rc;
  277. }
  278. const cmRtNetNode_t* _cmRtNetIndexToRemoteNode( cmRtNet_t* p, unsigned idx )
  279. {
  280. const cmRtNetNode_t* np = p->nodes;
  281. unsigned i = 0;
  282. for(; np!=NULL; np=np->link)
  283. if( np != p->localNode )
  284. {
  285. if( i == idx )
  286. return np;
  287. ++i;
  288. }
  289. return NULL;
  290. }
  291. const cmRtNetEnd_t* _cmRtNetIndexToEndpt( const cmRtNetNode_t* np, unsigned endIdx )
  292. {
  293. const cmRtNetEnd_t* ep = np->ends;
  294. unsigned i = 0;
  295. for(; ep!=NULL; ep=ep->link,++i)
  296. if( i==endIdx )
  297. return ep;
  298. return NULL;
  299. }
  300. const cmRtNetEnd_t* _cmRtNetFindEndpt( cmRtNet_t* p, unsigned nodeIdx, unsigned epIdx )
  301. {
  302. const cmRtNetNode_t* np;
  303. const cmRtNetEnd_t* ep;
  304. if((np = _cmRtNetIndexToRemoteNode( p, nodeIdx )) == NULL )
  305. return NULL;
  306. if((ep = _cmRtNetIndexToEndpt( np, epIdx )) == NULL )
  307. return NULL;
  308. return ep;
  309. }
  310. const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m )
  311. {
  312. if( m->selId==kNodeSelNetId || m->selId==kEndpointSelNetId )
  313. return (const cmChar_t*)(m+1);
  314. return "";
  315. }
  316. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg )
  317. {
  318. cmRtNetRC_t rc;
  319. if((rc = cmRtNetFree(hp)) != kOkNetRC )
  320. return rc;
  321. cmRtNet_t* p = cmMemAllocZ(cmRtNet_t,1);
  322. cmErrSetup(&p->err,&ctx->rpt,"cmRtNet");
  323. // allocate the UDP port
  324. if(cmUdpAlloc(ctx,&p->udpH) != kOkUdpRC )
  325. {
  326. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port allocate failed.");
  327. goto errLabel;
  328. }
  329. p->rtSubIdx = rtSubIdx;
  330. p->udpTimeOutMs = 50;
  331. p->udpRecvBufByteCnt = 8192;
  332. p->cbFunc = cbFunc;
  333. p->cbArg = cbArg;
  334. hp->h = p;
  335. errLabel:
  336. if(rc != kOkNetRC )
  337. _cmRtNetFree(p);
  338. return rc;
  339. }
  340. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp )
  341. {
  342. cmRtNetRC_t rc = kOkNetRC;
  343. if( hp==NULL || cmRtNetIsValid(*hp)==false )
  344. return rc;
  345. cmRtNet_t* p = _cmRtNetHandleToPtr(*hp);
  346. if((rc = _cmRtNetFree(p)) != kOkNetRC )
  347. return rc;
  348. hp->h = NULL;
  349. return rc;
  350. }
  351. const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h )
  352. {
  353. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  354. return cmUdpHostName(p->udpH);
  355. }
  356. bool cmRtNetIsValid( cmRtNetH_t h )
  357. { return h.h !=NULL; }
  358. cmUdpH_t cmRtNetUdpPortHandle( cmRtNetH_t h )
  359. {
  360. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  361. return p->udpH;
  362. }
  363. cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t srcSelId )
  364. {
  365. cmRtNetRC_t rc = kOkNetRC;
  366. cmRtNetEnd_t* ep = NULL;
  367. const cmChar_t* msgLabel = NULL;
  368. unsigned msgId = cmInvalidId;
  369. unsigned msgRtSubIdx = cmInvalidIdx;
  370. cmRtNetSelId_t selId = kEndpointSelNetId;
  371. const cmChar_t* rptLabel = "endpoint";
  372. if( np == NULL )
  373. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found.");
  374. // if we got here by receiving a 'done' msg from the remote node ...
  375. if( srcSelId == kDoneSelNetId )
  376. {
  377. // ... then mark the remote node as having recieved all endpoints
  378. unsigned n;
  379. if((n = _cmRtNetNodeEndpointCount(np)) != np->endPtCnt )
  380. rc = cmErrMsg(&p->err,kNodeEndCntErrNetRC,"The node '%s' expected %i endpoints but received %i.",cmStringNullGuard(np->label),np->endPtCnt,n);
  381. else
  382. np->flags = cmSetFlag(np->flags,kValidNodeNetFl);
  383. }
  384. // attempt to get the next local endpoint to send ...
  385. if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) != NULL )
  386. {
  387. msgLabel = ep->label; // ... send next local endpoint
  388. msgId = ep->id;
  389. msgRtSubIdx = ep->np->rtSubIdx;
  390. }
  391. else // .... all local endpoints have been sent
  392. {
  393. selId = kInvalidSelNetId;
  394. rptLabel = "done";
  395. // verify that no endpoints are available
  396. if( np->endPtIdx < p->localNode->endPtCnt )
  397. rc = cmErrMsg(&p->err,kSyncFailNetRC,"More endpoints are available to send but are not reachable.");
  398. else
  399. {
  400. // if the remote node still has endpts to send then continue
  401. // sending 'done' messages.
  402. if( np->endPtIdx==p->localNode->endPtCnt || srcSelId != kDoneSelNetId )
  403. selId = kDoneSelNetId;
  404. }
  405. }
  406. // selId is set to kInvalidSelNetId when we encounter the (stop) criteria
  407. if( selId != kInvalidSelNetId )
  408. {
  409. if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId,msgRtSubIdx)) != kOkNetRC )
  410. rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
  411. else
  412. _cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel));
  413. np->endPtIdx += 1;
  414. }
  415. return rc;
  416. }
  417. bool _cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt )
  418. {
  419. cmRtNetSyncMsg_t* m = (cmRtNetSyncMsg_t*)data;
  420. return dataByteCnt >= sizeof(cmRtNetSyncMsg_t) && m->hdr.selId == kNetSyncSelRtId;
  421. }
  422. // When the network message recieve function (See cmRtNetAlloc() 'cbFunc')
  423. // receives a message with the cmRtSysMsgHdr_t.selId == kNetSyncSelRtId
  424. // it should call this function to update the current sync state of the
  425. // cmRtNet.
  426. cmRtNetRC_t _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  427. {
  428. cmRtNetRC_t rc = kOkNetRC;
  429. cmRtNetSyncMsg_t m;
  430. m.label = NULL;
  431. assert( _cmRtNetIsSyncModeMsg(data,dataByteCnt));
  432. if( _cmRtNetDeserializeSyncMsg(data,dataByteCnt,&m) != kOkNetRC )
  433. {
  434. rc = cmErrMsg(&p->err,rc,"Net sync. receive failed due to deserialize fail.");
  435. goto errLabel;
  436. }
  437. _cmRtNetRpt(p,"recv from:%s\n",cmUdpAddrToString(p->udpH, fromAddr ));
  438. assert( m.hdr.selId == kNetSyncSelRtId );
  439. // attempt to locate the remote node which sent the msg
  440. cmRtNetNode_t* np = _cmRtNetFindNodeFromSockAddr(p,fromAddr);
  441. switch( m.selId )
  442. {
  443. case kHelloSelNetId:
  444. // if this is a response to a broadcast from the local node then ignore it
  445. if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label)) != NULL && strcmp(p->localNode->label,m.label)==0 )
  446. {
  447. const cmChar_t* fromAddrStr = cmUdpAddrToString(p->udpH,fromAddr);
  448. const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH));
  449. if( fromAddrStr!=NULL && localAddrStr!=NULL && strcmp(fromAddrStr,localAddrStr)!=0)
  450. cmErrMsg(&p->err,kDuplLocalNetRC,"The node label '%s' appears to be duplicated at address %s and locally.",cmStringNullGuard(m.label),fromAddrStr);
  451. np->sockaddr = *fromAddr;
  452. goto errLabel;
  453. }
  454. // fall through
  455. case kNodeSelNetId:
  456. {
  457. // if the node already exists ...
  458. if( np != NULL )
  459. {
  460. // ... delete it because we are about to get new info. about it.
  461. if((rc = _cmRtNetReleaseNode(p,np )) != kOkNetRC )
  462. goto errLabel;
  463. }
  464. // create a node proxy to represent the remote node
  465. // (Note:m.id == remote node endpoint count (i.e. the count of endpoints expected for the remote node.))
  466. if(( rc = _cmRtNetCreateNode(p,m.label,m.rtSubIdx,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
  467. goto errLabel;
  468. np = p->nodes; // newest node is always the first node
  469. // send response
  470. switch( m.selId )
  471. {
  472. case kHelloSelNetId:
  473. _cmRtNetRpt(p,"rcv hello\n"); // reply with local node
  474. rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt, p->localNode->rtSubIdx );
  475. break;
  476. case kNodeSelNetId:
  477. _cmRtNetRpt(p,"rcv node\n");
  478. _cmRtNetSendEndpointReplyMsg( p, np, m.selId ); // reply with first endpoint
  479. break;
  480. default:
  481. assert(0);
  482. }
  483. }
  484. break;
  485. case kDoneSelNetId:
  486. //case kEndpointAckSelNetId:
  487. rc = _cmRtNetSendEndpointReplyMsg(p,np,m.selId);
  488. break;
  489. case kEndpointSelNetId:
  490. {
  491. cmRtNetEnd_t* ep;
  492. // verify the remote node exists.
  493. if( np == NULL )
  494. {
  495. rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
  496. goto errLabel;
  497. }
  498. // attempt to find the end point
  499. if((ep = _cmRtNetFindNodeEnd(np, m.label)) != NULL )
  500. ep->id = m.id; // the endpoint was found update the endPtId
  501. else
  502. {
  503. // create a local proxy for the endpoint
  504. if((rc = _cmRtNetCreateEndpoint(p,np,m.label,m.id)) != kOkNetRC )
  505. goto errLabel;
  506. }
  507. // reply with a local endpoint or 'done' msg
  508. rc = _cmRtNetSendEndpointReplyMsg( p, np, m.selId );
  509. }
  510. break;
  511. default:
  512. assert(0);
  513. break;
  514. }
  515. errLabel:
  516. cmMemFree((cmChar_t*)m.label);
  517. return rc;
  518. }
  519. // This is called in the context of cmRtNetReceive().
  520. void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  521. {
  522. cmRtNet_t* p = (cmRtNet_t*)cbArg;
  523. if( _cmRtNetIsSyncModeMsg(data,dataByteCnt))
  524. _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
  525. p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
  526. }
  527. cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
  528. {
  529. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  530. cmRtNetRC_t rc;
  531. // release the local node
  532. if((rc = cmRtNetFinalize(h)) != kOkNetRC )
  533. goto errLabel;
  534. if( cmTextIsEmpty(bcastAddr) )
  535. {
  536. rc = cmErrMsg(&p->err,kInvalidArgNetRC,"The 'broadcast address' is not valid.");
  537. goto errLabel;
  538. }
  539. // if this is the local node then initialze the local socket
  540. if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl | kBroadcastUdpFl,_cmRtNetRecv,p,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
  541. {
  542. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port initialization failed.");
  543. goto errLabel;
  544. }
  545. // create the local node
  546. if((rc = _cmRtNetCreateNode(p,nodeLabel, p->rtSubIdx, ipAddr, port, NULL, kLocalNodeNetFl, 0)) != kOkNetRC )
  547. goto errLabel;
  548. // the last created node is always the first node on the list
  549. p->localNode = p->nodes;
  550. p->bcastAddr = cmMemResizeStr(p->bcastAddr,bcastAddr);
  551. // begin listening on the local port
  552. if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
  553. {
  554. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port failed to enter 'listen' mode.");
  555. goto errLabel;
  556. }
  557. errLabel:
  558. return rc;
  559. }
  560. bool cmRtNetIsInitialized( cmRtNetH_t h )
  561. {
  562. if( cmRtNetIsValid(h) == false )
  563. return false;
  564. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  565. return p->localNode != NULL && cmTextIsNotEmpty(p->bcastAddr);
  566. }
  567. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId )
  568. {
  569. cmRtNetRC_t rc = kOkNetRC;
  570. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  571. if( p->localNode == NULL )
  572. return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
  573. if((rc = _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId )) == kOkNetRC )
  574. p->localNode->endPtCnt += 1;
  575. return rc;
  576. }
  577. cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h )
  578. {
  579. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  580. _cmRtNetReleaseNodes(p);
  581. return kOkNetRC;
  582. }
  583. cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h )
  584. {
  585. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  586. // broadcast 'node' msg
  587. return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
  588. }
  589. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
  590. {
  591. cmRtNetRC_t rc = kOkNetRC;
  592. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  593. // Calling this function results in callbacks to _cmRtNetRecv() (above)
  594. if( cmUdpGetAvailData(p->udpH, NULL, NULL, NULL ) != kOkUdpRC )
  595. {
  596. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP port query failed.");
  597. goto errLabel;
  598. }
  599. errLabel:
  600. return rc;
  601. }
  602. cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
  603. {
  604. cmRtNetRC_t rc = kOkNetRC;
  605. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  606. cmRtNetNode_t* np;
  607. cmRtNetEnd_t* ep;
  608. if(( np = _cmRtNetFindNode(p,nodeLabel)) == NULL )
  609. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
  610. if(( ep = _cmRtNetFindNodeEnd(np, endptLabel )) == NULL )
  611. return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel));
  612. hp->h = ep;
  613. return rc;
  614. }
  615. cmRtNetRC_t _cmRtNetSend( cmRtNet_t* p, const cmRtNetEnd_t* ep, const void* msg, unsigned msgByteCnt )
  616. {
  617. cmRtNetRC_t rc = kOkNetRC;
  618. unsigned hN = sizeof(cmRtNetMsg_t);
  619. unsigned dN = hN + msgByteCnt;
  620. char data[ dN ];
  621. cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
  622. r->hdr.rtSubIdx = ep->np->rtSubIdx;
  623. r->hdr.selId = kMsgSelRtId;
  624. r->endptId = ep->id;
  625. memcpy(data+hN,msg,msgByteCnt);
  626. // ep->np->sockaddr identifies the node on the receiving cmRtNet.
  627. // cmRtNetMsg_t* r.endptId is then used by the receiving cmRtNet to indicate which endpoint on
  628. // the node the incoming message should be associated with.
  629. if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC )
  630. return cmErrMsg(&p->err,kUdpPortFailNetRC,"Send to node:%s endpt:%s failed.\n",cmStringNullGuard(ep->np->label),cmStringNullGuard(ep->label));
  631. return rc;
  632. }
  633. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt )
  634. {
  635. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  636. cmRtNetEnd_t* ep = (cmRtNetEnd_t*)epH.h;
  637. assert( ep != NULL );
  638. return _cmRtNetSend(p,ep,msg,msgByteCnt);
  639. }
  640. cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
  641. {
  642. cmRtNetRC_t rc = kOkNetRC;
  643. cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
  644. if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC )
  645. return rc;
  646. return cmRtNetSend(h,epH,msg,msgByteCnt);
  647. }
  648. cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt )
  649. {
  650. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  651. const cmRtNetEnd_t* ep;
  652. if((ep = _cmRtNetFindEndpt(p, nodeIdx, endptIdx )) == NULL )
  653. return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint at node index %i endpoint index %i was not found.",nodeIdx,endptIdx);
  654. return _cmRtNetSend( p, ep, msg, msgByteCnt );
  655. }
  656. bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl )
  657. {
  658. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  659. bool fl = cmIsFlag(p->flags,kReportSyncNetFl);
  660. p->flags = cmEnaFlag(p->flags,kReportSyncNetFl,enableFl);
  661. return fl;
  662. }
  663. bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h )
  664. {
  665. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  666. return cmIsFlag(p->flags,kReportSyncNetFl);
  667. }
  668. void cmRtNetReport( cmRtNetH_t h )
  669. {
  670. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  671. cmRpt_t* rpt = p->err.rpt;
  672. cmRtNetNode_t* np = p->nodes;
  673. for(; np!=NULL; np=np->link)
  674. {
  675. cmRptPrintf(rpt,"Node: %s ",np->label);
  676. if( np->addr != NULL )
  677. cmRptPrintf(rpt,"%s ",np->addr );
  678. if( cmIsFlag(np->flags,kLocalNodeNetFl) )
  679. cmRptPrintf(rpt,"LOCAL ");
  680. cmRptPrintf(rpt,"%s ",cmStringNullGuard(cmUdpAddrToString(p->udpH,&np->sockaddr)));
  681. if( np->port != kInvalidUdpPortNumber )
  682. cmRptPrintf(rpt,"%i ",np->port );
  683. cmRptPrintf(rpt,"\n");
  684. cmRtNetEnd_t* ep = np->ends;
  685. for(; ep!=NULL; ep=ep->link)
  686. {
  687. cmRptPrintf(rpt," endpt: %i %s\n",ep->id,cmStringNullGuard(ep->label ));
  688. }
  689. }
  690. }
  691. const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h )
  692. {
  693. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  694. return p->localNode->label;
  695. }
  696. unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h )
  697. {
  698. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  699. const cmRtNetNode_t* np = p->nodes;
  700. unsigned n = 0;
  701. for(; np!=NULL; np=np->link)
  702. if( np != p->localNode )
  703. ++n;
  704. return n;
  705. }
  706. const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx )
  707. {
  708. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  709. const cmRtNetNode_t* np;
  710. if((np = _cmRtNetIndexToRemoteNode( p, idx )) == NULL )
  711. return NULL;
  712. return np->label;
  713. }
  714. unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx )
  715. {
  716. const cmRtNetNode_t* np;
  717. const cmRtNetEnd_t* ep;
  718. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  719. unsigned n = 0;
  720. if((np = _cmRtNetIndexToRemoteNode( p, nodeIdx )) == NULL )
  721. return 0;
  722. for(ep=np->ends; ep!=NULL; ep=ep->link)
  723. ++n;
  724. return n;
  725. }
  726. cmRtNetRC_t cmRtNetRemoteNodeEndPoint(
  727. cmRtNetH_t h,
  728. unsigned nodeIdx,
  729. unsigned epIdx,
  730. const cmChar_t** labelRef,
  731. unsigned* idRef,
  732. unsigned* rsiRef )
  733. {
  734. const cmRtNetEnd_t* ep;
  735. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  736. if(( ep = _cmRtNetFindEndpt(p, nodeIdx, epIdx )) == NULL )
  737. {
  738. *labelRef = NULL;
  739. *idRef = cmInvalidId;
  740. *rsiRef = cmInvalidIdx;
  741. return kEndNotFoundNetRC;
  742. }
  743. *labelRef = ep->label;
  744. *idRef = ep->id;
  745. *rsiRef = ep->np->rtSubIdx;
  746. return kOkNetRC;
  747. }
  748. //==========================================================================
  749. #include "cmThread.h"
  750. typedef struct
  751. {
  752. cmThreadH_t thH;
  753. cmRtNetH_t netH;
  754. unsigned msgVal;
  755. } _cmRtNetTest_t;
  756. // This function is called within the context of cmRtNetReceive().
  757. void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  758. {
  759. //_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
  760. cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
  761. unsigned i = *(unsigned*)(data + sizeof(cmRtNetMsg_t));
  762. printf("rtSubIdx:%i endptId:%i %i\n",r->hdr.rtSubIdx,r->endptId,i);
  763. }
  764. bool _cmRtNetTestThreadFunc(void* param)
  765. {
  766. _cmRtNetTest_t* p = (_cmRtNetTest_t*)param;
  767. if( cmRtNetIsValid(p->netH) )
  768. {
  769. cmRtNetReceive(p->netH);
  770. }
  771. cmSleepMs(40);
  772. return true;
  773. }
  774. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
  775. {
  776. char c;
  777. _cmRtNetTest_t t;
  778. const unsigned rtSubIdx = 0;
  779. cmUdpPort_t port = 5876;
  780. _cmRtNetTest_t* p = &t;
  781. cmRtNetRC_t rc = kOkNetRC;
  782. const cmChar_t* localHostStr = mstrFl ? "master" : "slave";
  783. const cmChar_t* localEndpStr = mstrFl ? "master_ep" : "slave_ep";
  784. const cmChar_t* remoteHostStr = !mstrFl ? "master" : "slave";
  785. const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep";
  786. const cmChar_t* bcastAddr = "192.168.15.255";
  787. memset(&t,0,sizeof(t));
  788. if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
  789. goto errLabel;
  790. if((rc = cmRtNetAlloc(ctx,&p->netH,rtSubIdx,_cmRtNetTestRecv,p)) != kOkNetRC )
  791. goto errLabel;
  792. cmRtNetReportSyncEnable(p->netH,true); // enable sync. protocol reporting
  793. if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC)
  794. goto errLabel;
  795. if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC )
  796. goto errLabel;
  797. if( cmThreadPause(p->thH,0) != kOkThRC )
  798. goto errLabel;
  799. cmRptPrintf(&ctx->rpt,"%s t=transmit s=sync r=report q=quit\n", localHostStr );
  800. while( (c=getchar()) != 'q' )
  801. {
  802. switch(c)
  803. {
  804. case 'r':
  805. cmRtNetReport(p->netH);
  806. break;
  807. case 's':
  808. cmRtNetDoSync(p->netH);
  809. break;
  810. case 't':
  811. {
  812. if( cmRtNetSendByLabels(p->netH, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
  813. p->msgVal += 1;
  814. }
  815. break;
  816. }
  817. }
  818. errLabel:
  819. cmThreadDestroy(&p->thH);
  820. cmRtNetFree(&p->netH);
  821. return;
  822. }