libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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