2 Basic socket buffered IO
6 #include "memcached_io.h"
7 #include <sys/select.h>
15 static ssize_t
io_flush(memcached_server_st
*ptr
, memcached_return
*error
);
16 static void increment_udp_message_id(memcached_server_st
*ptr
);
18 static memcached_return
io_wait(memcached_server_st
*ptr
,
19 memc_read_or_write read_or_write
)
25 if (read_or_write
== MEM_WRITE
) /* write */
30 memset(&fds
, 0, sizeof(struct pollfd
));
35 ** We are going to block on write, but at least on Solaris we might block
36 ** on write if we haven't read anything from our input buffer..
37 ** Try to purge the input buffer if we don't do any flow control in the
38 ** application layer (just sending a lot of data etc)
39 ** The test is moved down in the purge function to avoid duplication of
42 if (read_or_write
== MEM_WRITE
)
44 memcached_return rc
=memcached_purge(ptr
);
45 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
46 return MEMCACHED_FAILURE
;
49 error
= poll(fds
, 1, ptr
->root
->poll_timeout
);
52 return MEMCACHED_SUCCESS
;
55 return MEMCACHED_TIMEOUT
;
58 /* Imposssible for anything other then -1 */
59 WATCHPOINT_ASSERT(error
== -1);
60 memcached_quit_server(ptr
, 1);
62 return MEMCACHED_FAILURE
;
67 void memcached_io_preread(memcached_st
*ptr
)
73 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
75 if (memcached_server_response_count(ptr
, x
) &&
76 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
80 data_read
= read(ptr
->hosts
[x
].fd
,
81 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
82 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
);
86 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
87 ptr
->hosts
[x
].read_data_length
+= data_read
;
93 memcached_return
memcached_io_read(memcached_server_st
*ptr
,
94 void *buffer
, size_t length
, ssize_t
*nread
)
102 if (!ptr
->read_buffer_length
)
108 data_read
= read(ptr
->fd
, ptr
->read_buffer
, MEMCACHED_MAX_BUFFER
);
111 else if (data_read
== -1)
113 ptr
->cached_errno
= errno
;
114 memcached_return rc
= MEMCACHED_UNKNOWN_READ_FAILURE
;
119 if ((rc
= io_wait(ptr
, MEM_READ
)) == MEMCACHED_SUCCESS
)
125 memcached_quit_server(ptr
, 1);
134 EOF. Any data received so far is incomplete
135 so discard it. This always reads by byte in case of TCP
136 and protocol enforcement happens at memcached_response()
137 looking for '\n'. We do not care for UDB which requests 8 bytes
138 at once. Generally, this means that connection went away. Since
139 for blocking I/O we do not return 0 and for non-blocking case
140 it will return EGAIN if data is not immediatly available.
142 memcached_quit_server(ptr
, 1);
144 return MEMCACHED_UNKNOWN_READ_FAILURE
;
148 ptr
->io_bytes_sent
= 0;
149 ptr
->read_data_length
= (size_t) data_read
;
150 ptr
->read_buffer_length
= (size_t) data_read
;
151 ptr
->read_ptr
= ptr
->read_buffer
;
158 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
160 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
161 length
-= difference
;
162 ptr
->read_ptr
+= difference
;
163 ptr
->read_buffer_length
-= difference
;
164 buffer_ptr
+= difference
;
168 *buffer_ptr
= *ptr
->read_ptr
;
170 ptr
->read_buffer_length
--;
176 ptr
->server_failure_counter
= 0;
177 *nread
= (ssize_t
)(buffer_ptr
- (char*)buffer
);
178 return MEMCACHED_SUCCESS
;
181 ssize_t
memcached_io_write(memcached_server_st
*ptr
,
182 const void *buffer
, size_t length
, char with_flush
)
184 size_t original_length
;
185 const char* buffer_ptr
;
187 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
189 original_length
= length
;
198 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
200 //UDP does not support partial writes
201 buffer_end
= MAX_UDP_DATAGRAM_LENGTH
;
202 should_write
= length
;
203 if (ptr
->write_buffer_offset
+ should_write
> buffer_end
)
208 buffer_end
= MEMCACHED_MAX_BUFFER
;
209 should_write
= buffer_end
- ptr
->write_buffer_offset
;
210 should_write
= (should_write
< length
) ? should_write
: length
;
213 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
214 memcpy(write_ptr
, buffer_ptr
, should_write
);
215 ptr
->write_buffer_offset
+= should_write
;
216 buffer_ptr
+= should_write
;
217 length
-= should_write
;
219 if (ptr
->write_buffer_offset
== buffer_end
&& ptr
->type
!= MEMCACHED_CONNECTION_UDP
)
224 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
225 sent_length
= io_flush(ptr
, &rc
);
226 if (sent_length
== -1)
229 /* If io_flush calls memcached_purge, sent_length may be 0 */
230 unlikely (sent_length
!= 0)
232 WATCHPOINT_ASSERT(sent_length
== (ssize_t
)buffer_end
);
240 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
241 if (io_flush(ptr
, &rc
) == -1)
245 return (ssize_t
) original_length
;
248 memcached_return
memcached_io_close(memcached_server_st
*ptr
)
253 return MEMCACHED_SUCCESS
;
255 /* in case of death shutdown to avoid blocking at close() */
258 r
= shutdown(ptr
->fd
, SHUT_RDWR
);
261 if (r
&& errno
!= ENOTCONN
)
263 WATCHPOINT_NUMBER(ptr
->fd
);
264 WATCHPOINT_ERRNO(errno
);
265 WATCHPOINT_ASSERT(errno
);
273 WATCHPOINT_ERRNO(errno
);
276 return MEMCACHED_SUCCESS
;
279 memcached_server_st
*memcached_io_get_readable_server(memcached_st
*memc
)
281 #define MAX_SERVERS_TO_POLL 100
282 struct pollfd fds
[MAX_SERVERS_TO_POLL
];
283 unsigned int host_index
= 0;
285 for (unsigned int x
= 0;
286 x
< memc
->number_of_hosts
&& host_index
< MAX_SERVERS_TO_POLL
;
289 if (memc
->hosts
[x
].read_buffer_length
> 0) /* I have data in the buffer */
290 return &memc
->hosts
[x
];
292 if (memcached_server_response_count(&memc
->hosts
[x
]) > 0)
294 fds
[host_index
].events
= POLLIN
;
295 fds
[host_index
].revents
= 0;
296 fds
[host_index
].fd
= memc
->hosts
[x
].fd
;
303 /* We have 0 or 1 server with pending events.. */
304 for (unsigned int x
= 0; x
< memc
->number_of_hosts
; ++x
)
305 if (memcached_server_response_count(&memc
->hosts
[x
]) > 0)
306 return &memc
->hosts
[x
];
311 int err
= poll(fds
, host_index
, memc
->poll_timeout
);
314 memc
->cached_errno
= errno
;
319 for (unsigned int x
= 0; x
< host_index
; ++x
)
320 if (fds
[x
].revents
& POLLIN
)
321 for (unsigned int y
= 0; y
< memc
->number_of_hosts
; ++y
)
322 if (memc
->hosts
[y
].fd
== fds
[x
].fd
)
323 return &memc
->hosts
[y
];
329 static ssize_t
io_flush(memcached_server_st
*ptr
,
330 memcached_return
*error
)
333 ** We might want to purge the input buffer if we haven't consumed
334 ** any output yet... The test for the limits is the purge is inline
335 ** in the purge function to avoid duplicating the logic..
339 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
340 rc
= memcached_purge(ptr
);
342 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_STORED
)
346 size_t return_length
;
347 char *local_write_ptr
= ptr
->write_buffer
;
348 size_t write_length
= ptr
->write_buffer_offset
;
350 *error
= MEMCACHED_SUCCESS
;
352 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
354 // UDP Sanity check, make sure that we are not sending somthing too big
355 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&& write_length
> MAX_UDP_DATAGRAM_LENGTH
)
358 if (ptr
->write_buffer_offset
== 0 || (ptr
->type
== MEMCACHED_CONNECTION_UDP
359 && ptr
->write_buffer_offset
== UDP_DATAGRAM_HEADER_LENGTH
))
362 /* Looking for memory overflows */
364 if (write_length
== MEMCACHED_MAX_BUFFER
)
365 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
366 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
372 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
373 WATCHPOINT_ASSERT(write_length
> 0);
375 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
376 increment_udp_message_id(ptr
);
377 sent_length
= write(ptr
->fd
, local_write_ptr
, write_length
);
379 if (sent_length
== -1)
381 ptr
->cached_errno
= errno
;
389 rc
= io_wait(ptr
, MEM_WRITE
);
391 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_TIMEOUT
)
394 memcached_quit_server(ptr
, 1);
398 memcached_quit_server(ptr
, 1);
399 *error
= MEMCACHED_ERRNO
;
404 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
&&
405 (size_t)sent_length
!= write_length
)
407 memcached_quit_server(ptr
, 1);
411 ptr
->io_bytes_sent
+= (uint32_t) sent_length
;
413 local_write_ptr
+= sent_length
;
414 write_length
-= (uint32_t) sent_length
;
415 return_length
+= (uint32_t) sent_length
;
418 WATCHPOINT_ASSERT(write_length
== 0);
419 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
420 // ptr->write_buffer_offset);
422 // if we are a udp server, the begining of the buffer is reserverd for
423 // the upd frame header
424 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
425 ptr
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
427 ptr
->write_buffer_offset
= 0;
429 return (ssize_t
) return_length
;
433 Eventually we will just kill off the server with the problem.
435 void memcached_io_reset(memcached_server_st
*ptr
)
437 memcached_quit_server(ptr
, 1);
441 * Read a given number of bytes from the server and place it into a specific
442 * buffer. Reset the IO channel on this server if an error occurs.
444 memcached_return
memcached_safe_read(memcached_server_st
*ptr
,
451 while (offset
< size
)
454 memcached_return rc
= memcached_io_read(ptr
, data
+ offset
, size
- offset
,
456 if (rc
!= MEMCACHED_SUCCESS
)
459 offset
+= (size_t) nread
;
462 return MEMCACHED_SUCCESS
;
465 memcached_return
memcached_io_readline(memcached_server_st
*ptr
,
469 bool line_complete
= false;
472 while (!line_complete
)
474 if (ptr
->read_buffer_length
== 0)
477 * We don't have any data in the buffer, so let's fill the read
478 * buffer. Call the standard read function to avoid duplicating
482 memcached_return rc
= memcached_io_read(ptr
, buffer_ptr
, 1, &nread
);
483 if (rc
!= MEMCACHED_SUCCESS
)
486 if (*buffer_ptr
== '\n')
493 /* Now let's look in the buffer and copy as we go! */
494 while (ptr
->read_buffer_length
&& total_nr
< size
&& !line_complete
)
496 *buffer_ptr
= *ptr
->read_ptr
;
497 if (*buffer_ptr
== '\n')
498 line_complete
= true;
499 --ptr
->read_buffer_length
;
505 if (total_nr
== size
)
506 return MEMCACHED_PROTOCOL_ERROR
;
509 return MEMCACHED_SUCCESS
;
513 * The udp request id consists of two seperate sections
515 * 2) The message number
516 * The thread id should only be set when the memcached_st struct is created
517 * and should not be changed.
519 * The message num is incremented for each new message we send, this function
520 * extracts the message number from message_id, increments it and then
521 * writes the new value back into the header
523 static void increment_udp_message_id(memcached_server_st
*ptr
)
525 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
526 uint16_t cur_req
= get_udp_datagram_request_id(header
);
527 int msg_num
= get_msg_num_from_request_id(cur_req
);
528 int thread_id
= get_thread_id_from_request_id(cur_req
);
530 if (((++msg_num
) & UDP_REQUEST_ID_THREAD_MASK
) != 0)
533 header
->request_id
= htons((uint16_t) (thread_id
| msg_num
));
536 memcached_return
memcached_io_init_udp_header(memcached_server_st
*ptr
, uint16_t thread_id
)
538 if (thread_id
> UDP_REQUEST_ID_MAX_THREAD_ID
)
539 return MEMCACHED_FAILURE
;
541 struct udp_datagram_header_st
*header
= (struct udp_datagram_header_st
*)ptr
->write_buffer
;
542 header
->request_id
= htons((uint16_t) (generate_udp_request_thread_id(thread_id
)));
543 header
->num_datagrams
= htons(1);
544 header
->sequence_number
= htons(0);
546 return MEMCACHED_SUCCESS
;