changeset 26:c6264c15f39d

simplify reader_func()
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sat, 28 Feb 2009 00:25:25 +0900
parents 011cb9337729
children 763cf84d2dc7
files recpt1/recpt1.c
diffstat 1 files changed, 55 insertions(+), 110 deletions(-) [+]
line wrap: on
line diff
--- a/recpt1/recpt1.c	Fri Feb 27 22:49:41 2009 +0900
+++ b/recpt1/recpt1.c	Sat Feb 28 00:25:25 2009 +0900
@@ -190,9 +190,9 @@
     int fileless = FALSE;
     int sfd = -1;
     pthread_t signal_thread = data->signal_thread;
-    struct sockaddr *addr = NULL;
-    BUFSZ *buf;
-    ARIB_STD_B25_BUFFER sbuf, dbuf;
+    struct sockaddr_in *addr = NULL;
+    BUFSZ *qbuf;
+    ARIB_STD_B25_BUFFER sbuf, dbuf, buf;
     int code;
 
     if(wfd == -1)
@@ -200,133 +200,77 @@
 
     if(use_udp) {
         sfd = data->sock_data->sfd;
-        addr = (struct sockaddr *)&data->sock_data->addr;
+        addr = &data->sock_data->addr;
     }
 
     while(1) {
+        ssize_t wc;
+        qbuf = dequeue(p_queue);
+        /* no entry in the queue */
+        if(qbuf == NULL) {
+            break;
+        }
 
-        if(fileless) {
+        sbuf.data = qbuf->buffer;
+        sbuf.size = qbuf->size;
+
+        buf = sbuf; /* default */
+
+        if(use_b25) {
+            /* write data to output file*/
+            code = b25_decode(dec, &sbuf, &dbuf);
+            if(code < 0)
+                fprintf(stderr, "b25_decode failed\n");
+            else
+                buf = dbuf;
+        }
 
-            buf = dequeue(p_queue);
-            /* no entry in the queue */
-            if(buf == NULL) {
-                break;
+        if(!fileless) {
+            wc = write(wfd, buf.data, buf.size);
+            if(wc < 0) {
+                if(errno == EPIPE)
+                    pthread_kill(signal_thread, SIGPIPE);
+                else
+                    pthread_kill(signal_thread, SIGUSR2);
             }
+        }
 
-            sbuf.data = buf->buffer;
-            sbuf.size = buf->size;
+        if(use_udp && sfd != -1) {
+            sendto(sfd, buf.data, buf.size, 0,
+                   (struct sockaddr *)addr, sizeof(*addr));
+        }
+
+        free(qbuf);
+
+        /* normal exit */
+        if((f_exit) && (!p_queue->num_used)) {
+
+            buf = sbuf; /* default */
 
             if(use_b25) {
-                /* write data to output file*/
-                code = b25_decode(dec, &sbuf, &dbuf);
-                if(code < 0) {
-                    fprintf(stderr, "b25_decode failed\n");
-                    break;
-                }
-
-                if(use_udp && sfd != -1) {
-                    sendto(sfd, dbuf.data, dbuf.size, 0,
-                           addr, sizeof(struct sockaddr_in));
-                }
-                free(buf);
-            }
-            else {
-                if(use_udp && sfd != -1) {
-                    sendto(sfd, sbuf.data, sbuf.size, 0,
-                           addr, sizeof(struct sockaddr_in));
-                }
-                free(buf);
+                code = b25_finish(dec, &sbuf, &dbuf);
+                if(code < 0)
+                    fprintf(stderr, "b25_finish failed\n");
+                else
+                    buf = dbuf;
             }
 
-            /* normal exit */
-            if((f_exit) && (!p_queue->num_used)) {
-                if(use_b25) {
-                    code = b25_finish(dec, &sbuf, &dbuf);
-                    if(code < 0) {
-                        fprintf(stderr, "b25_finish failed\n");
-                        break;
-                    }
-
-                    if(use_udp && sfd != -1) {
-                        sendto(sfd, dbuf.data, dbuf.size, 0,
-                               addr, sizeof(struct sockaddr_in));
-                    }
-                }
-                break;
-            }
-        } /* end of fileless */
-        else {
-
-            ssize_t wc;
-            buf = dequeue(p_queue);
-            /* no entry in the queue */
-            if(buf == NULL) {
-                break;
-            }
-
-            sbuf.data = buf->buffer;
-            sbuf.size = buf->size;
-
-            if(use_b25) {
-                /* write data to output file*/
-                code = b25_decode(dec, &sbuf, &dbuf);
-                if(code < 0) {
-                    fprintf(stderr, "b25_decode failed\n");
-                    break;
-                }
-                wc = write(wfd, dbuf.data, dbuf.size);
+            if(!fileless) {
+                wc = write(wfd, buf.data, buf.size);
                 if(wc < 0) {
                     if(errno == EPIPE)
                         pthread_kill(signal_thread, SIGPIPE);
                     else
                         pthread_kill(signal_thread, SIGUSR2);
                 }
-
-                if(use_udp && sfd != -1) {
-                    sendto(sfd, dbuf.data, dbuf.size, 0,
-                           addr, sizeof(struct sockaddr_in));
-                }
-                free(buf);
-            }
-            else {
-                wc = write(wfd, sbuf.data, sbuf.size);
-                if(wc < 0) {
-                    if(errno == EPIPE)
-                        pthread_kill(signal_thread, SIGPIPE);
-                    else
-                        pthread_kill(signal_thread, SIGUSR2);
-                }
-
-                if(use_udp && sfd != -1) {
-                    sendto(sfd, sbuf.data, sbuf.size, 0,
-                           addr, sizeof(struct sockaddr_in));
-                }
-                free(buf);
             }
 
-            /* normal exit */
-            if((f_exit) && (!p_queue->num_used)) {
-                if(use_b25) {
-                    code = b25_finish(dec, &sbuf, &dbuf);
-                    if(code < 0) {
-                        fprintf(stderr, "b25_finish failed\n");
-                        break;
-                    }
-                    wc = write(wfd, dbuf.data, dbuf.size);
-                    if(wc < 0) {
-                        if(errno == EPIPE)
-                            pthread_kill(signal_thread, SIGPIPE);
-                        else
-                            pthread_kill(signal_thread, SIGUSR2);
-                    }
+            if(use_udp && sfd != -1) {
+                sendto(sfd, buf.data, buf.size, 0,
+                       (struct sockaddr *)addr, sizeof(*addr));
+            }
 
-                    if(use_udp && sfd != -1) {
-                        sendto(sfd, dbuf.data, dbuf.size, 0,
-                               addr, sizeof(struct sockaddr_in));
-                    }
-                }
-                break;
-            }
+            break;
         }
     }
 
@@ -768,6 +712,7 @@
     while(1) {
         if(f_exit)
             break;
+
         time(&cur_time);
         bufptr = malloc(sizeof(BUFSZ));
         bufptr->size = read(tfd, bufptr->buffer, MAX_READ_SIZE);