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
));
33 error
= poll(fds
, 1, ptr
->root
->poll_timeout
);
36 return MEMCACHED_SUCCESS
;
39 return MEMCACHED_TIMEOUT
;
42 /* Imposssible for anything other then -1 */
43 WATCHPOINT_ASSERT(error
== -1);
44 memcached_quit_server(ptr
, 1);
46 return MEMCACHED_FAILURE
;
51 void memcached_io_preread(memcached_st
*ptr
)
57 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
59 if (memcached_server_response_count(ptr
, x
) &&
60 ptr
->hosts
[x
].read_data_length
< MEMCACHED_MAX_BUFFER
)
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
);
70 ptr
->hosts
[x
].read_buffer_length
+= data_read
;
71 ptr
->hosts
[x
].read_data_length
+= data_read
;
77 ssize_t
memcached_io_read(memcached_server_st
*ptr
,
78 char *buffer
, size_t length
)
87 if (!ptr
->read_buffer_length
)
93 data_read
= read(ptr
->fd
,
95 MEMCACHED_MAX_BUFFER
);
104 rc
= io_wait(ptr
, MEM_READ
);
106 if (rc
== MEMCACHED_SUCCESS
)
109 memcached_quit_server(ptr
, 1);
114 memcached_quit_server(ptr
, 1);
115 ptr
->cached_errno
= errno
;
122 /* If zero, just keep looping unless testing, then assert() */
125 WATCHPOINT_ASSERT(0);
131 ptr
->read_data_length
= data_read
;
132 ptr
->read_buffer_length
= data_read
;
133 ptr
->read_ptr
= ptr
->read_buffer
;
140 difference
= (length
> ptr
->read_buffer_length
) ? ptr
->read_buffer_length
: length
;
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
;
150 *buffer_ptr
= *ptr
->read_ptr
;
153 ptr
->read_buffer_length
--;
161 return (size_t)(buffer_ptr
- buffer
);
164 ssize_t
memcached_io_write(memcached_server_st
*ptr
,
165 char *buffer
, size_t length
, char with_flush
)
167 unsigned long long x
;
169 for (x
= 0; x
< length
; x
++)
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
);
175 if (ptr
->write_buffer_offset
== MEMCACHED_MAX_BUFFER
)
180 sent_length
= io_flush(ptr
, &rc
);
181 if (sent_length
== -1)
184 WATCHPOINT_ASSERT(sent_length
== MEMCACHED_MAX_BUFFER
);
191 if (io_flush(ptr
, &rc
) == -1)
198 memcached_return
memcached_io_close(memcached_server_st
*ptr
)
202 return MEMCACHED_SUCCESS
;
205 static ssize_t
io_flush(memcached_server_st
*ptr
,
206 memcached_return
*error
)
209 size_t return_length
;
210 char *local_write_ptr
= ptr
->write_buffer
;
211 size_t write_length
= ptr
->write_buffer_offset
;
213 *error
= MEMCACHED_SUCCESS
;
215 if (ptr
->write_buffer_offset
== 0)
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
));
226 if (ptr
->type
== MEMCACHED_CONNECTION_UDP
)
228 sent_length
= sendto(ptr
->fd
, local_write_ptr
, write_length
, 0,
229 (struct sockaddr
*)&ptr
->address_info
->ai_addr
,
230 sizeof(struct sockaddr
));
234 if ((ssize_t
)(sent_length
= write(ptr
->fd
, local_write_ptr
,
235 write_length
)) == -1)
244 rc
= io_wait(ptr
, MEM_WRITE
);
246 if (rc
== MEMCACHED_SUCCESS
)
249 memcached_quit_server(ptr
, 1);
253 memcached_quit_server(ptr
, 1);
254 ptr
->cached_errno
= errno
;
255 *error
= MEMCACHED_ERRNO
;
261 local_write_ptr
+= sent_length
;
262 write_length
-= sent_length
;
263 return_length
+= sent_length
;
266 WATCHPOINT_ASSERT(write_length
== 0);
267 WATCHPOINT_ASSERT(return_length
== ptr
->write_buffer_offset
);
268 ptr
->write_buffer_offset
= 0;
270 return return_length
;
274 Eventually we will just kill off the server with the problem.
276 void memcached_io_reset(memcached_server_st
*ptr
)
278 ptr
->write_buffer_offset
= 0;
279 memcached_quit_server(ptr
, 0);