libcm is a C development framework with an emphasis on audio signal processing applications.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

cmRtNet.c 30KB

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