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 #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_init(void);
int ftp_loop(void); loop_status_t ftp_loop(void);
void ftp_exit(void); void ftp_exit(void);

View File

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

View File

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