Merge up trunk
[m6w6/libmemcached] / libmemcached / protocol / common.h
index 036a13f6c49c713dd92c159c847b08f633533022..808a608609f1b18c9f0a43178dcd289561f878fc 100644 (file)
@@ -1,46 +1,93 @@
-/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
-#ifndef MEMCACHED_PROTOCOL_INTERNAL_H
-#define MEMCACHED_PROTOCOL_INTERNAL_H
-
-#include "config.h"
-#include <stdbool.h>
-#include <assert.h>
-#include <netinet/in.h>
-
-#ifdef linux
-/* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
- * optimize the conversion functions, but the prototypes generate warnings
- * from gcc. The conversion methods isn't the bottleneck for my app, so
- * just remove the warnings by undef'ing the optimization ..
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Libmemcached library
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
  */
-#undef ntohs
-#undef ntohl
-#undef htons
-#undef htonl
-
-#endif
 
+#pragma once
 
-/* Define this here, which will turn on the visibilty controls while we're
- * building libmemcached.
- */
-#define BUILDING_LIBMEMCACHED 1
+#include "config.h"
+#if !defined(__cplusplus)
+# include <stdbool.h>
+#endif
+#include <assert.h>
 
+#include <libmemcached/visibility.h>
 #include <libmemcached/protocol_handler.h>
 #include <libmemcached/protocol/cache.h>
 
-struct memcached_binary_protocol_st {
-  struct memcached_binary_protocol_callback_st *callback;
-  memcached_binary_protocol_recv_func recv;
-  memcached_binary_protocol_send_func send;
-  char *input_buffer;
+/*
+ * I don't really need the following two functions as function pointers
+ * in the instance handle, but I don't want to put them in the global
+ * namespace for those linking statically (personally I don't like that,
+ * but some people still do). If it ever shows up as a performance thing
+ * I'll look into optimizing this ;-)
+ */
+typedef bool (*drain_func)(memcached_protocol_client_st *client);
+typedef protocol_binary_response_status (*spool_func)(memcached_protocol_client_st *client,
+                                                      const void *data,
+                                                      size_t length);
+
+/**
+ * Definition of the per instance structure.
+ */
+struct memcached_protocol_st {
+  memcached_binary_protocol_callback_st *callback;
+  memcached_protocol_recv_func recv;
+  memcached_protocol_send_func send;
+
+  /*
+   * I really don't need these as funciton pointers, but I don't want
+   * to clutter the namespace if someone links statically.
+   */
+  drain_func drain;
+  spool_func spool;
+
+  /*
+   * To avoid keeping a buffer in each client all the time I have a
+   * bigger buffer in the instance that I read to initially, and then
+   * I try to parse and execute as much from the buffer. If I wasn't able
+   * to process all data I'll keep that in a per-connection buffer until
+   * the next time I can read from the socket.
+   */
+  uint8_t *input_buffer;
   size_t input_buffer_size;
+
   bool pedantic;
   /* @todo use multiple sized buffers */
   cache_t *buffer_cache;
 };
 
-
 struct chunk_st {
   /* Pointer to the data */
   char *data;
@@ -56,38 +103,61 @@ struct chunk_st {
 
 #define CHUNK_BUFFERSIZE 2048
 
-struct memcached_binary_protocol_client_st {
-  struct memcached_binary_protocol_st *root;
-  int sock;
+typedef memcached_protocol_event_t (*process_data)(struct memcached_protocol_client_st *client, ssize_t *length, void **endptr);
+
+enum ascii_cmd {
+  GET_CMD,
+  GETS_CMD,
+  SET_CMD,
+  ADD_CMD,
+  REPLACE_CMD,
+  CAS_CMD,
+  APPEND_CMD,
+  PREPEND_CMD,
+  DELETE_CMD,
+  INCR_CMD,
+  DECR_CMD,
+  STATS_CMD,
+  FLUSH_ALL_CMD,
+  VERSION_CMD,
+  QUIT_CMD,
+  VERBOSITY_CMD,
+  UNKNOWN_CMD
+};
+
+struct memcached_protocol_client_st {
+  memcached_protocol_st *root;
+  memcached_socket_t sock;
   int error;
 
   /* Linked list of data to send */
   struct chunk_st *output;
   struct chunk_st *output_tail;
 
-
-
-  char *input_buffer;
+  /*
+   * While we process input data, this is where we spool incomplete commands
+   * if we need to receive more data....
+   * @todo use the buffercace
+   */
+  uint8_t *input_buffer;
   size_t input_buffer_size;
   size_t input_buffer_offset;
-  char *curr_input;
 
+  /* The callback to the protocol handler to use (ascii or binary) */
+  process_data work;
 
-  struct chunk_st *input;
-  /* Pointer to the last chunk to avoid the need to traverse the complete list */
-  struct chunk_st *input_tail;
-  size_t bytes_available;
+  /*
+   * Should the spool data discard the data to send or not? (aka noreply in
+   * the ascii protocol..
+   */
+  bool mute;
 
+  /* Members used by the binary protocol */
   protocol_binary_request_header *current_command;
-  bool quiet;
-};
-
-LIBMEMCACHED_LOCAL
-bool memcached_binary_protocol_pedantic_check_request(protocol_binary_request_header *request);
-
-LIBMEMCACHED_LOCAL
-bool memcached_binary_protocol_pedantic_check_response(protocol_binary_request_header *request,
-                                                       protocol_binary_response_header *response);
 
+  /* Members used by the ascii protocol */
+  enum ascii_cmd ascii_command;
+};
 
-#endif
+#include "ascii_handler.h"
+#include "binary_handler.h"