libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmRtNet.c 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. const cmRtNetNode_t* _cmRtNetIndexToRemoteNode( cmRtNet_t* p, unsigned idx )
  277. {
  278. const cmRtNetNode_t* np = p->nodes;
  279. unsigned i = 0;
  280. for(; np!=NULL; np=np->link)
  281. if( np != p->localNode )
  282. {
  283. if( i == idx )
  284. return np;
  285. ++i;
  286. }
  287. return NULL;
  288. }
  289. const cmRtNetEnd_t* _cmRtNetIndexToEndpt( const cmRtNetNode_t* np, unsigned endIdx )
  290. {
  291. const cmRtNetEnd_t* ep = np->ends;
  292. unsigned i = 0;
  293. for(; ep!=NULL; ep=ep->link,++i)
  294. if( i==endIdx )
  295. return ep;
  296. return NULL;
  297. }
  298. const cmRtNetEnd_t* _cmRtNetFindEndpt( cmRtNet_t* p, unsigned nodeIdx, unsigned epIdx )
  299. {
  300. const cmRtNetNode_t* np;
  301. const cmRtNetEnd_t* ep;
  302. if((np = _cmRtNetIndexToRemoteNode( p, nodeIdx )) == NULL )
  303. return NULL;
  304. if((ep = _cmRtNetIndexToEndpt( np, epIdx )) == NULL )
  305. return NULL;
  306. return ep;
  307. }
  308. const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m )
  309. {
  310. if( m->selId==kNodeSelNetId || m->selId==kEndpointSelNetId )
  311. return (const cmChar_t*)(m+1);
  312. return "";
  313. }
  314. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg )
  315. {
  316. cmRtNetRC_t rc;
  317. if((rc = cmRtNetFree(hp)) != kOkNetRC )
  318. return rc;
  319. cmRtNet_t* p = cmMemAllocZ(cmRtNet_t,1);
  320. cmErrSetup(&p->err,&ctx->rpt,"cmRtNet");
  321. // allocate the UDP port
  322. if(cmUdpAlloc(ctx,&p->udpH) != kOkUdpRC )
  323. {
  324. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port allocate failed.");
  325. goto errLabel;
  326. }
  327. p->udpTimeOutMs = 50;
  328. p->udpRecvBufByteCnt = 8192;
  329. p->cbFunc = cbFunc;
  330. p->cbArg = cbArg;
  331. hp->h = p;
  332. errLabel:
  333. if(rc != kOkNetRC )
  334. _cmRtNetFree(p);
  335. return rc;
  336. }
  337. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp )
  338. {
  339. cmRtNetRC_t rc = kOkNetRC;
  340. if( hp==NULL || cmRtNetIsValid(*hp)==false )
  341. return rc;
  342. cmRtNet_t* p = _cmRtNetHandleToPtr(*hp);
  343. if((rc = _cmRtNetFree(p)) != kOkNetRC )
  344. return rc;
  345. hp->h = NULL;
  346. return rc;
  347. }
  348. const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h )
  349. {
  350. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  351. return cmUdpHostName(p->udpH);
  352. }
  353. bool cmRtNetIsValid( cmRtNetH_t h )
  354. { return h.h !=NULL; }
  355. cmUdpH_t cmRtNetUdpPortHandle( cmRtNetH_t h )
  356. {
  357. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  358. return p->udpH;
  359. }
  360. cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t srcSelId )
  361. {
  362. cmRtNetRC_t rc = kOkNetRC;
  363. cmRtNetEnd_t* ep = NULL;
  364. const cmChar_t* msgLabel = NULL;
  365. unsigned msgId = cmInvalidId;
  366. unsigned msgRtSubIdx = cmInvalidIdx;
  367. cmRtNetSelId_t selId = kEndpointSelNetId;
  368. const cmChar_t* rptLabel = "endpoint";
  369. if( np == NULL )
  370. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found.");
  371. // if we got here by receiving a 'done' msg from the remote node ...
  372. if( srcSelId == kDoneSelNetId )
  373. {
  374. // ... then mark the remote node as having recieved all endpoints
  375. unsigned n;
  376. if((n = _cmRtNetNodeEndpointCount(np)) != np->endPtCnt )
  377. rc = cmErrMsg(&p->err,kNodeEndCntErrNetRC,"The node '%s' expected %i endpoints but received %i.",cmStringNullGuard(np->label),np->endPtCnt,n);
  378. else
  379. np->flags = cmSetFlag(np->flags,kValidNodeNetFl);
  380. }
  381. // attempt to get the next local endpoint to send ...
  382. if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) != NULL )
  383. {
  384. msgLabel = ep->label; // ... send next local endpoint
  385. msgId = ep->id;
  386. msgRtSubIdx = ep->rtSubIdx;
  387. }
  388. else // .... all local endpoints have been sent
  389. {
  390. selId = kInvalidSelNetId;
  391. rptLabel = "done";
  392. // verify that no endpoints are available
  393. if( np->endPtIdx < p->localNode->endPtCnt )
  394. rc = cmErrMsg(&p->err,kSyncFailNetRC,"More endpoints are available to send but are not reachable.");
  395. else
  396. {
  397. // if the remote node still has endpts to send then continue
  398. // sending 'done' messages.
  399. if( np->endPtIdx==p->localNode->endPtCnt || srcSelId != kDoneSelNetId )
  400. selId = kDoneSelNetId;
  401. }
  402. }
  403. // selId is set to kInvalidSelNetId when we encounter the (stop) criteria
  404. if( selId != kInvalidSelNetId )
  405. {
  406. if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId,msgRtSubIdx)) != kOkNetRC )
  407. rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
  408. else
  409. _cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel));
  410. np->endPtIdx += 1;
  411. }
  412. return rc;
  413. }
  414. bool _cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt )
  415. {
  416. cmRtNetSyncMsg_t* m = (cmRtNetSyncMsg_t*)data;
  417. return dataByteCnt >= sizeof(cmRtNetSyncMsg_t) && m->hdr.selId == kNetSyncSelRtId;
  418. }
  419. // When the network message recieve function (See cmRtNetAlloc() 'cbFunc')
  420. // receives a message with the cmRtSysMsgHdr_t.selId == kNetSyncSelRtId
  421. // it should call this function to update the current sync state of the
  422. // cmRtNet.
  423. cmRtNetRC_t _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  424. {
  425. cmRtNetRC_t rc = kOkNetRC;
  426. cmRtNetSyncMsg_t m;
  427. m.label = NULL;
  428. assert( _cmRtNetIsSyncModeMsg(data,dataByteCnt));
  429. if( _cmRtNetDeserializeSyncMsg(data,dataByteCnt,&m) != kOkNetRC )
  430. {
  431. rc = cmErrMsg(&p->err,rc,"Net sync. receive failed due to deserialize fail.");
  432. goto errLabel;
  433. }
  434. _cmRtNetRpt(p,"recv from:%s\n",cmUdpAddrToString(p->udpH, fromAddr ));
  435. assert( m.hdr.selId == kNetSyncSelRtId );
  436. // attempt to locate the remote node which sent the msg
  437. cmRtNetNode_t* np = _cmRtNetFindNodeFromSockAddr(p,fromAddr);
  438. switch( m.selId )
  439. {
  440. case kHelloSelNetId:
  441. // if this is a response to a broadcast from the local node then ignore it
  442. if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label)) != NULL && strcmp(p->localNode->label,m.label)==0 )
  443. {
  444. const cmChar_t* fromAddrStr = cmUdpAddrToString(p->udpH,fromAddr);
  445. const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH));
  446. if( fromAddrStr!=NULL && localAddrStr!=NULL && strcmp(fromAddrStr,localAddrStr)!=0)
  447. cmErrMsg(&p->err,kDuplLocalNetRC,"The node label '%s' appears to be duplicated at address %s and locally.",cmStringNullGuard(m.label),fromAddrStr);
  448. np->sockaddr = *fromAddr;
  449. goto errLabel;
  450. }
  451. // fall through
  452. case kNodeSelNetId:
  453. {
  454. // if the node already exists ...
  455. if( np != NULL )
  456. {
  457. // ... delete it because we are about to get new info. about it.
  458. if((rc = _cmRtNetReleaseNode(p,np )) != kOkNetRC )
  459. goto errLabel;
  460. }
  461. // create a node proxy to represent the remote node
  462. // (Note:m.id == remote node endpoint count (i.e. the count of endpoints expected for the remote node.))
  463. if(( rc = _cmRtNetCreateNode(p,m.label,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
  464. goto errLabel;
  465. np = p->nodes; // newest node is always the first node
  466. // send response
  467. switch( m.selId )
  468. {
  469. case kHelloSelNetId:
  470. _cmRtNetRpt(p,"rcv hello\n"); // reply with local node
  471. rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
  472. break;
  473. case kNodeSelNetId:
  474. _cmRtNetRpt(p,"rcv node\n");
  475. _cmRtNetSendEndpointReplyMsg( p, np, m.selId ); // reply with first endpoint
  476. break;
  477. default:
  478. assert(0);
  479. }
  480. }
  481. break;
  482. case kDoneSelNetId:
  483. //case kEndpointAckSelNetId:
  484. rc = _cmRtNetSendEndpointReplyMsg(p,np,m.selId);
  485. break;
  486. case kEndpointSelNetId:
  487. {
  488. cmRtNetEnd_t* ep;
  489. // verify the remote node exists.
  490. if( np == NULL )
  491. {
  492. rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
  493. goto errLabel;
  494. }
  495. // attempt to find the end point
  496. if((ep = _cmRtNetFindNodeEnd(np, m.rtSubIdx, m.label)) != NULL )
  497. ep->id = m.id; // the endpoint was found update the endPtId
  498. else
  499. {
  500. // create a local proxy for the endpoint
  501. if((rc = _cmRtNetCreateEndpoint(p,np,m.rtSubIdx,m.label,m.id)) != kOkNetRC )
  502. goto errLabel;
  503. }
  504. // reply with a local endpoint or 'done' msg
  505. rc = _cmRtNetSendEndpointReplyMsg( p, np, m.selId );
  506. }
  507. break;
  508. default:
  509. assert(0);
  510. break;
  511. }
  512. errLabel:
  513. cmMemFree((cmChar_t*)m.label);
  514. return rc;
  515. }
  516. // This is called in the context of cmRtNetReceive().
  517. void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  518. {
  519. cmRtNet_t* p = (cmRtNet_t*)cbArg;
  520. if( _cmRtNetIsSyncModeMsg(data,dataByteCnt))
  521. _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
  522. p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
  523. }
  524. cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
  525. {
  526. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  527. cmRtNetRC_t rc;
  528. // release the local node
  529. if((rc = cmRtNetFinalize(h)) != kOkNetRC )
  530. goto errLabel;
  531. if( cmTextIsEmpty(bcastAddr) )
  532. {
  533. rc = cmErrMsg(&p->err,kInvalidArgNetRC,"The 'broadcast address' is not valid.");
  534. goto errLabel;
  535. }
  536. // if this is the local node then initialze the local socket
  537. if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl | kBroadcastUdpFl,_cmRtNetRecv,p,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
  538. {
  539. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port initialization failed.");
  540. goto errLabel;
  541. }
  542. // create the local node
  543. if((rc = _cmRtNetCreateNode(p,nodeLabel, ipAddr, port, NULL, kLocalNodeNetFl, 0)) != kOkNetRC )
  544. goto errLabel;
  545. // the last created node is always the first node on the list
  546. p->localNode = p->nodes;
  547. p->bcastAddr = cmMemResizeStr(p->bcastAddr,bcastAddr);
  548. // begin listening on the local port
  549. if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
  550. {
  551. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port failed to enter 'listen' mode.");
  552. goto errLabel;
  553. }
  554. errLabel:
  555. return rc;
  556. }
  557. bool cmRtNetIsInitialized( cmRtNetH_t h )
  558. {
  559. if( cmRtNetIsValid(h) == false )
  560. return false;
  561. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  562. return p->localNode != NULL && cmTextIsNotEmpty(p->bcastAddr);
  563. }
  564. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId )
  565. {
  566. cmRtNetRC_t rc = kOkNetRC;
  567. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  568. if( p->localNode == NULL )
  569. return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
  570. if((rc = _cmRtNetCreateEndpoint(p, p->localNode,rtSubIdx,endPtLabel,endPtId )) == kOkNetRC )
  571. p->localNode->endPtCnt += 1;
  572. return rc;
  573. }
  574. cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h )
  575. {
  576. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  577. _cmRtNetReleaseNodes(p);
  578. return kOkNetRC;
  579. }
  580. cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h )
  581. {
  582. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  583. // broadcast 'node' msg
  584. return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
  585. }
  586. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
  587. {
  588. cmRtNetRC_t rc = kOkNetRC;
  589. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  590. // Calling this function results in callbacks to _cmRtNetRecv() (above)
  591. if( cmUdpGetAvailData(p->udpH, NULL, NULL, NULL ) != kOkUdpRC )
  592. {
  593. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP port query failed.");
  594. goto errLabel;
  595. }
  596. errLabel:
  597. return rc;
  598. }
  599. cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
  600. {
  601. cmRtNetRC_t rc = kOkNetRC;
  602. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  603. cmRtNetNode_t* np;
  604. cmRtNetEnd_t* ep;
  605. if(( np = _cmRtNetFindNode(p,nodeLabel)) == NULL )
  606. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
  607. if(( ep = _cmRtNetFindNodeEnd(np, rtSubIdx, endptLabel )) == NULL )
  608. return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel));
  609. hp->h = ep;
  610. return rc;
  611. }
  612. cmRtNetRC_t _cmRtNetSend( cmRtNet_t* p, const cmRtNetEnd_t* ep, const void* msg, unsigned msgByteCnt )
  613. {
  614. cmRtNetRC_t rc = kOkNetRC;
  615. unsigned dN = sizeof(cmRtNetMsg_t) + msgByteCnt;
  616. char data[ dN ];
  617. cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
  618. r->hdr.rtSubIdx = ep->rtSubIdx;
  619. r->hdr.selId = kMsgSelRtId;
  620. r->endptId = ep->id;
  621. memcpy(data+sizeof(cmRtSysMsgHdr_t),msg,msgByteCnt);
  622. if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC )
  623. return cmErrMsg(&p->err,kUdpPortFailNetRC,"Send to node:%s endpt:%s failed.\n",cmStringNullGuard(ep->np->label),cmStringNullGuard(ep->label));
  624. return rc;
  625. }
  626. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt )
  627. {
  628. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  629. cmRtNetEnd_t* ep = (cmRtNetEnd_t*)epH.h;
  630. assert( ep != NULL );
  631. return _cmRtNetSend(p,ep,msg,msgByteCnt);
  632. }
  633. cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
  634. {
  635. cmRtNetRC_t rc = kOkNetRC;
  636. cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
  637. if((rc = cmRtNetEndpointHandle(h,nodeLabel,rtSubIdx,endptLabel,&epH)) != kOkNetRC )
  638. return rc;
  639. return cmRtNetSend(h,epH,msg,msgByteCnt);
  640. }
  641. cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt )
  642. {
  643. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  644. const cmRtNetEnd_t* ep;
  645. if((ep = _cmRtNetFindEndpt(p, nodeIdx, endptIdx )) == NULL )
  646. return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint at node index %i endpoint index %i was not found.",nodeIdx,endptIdx);
  647. return _cmRtNetSend( p, ep, msg, msgByteCnt );
  648. }
  649. bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl )
  650. {
  651. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  652. bool fl = cmIsFlag(p->flags,kReportSyncNetFl);
  653. p->flags = cmEnaFlag(p->flags,kReportSyncNetFl,enableFl);
  654. return fl;
  655. }
  656. bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h )
  657. {
  658. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  659. return cmIsFlag(p->flags,kReportSyncNetFl);
  660. }
  661. void cmRtNetReport( cmRtNetH_t h )
  662. {
  663. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  664. cmRpt_t* rpt = p->err.rpt;
  665. cmRtNetNode_t* np = p->nodes;
  666. for(; np!=NULL; np=np->link)
  667. {
  668. cmRptPrintf(rpt,"Node: %s ",np->label);
  669. if( np->addr != NULL )
  670. cmRptPrintf(rpt,"%s ",np->addr );
  671. if( cmIsFlag(np->flags,kLocalNodeNetFl) )
  672. cmRptPrintf(rpt,"LOCAL ");
  673. cmRptPrintf(rpt,"%s ",cmStringNullGuard(cmUdpAddrToString(p->udpH,&np->sockaddr)));
  674. if( np->port != kInvalidUdpPortNumber )
  675. cmRptPrintf(rpt,"%i ",np->port );
  676. cmRptPrintf(rpt,"\n");
  677. cmRtNetEnd_t* ep = np->ends;
  678. for(; ep!=NULL; ep=ep->link)
  679. {
  680. cmRptPrintf(rpt," endpt: %i %s\n",ep->id,cmStringNullGuard(ep->label ));
  681. }
  682. }
  683. }
  684. const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h )
  685. {
  686. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  687. return p->localNode->label;
  688. }
  689. unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h )
  690. {
  691. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  692. const cmRtNetNode_t* np = p->nodes;
  693. unsigned n = 0;
  694. for(; np!=NULL; np=np->link)
  695. if( np != p->localNode )
  696. ++n;
  697. return n;
  698. }
  699. const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx )
  700. {
  701. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  702. const cmRtNetNode_t* np;
  703. if((np = _cmRtNetIndexToRemoteNode( p, idx )) == NULL )
  704. return NULL;
  705. return np->label;
  706. }
  707. unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx )
  708. {
  709. const cmRtNetNode_t* np;
  710. const cmRtNetEnd_t* ep;
  711. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  712. unsigned n = 0;
  713. if((np = _cmRtNetIndexToRemoteNode( p, nodeIdx )) == NULL )
  714. return 0;
  715. for(ep=np->ends; ep!=NULL; ep=ep->link)
  716. ++n;
  717. return n;
  718. }
  719. cmRtNetRC_t cmRtNetRemoteNodeEndPoint(
  720. cmRtNetH_t h,
  721. unsigned nodeIdx,
  722. unsigned epIdx,
  723. const cmChar_t** labelRef,
  724. unsigned* idRef,
  725. unsigned* rsiRef )
  726. {
  727. const cmRtNetEnd_t* ep;
  728. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  729. if(( ep = _cmRtNetFindEndpt(p, nodeIdx, epIdx )) == NULL )
  730. {
  731. *labelRef = NULL;
  732. *idRef = cmInvalidId;
  733. *rsiRef = cmInvalidIdx;
  734. return kEndNotFoundNetRC;
  735. }
  736. *labelRef = ep->label;
  737. *idRef = ep->id;
  738. *rsiRef = ep->rtSubIdx;
  739. return kOkNetRC;
  740. }
  741. //==========================================================================
  742. #include "cmThread.h"
  743. typedef struct
  744. {
  745. cmThreadH_t thH;
  746. cmRtNetH_t netH;
  747. unsigned msgVal;
  748. } _cmRtNetTest_t;
  749. // This function is called within the context of cmRtNetReceive().
  750. void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  751. {
  752. //_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
  753. cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
  754. unsigned i = *(unsigned*)(data + sizeof(cmRtNetMsg_t));
  755. printf("rtSubIdx:%i endptId:%i %i\n",r->hdr.rtSubIdx,r->endptId,i);
  756. }
  757. bool _cmRtNetTestThreadFunc(void* param)
  758. {
  759. _cmRtNetTest_t* p = (_cmRtNetTest_t*)param;
  760. if( cmRtNetIsValid(p->netH) )
  761. {
  762. cmRtNetReceive(p->netH);
  763. }
  764. cmSleepMs(40);
  765. return true;
  766. }
  767. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
  768. {
  769. char c;
  770. _cmRtNetTest_t t;
  771. const unsigned rtSubIdx = 0;
  772. cmUdpPort_t port = 5876;
  773. _cmRtNetTest_t* p = &t;
  774. cmRtNetRC_t rc = kOkNetRC;
  775. const cmChar_t* localHostStr = mstrFl ? "master" : "slave";
  776. const cmChar_t* localEndpStr = mstrFl ? "master_ep" : "slave_ep";
  777. const cmChar_t* remoteHostStr = !mstrFl ? "master" : "slave";
  778. const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep";
  779. const cmChar_t* bcastAddr = "192.168.15.255";
  780. memset(&t,0,sizeof(t));
  781. if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
  782. goto errLabel;
  783. if((rc = cmRtNetAlloc(ctx,&p->netH,_cmRtNetTestRecv,p)) != kOkNetRC )
  784. goto errLabel;
  785. cmRtNetReportSyncEnable(p->netH,true); // enable sync. protocol reporting
  786. if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC)
  787. goto errLabel;
  788. if((rc = cmRtNetRegisterEndPoint(p->netH,rtSubIdx,localEndpStr, 0 )) != kOkNetRC )
  789. goto errLabel;
  790. if( cmThreadPause(p->thH,0) != kOkThRC )
  791. goto errLabel;
  792. cmRptPrintf(&ctx->rpt,"%s t=transmit s=sync r=report q=quit\n", localHostStr );
  793. while( (c=getchar()) != 'q' )
  794. {
  795. switch(c)
  796. {
  797. case 'r':
  798. cmRtNetReport(p->netH);
  799. break;
  800. case 's':
  801. cmRtNetDoSync(p->netH);
  802. break;
  803. case 't':
  804. {
  805. if( cmRtNetSendByLabels(p->netH, remoteHostStr, rtSubIdx, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
  806. p->msgVal += 1;
  807. }
  808. break;
  809. }
  810. }
  811. errLabel:
  812. cmThreadDestroy(&p->thH);
  813. cmRtNetFree(&p->netH);
  814. return;
  815. }