Signal handler, TC7200 workaround, misc stuffs
authorvolpol <volpol@packet-gain.de>
Fri, 13 Sep 2013 22:48:38 +0000 (22:48 +0000)
committervolpol <volpol@packet-gain.de>
Fri, 13 Sep 2013 22:48:38 +0000 (22:48 +0000)
CMakeLists.txt
curly.c
itemq.c
memfile.c

index 71683fba37aba6afddc4e5efd52e29b87b393969..4e4b15538102e529229fc792b4131c45c8ec5741 100644 (file)
@@ -4,6 +4,7 @@ set( CMAKE_C_FLAGS "-W -Wall -Wextra" )
 include_directories(${HLS_SOURCE_DIR})
 add_definitions(-DUSE_TCP)
 #add_definitions(-DDEBUG)
+add_definitions(-DUSE_TC7200_WORKAROUND)
 add_executable(hls curly.c itemq.c m3u8.c memfile.c)
 include(FindPkgConfig)
 pkg_check_modules(CURL REQUIRED libcurl)
diff --git a/curly.c b/curly.c
index 4f4d0672cf5b5439f71d0cefb2db387c758067c6..b8ad046e00bcaed3e30f5b986beb3fb1bf24e240 100644 (file)
--- a/curly.c
+++ b/curly.c
@@ -111,12 +111,12 @@ static int prepare_tcp_dream(const char *ip, unsigned short port) {
 
        goto ERR_SUCC;
 
-       ERR_LSTN:
-       ERR_BIND:
+ERR_LSTN:
+ERR_BIND:
                close(sock);
 
-       ERR_SOCK:
-       ERR_SUCC:
+ERR_SOCK:
+ERR_SUCC:
                return err;
 }
 
@@ -168,7 +168,7 @@ int curly_stream_init(const char *ip, unsigned short port) {
        sch = curl_easy_init();
        bufsize = BSIZE;
 
-//TODO error handling
+    //TODO error handling
 
        err = prepare_tcp_dream(ip, port);
 
@@ -186,12 +186,18 @@ void curly_stream_cleanup(void) {
        sch = NULL;
 }
 
-
+#ifdef USE_TC7200_WORKAROUND
+#define TC7200_DEATH_COUNT 3
+#warning TC7200 workaround in effect
+#endif
 int curly_stream(const char *from_url, unsigned long duration) {
 
        CURLcode cc = CURLE_COULDNT_CONNECT;
        int err = -1;
        double abps, size;
+#ifdef USE_TC7200_WORKAROUND
+    static int tc7200_deathcounter = 0;
+#endif
 
        WHOAMI;
 
@@ -232,6 +238,17 @@ int curly_stream(const char *from_url, unsigned long duration) {
 
        curl_easy_setopt(sch, CURLOPT_BUFFERSIZE, bufsize);
 
+#ifdef USE_TC7200_WORKAROUND
+    //this is to work around a fucked up tc7200 router
+    //thanks a lot, technicolor
+    if (TC7200_DEATH_COUNT == ++tc7200_deathcounter ){
+        putchar('*');
+        curl_easy_setopt(sch, CURLOPT_FRESH_CONNECT, 1);
+        //curl_easy_setopt(sch, CURLOPT_FORBID_REUSE, 1);
+        tc7200_deathcounter = 0;
+    }
+#endif
+
 #ifdef DEBUG
        curl_easy_setopt(sch, CURLOPT_VERBOSE, 1L);
 #endif
diff --git a/itemq.c b/itemq.c
index 35ddd82516f47ff203e5dc7578a1bcf341fd37e1..a5005d0bb3ab2979aa01c74bff4854e2f6f8aeb6 100644 (file)
--- a/itemq.c
+++ b/itemq.c
@@ -5,6 +5,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <time.h>
+#include <signal.h>
 #include "curly.h"
 #include "m3u8.h"
 #include "log.h"
 #define QVC_800_M3U8 "http://live1.qvc.jp/iPhone/800.m3u8"
 
 struct item;
-
-static int in_game;
+static volatile sig_atomic_t in_game;
 static struct item *head, *tail, *curr;
 static int live;
 
+
+static void handle_signal(int sig){
+    printf("Caught signal %d\n",sig);
+    switch (sig){
+    case SIGTERM:
+    case SIGINT:
+        if (!in_game) { puts("Hurry up!\n"); exit(-1);  }
+        in_game = 0;
+        break;
+    }
+}
+
 struct item {
        char url[1024];
        unsigned int hash;
@@ -96,13 +108,13 @@ static int compute_hash(const char *s) {
        return hash;
 }
 
-static void purge_run(void) {
+static void purge_run(int killemall) {
        struct item *n;
 
        WHOAMI;
 
-//purge here
-       while (head && head->purge) {
+    //purge here
+    while (head && (head->purge || killemall)) {
                DPRINT("Purging %s:%X:%d:%d\n", head->url, head->hash, head->done,
                                head->purge);
                n = head;
@@ -333,14 +345,15 @@ int main(int argc, char *argv[]) {
        reset_base(base, url);
 
        curly_init();
-    curly_stream_init("0.0.0.0",1234);
+    curly_stream_init("0.0.0.0",7120);
 
 
        last = now = 0;
        head = tail = curr = NULL;
        live = in_game = 1;
 
-
+    signal(SIGTERM, handle_signal);
+    signal(SIGINT,  handle_signal);
 
        while (in_game) {
                now = time(NULL);
@@ -363,14 +376,14 @@ int main(int argc, char *argv[]) {
                     mclose(f);
 #endif
                                        refreshed = 0;
-                                       purge_run();
+                    purge_run(0);
                                        print_list();
 
                                        if (head->is_streamlist){
                                                strcpy (url, choose_url());
                                                reset_base(base, url);
                                                curr = NULL; //so purge will not spare it
-                                               purge_run();
+                        purge_run(0);
                                                last = 0;
                                                continue;
                                        }
@@ -385,7 +398,7 @@ int main(int argc, char *argv[]) {
 
 
        }
-
+    purge_run(1);
        curly_stream_cleanup();
        curly_cleanup();
        return 0;
index c935a92dc8d3a9fee722f15bd2fb9a30b67c3632..5cb505544ba672f35087e81582b6eae576db1dd3 100644 (file)
--- a/memfile.c
+++ b/memfile.c
@@ -15,7 +15,8 @@ struct _MFILE {
 
 #define M_RD 1
 #define M_WR 2
-#define M_FREE 4
+#define M_AD 4
+#define M_FREE 16
 
 int mclose(MFILE *mf){
     WHOAMI;
@@ -42,7 +43,8 @@ MFILE *mopen(void *p, size_t sz, const char *mode){
                 if ('+' == mode[1]) r->flags |= M_RD;
                 break;
             case 'a':
-                r->flags |= M_WR;
+                r->flags |= M_WR | M_AD;
+                if ('+' == mode[1]) r->flags |= M_RD;
                 break;
         }
         if (NULL == r->pm) r->flags |= M_FREE;
@@ -51,9 +53,12 @@ MFILE *mopen(void *p, size_t sz, const char *mode){
     return r;
 }
 
+#define FIX_WPOS(x) do { if (x->flags & M_AD) x->wpos = x->length; } while (0)
+
 int mputc(int c, MFILE *mf){
         WHOAMI;
     if ((mf->flags & M_WR) == 0) return -1;
+    FIX_WPOS(mf);
     if (mf->wpos + 1 > mf->length){
         mf->pm = realloc(mf->pm, mf->length + 1);
         mf->length++;
@@ -80,7 +85,7 @@ char *mgets(char *s, int size, MFILE *mf){
         WHOAMI;
     size_t p = mf->rpos;
     int c,slen = 0;
-    do { c = mgetc(mf); } while ( (c != -1) && (++slen <= size-1) && (c != '\n'));
+    do { c = mgetc(mf); } while ( (c != -1) && (++slen < size-1) && (c != '\n'));
     if (0 == slen) return NULL;
 
     memcpy(s, mf->pm+p, slen);
@@ -100,6 +105,7 @@ size_t mwrite(const void *ptr, size_t size, size_t nmemb, MFILE *mf){
     void *npf;
     WHOAMI;
     if ((mf->flags & M_WR) == 0) return -1;
+    FIX_WPOS(mf);
     while (nmemb){
         if (mf->wpos + size > mf->length){
           npf = realloc(mf->pm, mf->wpos + size );