-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuffer.c
More file actions
136 lines (111 loc) · 3.13 KB
/
Copy pathbuffer.c
File metadata and controls
136 lines (111 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include "buffer.h"
struct buffer *new_buffer() {
struct buffer *buf = malloc(sizeof(struct buffer));
if (!buf) {
return NULL;
}
buf->data = malloc(BUFFER_SIZE);
memset(buf->data, 0, BUFFER_SIZE);
buf->size = BUFFER_SIZE;
buf->r_idx = 0;
buf->w_idx = 0;
return buf;
}
void buffer_free(struct buffer *buf) {
free(buf->data);
free(buf);
}
char buffer_read_char(struct buffer *buf) {
if (buf->size - buf->r_idx == buf->w_idx) {
return '\0';
}
char c = buf->data[buf->r_idx];
if (buf->r_idx + 1 < buf->size) {
buf->r_idx++;
} else {
buf->r_idx = 0;
}
return c;
}
int buffer_append(struct buffer *buf, char *data, size_t len) {
if (buf->size - buf->r_idx == buf->w_idx) {
return 0;
}
size_t write_size = 0;
size_t write_total = 0;
while(1) {
if (buf->size - buf->r_idx == buf->w_idx || write_total == len) {
break;
}
if (buf->r_idx > buf->w_idx) {
write_size = buf->r_idx - buf->w_idx;
} else if (buf->size - buf->w_idx < len) {
write_size = buf->size - buf->w_idx;
} else {
write_size = len;
}
memcpy(buf->data + buf->w_idx, data, write_size);
if (buf->w_idx + write_size < buf->size) {
buf->w_idx += write_size;
} else {
buf->w_idx = 0;
}
write_total += write_size;
}
return write_total;
}
// if return is -2, it means that the buffer is full
int buffer_read_from_socket(struct buffer *buf, int sock_fd) {
if (buf->size - buf->r_idx == buf->w_idx) {
return -2;
}
int read_size = 0;
if (buf->r_idx > buf->w_idx) {
read_size = buf->r_idx - buf->w_idx;
} else if (buf->size - buf->w_idx < BUFFER_SIZE) {
read_size = buf->size - buf->w_idx;
} else {
read_size = BUFFER_SIZE;
}
int ret = recv(sock_fd, buf->data + buf->w_idx, read_size, 0);
if (ret > 0) {
if (buf->w_idx + ret < buf->size) {
buf->w_idx += ret;
} else {
buf->w_idx = 0;
}
}
return ret;
}
// if return is -2, it means that the buffer is empty
int buffer_write_to_socket(struct buffer *buf, int sock_fd) {
if (buf->size - buf->r_idx == buf->w_idx) {
return -2;
}
int write_size = 0;
int write_total = 0;
while(1) {
if (buf->size - buf->r_idx == buf->w_idx) {
break;
}
write_size = buf->size - buf->r_idx;
if (buf->r_idx < buf->w_idx) {
write_size = buf->w_idx - buf->r_idx;
}
int ret = send(sock_fd, buf->data + buf->r_idx, write_size, 0);
if (ret <= 0) {
return ret;
}
write_total += ret;
if (buf->r_idx < buf->size) {
buf->r_idx += ret;
} else {
buf->r_idx = 0;
}
}
return write_total;
}