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 24KB

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