Project

General

Profile

network_write.patch

stbuehler, 2010-01-06 14:12

View differences:

patch/src/network_write.c 2010-01-02 01:24:37.000000000 +0800
36 36
*/
37 37
NETWORK_BACKEND_READ(read) {
38 38
	int toread;
39
	int read_offset;
39 40
	buffer *b;
40 41
	off_t r, start_bytes_in;
41 42
	off_t max_read = 256 * 1024;
......
50 51

  
51 52
	start_bytes_in = cq->bytes_in;
52 53

  
54

  
53 55
	/* use a chunk-size of 16k */
54
	do {
55
		toread = 16384;
56
	
57
#define TOREAD 16384
56 58

  
57
		b = chunkqueue_get_append_buffer(cq);
59
	do {
60
		toread = TOREAD;
61
		read_offset = 0;
58 62

  
59
		buffer_prepare_copy(b, toread);
63
		if (cq->last && cq->last->mem
64
			&& cq->last->mem->used < cq->last->mem->size) {
65
			b = cq->last->mem;
66
			if (b->used > 0) {
67
				toread = b->size - b->used;
68
				read_offset = b->used - 1;
69
			}
70
			else {
71
				toread = b->size - 1;
72
				read_offset = 0;
73
			}
74
		}
75
		else {
76
			b = chunkqueue_get_append_buffer(cq);
77
			buffer_prepare_copy(b, TOREAD+1);
78
		}
60 79

  
61
		if (-1 == (r = read(sock->fd, b->ptr, toread))) {
80
		if (-1 == (r = read(sock->fd, b->ptr + read_offset, toread))) {
62 81
			switch (errno) {
63 82
			case EAGAIN:
64 83
				/* remove the last chunk from the chunkqueue */
......
80 99

  
81 100
		read_something = 1;
82 101

  
83
		b->used = r;
84
		b->ptr[b->used++] = '\0';
102
		if (b->used == 0)  b->used = 1;  /* at first, reserve a position for '\0' */
103
		b->used += r;
104
		b->ptr[b->used-1] = '\0';
105

  
85 106
		cq->bytes_in += r;
86 107

  
87 108
		if (cq->bytes_in - start_bytes_in > max_read) break;