From 96015e38c895db145937b756beb78a4e7fb497aa Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 2 Jul 2014 07:38:46 -0400 Subject: [PATCH 1/4] B"H view1090: handle disconnection - keep trying to reconnect to server if disconnected --- view1090.c | 58 +++++++++++++++++++++++++++++++++++------------------- view1090.h | 2 ++ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/view1090.c b/view1090.c index 4830bca..59498be 100644 --- a/view1090.c +++ b/view1090.c @@ -115,6 +115,33 @@ void view1090Init(void) { // Prepare error correction tables modesInitErrorInfo(); } + +// Set up data connection +int setupConnection(struct client *c) { + int fd; + + // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here. + if ((fd = anetTcpConnect(Modes.aneterr, View1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) != ANET_ERR) { + // + // Setup a service callback client structure for a beast binary input (from dump1090) + // This is a bit dodgy under Windows. The fd parameter is a handle to the internet + // socket on which we are receiving data. Under Linux, these seem to start at 0 and + // count upwards. However, Windows uses "HANDLES" and these don't nececeriy start at 0. + // dump1090 limits fd to values less than 1024, and then uses the fd parameter to + // index into an array of clients. This is ok-ish if handles are allocated up from 0. + // However, there is no gaurantee that Windows will behave like this, and if Windows + // allocates a handle greater than 1024, then dump1090 won't like it. On my test machine, + // the first Windows handle is usually in the 0x54 (84 decimal) region. + + c->next = NULL; + c->buflen = 0; + c->fd = + c->service = + Modes.bis = fd; + Modes.clients = c; + } + return fd; +} // // ================================ Main ==================================== // @@ -181,6 +208,7 @@ void showCopyright(void) { int main(int argc, char **argv) { int j, fd; struct client *c; + char pk_buf[8]; // Set sane defaults @@ -244,34 +272,24 @@ int main(int argc, char **argv) { view1090Init(); // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here. - if ((fd = anetTcpConnect(Modes.aneterr, View1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) == ANET_ERR) { + c = (struct client *) malloc(sizeof(*c)); + if ((fd = setupConnection(c)) == ANET_ERR) { fprintf(stderr, "Failed to connect to %s:%d\n", View1090.net_input_beast_ipaddr, Modes.net_input_beast_port); exit(1); } - // - // Setup a service callback client structure for a beast binary input (from dump1090) - // This is a bit dodgy under Windows. The fd parameter is a handle to the internet - // socket on which we are receiving data. Under Linux, these seem to start at 0 and - // count upwards. However, Windows uses "HANDLES" and these don't nececeriy start at 0. - // dump1090 limits fd to values less than 1024, and then uses the fd parameter to - // index into an array of clients. This is ok-ish if handles are allocated up from 0. - // However, there is no gaurantee that Windows will behave like this, and if Windows - // allocates a handle greater than 1024, then dump1090 won't like it. On my test machine, - // the first Windows handle is usually in the 0x54 (84 decimal) region. - - c = (struct client *) malloc(sizeof(*c)); - c->next = NULL; - c->buflen = 0; - c->fd = - c->service = - Modes.bis = fd; - Modes.clients = c; // Keep going till the user does something that stops us while (!Modes.exit) { - modesReadFromClient(c,"",decodeBinMessage); interactiveRemoveStaleAircrafts(); interactiveShowData(); + if ((fd == ANET_ERR) || (recv(c->fd, pk_buf, sizeof(pk_buf), MSG_PEEK | MSG_DONTWAIT) == 0)) { + free(c); + usleep(1000000); + c = (struct client *) malloc(sizeof(*c)); + fd = setupConnection(c); + continue; + } + modesReadFromClient(c,"",decodeBinMessage); } // The user has stopped us, so close any socket we opened diff --git a/view1090.h b/view1090.h index c4c97fe..03ad96d 100644 --- a/view1090.h +++ b/view1090.h @@ -49,6 +49,8 @@ #include #include #include + #include + #include #include "rtl-sdr.h" #include "anet.h" #else From 350a57f858567a2ef85d40f63a7a2cc196eb1446 Mon Sep 17 00:00:00 2001 From: hhm Date: Wed, 2 Jul 2014 07:41:43 -0400 Subject: [PATCH 2/4] B"H view1090: do not block - do not block on network data --- view1090.c | 1 + 1 file changed, 1 insertion(+) diff --git a/view1090.c b/view1090.c index 59498be..2dbd323 100644 --- a/view1090.c +++ b/view1090.c @@ -122,6 +122,7 @@ int setupConnection(struct client *c) { // Try to connect to the selected ip address and port. We only support *ONE* input connection which we initiate.here. if ((fd = anetTcpConnect(Modes.aneterr, View1090.net_input_beast_ipaddr, Modes.net_input_beast_port)) != ANET_ERR) { + anetNonBlock(Modes.aneterr, fd); // // Setup a service callback client structure for a beast binary input (from dump1090) // This is a bit dodgy under Windows. The fd parameter is a handle to the internet From ec09cc7a71d6c2aa8b341568120af7fd68ab9f17 Mon Sep 17 00:00:00 2001 From: hhm Date: Thu, 3 Jul 2014 02:41:06 -0400 Subject: [PATCH 3/4] B"H view1090: sleep a bit between loops view1090 was using close to 100% CPU before, with the non-blocking commits and the reconnection code. sleep a bit between loop iterations to keep CPU usage low. CPU usage with this addition was down to <1% in testing. --- view1090.c | 1 + 1 file changed, 1 insertion(+) diff --git a/view1090.c b/view1090.c index 2dbd323..5ce15ad 100644 --- a/view1090.c +++ b/view1090.c @@ -291,6 +291,7 @@ int main(int argc, char **argv) { continue; } modesReadFromClient(c,"",decodeBinMessage); + usleep(100000); } // The user has stopped us, so close any socket we opened From a86eb7a61c1f5af57b74b851713627973982e720 Mon Sep 17 00:00:00 2001 From: hhm Date: Sun, 6 Jul 2014 05:58:44 -0400 Subject: [PATCH 4/4] B"H view1090: do not use pthreads It *seems* that pthreads are used in dump1090 for I/! sample reading only. So view1090 need not use pthread_exit. --- view1090.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/view1090.c b/view1090.c index 5ce15ad..644245d 100644 --- a/view1090.c +++ b/view1090.c @@ -298,11 +298,7 @@ int main(int argc, char **argv) { if (fd != ANET_ERR) {close(fd);} -#ifndef _WIN32 - pthread_exit(0); -#else return (0); -#endif } // //=========================================================================