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
));
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 if (memcached_purge(ptr
) != MEMCACHED_SUCCESS
|| memcached_purge(ptr
) != MEMCACHED_STORED
)
45 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 ssize_t
memcached_io_read(memcached_server_st
*ptr
,
94 void *buffer
, size_t length
)
102 uint8_t found_eof
= 0;
103 if (!ptr
->read_buffer_length
)
109 data_read
= read(ptr
->fd
,
111 MEMCACHED_MAX_BUFFER
);
114 else if (data_read
== -1)
116 ptr
->cached_errno
= errno
;
124 rc
= io_wait(ptr
, MEM_READ
);
126 if (rc
== MEMCACHED_SUCCESS
)
132 memcached_quit_server(ptr
, 1);
144 ptr
->io_bytes_sent
= 0;
145 ptr
->read_data_length
= data_read
;
146 ptr
->read_buffer_length
= data_read
;
147 ptr
->read_ptr
= ptr
->read_buffer
;
154 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
156 memcpy(buffer_ptr
, ptr
->read_ptr
, difference
);
157 length
-= difference
;
158 ptr
->read_ptr
+= difference
;
159 ptr
->read_buffer_length
-= difference
;
160 buffer_ptr
+= difference
;
164 *buffer_ptr
= *ptr
->read_ptr
;
166 ptr
->read_buffer_length
--;
175 return (size_t)(buffer_ptr
- (char*)buffer
);
178 ssize_t
memcached_io_write(memcached_server_st
*ptr
,
179 const void *buffer
, size_t length
, char with_flush
)
181 size_t original_length
;
182 const char* buffer_ptr
;
184 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
186 original_length
= length
;
194 should_write
= MEMCACHED_MAX_BUFFER
- ptr
->write_buffer_offset
;
195 write_ptr
= ptr
->write_buffer
+ ptr
->write_buffer_offset
;
197 should_write
= (should_write
< length
) ? should_write
: length
;
199 memcpy(write_ptr
, buffer_ptr
, should_write
);
200 ptr
->write_buffer_offset
+= should_write
;
201 buffer_ptr
+= should_write
;
202 length
-= should_write
;
204 if (ptr
->write_buffer_offset
== MEMCACHED_MAX_BUFFER
)
209 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
210 sent_length
= io_flush(ptr
, &rc
);
211 if (sent_length
== -1)
214 WATCHPOINT_ASSERT(sent_length
== MEMCACHED_MAX_BUFFER
);
221 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
222 if (io_flush(ptr
, &rc
) == -1)
226 return original_length
;
229 memcached_return
memcached_io_close(memcached_server_st
*ptr
)
234 return MEMCACHED_SUCCESS
;
236 /* in case of death shutdown to avoid blocking at close() */
239 r
= shutdown(ptr
->fd
, SHUT_RDWR
);
242 if (r
&& errno
!= ENOTCONN
)
244 WATCHPOINT_NUMBER(ptr
->fd
);
245 WATCHPOINT_ERRNO(errno
);
246 WATCHPOINT_ASSERT(errno
);
254 WATCHPOINT_ERRNO(errno
);
257 return MEMCACHED_SUCCESS
;
260 static ssize_t
io_flush(memcached_server_st
*ptr
,
261 memcached_return
*error
)
264 size_t return_length
;
265 char *local_write_ptr
= ptr
->write_buffer
;
266 size_t write_length
= ptr
->write_buffer_offset
;
268 *error
= MEMCACHED_SUCCESS
;
270 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
272 if (ptr
->write_buffer_offset
== 0)
275 /* Looking for memory overflows */
276 #if defined(HAVE_DEBUG)
277 if (write_length
== MEMCACHED_MAX_BUFFER
)
278 WATCHPOINT_ASSERT(ptr
->write_buffer
== local_write_ptr
);
279 WATCHPOINT_ASSERT((ptr
->write_buffer
+ MEMCACHED_MAX_BUFFER
) >= (local_write_ptr
+ write_length
));
285 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
286 WATCHPOINT_ASSERT(write_length
> 0);
288 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
292 ai
= ptr
->address_info
;
294 /* Crappy test code */
295 char buffer
[HUGE_STRING_LEN
+ 8];
296 memset(buffer
, 0, HUGE_STRING_LEN
+ 8);
297 memcpy (buffer
+8, local_write_ptr
, write_length
);
306 sent_length
= sendto(ptr
->fd
, buffer
, write_length
+ 8, 0,
307 (struct sockaddr
*)ai
->ai_addr
,
309 if (sent_length
== -1)
311 WATCHPOINT_ERRNO(errno
);
312 WATCHPOINT_ASSERT(0);
314 sent_length
-= 8; /* We remove the header */
320 ** We might want to purge the input buffer if we haven't consumed
321 ** any output yet... The test for the limits is the purge is inline
322 ** in the purge function to avoid duplicating the logic..
326 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
327 rc
= memcached_purge(ptr
);
329 if (rc
!= MEMCACHED_SUCCESS
|| rc
!= MEMCACHED_STORED
)
334 WATCHPOINT_ASSERT(ptr
->fd
!= -1);
335 if ((sent_length
= write(ptr
->fd
, local_write_ptr
,
336 write_length
)) == -1)
345 rc
= io_wait(ptr
, MEM_WRITE
);
347 if (rc
== MEMCACHED_SUCCESS
)
350 memcached_quit_server(ptr
, 1);
354 memcached_quit_server(ptr
, 1);
355 ptr
->cached_errno
= errno
;
356 *error
= MEMCACHED_ERRNO
;
362 ptr
->io_bytes_sent
+= sent_length
;
364 local_write_ptr
+= sent_length
;
365 write_length
-= sent_length
;
366 return_length
+= sent_length
;
369 WATCHPOINT_ASSERT(write_length
== 0);
370 // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
371 // ptr->write_buffer_offset);
372 ptr
->write_buffer_offset
= 0;
374 return return_length
;
378 Eventually we will just kill off the server with the problem.
380 void memcached_io_reset(memcached_server_st
*ptr
)
382 memcached_quit_server(ptr
, 0);