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
);
17 static memcached_return
io_wait(memcached_server_st
*ptr
,
18 memc_read_or_write read_or_write
)
24 if (read_or_write
== MEM_WRITE
) /* write */
25 flags
= POLLOUT
| POLLERR
;
27 flags
= POLLIN
| POLLERR
;
29 memset(&fds
, 0, sizeof(struct pollfd
));
34 ** We are going to block on write, but at least on Solaris we might block
35 ** on write if we haven't read anything from our input buffer..
36 ** Try to purge the input buffer if we don't do any flow control in the
37 ** application layer (just sending a lot of data etc)
38 ** The test is moved down in the purge function to avoid duplication of
41 if (read_or_write
== MEM_WRITE
)
44 error
= poll(fds
, 1, ptr
->root
->poll_timeout
);
47 return MEMCACHED_SUCCESS
;
50 return MEMCACHED_TIMEOUT
;
53 /* Imposssible for anything other then -1 */
54 WATCHPOINT_ASSERT(error
== -1);
55 memcached_quit_server(ptr
, 1);
57 return MEMCACHED_FAILURE
;
62 void memcached_io_preread(memcached_st
*ptr
)
68 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
70 if (memcached_server_response_count(ptr
, x
) &&
71 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
75 data_read
= read(ptr
->hosts
[x
].fd
,
76 ptr
->hosts
[x
].read_ptr
+ ptr
->hosts
[x
].read_data_length
,
77 MEMCACHED_MAX_BUFFER
- ptr
->hosts
[x
].read_data_length
);
81 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
82 ptr
->hosts
[x
].read_data_length
+= data_read
;
88 ssize_t
memcached_io_read(memcached_server_st
*ptr
,
89 void *buffer
, size_t length
)
98 if (!ptr
->read_buffer_length
)
104 data_read
= read(ptr
->fd
,
106 MEMCACHED_MAX_BUFFER
);
109 else if (data_read
== -1)
111 ptr
->cached_errno
= errno
;
119 rc
= io_wait(ptr
, MEM_READ
);
121 if (rc
== MEMCACHED_SUCCESS
)
127 memcached_quit_server(ptr
, 1);
139 ptr
->io_bytes_sent
= 0;
140 ptr
->read_data_length
= data_read
;
141 ptr
->read_buffer_length
= data_read
;
142 ptr
->read_ptr
= ptr
->read_buffer
;
149 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
151 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
152 length
-= difference
;
153 ptr
->read_ptr
+= difference
;
154 ptr
->read_buffer_length
-= difference
;
155 buffer_ptr
+= difference
;
159 *buffer_ptr
= *ptr
->read_ptr
;
161 ptr
->read_buffer_length
--;
170 return (size_t)(buffer_ptr
- (char*)buffer
);
173 ssize_t
memcached_io_write(memcached_server_st
*ptr
,
174 const void *buffer
, size_t length
, char with_flush
)
176 size_t original_length
;
177 const char* buffer_ptr
;
179 original_length
= length
;
187 should_write
= MEMCACHED_MAX_BUFFER
- ptr
->write_buffer_offset
;
188 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
190 should_write
= (should_write
< length
) ? should_write
: length
;
192 memcpy(write_ptr
, buffer_ptr
, should_write
);
193 ptr
->write_buffer_offset
+= should_write
;
194 buffer_ptr
+= should_write
;
195 length
-= should_write
;
197 if (ptr
->write_buffer_offset
== MEMCACHED_MAX_BUFFER
)
202 sent_length
= io_flush(ptr
, &rc
);
203 if (sent_length
== -1)
206 WATCHPOINT_ASSERT(sent_length
== MEMCACHED_MAX_BUFFER
);
213 if (io_flush(ptr
, &rc
) == -1)
217 return original_length
;
220 memcached_return
memcached_io_close(memcached_server_st
*ptr
)
225 return MEMCACHED_SUCCESS
;
227 /* in case of death shutdown to avoid blocking at close() */
230 r
= shutdown(ptr
->fd
, SHUT_RDWR
);
233 if (r
&& errno
!= ENOTCONN
)
235 WATCHPOINT_NUMBER(ptr
->fd
);
236 WATCHPOINT_ERRNO(errno
);
237 WATCHPOINT_ASSERT(errno
);
245 WATCHPOINT_ERRNO(errno
);
248 return MEMCACHED_SUCCESS
;
251 static ssize_t
io_flush(memcached_server_st
*ptr
,
252 memcached_return
*error
)
255 size_t return_length
;
256 char *local_write_ptr
= ptr
->write_buffer
;
257 size_t write_length
= ptr
->write_buffer_offset
;
259 *error
= MEMCACHED_SUCCESS
;
261 if (ptr
->write_buffer_offset
== 0)
264 /* Looking for memory overflows */
265 #if defined(HAVE_DEBUG)
266 if (write_length
== MEMCACHED_MAX_BUFFER
)
267 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
268 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
274 WATCHPOINT_ASSERT(write_length
> 0);
276 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
280 ai
= ptr
->address_info
;
282 /* Crappy test code */
283 char buffer
[HUGE_STRING_LEN
+ 8];
284 memset(buffer
, 0, HUGE_STRING_LEN
+ 8);
285 memcpy (buffer
+8, local_write_ptr
, write_length
);
294 sent_length
= sendto(ptr
->fd
, buffer
, write_length
+ 8, 0,
295 (struct sockaddr
*)ai
->ai_addr
,
297 if (sent_length
== -1)
299 WATCHPOINT_ERRNO(errno
);
300 WATCHPOINT_ASSERT(0);
302 sent_length
-= 8; /* We remove the header */
307 ** We might want to purge the input buffer if we haven't consumed
308 ** any output yet... The test for the limits is the purge is inline
309 ** in the purge function to avoid duplicating the logic..
311 memcached_purge(ptr
);
313 if ((sent_length
= write(ptr
->fd
, local_write_ptr
,
314 write_length
)) == -1)
323 rc
= io_wait(ptr
, MEM_WRITE
);
325 if (rc
== MEMCACHED_SUCCESS
)
328 memcached_quit_server(ptr
, 1);
332 memcached_quit_server(ptr
, 1);
333 ptr
->cached_errno
= errno
;
334 *error
= MEMCACHED_ERRNO
;
340 ptr
->io_bytes_sent
+= sent_length
;
342 local_write_ptr
+= sent_length
;
343 write_length
-= sent_length
;
344 return_length
+= sent_length
;
347 WATCHPOINT_ASSERT(write_length
== 0);
348 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
349 // ptr->write_buffer_offset);
350 ptr
->write_buffer_offset
= 0;
352 return return_length
;
356 Eventually we will just kill off the server with the problem.
358 void memcached_io_reset(memcached_server_st
*ptr
)
360 memcached_quit_server(ptr
, 0);