Home AMX User Forum AMX General Discussion

Odd problem w/ RETURN?

I'm running this code:
if(nNext)
      {
      nNext++;
      if(nNext > SSERV_NUM_SERVERS)
           {
           nNext = SSERV_FIRST_SSERVER;
           }
      else if(nNext < SSERV_FIRST_SSERVER)
           {
           nNext = SSERV_FIRST_SSERVER;
           }
      for(i = nNext; i <= SSERV_NUM_SERVERS; i ++)
           {
           if(nSServerStats[i][SSERV_STAT_STATE] == IP_SERVER_STOPPED && 
            !nSServerStats[i][SSERV_STAT_AUTOSTART] && nSServerStats[i][SSERV_STAT_TYPE] == 1) //stopped and not autostart
            {
            fnSServer_StopExtras(i);//stops others if listening?
            nSServPendingListener = i;
[COLOR=#FF0000]fnSServer_DeBug("'STARTING SERVER [ ',itoa(i),' ] to listen on port (',itoa(nSServerStats[i][SSERV_STAT_IP_PORT]),') <',ITOA(__LINE__),'>'");[/COLOR]
            SEND_COMMAND vSERVER_Arry[i],'START_SERVER';            

            RETURN;
            }
           }
      }

Diagnostics:
Line    290 (00:00:08)::  (***********************************************************)
Line    291 (00:00:08)::         Sitrep Server Mod 4, Has Client Connected           
Line    292 (00:00:08)::  (***********************************************************)
Line    293 (00:00:08)::  $0D
Line    294 (00:00:08)::  SERVER-mod 4,(1) DEBUG: CLIENT ONLINE
Line    295 (00:00:08)::  SERVER.axi -,(1) DEBUG: RX SERVER-[4]: STATE CHANGE, NEW STATE-[ CONNECTED ], OLD STATE-[ LISTENING ] <873>
Line    296 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 2 ] not listening, do nothing <692>
Line    297 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 3 ] not listening, do nothing <692>
Line    298 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 4 ] not listening, do nothing <692>
Line    299 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 5 ] last started, left to listen <676>
Line    300 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 6 ] not listening, do nothing <692>
[COLOR=#FF0000]Line    301 (00:00:08)::  SERVER.axi -,(1) DEBUG: STARTING SERVER [ 5 ] to listen on port (8101) <656>[/COLOR]
Line    302 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 2 ] not listening, do nothing <692>
Line    303 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 3 ] not listening, do nothing <692>
Line    304 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 4 ] not listening, do nothing <692>
Line    305 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 5 ] not listening, do nothing <692>
Line    306 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 6 ] last started, left to listen <676>
Line    307 (00:00:08)::  SERVER.axi -,(1) DEBUG: STARTING SERVER [ 6 ] to listen on port (8101) <656>
Line    308 (00:00:08)::  SERVER-mod 5,(1) DEBUG: Virtual RX Cmd: (START_SERVER) :DEBUG<1426>
Line    309 (00:00:08)::  SERVER-mod 5,(1) DEBUG: OPENING SERVER :DEBUG<250>
Line    310 (00:00:08)::  SERVER-mod 5,(1) DEBUG: SITREP Server Open successful, listening on port (8101) :DEBUG<266>
Line    311 (00:00:08)::  SERVER-mod 6,(1) DEBUG: Virtual RX Cmd: (START_SERVER) :DEBUG<1426>
Line    312 (00:00:08)::  SERVER-mod 6,(1) DEBUG: OPENING SERVER :DEBUG<250>
Line    313 (00:00:08)::  SERVER-mod 6,(1) DEBUG: SITREP Server Open successful, listening on port (8101) :DEBUG<266>
Line    314 (00:00:08)::  SERVER.axi -,(1) DEBUG: RX SERVER-[5]: STATE CHANGE, NEW STATE-[ PENDING ], OLD STATE-[ STOPPED ] <873>
Line    315 (00:00:08)::  SERVER.axi -,(1) DEBUG: RX SERVER-[5]: STATE CHANGE, NEW STATE-[ LISTENING ], OLD STATE-[ PENDING ] <873>
Line    316 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 2 ] not listening, do nothing <692>
Line    317 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 3 ] not listening, do nothing <692>
Line    318 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 4 ] not listening, do nothing <692>
Line    319 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 5 ] last started, left to listen <676>
Line    320 (00:00:08)::  SERVER.axi -,(1) DEBUG: STOP SERVER [ 6 ] not listening, do nothing <692>
Line    321 (00:00:08)::  SERVER.axi -,(1) DEBUG: RX SERVER-[6]: STATE CHANGE, NEW STATE-[ PENDING ], OLD STATE-[ STOPPED ] <873>
Line    322 (00:00:08)::  SERVER-mod 4,(1) DEBUG: STRING: *********$0D$0A :DEBUG<1338>
On line 301 we can see it the code reached the line high lighted in red in the snippet and it should have then sent the command and hit the RETURN and exited the function but instead it finished the for loop which is this case max's out at 6. This doesn't normally happen either and if it did exit this function there would have been other diagnostic lines printed before this function could be called again. Very odd?

The STOP SERVER lines that precede and follow the red high lighted line in the diagnostics print out are from the fnSServer_StopExtras(i) function called just before the red high lighted line in the code snippet.
DEFINE_FUNCTION fnSServer_StopExtras(INTEGER iIndx) //stop all listening servers but one

     {
     STACK_VAR INTEGER i;

     for(i = SSERV_FIRST_SSERVER; i <= SSERV_NUM_SERVERS; i ++)
      {
      if(i == iIndx)//server just started
           {
           fnSServer_DeBug("'STOP SERVER [ ',itoa(i),' ] last started, left to listen <',ITOA(__LINE__),'>'");
           }
      else if(nSServerStats[i][SSERV_STAT_STATE] == IP_SERVER_LISTENING)
           {
           if(nSServerStats[i][SSERV_STAT_AUTOSTART] == 1) 
            {
            fnSServer_DeBug("'STOP SERVER [ ',itoa(i),' ] listening and set to "auto connect", leaving alone <',ITOA(__LINE__),'>'");
            }
           else//shut down all listening other than the last to start
            {
            fnSServer_DeBug("'STOP SERVER [ ',itoa(i),' ] listening, sending "stop" cmd to mod (',itoa(i),') <',ITOA(__LINE__),'>'");
            SEND_COMMAND vSERVER_Arry[i],'STOP_SERVER';
            }
           }
      else
           {
           fnSServer_DeBug("'STOP SERVER [ ',itoa(i),' ] not listening, do nothing <',ITOA(__LINE__),'>'");
           }           
      }

     RETURN;
     }

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    I've never put a RETURN inside a FOR loop before and I can't explain what you are seeing (seems like it should work) but as a suggestion I would replace the RETURN with a BREAK and move the RETURN after the FOR.
  • viningvining Posts: 4,368
    Yeah, just thought it would be a little more expedient to use the RETURN there instead of BREAK and a later RETURN.
  • John NagyJohn Nagy Posts: 1,734
    Not sure how the rules apply here as I haven't encountered it, but way-back I recall in BASIC that to interrupt a counting loop left that loop on the stack, and you needed to POP it to clean up if that occurred. Sounds similar. Break ends the incomplete count of the loop, return makes it wander away unsatisfied, lurking in wait for more loops.
  • viningvining Posts: 4,368
    Ok I found my screw up, a function with out any debug was being called by a clocking function in my main code. I thought this particular function wasn't being called at all and didn't look beyond my local code to verify and of course there was no debug strings in between it and this loop. any other means of doing this had numerous debug strings in the path. It doesn't help being so rusty either as I haven't really written much at all in the last couple of years. I did write a module last week and I wanted to use a previous recent module as a template and I couldn't even think of which of my modules would be the most recent and possibly better written. It's also odd sitting in front of my PC until 10 pm like was the norm back in the day.

    Anyway I'm sure using RETURN the way I did is fine and I'm sure I've done this many times, just can't remember?
Sign In or Register to comment.