X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=example%2Fmemcached_light.c;h=86087c5a9b7010140a4de95a0ade3b1e2c5fb72f;hb=125682af2435cac4f914ad68cacfe079883fa170;hp=743f2d1c9ff2ca9f5dde12cb19da680c0785b9a8;hpb=d292bcb63666dad0277e9658916514f64b7200b0;p=m6w6%2Flibmemcached diff --git a/example/memcached_light.c b/example/memcached_light.c index 743f2d1c..86087c5a 100644 --- a/example/memcached_light.c +++ b/example/memcached_light.c @@ -15,8 +15,15 @@ * the more "logical" interface. * memcached_light.c - This file sets up all of the sockets and run the main * message loop. + * + * + * config.h is included so that I can use the ntohll/htonll on platforms that + * doesn't have that (this is a private function inside libmemcached, so you + * cannot use it directly from libmemcached without special modifications to + * the library) */ +#include "config.h" #include #include #include @@ -33,9 +40,10 @@ #include #include #include "storage.h" +#include "memcached_light.h" -extern struct memcached_binary_protocol_callback_st interface_v0_impl; -extern struct memcached_binary_protocol_callback_st interface_v1_impl; +extern memcached_binary_protocol_callback_st interface_v0_impl; +extern memcached_binary_protocol_callback_st interface_v1_impl; static int server_sockets[1024]; static int num_server_sockets= 0; @@ -83,12 +91,14 @@ static int server_socket(const char *port) { } if ((flags & O_NONBLOCK) != O_NONBLOCK) + { if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1) { perror("Failed to set socket to nonblocking mode"); close(sock); continue; } + } flags= 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&flags, sizeof(flags)) != 0) @@ -218,7 +228,13 @@ int main(int argc, char **argv) { bool port_specified= false; int cmd; - struct memcached_binary_protocol_callback_st *interface= &interface_v0_impl; + memcached_binary_protocol_callback_st *interface= &interface_v0_impl; + + /* + * We need to initialize the handlers manually due to a bug in the + * warnings generated by struct initialization in gcc (all the way up to 4.4) + */ + initialize_iterface_v0_handler(); while ((cmd= getopt(argc, argv, "v1p:?")) != EOF) { @@ -264,8 +280,8 @@ int main(int argc, char **argv) interface->post_execute= post_execute; interface->unknown= unknown; - struct memcached_binary_protocol_st *protocol_handle; - if ((protocol_handle= memcached_binary_protocol_create_instance()) == NULL) + struct memcached_protocol_st *protocol_handle; + if ((protocol_handle= memcached_protocol_create_instance()) == NULL) { fprintf(stderr, "Failed to allocate protocol handle\n"); return 1; @@ -284,7 +300,8 @@ int main(int argc, char **argv) return 0; } -static void work(void) { +static void work(void) +{ #define MAX_SERVERS_TO_POLL 100 struct pollfd fds[MAX_SERVERS_TO_POLL]; int max_poll; @@ -327,11 +344,11 @@ static void work(void) { continue; } - struct memcached_binary_protocol_st *protocol; + struct memcached_protocol_st *protocol; protocol= socket_userdata_map[fds[x].fd]; - struct memcached_binary_protocol_client_st* c; - c= memcached_binary_protocol_create_client(protocol, sock); + struct memcached_protocol_client_st* c; + c= memcached_protocol_create_client(protocol, sock); if (c == NULL) { fprintf(stderr, "Failed to create client\n"); @@ -349,23 +366,22 @@ static void work(void) { else { /* drive the client */ - struct memcached_binary_protocol_client_st* c; + struct memcached_protocol_client_st* c; c= socket_userdata_map[fds[x].fd]; assert(c != NULL); fds[max_poll].events= 0; - switch (memcached_binary_protocol_client_work(c)) - { - case WRITE_EVENT: - case READ_WRITE_EVENT: + memcached_protocol_event_t events= memcached_protocol_client_work(c); + if (events & MEMCACHED_PROTOCOL_WRITE_EVENT) fds[max_poll].events= POLLOUT; - /* FALLTHROUGH */ - case READ_EVENT: - fds[max_poll].events |= POLLIN; - break; - case ERROR_EVENT: - default: /* ERROR or unknown state.. close */ - memcached_binary_protocol_client_destroy(c); + + if (events & MEMCACHED_PROTOCOL_READ_EVENT) + fds[max_poll].events= POLLIN; + + if (!(events & MEMCACHED_PROTOCOL_PAUSE_EVENT || + fds[max_poll].events != 0)) + { + memcached_protocol_client_destroy(c); close(fds[x].fd); fds[x].events= 0;