diff options
Diffstat (limited to 'plugin/handler_socket/libhsclient')
25 files changed, 2015 insertions, 0 deletions
diff --git a/plugin/handler_socket/libhsclient/COPYRIGHT.txt b/plugin/handler_socket/libhsclient/COPYRIGHT.txt new file mode 100644 index 00000000..41dda127 --- /dev/null +++ b/plugin/handler_socket/libhsclient/COPYRIGHT.txt @@ -0,0 +1,27 @@ + + Copyright (c) 2010 DeNA Co.,Ltd. + 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. + * Neither the name of DeNA Co.,Ltd. nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY DeNA Co.,Ltd. "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 DeNA Co.,Ltd. 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. + diff --git a/plugin/handler_socket/libhsclient/Makefile.am b/plugin/handler_socket/libhsclient/Makefile.am new file mode 100644 index 00000000..343b4186 --- /dev/null +++ b/plugin/handler_socket/libhsclient/Makefile.am @@ -0,0 +1,12 @@ +CXXFLAGS += -fimplicit-templates +instdir = $(includedir)/handlersocket +# TODO: these headers should be in dena/ +inst_HEADERS = allocator.hpp config.hpp mutex.hpp string_util.hpp \ + auto_addrinfo.hpp escape.hpp socket.hpp thread.hpp auto_file.hpp \ + fatal.hpp string_buffer.hpp util.hpp auto_ptrcontainer.hpp \ + hstcpcli.hpp string_ref.hpp +lib_LTLIBRARIES = libhsclient.la +libhsclient_la_SOURCES = config.cpp escape.cpp fatal.cpp hstcpcli.cpp \ + socket.cpp string_util.cpp +libhsclient_la_CFLAGS = $(AM_CFLAGS) +libhsclient_la_CXXFLAGS = $(AM_CXXFLAGS) diff --git a/plugin/handler_socket/libhsclient/Makefile.plain b/plugin/handler_socket/libhsclient/Makefile.plain new file mode 100644 index 00000000..9e6277b6 --- /dev/null +++ b/plugin/handler_socket/libhsclient/Makefile.plain @@ -0,0 +1,27 @@ + +CXX = g++ -Wall -g -fno-rtti -fno-exceptions -fPIC -DPIC +LDFLAGS = + +CXXFLAGS += -O3 -DNDEBUG + +COMMON_OBJS = config.o fatal.o socket.o string_util.o escape.o +HSCLIENT_OBJS = $(COMMON_OBJS) hstcpcli.o + +all: libhsclient.a + +libhsclient.a: $(HSCLIENT_OBJS) + $(AR) rc $@ $^ + $(AR) s $@ + +clean: + rm -f *.a *.so *.o + +LIBDIR = $(shell \ + if [ -e /usr/lib64/mysql ]; then echo /usr/lib64; else echo /usr/lib; fi) + +install: libhsclient.a + sudo sh -c 'cp libhsclient.a libhsclient.a.cpy && \ + mv libhsclient.a.cpy $(LIBDIR)/libhsclient.a && \ + mkdir -p /usr/include/handlersocket && \ + cp -a *.hpp /usr/include/handlersocket/' + diff --git a/plugin/handler_socket/libhsclient/allocator.hpp b/plugin/handler_socket/libhsclient/allocator.hpp new file mode 100644 index 00000000..dd3a28ba --- /dev/null +++ b/plugin/handler_socket/libhsclient/allocator.hpp @@ -0,0 +1,64 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_ALLOCATOR_HPP +#define DENA_ALLOCATOR_HPP + +#include <stdlib.h> +#include <string.h> + +#if 0 +extern "C" { +#include <tlsf.h> +}; +#define DENA_MALLOC(x) tlsf_malloc(x) +#define DENA_REALLOC(x, y) tlsf_realloc(x, y) +#define DENA_FREE(x) tlsf_free(x) +#define DENA_NEWCHAR(x) static_cast<char *>(tlsf_malloc(x)) +#define DENA_DELETE(x) tlsf_free(x) +typedef std::allocator<int> allocator_type; +#endif + +#if 1 +#define DENA_MALLOC(x) malloc(x) +#define DENA_REALLOC(x, y) realloc(x, y) +#define DENA_FREE(x) free(x) +#define DENA_NEWCHAR(x) (new char[x]) +#define DENA_DELETE(x) (delete [] x) +typedef std::allocator<int> allocator_type; +#endif + +#if 1 +#define DENA_ALLOCA_ALLOCATE(typ, len) \ + (typ *) alloca((len) * sizeof(typ)) +#define DENA_ALLOCA_FREE(x) +#else +#define DENA_ALLOCA_ALLOCATE(typ, len) \ + static_cast<typ *>(malloc((len) * sizeof(typ))) +#define DENA_ALLOCA_FREE(x) free(x) +#endif + +namespace dena { + +template <typename T> struct auto_alloca_free { + auto_alloca_free(T *value) : value(value) { } + ~auto_alloca_free() { + /* no-op if alloca() is used */ + DENA_ALLOCA_FREE(value); + } + private: + auto_alloca_free(const auto_alloca_free&); + auto_alloca_free& operator =(const auto_alloca_free&); + private: + T *value; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/auto_addrinfo.hpp b/plugin/handler_socket/libhsclient/auto_addrinfo.hpp new file mode 100644 index 00000000..f9db70fe --- /dev/null +++ b/plugin/handler_socket/libhsclient/auto_addrinfo.hpp @@ -0,0 +1,52 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_AUTO_ADDRINFO_HPP +#define DENA_AUTO_ADDRINFO_HPP + +#include <my_global.h> +#include <sys/socket.h> +#include <netdb.h> +#include <string.h> +#include "util.hpp" + +typedef SOCKET_SIZE_TYPE size_socket; + +namespace dena { + +struct auto_addrinfo : private noncopyable { + auto_addrinfo() : addr(0) { } + ~auto_addrinfo() { + reset(); + } + void reset(addrinfo *a = 0) { + if (addr != 0) { + freeaddrinfo(addr); + } + addr = a; + } + const addrinfo *get() const { return addr; } + int resolve(const char *node, const char *service, int flags = 0, + int family = AF_UNSPEC, int socktype = SOCK_STREAM, int protocol = 0) { + addrinfo hints; + reset(); + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = flags; + hints.ai_family = family; + hints.ai_socktype = socktype; + hints.ai_protocol = protocol; + return getaddrinfo(node, service, &hints, &addr); + } + private: + addrinfo *addr; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/auto_file.hpp b/plugin/handler_socket/libhsclient/auto_file.hpp new file mode 100644 index 00000000..03c357f4 --- /dev/null +++ b/plugin/handler_socket/libhsclient/auto_file.hpp @@ -0,0 +1,69 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_AUTO_FILE_HPP +#define DENA_AUTO_FILE_HPP + +/* Workaround for _LARGE_FILES and _LARGE_FILE_API incompatibility on AIX */ +#if defined(_AIX) && defined(_LARGE_FILE_API) +#undef _LARGE_FILE_API +#endif + +#include <unistd.h> +#include <sys/types.h> +#include <dirent.h> +#include <stdio.h> + +#include "util.hpp" + +namespace dena { + +struct auto_file : private noncopyable { + auto_file() : fd(-1) { } + ~auto_file() { + reset(); + } + int get() const { return fd; } + int close() { + if (fd < 0) { + return 0; + } + const int r = ::close(fd); + fd = -1; + return r; + } + void reset(int x = -1) { + if (fd >= 0) { + this->close(); + } + fd = x; + } + private: + int fd; +}; + +struct auto_dir : private noncopyable { + auto_dir() : dp(0) { } + ~auto_dir() { + reset(); + } + DIR *get() const { return dp; } + void reset(DIR *d = 0) { + if (dp != 0) { + closedir(dp); + } + dp = d; + } + private: + DIR *dp; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/auto_ptrcontainer.hpp b/plugin/handler_socket/libhsclient/auto_ptrcontainer.hpp new file mode 100644 index 00000000..314bc151 --- /dev/null +++ b/plugin/handler_socket/libhsclient/auto_ptrcontainer.hpp @@ -0,0 +1,67 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_AUTO_PTRCONTAINER_HPP +#define DENA_AUTO_PTRCONTAINER_HPP + +namespace dena { + +template <typename Tcnt> +struct auto_ptrcontainer { + typedef Tcnt container_type; + typedef typename container_type::value_type value_type; + typedef typename container_type::pointer pointer; + typedef typename container_type::reference reference; + typedef typename container_type::const_reference const_reference; + typedef typename container_type::size_type size_type; + typedef typename container_type::difference_type difference_type; + typedef typename container_type::iterator iterator; + typedef typename container_type::const_iterator const_iterator; + typedef typename container_type::reverse_iterator reverse_iterator; + typedef typename container_type::const_reverse_iterator + const_reverse_iterator; + iterator begin() { return cnt.begin(); } + const_iterator begin() const { return cnt.begin(); } + iterator end() { return cnt.end(); } + const_iterator end() const { return cnt.end(); } + reverse_iterator rbegin() { return cnt.rbegin(); } + reverse_iterator rend() { return cnt.rend(); } + const_reverse_iterator rbegin() const { return cnt.rbegin(); } + const_reverse_iterator rend() const { return cnt.rend(); } + size_type size() const { return cnt.size(); } + size_type max_size() const { return cnt.max_size(); } + bool empty() const { return cnt.empty(); } + reference front() { return cnt.front(); } + const_reference front() const { cnt.front(); } + reference back() { return cnt.back(); } + const_reference back() const { cnt.back(); } + void swap(auto_ptrcontainer& x) { cnt.swap(x.cnt); } + ~auto_ptrcontainer() { + for (iterator i = begin(); i != end(); ++i) { + delete *i; + } + } + template <typename Tap> void push_back_ptr(Tap& ap) { + cnt.push_back(ap.get()); + ap.release(); + } + void erase_ptr(iterator i) { + delete *i; + cnt.erase(i); + } + reference operator [](size_type n) { return cnt[n]; } + const_reference operator [](size_type n) const { return cnt[n]; } + void clear() { cnt.clear(); } + private: + Tcnt cnt; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/config.cpp b/plugin/handler_socket/libhsclient/config.cpp new file mode 100644 index 00000000..3c90b36d --- /dev/null +++ b/plugin/handler_socket/libhsclient/config.cpp @@ -0,0 +1,67 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "config.hpp" + +namespace dena { + +unsigned int verbose_level = 0; + +std::string +config::get_str(const std::string& key, const std::string& def) const +{ + const_iterator iter = this->find(key); + if (iter == this->end()) { + DENA_VERBOSE(10, fprintf(stderr, "CONFIG: %s=%s(default)\n", key.c_str(), + def.c_str())); + return def; + } + DENA_VERBOSE(10, fprintf(stderr, "CONFIG: %s=%s\n", key.c_str(), + iter->second.c_str())); + return iter->second; +} + +long long +config::get_int(const std::string& key, long long def) const +{ + const_iterator iter = this->find(key); + if (iter == this->end()) { + DENA_VERBOSE(10, fprintf(stderr, "CONFIG: %s=%lld(default)\n", key.c_str(), + def)); + return def; + } + const long long r = atoll(iter->second.c_str()); + DENA_VERBOSE(10, fprintf(stderr, "CONFIG: %s=%lld\n", key.c_str(), r)); + return r; +} + +void +parse_args(int argc, char **argv, config& conf) +{ + for (int i = 1; i < argc; ++i) { + const char *const arg = argv[i]; + const char *const eq = strchr(arg, '='); + if (eq == 0) { + continue; + } + const std::string key(arg, eq - arg); + const std::string val(eq + 1); + conf[key] = val; + } + config::const_iterator iter = conf.find("verbose"); + if (iter != conf.end()) { + verbose_level = atoi(iter->second.c_str()); + } +} + +}; + diff --git a/plugin/handler_socket/libhsclient/config.hpp b/plugin/handler_socket/libhsclient/config.hpp new file mode 100644 index 00000000..c9f16c76 --- /dev/null +++ b/plugin/handler_socket/libhsclient/config.hpp @@ -0,0 +1,32 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_CONFIG_HPP +#define DENA_CONFIG_HPP + +#include <string> +#include <map> + +#define DENA_VERBOSE(lv, x) if (dena::verbose_level >= (lv)) { (x); } + +namespace dena { + +struct config : public std::map<std::string, std::string> { + std::string get_str(const std::string& key, const std::string& def = "") + const; + long long get_int(const std::string& key, long long def = 0) const; +}; + +void parse_args(int argc, char **argv, config& conf); + +extern unsigned int verbose_level; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/escape.cpp b/plugin/handler_socket/libhsclient/escape.cpp new file mode 100644 index 00000000..d4df8ae8 --- /dev/null +++ b/plugin/handler_socket/libhsclient/escape.cpp @@ -0,0 +1,127 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <stdio.h> + +#include "escape.hpp" +#include "string_buffer.hpp" +#include "fatal.hpp" +#include "string_util.hpp" + +#define DBG_OP(x) +#define DBG_BUF(x) + +namespace dena { + +enum special_char_t { + special_char_escape_prefix = 0x01, /* SOH */ + special_char_noescape_min = 0x10, /* DLE */ + special_char_escape_shift = 0x40, /* '@' */ +}; + +void +escape_string(char *& wp, const char *start, const char *finish) +{ + while (start != finish) { + const unsigned char c = *start; + if (c >= special_char_noescape_min) { + wp[0] = c; /* no need to escape */ + } else { + wp[0] = special_char_escape_prefix; + ++wp; + wp[0] = c + special_char_escape_shift; + } + ++start; + ++wp; + } +} + +void +escape_string(string_buffer& ar, const char *start, const char *finish) +{ + const size_t buflen = (finish - start) * 2; + char *const wp_begin = ar.make_space(buflen); + char *wp = wp_begin; + escape_string(wp, start, finish); + ar.space_wrote(wp - wp_begin); +} + +bool +unescape_string(char *& wp, const char *start, const char *finish) +{ + /* works even if wp == start */ + while (start != finish) { + const unsigned char c = *start; + if (c != special_char_escape_prefix) { + wp[0] = c; + } else if (start + 1 != finish) { + ++start; + const unsigned char cn = *start; + if (cn < special_char_escape_shift) { + return false; + } + wp[0] = cn - special_char_escape_shift; + } else { + return false; + } + ++start; + ++wp; + } + return true; +} + +bool +unescape_string(string_buffer& ar, const char *start, const char *finish) +{ + const size_t buflen = finish - start; + char *const wp_begin = ar.make_space(buflen); + char *wp = wp_begin; + const bool r = unescape_string(wp, start, finish); + ar.space_wrote(wp - wp_begin); + return r; +} + +uint32_t +read_ui32(char *& start, char *finish) +{ + char *const n_begin = start; + read_token(start, finish); + char *const n_end = start; + uint32_t v = 0; + for (char *p = n_begin; p != n_end; ++p) { + const char ch = p[0]; + if (ch >= '0' && ch <= '9') { + v *= 10; + v += (ch - '0'); + } + } + return v; +} + +void +write_ui32(string_buffer& buf, uint32_t v) +{ + char *wp = buf.make_space(12); + int len = snprintf(wp, 12, "%u", v); + if (len > 0) { + buf.space_wrote(len); + } +} + +void +write_ui64(string_buffer& buf, uint64_t v) +{ + char *wp = buf.make_space(22); + int len = snprintf(wp, 22, "%llu", static_cast<unsigned long long>(v)); + if (len > 0) { + buf.space_wrote(len); + } +} + +}; + diff --git a/plugin/handler_socket/libhsclient/escape.hpp b/plugin/handler_socket/libhsclient/escape.hpp new file mode 100644 index 00000000..b928defe --- /dev/null +++ b/plugin/handler_socket/libhsclient/escape.hpp @@ -0,0 +1,66 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <stdint.h> + +#include "string_buffer.hpp" +#include "string_ref.hpp" +#include "string_util.hpp" + +#ifndef DENA_ESCAPE_HPP +#define DENA_ESCAPE_HPP + +namespace dena { + +void escape_string(char *& wp, const char *start, const char *finish); +void escape_string(string_buffer& ar, const char *start, const char *finish); +bool unescape_string(char *& wp, const char *start, const char *finish); + /* unescaped_string() works even if wp == start */ +bool unescape_string(string_buffer& ar, const char *start, const char *finish); + +uint32_t read_ui32(char *& start, char *finish); +void write_ui32(string_buffer& buf, uint32_t v); +void write_ui64(string_buffer& buf, uint64_t v); + +inline bool +is_null_expression(const char *start, const char *finish) +{ + return (finish == start + 1 && start[0] == 0); +} + +inline void +read_token(char *& start, char *finish) +{ + char *const p = memchr_char(start, '\t', finish - start); + if (p == 0) { + start = finish; + } else { + start = p; + } +} + +inline void +skip_token_delim_fold(char *& start, char *finish) +{ + while (start != finish && start[0] == '\t') { + ++start; + } +} + +inline void +skip_one(char *& start, char *finish) +{ + if (start != finish) { + ++start; + } +} + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/fatal.cpp b/plugin/handler_socket/libhsclient/fatal.cpp new file mode 100644 index 00000000..8e109cf1 --- /dev/null +++ b/plugin/handler_socket/libhsclient/fatal.cpp @@ -0,0 +1,29 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <syslog.h> +#include <unistd.h> + +#include "fatal.hpp" + +namespace dena { + +const int opt_syslog = LOG_ERR | LOG_PID | LOG_CONS; + +void +fatal_abort(const std::string& message) +{ + fprintf(stderr, "FATAL_COREDUMP: %s\n", message.c_str()); + syslog(opt_syslog, "FATAL_COREDUMP: %s", message.c_str()); + abort(); +} + +}; + diff --git a/plugin/handler_socket/libhsclient/fatal.hpp b/plugin/handler_socket/libhsclient/fatal.hpp new file mode 100644 index 00000000..5eaa3db8 --- /dev/null +++ b/plugin/handler_socket/libhsclient/fatal.hpp @@ -0,0 +1,21 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_FATAL_HPP +#define DENA_FATAL_HPP + +#include <string> + +namespace dena { + +void fatal_abort(const std::string& message); + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/hstcpcli.cpp b/plugin/handler_socket/libhsclient/hstcpcli.cpp new file mode 100644 index 00000000..461bed3f --- /dev/null +++ b/plugin/handler_socket/libhsclient/hstcpcli.cpp @@ -0,0 +1,442 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <my_global.h> +#include <stdexcept> + +#include "hstcpcli.hpp" +#include "auto_file.hpp" +#include "string_util.hpp" +#include "auto_addrinfo.hpp" +#include "escape.hpp" +#include "util.hpp" + +/* TODO */ +#if !defined(__linux__) && !defined(__FreeBSD__) && !defined(MSG_NOSIGNAL) +#define MSG_NOSIGNAL 0 +#endif + +#define DBG(x) + +namespace dena { + +struct hstcpcli : public hstcpcli_i, private noncopyable { + hstcpcli(const socket_args& args); + virtual void close(); + virtual int reconnect(); + virtual bool stable_point(); + virtual void request_buf_open_index(size_t pst_id, const char *dbn, + const char *tbl, const char *idx, const char *retflds, const char *filflds); + virtual void request_buf_auth(const char *secret, const char *typ); + virtual void request_buf_exec_generic(size_t pst_id, const string_ref& op, + const string_ref *kvs, size_t kvslen, uint32_t limit, uint32_t skip, + const string_ref& mod_op, const string_ref *mvs, size_t mvslen, + const hstcpcli_filter *fils, size_t filslen, int invalues_keypart, + const string_ref *invalues, size_t invalueslen); + virtual int request_send(); + virtual int response_recv(size_t& num_flds_r); + virtual const string_ref *get_next_row(); + virtual void response_buf_remove(); + virtual int get_error_code(); + virtual std::string get_error(); + private: + int read_more(); + void clear_error(); + int set_error(int code, const std::string& str); + private: + auto_file fd; + socket_args sargs; + string_buffer readbuf; + string_buffer writebuf; + size_t response_end_offset; /* incl newline */ + size_t cur_row_offset; + size_t num_flds; + size_t num_req_bufd; /* buffered but not yet sent */ + size_t num_req_sent; /* sent but not yet received */ + size_t num_req_rcvd; /* received but not yet removed */ + int error_code; + std::string error_str; + std::vector<string_ref> flds; +}; + +hstcpcli::hstcpcli(const socket_args& args) + : sargs(args), response_end_offset(0), cur_row_offset(0), num_flds(0), + num_req_bufd(0), num_req_sent(0), num_req_rcvd(0), error_code(0) +{ + std::string err; + if (socket_connect(fd, sargs, err) != 0) { + set_error(-1, err); + } +} + +void +hstcpcli::close() +{ + fd.close(); + readbuf.clear(); + writebuf.clear(); + flds.clear(); + response_end_offset = 0; + cur_row_offset = 0; + num_flds = 0; + num_req_bufd = 0; + num_req_sent = 0; + num_req_rcvd = 0; +} + +int +hstcpcli::reconnect() +{ + clear_error(); + close(); + std::string err; + if (socket_connect(fd, sargs, err) != 0) { + set_error(-1, err); + } + return error_code; +} + +bool +hstcpcli::stable_point() +{ + /* returns true if cli can send a new request */ + return fd.get() >= 0 && num_req_bufd == 0 && num_req_sent == 0 && + num_req_rcvd == 0 && response_end_offset == 0; +} + +int +hstcpcli::get_error_code() +{ + return error_code; +} + +std::string +hstcpcli::get_error() +{ + return error_str; +} + +int +hstcpcli::read_more() +{ + const size_t block_size = 4096; // FIXME + char *const wp = readbuf.make_space(block_size); + const ssize_t rlen = read(fd.get(), wp, block_size); + if (rlen <= 0) { + if (rlen < 0) { + error_str = "read: failed"; + } else { + error_str = "read: eof"; + } + return rlen; + } + readbuf.space_wrote(rlen); + return rlen; +} + +void +hstcpcli::clear_error() +{ + DBG(fprintf(stderr, "CLEAR_ERROR: %d\n", error_code)); + error_code = 0; + error_str.clear(); +} + +int +hstcpcli::set_error(int code, const std::string& str) +{ + DBG(fprintf(stderr, "SET_ERROR: %d\n", code)); + error_code = code; + error_str = str; + return error_code; +} + +void +hstcpcli::request_buf_open_index(size_t pst_id, const char *dbn, + const char *tbl, const char *idx, const char *retflds, const char *filflds) +{ + if (num_req_sent > 0 || num_req_rcvd > 0) { + close(); + set_error(-1, "request_buf_open_index: protocol out of sync"); + return; + } + const string_ref dbn_ref(dbn, strlen(dbn)); + const string_ref tbl_ref(tbl, strlen(tbl)); + const string_ref idx_ref(idx, strlen(idx)); + const string_ref rfs_ref(retflds, strlen(retflds)); + writebuf.append_literal("P\t"); + append_uint32(writebuf, pst_id); // FIXME size_t ? + writebuf.append_literal("\t"); + writebuf.append(dbn_ref.begin(), dbn_ref.end()); + writebuf.append_literal("\t"); + writebuf.append(tbl_ref.begin(), tbl_ref.end()); + writebuf.append_literal("\t"); + writebuf.append(idx_ref.begin(), idx_ref.end()); + writebuf.append_literal("\t"); + writebuf.append(rfs_ref.begin(), rfs_ref.end()); + if (filflds != 0) { + const string_ref fls_ref(filflds, strlen(filflds)); + writebuf.append_literal("\t"); + writebuf.append(fls_ref.begin(), fls_ref.end()); + } + writebuf.append_literal("\n"); + ++num_req_bufd; +} + +void +hstcpcli::request_buf_auth(const char *secret, const char *typ) +{ + if (num_req_sent > 0 || num_req_rcvd > 0) { + close(); + set_error(-1, "request_buf_auth: protocol out of sync"); + return; + } + if (typ == 0) { + typ = "1"; + } + const string_ref typ_ref(typ, strlen(typ)); + const string_ref secret_ref(secret, strlen(secret)); + writebuf.append_literal("A\t"); + writebuf.append(typ_ref.begin(), typ_ref.end()); + writebuf.append_literal("\t"); + writebuf.append(secret_ref.begin(), secret_ref.end()); + writebuf.append_literal("\n"); + ++num_req_bufd; +} + +namespace { + +void +append_delim_value(string_buffer& buf, const char *start, const char *finish) +{ + if (start == 0) { + /* null */ + const char t[] = "\t\0"; + buf.append(t, t + 2); + } else { + /* non-null */ + buf.append_literal("\t"); + escape_string(buf, start, finish); + } +} + +}; + +void +hstcpcli::request_buf_exec_generic(size_t pst_id, const string_ref& op, + const string_ref *kvs, size_t kvslen, uint32_t limit, uint32_t skip, + const string_ref& mod_op, const string_ref *mvs, size_t mvslen, + const hstcpcli_filter *fils, size_t filslen, int invalues_keypart, + const string_ref *invalues, size_t invalueslen) +{ + if (num_req_sent > 0 || num_req_rcvd > 0) { + close(); + set_error(-1, "request_buf_exec_generic: protocol out of sync"); + return; + } + append_uint32(writebuf, pst_id); // FIXME size_t ? + writebuf.append_literal("\t"); + writebuf.append(op.begin(), op.end()); + writebuf.append_literal("\t"); + append_uint32(writebuf, kvslen); // FIXME size_t ? + for (size_t i = 0; i < kvslen; ++i) { + const string_ref& kv = kvs[i]; + append_delim_value(writebuf, kv.begin(), kv.end()); + } + if (limit != 0 || skip != 0 || invalues_keypart >= 0 || + mod_op.size() != 0 || filslen != 0) { + /* has more option */ + writebuf.append_literal("\t"); + append_uint32(writebuf, limit); // FIXME size_t ? + if (skip != 0 || invalues_keypart >= 0 || + mod_op.size() != 0 || filslen != 0) { + writebuf.append_literal("\t"); + append_uint32(writebuf, skip); // FIXME size_t ? + } + if (invalues_keypart >= 0) { + writebuf.append_literal("\t@\t"); + append_uint32(writebuf, invalues_keypart); + writebuf.append_literal("\t"); + append_uint32(writebuf, invalueslen); + for (size_t i = 0; i < invalueslen; ++i) { + const string_ref& s = invalues[i]; + append_delim_value(writebuf, s.begin(), s.end()); + } + } + for (size_t i = 0; i < filslen; ++i) { + const hstcpcli_filter& f = fils[i]; + writebuf.append_literal("\t"); + writebuf.append(f.filter_type.begin(), f.filter_type.end()); + writebuf.append_literal("\t"); + writebuf.append(f.op.begin(), f.op.end()); + writebuf.append_literal("\t"); + append_uint32(writebuf, f.ff_offset); + append_delim_value(writebuf, f.val.begin(), f.val.end()); + } + if (mod_op.size() != 0) { + writebuf.append_literal("\t"); + writebuf.append(mod_op.begin(), mod_op.end()); + for (size_t i = 0; i < mvslen; ++i) { + const string_ref& mv = mvs[i]; + append_delim_value(writebuf, mv.begin(), mv.end()); + } + } + } + writebuf.append_literal("\n"); + ++num_req_bufd; +} + +int +hstcpcli::request_send() +{ + if (error_code < 0) { + return error_code; + } + clear_error(); + if (fd.get() < 0) { + close(); + return set_error(-1, "write: closed"); + } + if (num_req_bufd == 0 || num_req_sent > 0 || num_req_rcvd > 0) { + close(); + return set_error(-1, "request_send: protocol out of sync"); + } + const size_t wrlen = writebuf.size(); + const ssize_t r = send(fd.get(), writebuf.begin(), wrlen, MSG_NOSIGNAL); + if (r <= 0) { + close(); + return set_error(-1, r < 0 ? "write: failed" : "write: eof"); + } + writebuf.erase_front(r); + if (static_cast<size_t>(r) != wrlen) { + close(); + return set_error(-1, "write: incomplete"); + } + num_req_sent = num_req_bufd; + num_req_bufd = 0; + DBG(fprintf(stderr, "REQSEND 0\n")); + return 0; +} + +int +hstcpcli::response_recv(size_t& num_flds_r) +{ + if (error_code < 0) { + return error_code; + } + clear_error(); + if (num_req_bufd > 0 || num_req_sent == 0 || num_req_rcvd > 0 || + response_end_offset != 0) { + close(); + return set_error(-1, "response_recv: protocol out of sync"); + } + cur_row_offset = 0; + num_flds_r = num_flds = 0; + if (fd.get() < 0) { + return set_error(-1, "read: closed"); + } + size_t offset = 0; + while (true) { + const char *const lbegin = readbuf.begin() + offset; + const char *const lend = readbuf.end(); + const char *const nl = memchr_char(lbegin, '\n', lend - lbegin); + if (nl != 0) { + offset = (nl + 1) - readbuf.begin(); + break; + } + if (read_more() <= 0) { + close(); + return set_error(-1, "read: eof"); + } + } + response_end_offset = offset; + --num_req_sent; + ++num_req_rcvd; + char *start = readbuf.begin(); + char *const finish = start + response_end_offset - 1; + const size_t resp_code = read_ui32(start, finish); + skip_one(start, finish); + num_flds_r = num_flds = read_ui32(start, finish); + if (resp_code != 0) { + skip_one(start, finish); + char *const err_begin = start; + read_token(start, finish); + char *const err_end = start; + std::string e = std::string(err_begin, err_end - err_begin); + if (e.empty()) { + e = "unknown_error"; + } + return set_error(resp_code, e); + } + cur_row_offset = start - readbuf.begin(); + DBG(fprintf(stderr, "[%s] ro=%zu eol=%zu\n", + std::string(readbuf.begin(), readbuf.begin() + response_end_offset) + .c_str(), + cur_row_offset, response_end_offset)); + DBG(fprintf(stderr, "RES 0\n")); + return 0; +} + +const string_ref * +hstcpcli::get_next_row() +{ + if (num_flds == 0) { + DBG(fprintf(stderr, "GNR NF 0\n")); + return 0; + } + if (flds.size() < num_flds) { + flds.resize(num_flds); + } + char *start = readbuf.begin() + cur_row_offset; + char *const finish = readbuf.begin() + response_end_offset - 1; + if (start >= finish) { /* start[0] == nl */ + DBG(fprintf(stderr, "GNR FIN 0 %p %p\n", start, finish)); + return 0; + } + for (size_t i = 0; i < num_flds; ++i) { + skip_one(start, finish); + char *const fld_begin = start; + read_token(start, finish); + char *const fld_end = start; + char *wp = fld_begin; + if (is_null_expression(fld_begin, fld_end)) { + /* null */ + flds[i] = string_ref(); + } else { + unescape_string(wp, fld_begin, fld_end); /* in-place */ + flds[i] = string_ref(fld_begin, wp); + } + } + cur_row_offset = start - readbuf.begin(); + return &flds[0]; +} + +void +hstcpcli::response_buf_remove() +{ + if (response_end_offset == 0) { + close(); + set_error(-1, "response_buf_remove: protocol out of sync"); + return; + } + readbuf.erase_front(response_end_offset); + response_end_offset = 0; + --num_req_rcvd; + cur_row_offset = 0; + num_flds = 0; + flds.clear(); +} + +hstcpcli_ptr +hstcpcli_i::create(const socket_args& args) +{ + return hstcpcli_ptr(new hstcpcli(args)); +} + +}; + diff --git a/plugin/handler_socket/libhsclient/hstcpcli.hpp b/plugin/handler_socket/libhsclient/hstcpcli.hpp new file mode 100644 index 00000000..11dec8eb --- /dev/null +++ b/plugin/handler_socket/libhsclient/hstcpcli.hpp @@ -0,0 +1,62 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_HSTCPCLI_HPP +#define DENA_HSTCPCLI_HPP + +#include <sys/types.h> +#include <sys/socket.h> +#include <string> +#include <memory> + +#include "config.hpp" +#include "socket.hpp" +#include "string_ref.hpp" +#include "string_buffer.hpp" + +namespace dena { + +struct hstcpcli_filter { + string_ref filter_type; + string_ref op; + size_t ff_offset; + string_ref val; + hstcpcli_filter() : ff_offset(0) { } +}; + +struct hstcpcli_i; +typedef std::auto_ptr<hstcpcli_i> hstcpcli_ptr; + +struct hstcpcli_i { + virtual ~hstcpcli_i() { } + virtual void close() = 0; + virtual int reconnect() = 0; + virtual bool stable_point() = 0; + virtual void request_buf_auth(const char *secret, const char *typ) = 0; + virtual void request_buf_open_index(size_t pst_id, const char *dbn, + const char *tbl, const char *idx, const char *retflds, + const char *filflds = 0) = 0; + virtual void request_buf_exec_generic(size_t pst_id, const string_ref& op, + const string_ref *kvs, size_t kvslen, uint32_t limit, uint32_t skip, + const string_ref& mod_op, const string_ref *mvs, size_t mvslen, + const hstcpcli_filter *fils = 0, size_t filslen = 0, + int invalues_keypart = -1, const string_ref *invalues = 0, + size_t invalueslen = 0) = 0; // FIXME: too long + virtual int request_send() = 0; + virtual int response_recv(size_t& num_flds_r) = 0; + virtual const string_ref *get_next_row() = 0; + virtual void response_buf_remove() = 0; + virtual int get_error_code() = 0; + virtual std::string get_error() = 0; + static hstcpcli_ptr create(const socket_args& args); +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/libhsclient.spec.template b/plugin/handler_socket/libhsclient/libhsclient.spec.template new file mode 100644 index 00000000..3e4dfe04 --- /dev/null +++ b/plugin/handler_socket/libhsclient/libhsclient.spec.template @@ -0,0 +1,39 @@ +Summary: handlersocket client library +Name: libhsclient +Version: HANDLERSOCKET_VERSION +Release: 1%{?dist} +Group: System Environment/Libraries +License: BSD +Source: libhsclient.tar.gz +Packager: Akira Higuchi <higuchi dot akira at dena dot jp> +BuildRoot: /var/tmp/%{name}-%{version}-root + +%description + +%prep +%setup -n %{name} + +%define _use_internal_dependency_generator 0 + +%build +make -f Makefile.plain + +%install +rm -rf $RPM_BUILD_ROOT +mkdir -p $RPM_BUILD_ROOT/usr/include/handlersocket +mkdir -p $RPM_BUILD_ROOT/%{_bindir} +mkdir -p $RPM_BUILD_ROOT/%{_libdir} +install -m 755 libhsclient.a $RPM_BUILD_ROOT/%{_libdir} +install -m 644 *.hpp $RPM_BUILD_ROOT/usr/include/handlersocket/ + +%post +/sbin/ldconfig + +%postun +/sbin/ldconfig + +%files +%defattr(-, root, root) +/usr/include/* +%{_libdir}/*.a + diff --git a/plugin/handler_socket/libhsclient/mutex.hpp b/plugin/handler_socket/libhsclient/mutex.hpp new file mode 100644 index 00000000..9cef2757 --- /dev/null +++ b/plugin/handler_socket/libhsclient/mutex.hpp @@ -0,0 +1,51 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_MUTEX_HPP +#define DENA_MUTEX_HPP + +#include <pthread.h> +#include <stdlib.h> + +#include "fatal.hpp" +#include "util.hpp" + +namespace dena { + +struct condition; + +struct mutex : private noncopyable { + friend struct condition; + mutex() { + if (pthread_mutex_init(&mtx, 0) != 0) { + fatal_abort("pthread_mutex_init"); + } + } + ~mutex() { + if (pthread_mutex_destroy(&mtx) != 0) { + fatal_abort("pthread_mutex_destroy"); + } + } + void lock() const { + if (pthread_mutex_lock(&mtx) != 0) { + fatal_abort("pthread_mutex_lock"); + } + } + void unlock() const { + if (pthread_mutex_unlock(&mtx) != 0) { + fatal_abort("pthread_mutex_unlock"); + } + } + private: + mutable pthread_mutex_t mtx; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/socket.cpp b/plugin/handler_socket/libhsclient/socket.cpp new file mode 100644 index 00000000..f1cdc9fb --- /dev/null +++ b/plugin/handler_socket/libhsclient/socket.cpp @@ -0,0 +1,185 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <my_global.h> + +#include <stdexcept> +#include <string.h> +#include <signal.h> +#include <sys/un.h> + +#include "socket.hpp" +#include "string_util.hpp" +#include "fatal.hpp" + +namespace dena { + +void +ignore_sigpipe() +{ + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { + fatal_abort("SIGPIPE SIG_IGN"); + } +} + +void +socket_args::set(const config& conf) +{ + timeout = conf.get_int("timeout", 600); + listen_backlog = conf.get_int("listen_backlog", 256); + std::string node = conf.get_str("host", ""); + std::string port = conf.get_str("port", ""); + if (!node.empty() || !port.empty()) { + if (family == AF_UNIX || node == "/") { + set_unix_domain(port.c_str()); + } else { + const char *nd = node.empty() ? 0 : node.c_str(); + if (resolve(nd, port.c_str()) != 0) { + fatal_abort("getaddrinfo failed: " + node + ":" + port); + } + } + } + sndbuf = conf.get_int("sndbuf", 0); + rcvbuf = conf.get_int("rcvbuf", 0); +} + +void +socket_args::set_unix_domain(const char *path) +{ + family = AF_UNIX; + addr = sockaddr_storage(); + addrlen = sizeof(sockaddr_un); + sockaddr_un *const ap = reinterpret_cast<sockaddr_un *>(&addr); + ap->sun_family = AF_UNIX; + strncpy(ap->sun_path, path, sizeof(ap->sun_path) - 1); +} + +int +socket_args::resolve(const char *node, const char *service) +{ + const int flags = (node == 0) ? AI_PASSIVE : 0; + auto_addrinfo ai; + addr = sockaddr_storage(); + addrlen = 0; + const int r = ai.resolve(node, service, flags, family, socktype, protocol); + if (r != 0) { + return r; + } + memcpy(&addr, ai.get()->ai_addr, ai.get()->ai_addrlen); + addrlen = ai.get()->ai_addrlen; + return 0; +} + +int +socket_set_options(auto_file& fd, const socket_args& args, std::string& err_r) +{ + if (args.timeout != 0 && !args.nonblocking) { + struct timeval tv; + tv.tv_sec = args.timeout; + tv.tv_usec = 0; + if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) { + return errno_string("setsockopt SO_RCVTIMEO", errno, err_r); + } + tv.tv_sec = args.timeout; + tv.tv_usec = 0; + if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) { + return errno_string("setsockopt SO_RCVTIMEO", errno, err_r); + } + } + if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) { + return errno_string("fcntl O_NONBLOCK", errno, err_r); + } + if (args.sndbuf != 0) { + const int v = args.sndbuf; + if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) != 0) { + return errno_string("setsockopt SO_SNDBUF", errno, err_r); + } + } + if (args.rcvbuf != 0) { + const int v = args.rcvbuf; + if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) != 0) { + return errno_string("setsockopt SO_RCVBUF", errno, err_r); + } + } + return 0; +} + +int +socket_open(auto_file& fd, const socket_args& args, std::string& err_r) +{ + fd.reset(socket(args.family, args.socktype, args.protocol)); + if (fd.get() < 0) { + return errno_string("socket", errno, err_r); + } + return socket_set_options(fd, args, err_r); +} + +int +socket_connect(auto_file& fd, const socket_args& args, std::string& err_r) +{ + int r = 0; + if ((r = socket_open(fd, args, err_r)) != 0) { + return r; + } + if (connect(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr), + args.addrlen) != 0) { + if (!args.nonblocking || errno != EINPROGRESS) { + return errno_string("connect", errno, err_r); + } + } + return 0; +} + +int +socket_bind(auto_file& fd, const socket_args& args, std::string& err_r) +{ + fd.reset(socket(args.family, args.socktype, args.protocol)); + if (fd.get() < 0) { + return errno_string("socket", errno, err_r); + } + if (args.reuseaddr) { + if (args.family == AF_UNIX) { + const sockaddr_un *const ap = + reinterpret_cast<const sockaddr_un *>(&args.addr); + if (unlink(ap->sun_path) != 0 && errno != ENOENT) { + return errno_string("unlink uds", errno, err_r); + } + } else { + int v = 1; + if (setsockopt(fd.get(), SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) != 0) { + return errno_string("setsockopt SO_REUSEADDR", errno, err_r); + } + } + } + if (bind(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr), + args.addrlen) != 0) { + return errno_string("bind", errno, err_r); + } + if (listen(fd.get(), args.listen_backlog) != 0) { + return errno_string("listen", errno, err_r); + } + if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) { + return errno_string("fcntl O_NONBLOCK", errno, err_r); + } + return 0; +} + +int +socket_accept(int listen_fd, auto_file& fd, const socket_args& args, + sockaddr_storage& addr_r, size_socket& addrlen_r, std::string& err_r) +{ + fd.reset(accept(listen_fd, reinterpret_cast<sockaddr *>(&addr_r), + &addrlen_r)); + if (fd.get() < 0) { + return errno_string("accept", errno, err_r); + } + return socket_set_options(fd, args, err_r); +} + +}; + diff --git a/plugin/handler_socket/libhsclient/socket.hpp b/plugin/handler_socket/libhsclient/socket.hpp new file mode 100644 index 00000000..c6e638c9 --- /dev/null +++ b/plugin/handler_socket/libhsclient/socket.hpp @@ -0,0 +1,51 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_SOCKET_HPP +#define DENA_SOCKET_HPP + +#include <string> + +#include "auto_addrinfo.hpp" +#include "auto_file.hpp" +#include "config.hpp" + +namespace dena { + +struct socket_args { + sockaddr_storage addr; + size_socket addrlen; + int family; + int socktype; + int protocol; + int timeout; + int listen_backlog; + bool reuseaddr; + bool nonblocking; + bool use_epoll; + int sndbuf; + int rcvbuf; + socket_args() : addr(), addrlen(0), family(AF_INET), socktype(SOCK_STREAM), + protocol(0), timeout(600), listen_backlog(256), + reuseaddr(true), nonblocking(false), use_epoll(false), + sndbuf(0), rcvbuf(0) { } + void set(const config& conf); + void set_unix_domain(const char *path); + int resolve(const char *node, const char *service); +}; + +void ignore_sigpipe(); +int socket_bind(auto_file& fd, const socket_args& args, std::string& err_r); +int socket_connect(auto_file& fd, const socket_args& args, std::string& err_r); +int socket_accept(int listen_fd, auto_file& fd, const socket_args& args, + sockaddr_storage& addr_r, size_socket& addrlen_r, std::string& err_r); + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/string_buffer.hpp b/plugin/handler_socket/libhsclient/string_buffer.hpp new file mode 100644 index 00000000..708c0df3 --- /dev/null +++ b/plugin/handler_socket/libhsclient/string_buffer.hpp @@ -0,0 +1,118 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_STRING_BUFFER_HPP +#define DENA_STRING_BUFFER_HPP + +#include <vector> +#include <stdlib.h> +#include <string.h> + +#include "util.hpp" +#include "allocator.hpp" +#include "fatal.hpp" + +namespace dena { + +struct string_buffer : private noncopyable { + string_buffer() : buffer(0), begin_offset(0), end_offset(0), alloc_size(0) { } + ~string_buffer() { + DENA_FREE(buffer); + } + const char *begin() const { + return buffer + begin_offset; + } + const char *end() const { + return buffer + end_offset; + } + char *begin() { + return buffer + begin_offset; + } + char *end() { + return buffer + end_offset; + } + size_t size() const { + return end_offset - begin_offset; + } + void clear() { + begin_offset = end_offset = 0; + } + void resize(size_t len) { + if (size() < len) { + reserve(len); + memset(buffer + end_offset, 0, len - size()); + } + end_offset = begin_offset + len; + } + void reserve(size_t len) { + if (alloc_size >= begin_offset + len) { + return; + } + size_t asz = alloc_size; + while (asz < begin_offset + len) { + if (asz == 0) { + asz = 16; + } + const size_t asz_n = asz << 1; + if (asz_n < asz) { + fatal_abort("string_buffer::resize() overflow"); + } + asz = asz_n; + } + void *const p = DENA_REALLOC(buffer, asz); + if (p == 0) { + fatal_abort("string_buffer::resize() realloc"); + } + buffer = static_cast<char *>(p); + alloc_size = asz; + } + void erase_front(size_t len) { + if (len >= size()) { + clear(); + } else { + begin_offset += len; + } + } + char *make_space(size_t len) { + reserve(size() + len); + return buffer + end_offset; + } + void space_wrote(size_t len) { + len = std::min(len, alloc_size - end_offset); + end_offset += len; + } + template <size_t N> + void append_literal(const char (& str)[N]) { + append(str, str + N - 1); + } + void append(const char *start, const char *finish) { + const size_t len = finish - start; + reserve(size() + len); + memcpy(buffer + end_offset, start, len); + end_offset += len; + } + void append_2(const char *s1, const char *f1, const char *s2, + const char *f2) { + const size_t l1 = f1 - s1; + const size_t l2 = f2 - s2; + reserve(end_offset + l1 + l2); + memcpy(buffer + end_offset, s1, l1); + memcpy(buffer + end_offset + l1, s2, l2); + end_offset += l1 + l2; + } + private: + char *buffer; + size_t begin_offset; + size_t end_offset; + size_t alloc_size; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/string_ref.hpp b/plugin/handler_socket/libhsclient/string_ref.hpp new file mode 100644 index 00000000..c5f93065 --- /dev/null +++ b/plugin/handler_socket/libhsclient/string_ref.hpp @@ -0,0 +1,63 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_STRING_REF_HPP +#define DENA_STRING_REF_HPP + +#include <vector> +#include <string.h> + +namespace dena { + +struct string_wref { + typedef char value_type; + char *begin() const { return start; } + char *end() const { return start + length; } + size_t size() const { return length; } + private: + char *start; + size_t length; + public: + string_wref(char *s = 0, size_t len = 0) : start(s), length(len) { } +}; + +struct string_ref { + typedef const char value_type; + const char *begin() const { return start; } + const char *end() const { return start + length; } + size_t size() const { return length; } + private: + const char *start; + size_t length; + public: + string_ref(const char *s = 0, size_t len = 0) : start(s), length(len) { } + string_ref(const char *s, const char *f) : start(s), length(f - s) { } + string_ref(const string_wref& w) : start(w.begin()), length(w.size()) { } +}; + +template <size_t N> inline bool +operator ==(const string_ref& x, const char (& y)[N]) { + return (x.size() == N - 1) && (::memcmp(x.begin(), y, N - 1) == 0); +} + +inline bool +operator ==(const string_ref& x, const string_ref& y) { + return (x.size() == y.size()) && + (::memcmp(x.begin(), y.begin(), x.size()) == 0); +} + +inline bool +operator !=(const string_ref& x, const string_ref& y) { + return (x.size() != y.size()) || + (::memcmp(x.begin(), y.begin(), x.size()) != 0); +} + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/string_util.cpp b/plugin/handler_socket/libhsclient/string_util.cpp new file mode 100644 index 00000000..8ee6000f --- /dev/null +++ b/plugin/handler_socket/libhsclient/string_util.cpp @@ -0,0 +1,182 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#include <errno.h> +#include <stdio.h> + +#include "string_util.hpp" + +namespace dena { + +string_wref +get_token(char *& wp, char *wp_end, char delim) +{ + char *const wp_begin = wp; + char *const p = memchr_char(wp_begin, delim, wp_end - wp_begin); + if (p == 0) { + wp = wp_end; + return string_wref(wp_begin, wp_end - wp_begin); + } + wp = p + 1; + return string_wref(wp_begin, p - wp_begin); +} + +template <typename T> T +atoi_tmpl_nocheck(const char *start, const char *finish) +{ + T v = 0; + for (; start != finish; ++start) { + const char c = *start; + if (c < '0' || c > '9') { + break; + } + v *= 10; + v += static_cast<T>(c - '0'); + } + return v; +} + +template <typename T> T +atoi_signed_tmpl_nocheck(const char *start, const char *finish) +{ + T v = 0; + bool negative = false; + if (start != finish) { + if (start[0] == '-') { + ++start; + negative = true; + } else if (start[0] == '+') { + ++start; + } + } + for (; start != finish; ++start) { + const char c = *start; + if (c < '0' || c > '9') { + break; + } + v *= 10; + if (negative) { + v -= static_cast<T>(c - '0'); + } else { + v += static_cast<T>(c - '0'); + } + } + return v; +} + +uint32_t +atoi_uint32_nocheck(const char *start, const char *finish) +{ + return atoi_tmpl_nocheck<uint32_t>(start, finish); +} + +long long +atoll_nocheck(const char *start, const char *finish) +{ + return atoi_signed_tmpl_nocheck<long long>(start, finish); +} + +void +append_uint32(string_buffer& buf, uint32_t v) +{ + char *const wp = buf.make_space(64); + const int len = snprintf(wp, 64, "%lu", static_cast<unsigned long>(v)); + if (len > 0) { + buf.space_wrote(len); + } +} + +std::string +to_stdstring(uint32_t v) +{ + char buf[64]; + snprintf(buf, sizeof(buf), "%lu", static_cast<unsigned long>(v)); + return std::string(buf); +} + +int +errno_string(const char *s, int en, std::string& err_r) +{ + char buf[64]; + snprintf(buf, sizeof(buf), "%s: %d", s, en); + err_r = std::string(buf); + return en; +} + +template <typename T> size_t +split_tmpl_arr(char delim, const T& buf, T *parts, size_t parts_len) +{ + typedef typename T::value_type value_type; + size_t i = 0; + value_type *start = buf.begin(); + value_type *const finish = buf.end(); + for (i = 0; i < parts_len; ++i) { + value_type *const p = memchr_char(start, delim, finish - start); + if (p == 0) { + parts[i] = T(start, finish - start); + ++i; + break; + } + parts[i] = T(start, p - start); + start = p + 1; + } + const size_t r = i; + for (; i < parts_len; ++i) { + parts[i] = T(); + } + return r; +} + +size_t +split(char delim, const string_ref& buf, string_ref *parts, + size_t parts_len) +{ + return split_tmpl_arr(delim, buf, parts, parts_len); +} + +size_t +split(char delim, const string_wref& buf, string_wref *parts, + size_t parts_len) +{ + return split_tmpl_arr(delim, buf, parts, parts_len); +} + +template <typename T, typename V> size_t +split_tmpl_vec(char delim, const T& buf, V& parts) +{ + typedef typename T::value_type value_type; + size_t i = 0; + value_type *start = buf.begin(); + value_type *const finish = buf.end(); + while (true) { + value_type *const p = memchr_char(start, delim, finish - start); + if (p == 0) { + parts.push_back(T(start, finish - start)); + break; + } + parts.push_back(T(start, p - start)); + start = p + 1; + } + const size_t r = i; + return r; +} + +size_t +split(char delim, const string_ref& buf, std::vector<string_ref>& parts_r) +{ + return split_tmpl_vec(delim, buf, parts_r); +} + +size_t +split(char delim, const string_wref& buf, std::vector<string_wref>& parts_r) +{ + return split_tmpl_vec(delim, buf, parts_r); +} + +}; + diff --git a/plugin/handler_socket/libhsclient/string_util.hpp b/plugin/handler_socket/libhsclient/string_util.hpp new file mode 100644 index 00000000..45cc5b00 --- /dev/null +++ b/plugin/handler_socket/libhsclient/string_util.hpp @@ -0,0 +1,53 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_STRING_UTIL_HPP +#define DENA_STRING_UTIL_HPP + +#include <string> +#include <string.h> +#include <stdint.h> + +#include "string_buffer.hpp" +#include "string_ref.hpp" + +namespace dena { + +inline const char * +memchr_char(const char *s, int c, size_t n) +{ + return static_cast<const char *>(memchr(s, c, n)); +} + +inline char * +memchr_char(char *s, int c, size_t n) +{ + return static_cast<char *>(memchr(s, c, n)); +} + +string_wref get_token(char *& wp, char *wp_end, char delim); +uint32_t atoi_uint32_nocheck(const char *start, const char *finish); +std::string to_stdstring(uint32_t v); +void append_uint32(string_buffer& buf, uint32_t v); +long long atoll_nocheck(const char *start, const char *finish); + +int errno_string(const char *s, int en, std::string& err_r); + +size_t split(char delim, const string_ref& buf, string_ref *parts, + size_t parts_len); +size_t split(char delim, const string_wref& buf, string_wref *parts, + size_t parts_len); +size_t split(char delim, const string_ref& buf, + std::vector<string_ref>& parts_r); +size_t split(char delim, const string_wref& buf, + std::vector<string_wref>& parts_r); + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/thread.hpp b/plugin/handler_socket/libhsclient/thread.hpp new file mode 100644 index 00000000..8a436554 --- /dev/null +++ b/plugin/handler_socket/libhsclient/thread.hpp @@ -0,0 +1,84 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_THREAD_HPP +#define DENA_THREAD_HPP + +#include <stdexcept> +#include <pthread.h> + +#include "fatal.hpp" + +namespace dena { + +template <typename T> +struct thread : private noncopyable { + template <typename Ta> thread(const Ta& arg, size_t stack_sz = 256 * 1024) + : obj(arg), thr(0), need_join(false), stack_size(stack_sz) { } + template <typename Ta0, typename Ta1> thread(const Ta0& a0, + volatile Ta1& a1, size_t stack_sz = 256 * 1024) + : obj(a0, a1), thr(0), need_join(false), stack_size(stack_sz) { } + ~thread() { + join(); + } + void start() { + if (!start_nothrow()) { + fatal_abort("thread::start"); + } + } + bool start_nothrow() { + if (need_join) { + return need_join; /* true */ + } + void *const arg = this; + pthread_attr_t attr; + if (pthread_attr_init(&attr) != 0) { + fatal_abort("pthread_attr_init"); + } + if (pthread_attr_setstacksize(&attr, stack_size) != 0) { + fatal_abort("pthread_attr_setstacksize"); + } + const int r = pthread_create(&thr, &attr, thread_main, arg); + if (pthread_attr_destroy(&attr) != 0) { + fatal_abort("pthread_attr_destroy"); + } + if (r != 0) { + return need_join; /* false */ + } + need_join = true; + return need_join; /* true */ + } + void join() { + if (!need_join) { + return; + } + int e = 0; + if ((e = pthread_join(thr, 0)) != 0) { + fatal_abort("pthread_join"); + } + need_join = false; + } + T& operator *() { return obj; } + T *operator ->() { return &obj; } + private: + static void *thread_main(void *arg) { + thread *p = static_cast<thread *>(arg); + p->obj(); + return 0; + } + private: + T obj; + pthread_t thr; + bool need_join; + size_t stack_size; +}; + +}; + +#endif + diff --git a/plugin/handler_socket/libhsclient/util.hpp b/plugin/handler_socket/libhsclient/util.hpp new file mode 100644 index 00000000..93d78cc7 --- /dev/null +++ b/plugin/handler_socket/libhsclient/util.hpp @@ -0,0 +1,25 @@ + +// vim:sw=2:ai + +/* + * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved. + * See COPYRIGHT.txt for details. + */ + +#ifndef DENA_UTIL_HPP +#define DENA_UTIL_HPP + +namespace dena { + +/* boost::noncopyable */ +struct noncopyable { + noncopyable() { } + private: + noncopyable(const noncopyable&); + noncopyable& operator =(const noncopyable&); +}; + +}; + +#endif + |