Просмотр исходного кода

cmRtNet.h/c: Added cmRtNetNodeIndex().

master
kpl 10 лет назад
Родитель
Сommit
335ca94b6c
2 измененных файлов: 38 добавлений и 19 удалений
  1. 31
    12
      cmRtNet.c
  2. 7
    7
      cmRtNet.h

+ 31
- 12
cmRtNet.c Просмотреть файл

@@ -97,15 +97,24 @@ void _cmRtNetRpt( cmRtNet_t* p, const cmChar_t* fmt, ... )
97 97
   va_end(vl);
98 98
 }
99 99
 
100
-cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label )
100
+cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label, unsigned* idxRef )
101 101
 {
102 102
   if( label == NULL )
103 103
     return NULL;
104 104
 
105
+  if( idxRef != NULL )
106
+    *idxRef = cmInvalidIdx;
107
+
105 108
   cmRtNetNode_t* np = p->nodes;
106
-  for(; np!=NULL; np=np->link)
109
+  unsigned i;
110
+  for(i=0; np!=NULL; np=np->link,++i)
107 111
     if( strcmp(label,np->label)==0)
112
+    {
113
+      if( idxRef != NULL )
114
+        *idxRef = i;
115
+
108 116
       return np;
117
+    }
109 118
 
110 119
   return NULL;
111 120
 }
@@ -190,8 +199,8 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, unsigned rt
190 199
   if( cmTextIsEmpty(label) )
191 200
     return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
192 201
 
193
-  if((np = _cmRtNetFindNode(p,label)) != NULL )
194
-    cmErrWarnMsg(&p->err,kDuplLabelNetRC,"The node label '%s' is already in use.",cmStringNullGuard(label));
202
+  if((np = _cmRtNetFindNode(p,label,NULL)) != NULL )
203
+    cmErrWarnMsg(&p->err,kOkNetRC/*kDuplLabelNetRC*/,"The node label '%s' is being reused.",cmStringNullGuard(label));
195 204
   else
196 205
   {
197 206
     np           = cmMemAllocZ(cmRtNetNode_t,1);
@@ -569,7 +578,7 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
569 578
   {
570 579
     case kHelloSelNetId:    
571 580
       // if this is a response to a broadcast from the local node then ignore it
572
-      if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label)) != NULL && strcmp(p->localNode->label,m.label)==0 )
581
+      if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label,NULL)) != NULL && strcmp(p->localNode->label,m.label)==0 )
573 582
       {
574 583
         const cmChar_t* fromAddrStr  = cmUdpAddrToString(p->udpH,fromAddr);
575 584
         const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH));
@@ -794,12 +803,6 @@ cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
794 803
   return rc;
795 804
 }
796 805
 
797
-unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a )
798
-{
799
-  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
800
-  return _cmRtNetAddrToNodeIndex(p,a);
801
-}
802
-
803 806
 
804 807
 cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
805 808
 {
@@ -808,7 +811,7 @@ cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, cons
808 811
   cmRtNetNode_t* np;
809 812
   cmRtNetEnd_t*  ep;
810 813
 
811
-  if(( np = _cmRtNetFindNode(p,nodeLabel)) == NULL )
814
+  if(( np = _cmRtNetFindNode(p,nodeLabel,NULL)) == NULL )
812 815
     return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
813 816
 
814 817
 
@@ -966,6 +969,22 @@ unsigned        cmRtNetRemoteNodeCount( cmRtNetH_t h )
966 969
   return n;
967 970
 }
968 971
 
972
+unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a )
973
+{
974
+  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
975
+  return _cmRtNetAddrToNodeIndex(p,a);
976
+}
977
+
978
+unsigned        cmRtNetRemoteNodeIndex( cmRtNetH_t h, const cmChar_t* label )
979
+{
980
+  cmRtNet_t* p  = _cmRtNetHandleToPtr( h );
981
+  unsigned   idx = cmInvalidIdx;
982
+
983
+  _cmRtNetFindNode(p,label,&idx);
984
+    
985
+  return idx;
986
+}
987
+
969 988
 const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx )
970 989
 {
971 990
   cmRtNet_t*           p  = _cmRtNetHandleToPtr( h );

+ 7
- 7
cmRtNet.h Просмотреть файл

@@ -49,11 +49,11 @@ extern "C" {
49 49
   // selector id's for cmRtNetSyncMsg_t.selId.
50 50
   typedef enum
51 51
   {
52
-    kHelloSelNetId,         // broadcast msg (label=node label, id=endpt cnt)
53
-    kNodeSelNetId,          // define remote node  (label=remote node label,  id=endpt cnt)
54
-    kEndpointSelNetId,      // define remote endpt (label=remote endpt label, id=endpt id)
55
-    kDoneSelNetId,          // declare all endpts sent
56
-    kInvalidSelNetId
52
+    kHelloSelNetId,         // 0 broadcast msg (label=node label, id=endpt cnt)
53
+    kNodeSelNetId,          // 1 define remote node  (label=remote node label,  id=endpt cnt)
54
+    kEndpointSelNetId,      // 2 define remote endpt (label=remote endpt label, id=endpt id)
55
+    kDoneSelNetId,          // 3 declare all endpts sent
56
+    kInvalidSelNetId        // 4
57 57
   } cmRtNetSelId_t;
58 58
 
59 59
 
@@ -117,8 +117,6 @@ extern "C" {
117 117
   // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
118 118
   cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
119 119
 
120
-  // Return the index of the node associated with sockaddr_in.
121
-  unsigned    cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
122 120
 
123 121
   // Get a remote end point handle for use with cmRtNetSend.
124 122
   cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
@@ -150,6 +148,8 @@ extern "C" {
150 148
   // {nodeIdx, epIdx} does not identify a valid endpoint.
151 149
   const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h );
152 150
   unsigned        cmRtNetRemoteNodeCount( cmRtNetH_t h );
151
+  unsigned        cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
152
+  unsigned        cmRtNetRemoteNodeIndex( cmRtNetH_t h, const cmChar_t* label );
153 153
   const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx );
154 154
   unsigned        cmRtNetRemoteNodeEndPointCount(   cmRtNetH_t h, unsigned nodeIdx );
155 155
   cmRtNetRC_t     cmRtNetRemoteNodeEndPoint( 

Загрузка…
Отмена
Сохранить