Merge up trunk
[m6w6/libmemcached] / libmemcached / protocol / protocol_handler.c
index adba9ad7f73bfa748d7299d6adfbbc98079fc592..fcab1419f7c100083a232b0c405cb0ccf65da806 100644 (file)
@@ -3,7 +3,6 @@
 
 #include <stdlib.h>
 #include <sys/types.h>
-#include <sys/socket.h>
 #include <errno.h>
 #include <stdbool.h>
 #include <string.h>
@@ -29,7 +28,7 @@
  * @return the number of bytes transferred of -1 upon error
  */
 static ssize_t default_recv(const void *cookie,
-                            int sock,
+                            memcached_socket_t sock,
                             void *buf,
                             size_t nbytes)
 {
@@ -49,7 +48,7 @@ static ssize_t default_recv(const void *cookie,
  * @return the number of bytes transferred of -1 upon error
  */
 static ssize_t default_send(const void *cookie,
-                            int fd,
+                            memcached_socket_t fd,
                             const void *buf,
                             size_t nbytes)
 {
@@ -79,13 +78,13 @@ static bool drain_output(struct memcached_protocol_client_st *client)
 
     if (len == -1)
     {
-      if (errno == EWOULDBLOCK)
+      if (get_socket_errno() == EWOULDBLOCK)
       {
         return true;
       }
-      else if (errno != EINTR)
+      else if (get_socket_errno() != EINTR)
       {
-        client->error= errno;
+        client->error= get_socket_errno();
         return false;
       }
     }
@@ -124,13 +123,13 @@ static struct chunk_st *allocate_output_chunk(struct memcached_protocol_client_s
     return NULL;
   }
 
-  ret->offset = ret->nbytes = 0;
-  ret->next = NULL;
-  ret->size = CHUNK_BUFFERSIZE;
+  ret->offset= ret->nbytes= 0;
+  ret->next= NULL;
+  ret->size= CHUNK_BUFFERSIZE;
   ret->data= (void*)(ret + 1);
   if (client->output == NULL)
   {
-    client->output = client->output_tail = ret;
+    client->output= client->output_tail= ret;
   }
   else
   {
@@ -159,7 +158,7 @@ static protocol_binary_response_status spool_output(struct memcached_protocol_cl
     return PROTOCOL_BINARY_RESPONSE_SUCCESS;
   }
 
-  size_t offset = 0;
+  size_t offset= 0;
 
   struct chunk_st *chunk= client->output;
   while (offset < length)
@@ -172,10 +171,10 @@ static protocol_binary_response_status spool_output(struct memcached_protocol_cl
       }
     }
 
-    size_t bulk = length - offset;
+    size_t bulk= length - offset;
     if (bulk > chunk->size - chunk->nbytes)
     {
-      bulk = chunk->size - chunk->nbytes;
+      bulk= chunk->size - chunk->nbytes;
     }
 
     memcpy(chunk->data + chunk->nbytes, data, bulk);
@@ -194,7 +193,7 @@ static protocol_binary_response_status spool_output(struct memcached_protocol_cl
  * so the implementors needs to provide an implementation of that interface
  *
  */
-static enum MEMCACHED_PROTOCOL_EVENT determine_protocol(struct memcached_protocol_client_st *client, ssize_t *length, void **endptr)
+static memcached_protocol_event_t determine_protocol(struct memcached_protocol_client_st *client, ssize_t *length, void **endptr)
 {
   if (*client->root->input_buffer == (uint8_t)PROTOCOL_BINARY_REQ)
   {
@@ -220,7 +219,7 @@ static enum MEMCACHED_PROTOCOL_EVENT determine_protocol(struct memcached_protoco
     const char *err= "CLIENT_ERROR: Unsupported protocol\r\n";
     client->root->spool(client, err, strlen(err));
     client->root->drain(client);
-    return ERROR_EVENT; /* Unsupported protocol */
+    return MEMCACHED_PROTOCOL_ERROR_EVENT; /* Unsupported protocol */
   }
 
   return client->work(client, length, endptr);
@@ -250,10 +249,10 @@ struct memcached_protocol_st *memcached_protocol_create_instance(void)
       return NULL;
     }
 
-    ret->buffer_cache = cache_create("protocol_handler",
+    ret->buffer_cache= cache_create("protocol_handler",
                                      CHUNK_BUFFERSIZE + sizeof(struct chunk_st),
                                      0, NULL, NULL);
-    if (ret->buffer_cache == NULL) 
+    if (ret->buffer_cache == NULL)
     {
       free(ret->input_buffer);
       free(ret);
@@ -270,7 +269,7 @@ void memcached_protocol_destroy_instance(struct memcached_protocol_st *instance)
   free(instance);
 }
 
-struct memcached_protocol_client_st *memcached_protocol_create_client(struct memcached_protocol_st *instance, int sock)
+struct memcached_protocol_client_st *memcached_protocol_create_client(struct memcached_protocol_st *instance, memcached_socket_t sock)
 {
   struct memcached_protocol_client_st *ret= calloc(1, sizeof(*ret));
   if (ret != NULL)
@@ -288,7 +287,7 @@ void memcached_protocol_client_destroy(struct memcached_protocol_client_st *clie
   free(client);
 }
 
-enum MEMCACHED_PROTOCOL_EVENT memcached_protocol_client_work(struct memcached_protocol_client_st *client)
+memcached_protocol_event_t memcached_protocol_client_work(struct memcached_protocol_client_st *client)
 {
   /* Try to send data and read from the socket */
   bool more_data= true;
@@ -314,9 +313,10 @@ enum MEMCACHED_PROTOCOL_EVENT memcached_protocol_client_work(struct memcached_pr
       }
 
       void *endptr;
-      if (client->work(client, &len, &endptr) == ERROR_EVENT)
+      memcached_protocol_event_t events= client->work(client, &len, &endptr);
+      if (events == MEMCACHED_PROTOCOL_ERROR_EVENT)
       {
-        return ERROR_EVENT;
+        return MEMCACHED_PROTOCOL_ERROR_EVENT;
       }
 
       if (len > 0)
@@ -327,7 +327,7 @@ enum MEMCACHED_PROTOCOL_EVENT memcached_protocol_client_work(struct memcached_pr
         if (client->input_buffer == NULL)
         {
           client->error= ENOMEM;
-          return ERROR_EVENT;
+          return MEMCACHED_PROTOCOL_ERROR_EVENT;
         }
         memcpy(client->input_buffer, endptr, (size_t)len);
         client->input_buffer_offset= (size_t)len;
@@ -338,24 +338,28 @@ enum MEMCACHED_PROTOCOL_EVENT memcached_protocol_client_work(struct memcached_pr
     {
       /* Connection closed */
       drain_output(client);
-      return ERROR_EVENT;
+      return MEMCACHED_PROTOCOL_ERROR_EVENT;
     }
     else
     {
-      if (errno != EWOULDBLOCK)
+      if (get_socket_errno() != EWOULDBLOCK)
       {
-        client->error= errno;
+        client->error= get_socket_errno();
         /* mark this client as terminated! */
-        return ERROR_EVENT;
+        return MEMCACHED_PROTOCOL_ERROR_EVENT;
       }
-      more_data = false;
+      more_data= false;
     }
   } while (more_data);
 
   if (!drain_output(client))
   {
-    return ERROR_EVENT;
+    return MEMCACHED_PROTOCOL_ERROR_EVENT;
   }
 
-  return (client->output) ? READ_WRITE_EVENT : READ_EVENT;
+  memcached_protocol_event_t ret= MEMCACHED_PROTOCOL_READ_EVENT;
+  if (client->output)
+    ret|= MEMCACHED_PROTOCOL_READ_EVENT;
+
+  return ret;
 }