From 647f5b82467172c55e456b6833d4c2f0bdc0bc5a Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 29 Jul 2011 15:00:34 -0700 Subject: [PATCH] Merge in simple extrace for strings, style issues in io --- libmemcached/common.h | 4 ++ libmemcached/delete.cc | 2 + libmemcached/include.am | 2 + libmemcached/io.cc | 5 ++- libmemcached/io.h | 48 +---------------------- libmemcached/io.hpp | 83 ++++++++++++++++++++++++++++++++++++++++ libmemcached/response.cc | 24 +++++++++++- libmemcached/string.h | 8 ---- libmemcached/string.hpp | 47 +++++++++++++++++++++++ 9 files changed, 165 insertions(+), 58 deletions(-) create mode 100644 libmemcached/io.hpp create mode 100644 libmemcached/string.hpp diff --git a/libmemcached/common.h b/libmemcached/common.h index d4b2c7df..49d309c8 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -92,7 +92,11 @@ memcached_return_t memcached_server_execute(memcached_st *ptr, #include #include #include +#ifdef __cplusplus +#include +#include #include +#endif #include #include #include diff --git a/libmemcached/delete.cc b/libmemcached/delete.cc index de4a53dc..ea3b6025 100644 --- a/libmemcached/delete.cc +++ b/libmemcached/delete.cc @@ -161,7 +161,9 @@ memcached_return_t memcached_delete_by_key(memcached_st *ptr, return MEMCACHED_WRITE_FAILURE; if (send_length + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH) + { memcached_io_write(instance, NULL, 0, true); + } } rc= memcached_do(instance, buffer, (size_t)send_length, to_write); diff --git a/libmemcached/include.am b/libmemcached/include.am index f8e20d58..3a0a0ac4 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -21,6 +21,7 @@ noinst_HEADERS+= \ libmemcached/initialize_query.h \ libmemcached/internal.h \ libmemcached/io.h \ + libmemcached/io.hpp \ libmemcached/is.h \ libmemcached/libmemcached_probes.h \ libmemcached/memory.h \ @@ -29,6 +30,7 @@ noinst_HEADERS+= \ libmemcached/protocol/binary_handler.h \ libmemcached/protocol/common.h \ libmemcached/response.h \ + libmemcached/string.hpp \ libmemcached/virtual_bucket.h nobase_include_HEADERS+= \ diff --git a/libmemcached/io.cc b/libmemcached/io.cc index f706048e..0ed565d7 100644 --- a/libmemcached/io.cc +++ b/libmemcached/io.cc @@ -846,10 +846,11 @@ memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr, memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr, char *buffer_ptr, - size_t size) + size_t size, + size_t& total_nr) { + total_nr= 0; bool line_complete= false; - size_t total_nr= 0; while (not line_complete) { diff --git a/libmemcached/io.h b/libmemcached/io.h index 0d1e6193..0a05b92a 100644 --- a/libmemcached/io.h +++ b/libmemcached/io.h @@ -38,8 +38,6 @@ #pragma once -#include - #define MAX_UDP_DATAGRAM_LENGTH 1400 #define UDP_DATAGRAM_HEADER_LENGTH 8 #define UDP_REQUEST_ID_MSG_SIG_DIGITS 10 @@ -66,54 +64,12 @@ struct libmemcached_io_vector_st const void *buffer; }; -#ifdef __cplusplus -extern "C" { -#endif - LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_wait_for_write(memcached_server_write_instance_st ptr); +ssize_t memcached_io_write(memcached_server_write_instance_st ptr, + const void *buffer, size_t length, bool with_flush); LIBMEMCACHED_LOCAL ssize_t memcached_io_writev(memcached_server_write_instance_st ptr, const struct libmemcached_io_vector_st *vector, size_t number_of, bool with_flush); -LIBMEMCACHED_LOCAL -ssize_t memcached_io_write(memcached_server_write_instance_st ptr, - const void *buffer, size_t length, bool with_flush); - -LIBMEMCACHED_LOCAL -void memcached_io_reset(memcached_server_write_instance_st ptr); - -LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, - void *buffer, size_t length, ssize_t *nread); - -/* Read a line (terminated by '\n') into the buffer */ -LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr, - char *buffer_ptr, - size_t size); - -LIBMEMCACHED_LOCAL -void memcached_io_close(memcached_server_write_instance_st ptr); - -/* Read n bytes of data from the server and store them in dta */ -LIBMEMCACHED_LOCAL -memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr, - void *dta, - size_t size); - -LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr, - uint16_t thread_id); - -LIBMEMCACHED_LOCAL -memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc); - -LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_slurp(memcached_server_write_instance_st ptr); - -#ifdef __cplusplus -} -#endif diff --git a/libmemcached/io.hpp b/libmemcached/io.hpp new file mode 100644 index 00000000..7413ac4b --- /dev/null +++ b/libmemcached/io.hpp @@ -0,0 +1,83 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * LibMemcached + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2006-2009 Brian Aker + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +LIBMEMCACHED_LOCAL +memcached_return_t memcached_io_wait_for_write(memcached_server_write_instance_st ptr); + +LIBMEMCACHED_LOCAL +void memcached_io_reset(memcached_server_write_instance_st ptr); + +LIBMEMCACHED_LOCAL +memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, + void *buffer, size_t length, ssize_t *nread); + +/* Read a line (terminated by '\n') into the buffer */ +LIBMEMCACHED_LOCAL +memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr, + char *buffer_ptr, + size_t size, + size_t& total); + +LIBMEMCACHED_LOCAL +void memcached_io_close(memcached_server_write_instance_st ptr); + +/* Read n bytes of data from the server and store them in dta */ +LIBMEMCACHED_LOCAL +memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr, + void *dta, + size_t size); + +LIBMEMCACHED_LOCAL +memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr, + uint16_t thread_id); + +LIBMEMCACHED_LOCAL +memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc); + +LIBMEMCACHED_LOCAL +memcached_return_t memcached_io_slurp(memcached_server_write_instance_st ptr); + +#ifdef __cplusplus +} +#endif diff --git a/libmemcached/response.cc b/libmemcached/response.cc index 1548b4ed..dddcb349 100644 --- a/libmemcached/response.cc +++ b/libmemcached/response.cc @@ -36,6 +36,7 @@ */ #include +#include static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, @@ -252,7 +253,9 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta char *buffer, size_t buffer_length, memcached_result_st *result) { - memcached_return_t rc= memcached_io_readline(ptr, buffer, buffer_length); + size_t total_read; + memcached_return_t rc= memcached_io_readline(ptr, buffer, buffer_length, total_read); + if (memcached_failed(rc)) { return rc; @@ -287,8 +290,25 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta } else if (buffer[1] == 'E') /* SERVER_ERROR */ { - char *startptr= buffer + 13, *endptr= startptr; + if (total_read == memcached_literal_param_size("SERVER_ERROR")) + { + return MEMCACHED_SERVER_ERROR; + } + + if (total_read > memcached_literal_param_size("SERVER_ERROR object too large for cache") and + (memcmp(buffer, memcached_literal_param("SERVER_ERROR object too large for cache")) == 0)) + { + return MEMCACHED_E2BIG; + } + + // Move past the basic error message and whitespace + char *startptr= buffer + memcached_literal_param_size("SERVER_ERROR"); + if (startptr[0] == ' ') + { + startptr++; + } + char *endptr= startptr; while (*endptr != '\r' && *endptr != '\n') endptr++; return memcached_set_error(*ptr, MEMCACHED_SERVER_ERROR, MEMCACHED_AT, startptr, size_t(endptr - startptr)); diff --git a/libmemcached/string.h b/libmemcached/string.h index 3a3fdf53..360d9008 100644 --- a/libmemcached/string.h +++ b/libmemcached/string.h @@ -110,12 +110,4 @@ void memcached_string_set_length(memcached_string_st *self, size_t length); } #endif -#ifdef __cplusplus -#define memcached_literal_param(X) (X), (static_cast((sizeof(X) - 1))) -#else -#define memcached_literal_param(X) (X), ((size_t)((sizeof(X) - 1))) -#endif - -#define memcached_string_make_from_cstr(X) (X), ((X) ? strlen(X) : 0) - #endif diff --git a/libmemcached/string.hpp b/libmemcached/string.hpp new file mode 100644 index 00000000..5703bb40 --- /dev/null +++ b/libmemcached/string.hpp @@ -0,0 +1,47 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libmcachedd client library. + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * All rights reserved. + * + * 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. + * + */ + +#pragma once + +#include "util/string.hpp" + +#define memcached_literal_param util_literal_param +#define memcached_literal_param_size util_literal_param_size +#define memcached_string_make_from_cstr util_string_make_from_cstr +#define memcached_array_length util_array_length + + -- 2.30.2