Mercurial > pt1
comparison recpt1/recpt1.c @ 26:c6264c15f39d
simplify reader_func()
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Sat, 28 Feb 2009 00:25:25 +0900 |
parents | 011cb9337729 |
children | 763cf84d2dc7 |
comparison
equal
deleted
inserted
replaced
25:011cb9337729 | 26:c6264c15f39d |
---|---|
188 int use_b25 = dec ? TRUE : FALSE; | 188 int use_b25 = dec ? TRUE : FALSE; |
189 int use_udp = data->sock_data ? TRUE : FALSE; | 189 int use_udp = data->sock_data ? TRUE : FALSE; |
190 int fileless = FALSE; | 190 int fileless = FALSE; |
191 int sfd = -1; | 191 int sfd = -1; |
192 pthread_t signal_thread = data->signal_thread; | 192 pthread_t signal_thread = data->signal_thread; |
193 struct sockaddr *addr = NULL; | 193 struct sockaddr_in *addr = NULL; |
194 BUFSZ *buf; | 194 BUFSZ *qbuf; |
195 ARIB_STD_B25_BUFFER sbuf, dbuf; | 195 ARIB_STD_B25_BUFFER sbuf, dbuf, buf; |
196 int code; | 196 int code; |
197 | 197 |
198 if(wfd == -1) | 198 if(wfd == -1) |
199 fileless = TRUE; | 199 fileless = TRUE; |
200 | 200 |
201 if(use_udp) { | 201 if(use_udp) { |
202 sfd = data->sock_data->sfd; | 202 sfd = data->sock_data->sfd; |
203 addr = (struct sockaddr *)&data->sock_data->addr; | 203 addr = &data->sock_data->addr; |
204 } | 204 } |
205 | 205 |
206 while(1) { | 206 while(1) { |
207 | 207 ssize_t wc; |
208 if(fileless) { | 208 qbuf = dequeue(p_queue); |
209 | 209 /* no entry in the queue */ |
210 buf = dequeue(p_queue); | 210 if(qbuf == NULL) { |
211 /* no entry in the queue */ | 211 break; |
212 if(buf == NULL) { | 212 } |
213 break; | 213 |
214 } | 214 sbuf.data = qbuf->buffer; |
215 | 215 sbuf.size = qbuf->size; |
216 sbuf.data = buf->buffer; | 216 |
217 sbuf.size = buf->size; | 217 buf = sbuf; /* default */ |
218 | |
219 if(use_b25) { | |
220 /* write data to output file*/ | |
221 code = b25_decode(dec, &sbuf, &dbuf); | |
222 if(code < 0) | |
223 fprintf(stderr, "b25_decode failed\n"); | |
224 else | |
225 buf = dbuf; | |
226 } | |
227 | |
228 if(!fileless) { | |
229 wc = write(wfd, buf.data, buf.size); | |
230 if(wc < 0) { | |
231 if(errno == EPIPE) | |
232 pthread_kill(signal_thread, SIGPIPE); | |
233 else | |
234 pthread_kill(signal_thread, SIGUSR2); | |
235 } | |
236 } | |
237 | |
238 if(use_udp && sfd != -1) { | |
239 sendto(sfd, buf.data, buf.size, 0, | |
240 (struct sockaddr *)addr, sizeof(*addr)); | |
241 } | |
242 | |
243 free(qbuf); | |
244 | |
245 /* normal exit */ | |
246 if((f_exit) && (!p_queue->num_used)) { | |
247 | |
248 buf = sbuf; /* default */ | |
218 | 249 |
219 if(use_b25) { | 250 if(use_b25) { |
220 /* write data to output file*/ | 251 code = b25_finish(dec, &sbuf, &dbuf); |
221 code = b25_decode(dec, &sbuf, &dbuf); | 252 if(code < 0) |
222 if(code < 0) { | 253 fprintf(stderr, "b25_finish failed\n"); |
223 fprintf(stderr, "b25_decode failed\n"); | 254 else |
224 break; | 255 buf = dbuf; |
225 } | 256 } |
226 | 257 |
227 if(use_udp && sfd != -1) { | 258 if(!fileless) { |
228 sendto(sfd, dbuf.data, dbuf.size, 0, | 259 wc = write(wfd, buf.data, buf.size); |
229 addr, sizeof(struct sockaddr_in)); | |
230 } | |
231 free(buf); | |
232 } | |
233 else { | |
234 if(use_udp && sfd != -1) { | |
235 sendto(sfd, sbuf.data, sbuf.size, 0, | |
236 addr, sizeof(struct sockaddr_in)); | |
237 } | |
238 free(buf); | |
239 } | |
240 | |
241 /* normal exit */ | |
242 if((f_exit) && (!p_queue->num_used)) { | |
243 if(use_b25) { | |
244 code = b25_finish(dec, &sbuf, &dbuf); | |
245 if(code < 0) { | |
246 fprintf(stderr, "b25_finish failed\n"); | |
247 break; | |
248 } | |
249 | |
250 if(use_udp && sfd != -1) { | |
251 sendto(sfd, dbuf.data, dbuf.size, 0, | |
252 addr, sizeof(struct sockaddr_in)); | |
253 } | |
254 } | |
255 break; | |
256 } | |
257 } /* end of fileless */ | |
258 else { | |
259 | |
260 ssize_t wc; | |
261 buf = dequeue(p_queue); | |
262 /* no entry in the queue */ | |
263 if(buf == NULL) { | |
264 break; | |
265 } | |
266 | |
267 sbuf.data = buf->buffer; | |
268 sbuf.size = buf->size; | |
269 | |
270 if(use_b25) { | |
271 /* write data to output file*/ | |
272 code = b25_decode(dec, &sbuf, &dbuf); | |
273 if(code < 0) { | |
274 fprintf(stderr, "b25_decode failed\n"); | |
275 break; | |
276 } | |
277 wc = write(wfd, dbuf.data, dbuf.size); | |
278 if(wc < 0) { | 260 if(wc < 0) { |
279 if(errno == EPIPE) | 261 if(errno == EPIPE) |
280 pthread_kill(signal_thread, SIGPIPE); | 262 pthread_kill(signal_thread, SIGPIPE); |
281 else | 263 else |
282 pthread_kill(signal_thread, SIGUSR2); | 264 pthread_kill(signal_thread, SIGUSR2); |
283 } | 265 } |
284 | 266 } |
285 if(use_udp && sfd != -1) { | 267 |
286 sendto(sfd, dbuf.data, dbuf.size, 0, | 268 if(use_udp && sfd != -1) { |
287 addr, sizeof(struct sockaddr_in)); | 269 sendto(sfd, buf.data, buf.size, 0, |
288 } | 270 (struct sockaddr *)addr, sizeof(*addr)); |
289 free(buf); | 271 } |
290 } | 272 |
291 else { | 273 break; |
292 wc = write(wfd, sbuf.data, sbuf.size); | |
293 if(wc < 0) { | |
294 if(errno == EPIPE) | |
295 pthread_kill(signal_thread, SIGPIPE); | |
296 else | |
297 pthread_kill(signal_thread, SIGUSR2); | |
298 } | |
299 | |
300 if(use_udp && sfd != -1) { | |
301 sendto(sfd, sbuf.data, sbuf.size, 0, | |
302 addr, sizeof(struct sockaddr_in)); | |
303 } | |
304 free(buf); | |
305 } | |
306 | |
307 /* normal exit */ | |
308 if((f_exit) && (!p_queue->num_used)) { | |
309 if(use_b25) { | |
310 code = b25_finish(dec, &sbuf, &dbuf); | |
311 if(code < 0) { | |
312 fprintf(stderr, "b25_finish failed\n"); | |
313 break; | |
314 } | |
315 wc = write(wfd, dbuf.data, dbuf.size); | |
316 if(wc < 0) { | |
317 if(errno == EPIPE) | |
318 pthread_kill(signal_thread, SIGPIPE); | |
319 else | |
320 pthread_kill(signal_thread, SIGUSR2); | |
321 } | |
322 | |
323 if(use_udp && sfd != -1) { | |
324 sendto(sfd, dbuf.data, dbuf.size, 0, | |
325 addr, sizeof(struct sockaddr_in)); | |
326 } | |
327 } | |
328 break; | |
329 } | |
330 } | 274 } |
331 } | 275 } |
332 | 276 |
333 return NULL; | 277 return NULL; |
334 } | 278 } |
766 | 710 |
767 /* read from tuner */ | 711 /* read from tuner */ |
768 while(1) { | 712 while(1) { |
769 if(f_exit) | 713 if(f_exit) |
770 break; | 714 break; |
715 | |
771 time(&cur_time); | 716 time(&cur_time); |
772 bufptr = malloc(sizeof(BUFSZ)); | 717 bufptr = malloc(sizeof(BUFSZ)); |
773 bufptr->size = read(tfd, bufptr->buffer, MAX_READ_SIZE); | 718 bufptr->size = read(tfd, bufptr->buffer, MAX_READ_SIZE); |
774 if(bufptr->size <= 0) { | 719 if(bufptr->size <= 0) { |
775 if((cur_time - start_time) >= recsec && !indefinite) { | 720 if((cur_time - start_time) >= recsec && !indefinite) { |