Refactor of generate tests.
[awesomized/libmemcached] / lib / memcached_io.c
1 /*
2 Basic socket buffered IO
3 */
4
5 #include "common.h"
6 #include "memcached_io.h"
7 #include <sys/select.h>
8 #include <poll.h>
9
10 typedef enum {
11 MEM_READ,
12 MEM_WRITE,
13 } memc_read_or_write;
14
15 static ssize_t io_flush(memcached_server_st *ptr, memcached_return *error);
16
17 static memcached_return io_wait(memcached_server_st *ptr,
18 memc_read_or_write read_or_write)
19 {
20 struct pollfd fds[1];
21 short flags= 0;
22 int error;
23
24 if (read_or_write == MEM_WRITE) /* write */
25 flags= POLLOUT | POLLERR;
26 else
27 flags= POLLIN | POLLERR;
28
29 memset(&fds, 0, sizeof(struct pollfd));
30 fds[0].fd= ptr->fd;
31 fds[0].events= flags;
32
33 error= poll(fds, 1, ptr->root->poll_timeout);
34
35 if (error == 1)
36 return MEMCACHED_SUCCESS;
37 else if (error == 0)
38 {
39 return MEMCACHED_TIMEOUT;
40 }
41
42 /* Imposssible for anything other then -1 */
43 WATCHPOINT_ASSERT(error == -1);
44 memcached_quit_server(ptr, 1);
45
46 return MEMCACHED_FAILURE;
47
48 }
49
50 #ifdef UNUSED
51 void memcached_io_preread(memcached_st *ptr)
52 {
53 unsigned int x;
54
55 return;
56
57 for (x= 0; x < ptr->number_of_hosts; x++)
58 {
59 if (memcached_server_response_count(ptr, x) &&
60 ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
61 {
62 size_t data_read;
63
64 data_read= read(ptr->hosts[x].fd,
65 ptr->hosts[x].read_ptr + ptr->hosts[x].read_data_length,
66 MEMCACHED_MAX_BUFFER - ptr->hosts[x].read_data_length);
67 if (data_read == -1)
68 continue;
69
70 ptr->hosts[x].read_buffer_length+= data_read;
71 ptr->hosts[x].read_data_length+= data_read;
72 }
73 }
74 }
75 #endif
76
77 ssize_t memcached_io_read(memcached_server_st *ptr,
78 char *buffer, size_t length)
79 {
80 char *buffer_ptr;
81
82 buffer_ptr= buffer;
83
84 while (length)
85 {
86 uint8_t found_eof= 0;
87 if (!ptr->read_buffer_length)
88 {
89 size_t data_read;
90
91 while (1)
92 {
93 data_read= read(ptr->fd,
94 ptr->read_buffer,
95 MEMCACHED_MAX_BUFFER);
96 if (data_read == -1)
97 {
98 switch (errno)
99 {
100 case EAGAIN:
101 {
102 memcached_return rc;
103
104 rc= io_wait(ptr, MEM_READ);
105
106 if (rc == MEMCACHED_SUCCESS)
107 continue;
108
109 memcached_quit_server(ptr, 1);
110 return -1;
111 }
112 default:
113 {
114 memcached_quit_server(ptr, 1);
115 ptr->cached_errno= errno;
116 return -1;
117 }
118 }
119 }
120 else if (data_read)
121 break;
122 /* If zero, just keep looping unless testing, then assert() */
123 else
124 {
125 WATCHPOINT_ASSERT(0);
126 found_eof= 1;
127 break;
128 }
129 }
130
131 ptr->read_data_length= data_read;
132 ptr->read_buffer_length= data_read;
133 ptr->read_ptr= ptr->read_buffer;
134 }
135
136 if (length > 1)
137 {
138 size_t difference;
139
140 difference= (length > ptr->read_buffer_length) ? ptr->read_buffer_length : length;
141
142 memcpy(buffer_ptr, ptr->read_ptr, difference);
143 length -= difference;
144 ptr->read_ptr+= difference;
145 ptr->read_buffer_length-= difference;
146 buffer_ptr+= difference;
147 }
148 else
149 {
150 *buffer_ptr= *ptr->read_ptr;
151 length--;
152 ptr->read_ptr++;
153 ptr->read_buffer_length--;
154 buffer_ptr++;
155 }
156
157 if (found_eof)
158 break;
159 }
160
161 return (size_t)(buffer_ptr - buffer);
162 }
163
164 ssize_t memcached_io_write(memcached_server_st *ptr,
165 char *buffer, size_t length, char with_flush)
166 {
167 unsigned long long x;
168
169 for (x= 0; x < length; x++)
170 {
171 ptr->write_buffer[ptr->write_buffer_offset]= buffer[x];
172 ptr->write_buffer_offset++;
173 WATCHPOINT_ASSERT(ptr->write_buffer_offset <= MEMCACHED_MAX_BUFFER);
174
175 if (ptr->write_buffer_offset == MEMCACHED_MAX_BUFFER)
176 {
177 memcached_return rc;
178 ssize_t sent_length;
179
180 sent_length= io_flush(ptr, &rc);
181 if (sent_length == -1)
182 return -1;
183
184 WATCHPOINT_ASSERT(sent_length == MEMCACHED_MAX_BUFFER);
185 }
186 }
187
188 if (with_flush)
189 {
190 memcached_return rc;
191 if (io_flush(ptr, &rc) == -1)
192 return -1;
193 }
194
195 return length;
196 }
197
198 memcached_return memcached_io_close(memcached_server_st *ptr)
199 {
200 close(ptr->fd);
201
202 return MEMCACHED_SUCCESS;
203 }
204
205 static ssize_t io_flush(memcached_server_st *ptr,
206 memcached_return *error)
207 {
208 size_t sent_length;
209 size_t return_length;
210 char *local_write_ptr= ptr->write_buffer;
211 size_t write_length= ptr->write_buffer_offset;
212
213 *error= MEMCACHED_SUCCESS;
214
215 if (ptr->write_buffer_offset == 0)
216 return 0;
217
218 /* Looking for memory overflows */
219 if (write_length == MEMCACHED_MAX_BUFFER)
220 WATCHPOINT_ASSERT(ptr->write_buffer == local_write_ptr);
221 WATCHPOINT_ASSERT((ptr->write_buffer + MEMCACHED_MAX_BUFFER) >= (local_write_ptr + write_length));
222 return_length= 0;
223 while (write_length)
224 {
225 sent_length= 0;
226 if (ptr->type == MEMCACHED_CONNECTION_UDP)
227 {
228 sent_length= sendto(ptr->fd, local_write_ptr, write_length, 0,
229 (struct sockaddr *)&ptr->address_info->ai_addr,
230 sizeof(struct sockaddr));
231 }
232 else
233 {
234 if ((ssize_t)(sent_length= write(ptr->fd, local_write_ptr,
235 write_length)) == -1)
236 {
237 switch (errno)
238 {
239 case ENOBUFS:
240 continue;
241 case EAGAIN:
242 {
243 memcached_return rc;
244 rc= io_wait(ptr, MEM_WRITE);
245
246 if (rc == MEMCACHED_SUCCESS)
247 continue;
248
249 memcached_quit_server(ptr, 1);
250 return -1;
251 }
252 default:
253 memcached_quit_server(ptr, 1);
254 ptr->cached_errno= errno;
255 *error= MEMCACHED_ERRNO;
256 return -1;
257 }
258 }
259 }
260
261 local_write_ptr+= sent_length;
262 write_length-= sent_length;
263 return_length+= sent_length;
264 }
265
266 WATCHPOINT_ASSERT(write_length == 0);
267 WATCHPOINT_ASSERT(return_length == ptr->write_buffer_offset);
268 ptr->write_buffer_offset= 0;
269
270 return return_length;
271 }
272
273 /*
274 Eventually we will just kill off the server with the problem.
275 */
276 void memcached_io_reset(memcached_server_st *ptr)
277 {
278 ptr->write_buffer_offset= 0;
279 memcached_quit_server(ptr, 0);
280 }