From d38cea4e7be2d6e5940631067d9faf77e4f063bf Mon Sep 17 00:00:00 2001 From: Michael Theall Date: Sat, 16 Jan 2016 00:38:58 -0600 Subject: [PATCH] Restart server when network drops --- include/ftp.h | 14 +++++++++++--- source/ftp.c | 11 +++++++---- source/main.c | 45 ++++++++++++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/include/ftp.h b/include/ftp.h index ac402ba..279586e 100644 --- a/include/ftp.h +++ b/include/ftp.h @@ -1,5 +1,13 @@ #pragma once -int ftp_init(void); -int ftp_loop(void); -void ftp_exit(void); +/*! Loop status */ +typedef enum +{ + LOOP_CONTINUE, /*!< Continue looping */ + LOOP_RESTART, /*!< Reinitialize */ + LOOP_EXIT, /*!< Terminate looping */ +} loop_status_t; + +int ftp_init(void); +loop_status_t ftp_loop(void); +void ftp_exit(void); diff --git a/source/ftp.c b/source/ftp.c index b02f085..318f775 100644 --- a/source/ftp.c +++ b/source/ftp.c @@ -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 diff --git a/source/main.c b/source/main.c index 666a77c..1695ab3 100644 --- a/source/main.c +++ b/source/main.c @@ -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) - console_render(); - else - return; + status = callback(); + console_render(); + 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,14 +81,17 @@ main(int argc, console_init(); console_set_status("\n" GREEN STATUS_STRING RESET); - /* initialize ftp subsystem */ - if(ftp_init() == 0) + while(status == LOOP_RESTART) { - /* ftp loop */ - loop(ftp_loop); + /* initialize ftp subsystem */ + if(ftp_init() == 0) + { + /* ftp loop */ + status = loop(ftp_loop); - /* done with ftp */ - ftp_exit(); + /* done with ftp */ + ftp_exit(); + } } console_print("Press B to exit\n");