libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmRtNet.c 31KB

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