Exemple #1
0
int Dir_stream_file(FileRecord *file, Connection *conn)
{
    ssize_t sent = 0;
    size_t total = 0;
    off_t offset = 0;
    size_t block_size = MAX_SEND_BUFFER;

    // For the non-sendfile slowpath
    char *file_buffer = NULL;
    int nread = 0;
    int amt = 0;
    int tempfd = -1;

    int rc = Dir_send_header(file, conn);
    check_debug(rc, "Failed to write header to socket.");

    if(conn->ssl == NULL) {
        for(total = 0; fdwait(conn->fd, 'w') == 0 && total < file->sb.st_size;
            total += sent) {
            sent = Dir_send(conn->fd, file->fd, &offset, block_size);
            check_debug(sent > 0, "Failed to sendfile on socket: %d from "
                        "file %d", conn->fd, file->fd);
        }
    }
    else {
        // We have to reopen the file, so we don't get ourselves into seek
        // position trouble
        int tempfd = open((const char *)(file->full_path->data), O_RDONLY);
        check(tempfd >= 0, "Could not reopen open file");

        file_buffer = malloc(MAX_SEND_BUFFER);
        check_mem(file_buffer);

        while((nread = fdread(tempfd, file_buffer, MAX_SEND_BUFFER)) > 0) {
            for(amt = 0, sent = 0; sent < nread; sent += amt) {
                amt = conn->send(conn, file_buffer + sent, nread - sent);
                check_debug(amt > 0, "Failed to send on socket: %d from "
                            "file %d", conn->fd, tempfd);
            }
            total += nread;
        }
        free(file_buffer);
        close(tempfd); tempfd = -1;
    }
    
    check(total <= file->sb.st_size, 
            "Wrote way too much, wrote %d but size was %d",
            (int)total, (int)file->sb.st_size);

    check(total == file->sb.st_size,
            "Sent other than expected, sent: %d, but expected: %d", 
            (int)total, (int)file->sb.st_size);

    return total;

error:
    if(file_buffer) free(file_buffer);
    if(tempfd >= 0) close(tempfd);
    return -1;
}
Exemple #2
0
void
fetchtask(void *v)
{
	int fd, n;
	char buf[512];
	struct timeval stTv;

	
	//fprintf(stderr, "starting...\n");
	for(;;){
		if((fd = netdial(TCP, server, 80)) < 0){
			fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate());
			continue;
		}
		stTv.tv_sec = 60;
		stTv.tv_usec = 0;
		setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&stTv, sizeof(stTv));
		snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server);
		fdwrite(fd, buf, strlen(buf));
		while((n = fdread(fd, buf, sizeof buf)) > 0)
			;
		if (n < 0)
		{
			fail++;
		}else{
			sucess++;
		}
		close(fd);
	}
}
void
proxytask(void *v)
{
	int fd ;
	int n = 0 ;
	char buf[1024];
	fd = (int)v;
	
	while((n = fdread(&fd, buf, sizeof buf)) > 0) {
		fdwrite(&fd, buf, n);
	}
	close(fd);

}
Exemple #4
0
void
rwtask(void *v)
{
	int *a, rfd, wfd, n;
	char buf[2048];
	a = v;
	rfd = a[0];
	wfd = a[1];
	free(a);
	while ((n = fdread(rfd, buf, sizeof buf)) > 0) {
		fdwrite(wfd, buf, n);
	}
	shutdown(wfd, SHUT_WR);
	close(rfd);
}
Exemple #5
0
static int
fdreadn(int fd, void *p, int n)
{
	int m, q = n;
	uint8 *buf = p;

	while(n>0){
		if((m = fdread(fd, buf, n)) < 0)
			return m;
		else if(m == 0)
			return q-n;
		n -= m;
		buf += m;
	}

	return q;
}
void taskmain(int argc, char* argv[])
{
	char buf[100];
	//HANDLE ev = CreateEvent(0, TRUE, FALSE, 0);
	FD* h = taskopen(_T("D:\\Tracer.java"), GENERIC_READ, 0, OPEN_EXISTING, 0);
	if (h == 0)
	{
		puts("f****d");
		abort();
	}

	//handlewait(ev);

	while (1)
	{
	if (fdread(h, buf, 99) == -1)
	{
		abort();
	}
	buf[99] = '\0';
	puts(buf);
	}

/*
	if (fdread(h, buf, 100) == -1)
	{
		abort();
	}
	buf[99] = '\0';
	puts(buf);*/


	taskexit(0);


	//taskcreate(test, 0, 32768);
	//while (1)
	//{
	//	puts("turtles");
	//	taskdelay(500);
	//	taskyield();
	//}
}
Exemple #7
0
void
fetchtask(ltctx *lt, void *v)
{
    int fd, n;
    char buf[512];

    fprintf(stderr, "starting...\n");
    for(;;) {
        if((fd = netdial(lt, TCP, server, 80)) < 0) {
            fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate(lt));
            continue;
        }
        snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server);
        fdwrite(lt, fd, buf, strlen(buf));
        while((n = fdread(lt, fd, buf, sizeof buf)) > 0)
            ;
        close(fd);
        write(1, ".", 1);
    }
}
Exemple #8
0
void fetchtask(void *v)
{
    int fd, n;
    char buf[512];

    fprintf(stderr, "starting...\n");
    for (; ;) {
        if ((fd = netdial(TCP, server, 80)) < 0) { // 异步连接服务器, 会造成协程切换
            fprintf(stderr, "dial %s: %s (%s)\n", server, strerror(errno), taskgetstate());
            continue;
        }
        snprintf(buf, sizeof buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", url, server);
        fdwrite(&fd, buf, strlen(buf)); // 异步数据读写, 这里可能会造成协程切换, 因为一定有阻塞操作
        while ((n = fdread(&fd, buf, sizeof buf)) > 0) { // 异步读取
            buf[n] = '\0';
            printf("buf:%s", buf);
        }
        close(fd);
        write(1, ".", 1);
    }
}
Exemple #9
0
int bsd_sendfile(int out_fd, int in_fd, off_t *offset, size_t count) {
    char buf[BSD_SENDFILE_BUF_SIZE];
    int tot, cur, rem, sent;
    int ret = -1;
    off_t orig_offset = 0;

    if (offset != NULL) {
        orig_offset = lseek(in_fd, 0, SEEK_CUR);
        check(orig_offset >= 0, "lseek failure when determining current position");
        check(lseek(in_fd, *offset, SEEK_SET) >= 0, "lseek failure when setting new position");
    }

    for (tot = 0, rem = count, cur = rem; cur != 0 && tot < count; tot += cur, rem -= cur) {
        if (rem >= BSD_SENDFILE_BUF_SIZE) {
            cur = BSD_SENDFILE_BUF_SIZE;
        } else {
            cur = rem;
        }

        cur = fdread(in_fd, buf, cur);
        check(cur >= 0, "Internal sendfile emulation failed: fdread: %i", cur);

        if (cur != 0) {
            sent = fdwrite(out_fd, buf, cur);
            check(sent == cur, "Internal sendfile emulation failed: fdread: %i, fdwrite: %i", cur, sent);
        }
    }

    ret = tot;

error:
    if (offset != NULL) {
        if (ret != -1) {
            *offset += tot;
        }
        lseek(in_fd, orig_offset, SEEK_SET);
    }
    return ret;
}
Exemple #10
0
inline
int cornet_read(int fd, char* buf, int len) {
    return fdread(fd, buf, len);
}
Exemple #11
0
static ssize_t file_recv(IOBuf *iob, char *buffer, int len)
{
    return fdread(iob->fd, buffer, len);
}