Restart server when network drops

This commit is contained in:
Michael Theall 2016-01-16 00:38:58 -06:00
parent 2ff33ca398
commit d38cea4e7b
3 changed files with 46 additions and 24 deletions

View File

@ -1,5 +1,13 @@
#pragma once
/*! Loop status */
typedef enum
{
LOOP_CONTINUE, /*!< Continue looping */
LOOP_RESTART, /*!< Reinitialize */
LOOP_EXIT, /*!< Terminate looping */
} loop_status_t;
int ftp_init(void);
int ftp_loop(void);
loop_status_t ftp_loop(void);
void ftp_exit(void);

View File

@ -1266,7 +1266,7 @@ ftp_exit(void)
#endif
}
int
loop_status_t
ftp_loop(void)
{
int rc;
@ -1280,8 +1280,11 @@ ftp_loop(void)
rc = poll(&pollinfo, 1, 0);
if(rc < 0)
{
if(errno == ENETDOWN)
return LOOP_RESTART;
console_print(RED "poll: %d %s\n" RESET, errno, strerror(errno));
return -1;
return LOOP_EXIT;
}
else if(rc > 0)
{
@ -1302,10 +1305,10 @@ ftp_loop(void)
#ifdef _3DS
hidScanInput();
if(hidKeysDown() & KEY_B)
return -1;
return LOOP_EXIT;
#endif
return 0;
return LOOP_CONTINUE;
}
static void

View File

@ -10,18 +10,24 @@
/*! looping mechanism
*
* @param[in] callback function to call during each iteration
*
* @returns loop status
*/
static void
loop(int (*callback)(void))
static loop_status_t
loop(loop_status_t (*callback)(void))
{
loop_status_t status = LOOP_CONTINUE;
#ifdef _3DS
while(aptMainLoop())
{
if(callback() == 0)
status = callback();
console_render();
else
return;
if(status != LOOP_CONTINUE)
return status;
}
return LOOP_EXIT;
#else
for(;;)
callback();
@ -30,9 +36,9 @@ loop(int (*callback)(void))
/*! wait until the B button is pressed
*
* @returns -1 if B was pressed
* @returns loop status
*/
static int
static loop_status_t
wait_for_b(void)
{
#ifdef _3DS
@ -41,12 +47,12 @@ wait_for_b(void)
/* check if B was pressed */
if(hidKeysDown() & KEY_B)
return -1;
return LOOP_EXIT;
/* B was not pressed */
return 0;
return LOOP_CONTINUE;
#else
return -1;
return LOOP_EXIT;
#endif
}
@ -61,6 +67,8 @@ int
main(int argc,
char *argv[])
{
loop_status_t status = LOOP_RESTART;
#ifdef _3DS
/* initialize needed 3DS services */
acInit();
@ -73,15 +81,18 @@ main(int argc,
console_init();
console_set_status("\n" GREEN STATUS_STRING RESET);
while(status == LOOP_RESTART)
{
/* initialize ftp subsystem */
if(ftp_init() == 0)
{
/* ftp loop */
loop(ftp_loop);
status = loop(ftp_loop);
/* done with ftp */
ftp_exit();
}
}
console_print("Press B to exit\n");
loop(wait_for_b);