Ejemplo n.º 1
0
ssize_t mlib_readchn(chnid_t id, void *buf, size_t size)
{
	ssize_t tbfsize, len;

	tbfsize = mytbf_fetchtoken(channel[id].tbf, size);

	while (1) {
		//printf("fd is %d, file is %s\n", channel[id].fd, channel[id].mp3glob.gl_pathv[channel[id].pos]);

		len = pread(channel[id].fd, buf, tbfsize, channel[id].offset);
		if (len < 0) {
			syslog(LOG_WARNING, "Media file %s read failed", channel[id].mp3glob.gl_pathv[channel[id].pos]);
			channel[id].offset = 0;
			open_next(id);
		} else if (!len) {
			channel[id].offset = 0;
			open_next(id);
		} else {
			channel[id].offset += len;
			break;
		}
	}

	mytbf_returntoken(channel[id].tbf, tbfsize - len);

	return len;
}
Ejemplo n.º 2
0
int main(){
    int fd;
    char buf[BUFSIZE];
    int len, pos, size, ret;
    mytbf_t *tbf;

    fd = open("/etc/services", O_RDONLY);
    if(fd < 0){
        perror("open()");
        exit(1);
    }

    tbf = mytbf_init(10, 1000);
    if(tbf == NULL){
        perror("mytbf_init()");
        exit(1);
    }

    while(1){
        size = mytbf_fetchtoken(tbf, BUFSIZE);
        len = read(fd, buf, size);
        if(len < 0){
            perror("read()");
        }
        else if(len == 0){
            break;
        }
        mytbf_returntoken(tbf, size - len);
        pos = 0;
        while(len > 0){
            ret = write(1, buf + pos, len);
            len -= ret;
            pos += ret;
        }
    }

    close(fd);
    return 0;
}
Ejemplo n.º 3
0
int main(int argc,char **argv) {
	int sfd,dfd = 1;
	char buf[BUFSIZE];
	int len,ret,pos,size;
	mytbf_t *tbf;

	if(argc < 2) {
		fprintf(stderr, "Usage...\n");
		exit(1);
	}

	tbf = mytbf_init(CPS, BURST);
	if(tbf == NULL) {
		fprintf(stderr,"mytbf_init() failed.\n");
		exit(1);
	}

	do {
		sfd = open(argv[1], O_RDONLY);
		if(sfd < 0) {
			if(errno != EINTR) {
				perror("open()");
				exit(1);
			}
		}
	} while(sfd < 0);


	while(1){
		size = mytbf_fetchtoken(tbf,BUFSIZE);
		if(size < 0) {
			fprintf(stderr,"mytbf_fetchtoken():%s\n",strerror(-size));
			exit(1);
		}

		while((len = read(sfd,buf,size)) < 0) {
			if(errno == EINTR)
				continue;
			perror("read()");
			break;
		}
		if(len == 0) break;

		// len > 0
		if(size - len > 0)
			mytbf_returntoken(tbf,size-len);

		pos = 0;

		while(len > 0) {
			ret = write(dfd,buf+pos,len);
			if(ret < 0) {
				if(errno == EINTR)
					continue;
				perror("write()");
				exit(1);
			}
			pos += ret;
			len -= ret;
		}
	}

	close(sfd);
	mytbf_destroy(tbf);

	exit(0);
}