comparison recpt1/recpt1.c @ 28:97c820e30737

limit write length at once so that udp/stdout works well.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sun, 01 Mar 2009 22:42:09 +0900
parents 763cf84d2dc7
children 6004fe4f37f9
comparison
equal deleted inserted replaced
27:763cf84d2dc7 28:97c820e30737
21 #include "pt1_ioctl.h" 21 #include "pt1_ioctl.h"
22 22
23 #include "recpt1.h" 23 #include "recpt1.h"
24 #include "decoder.h" 24 #include "decoder.h"
25 25
26 /* maximum write length at once */
27 #define SIZE_CHANK 1316
28
26 /* globals */ 29 /* globals */
27 int f_exit = FALSE; 30 int f_exit = FALSE;
28 31
29 /* type definitions */ 32 /* type definitions */
30 typedef struct sock_data { 33 typedef struct sock_data {
202 sfd = data->sock_data->sfd; 205 sfd = data->sock_data->sfd;
203 addr = &data->sock_data->addr; 206 addr = &data->sock_data->addr;
204 } 207 }
205 208
206 while(1) { 209 while(1) {
207 ssize_t wc; 210 ssize_t wc = 0;
208 qbuf = dequeue(p_queue); 211 qbuf = dequeue(p_queue);
209 /* no entry in the queue */ 212 /* no entry in the queue */
210 if(qbuf == NULL) { 213 if(qbuf == NULL) {
211 break; 214 break;
212 } 215 }
215 sbuf.size = qbuf->size; 218 sbuf.size = qbuf->size;
216 219
217 buf = sbuf; /* default */ 220 buf = sbuf; /* default */
218 221
219 if(use_b25) { 222 if(use_b25) {
220 /* write data to output file*/
221 code = b25_decode(dec, &sbuf, &dbuf); 223 code = b25_decode(dec, &sbuf, &dbuf);
222 if(code < 0) 224 if(code < 0)
223 fprintf(stderr, "b25_decode failed\n"); 225 fprintf(stderr, "b25_decode failed\n");
224 else 226 else
225 buf = dbuf; 227 buf = dbuf;
226 } 228 }
227 229
228 if(!fileless) { 230 if(!fileless) {
229 wc = write(wfd, buf.data, buf.size); 231 /* write data to output file */
230 if(wc < 0) { 232 int size_remain = buf.size;
231 if(errno == EPIPE) 233 int offset = 0;
232 pthread_kill(signal_thread, SIGPIPE); 234 while(size_remain > 0) {
233 else 235 int ws = size_remain < SIZE_CHANK ? size_remain : SIZE_CHANK;
234 pthread_kill(signal_thread, SIGUSR2); 236 wc = write(wfd, buf.data + offset, ws);
237 if(wc < 0) {
238 if(errno == EPIPE)
239 pthread_kill(signal_thread, SIGPIPE);
240 else
241 pthread_kill(signal_thread, SIGUSR2);
242 break;
243 }
244 size_remain -= wc;
245 offset += wc;
235 } 246 }
236 } 247 }
237 248
238 if(use_udp && sfd != -1) { 249 if(use_udp && sfd != -1) {
239 sendto(sfd, buf.data, buf.size, 0, 250 /* write data to socket */
240 (struct sockaddr *)addr, sizeof(*addr)); 251 int size_remain = buf.size;
252 int offset = 0;
253 while(size_remain > 0) {
254 int ws = size_remain < SIZE_CHANK ? size_remain : SIZE_CHANK;
255 wc = write(sfd, buf.data + offset, ws);
256 if(wc < 0) {
257 if(errno == EPIPE)
258 pthread_kill(signal_thread, SIGPIPE);
259 break;
260 }
261 size_remain -= wc;
262 offset += wc;
263 }
241 } 264 }
242 265
243 free(qbuf); 266 free(qbuf);
244 267
245 /* normal exit */ 268 /* normal exit */
264 pthread_kill(signal_thread, SIGUSR2); 287 pthread_kill(signal_thread, SIGUSR2);
265 } 288 }
266 } 289 }
267 290
268 if(use_udp && sfd != -1) { 291 if(use_udp && sfd != -1) {
269 sendto(sfd, buf.data, buf.size, 0, 292 wc = write(sfd, buf.data, buf.size);
270 (struct sockaddr *)addr, sizeof(*addr)); 293 if(wc < 0) {
294 if(errno == EPIPE)
295 pthread_kill(signal_thread, SIGPIPE);
296 }
271 } 297 }
272 298
273 break; 299 break;
274 } 300 }
275 } 301 }
660 perror("gethostbyname"); 686 perror("gethostbyname");
661 return 1; 687 return 1;
662 } 688 }
663 ia.s_addr = *(in_addr_t*) (hoste->h_addr_list[0]); 689 ia.s_addr = *(in_addr_t*) (hoste->h_addr_list[0]);
664 } 690 }
691 if((sockdata->sfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
692 perror("socket");
693 return 1;
694 }
695
665 sockdata->addr.sin_family = AF_INET; 696 sockdata->addr.sin_family = AF_INET;
666 sockdata->addr.sin_port = htons (port_to); 697 sockdata->addr.sin_port = htons (port_to);
667 sockdata->addr.sin_addr.s_addr = ia.s_addr; 698 sockdata->addr.sin_addr.s_addr = ia.s_addr;
668 if((sockdata->sfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { 699
669 perror("socket"); 700 if(connect(sockdata->sfd, (struct sockaddr *)&sockdata->addr,
701 sizeof(sockdata->addr)) < 0) {
702 perror("connect");
670 return 1; 703 return 1;
671 } 704 }
672 } 705 }
673 706
674 /* setup tuner */ 707 /* setup tuner */