一、第一部分
這一部分的頭檔案可以提供給外部client使用。
1、#include <libmemcached/memcached.h>
該檔案被client包含,該檔案中隻是進一步#include <libmemcached-1.0/memcached.h>。
libmemcached-1.0/memcached.h:包含了libmemcached-1.0下的所有頭檔案,并包含libhashkit-1.0/hashkit.h。
#pragma once //與#ifndef方式作用相同,但是#pragma once由編譯器提供保障,但是如果一個檔案有多個拷貝,不能保證不被重複包含。
#include <inttypes.h> //提供了各種位寬的整數類型輸入輸出時的轉換标志宏
#include <stdlib.h>
#include <sys/types.h>
#if !defined(__cplusplus)
# include <stdbool.h>
#endif
#include <libmemcached-1.0/visibility.h>
#include <libmemcached-1.0/configure.h>
#include <libmemcached-1.0/platform.h>
#include <libmemcached-1.0/limits.h>
#include <libmemcached-1.0/defaults.h>
#include <libmemcached-1.0/types/behavior.h>
#include <libmemcached-1.0/types/callback.h>
#include <libmemcached-1.0/types/connection.h>
#include <libmemcached-1.0/types/hash.h>
#include <libmemcached-1.0/types/return.h>
#include <libmemcached-1.0/types/server_distribution.h>
#include <libmemcached-1.0/return.h>
#include <libmemcached-1.0/types.h>
#include <libmemcached-1.0/callbacks.h>
#include <libmemcached-1.0/alloc.h>
#include <libmemcached-1.0/triggers.h>
#include <libhashkit-1.0/hashkit.h> //該檔案包含了libhashkit-1.0下的所有頭檔案,并聲明了一些接口
#include <libmemcached-1.0/struct/callback.h>
#include <libmemcached-1.0/struct/string.h>
#include <libmemcached-1.0/struct/result.h>
#include <libmemcached-1.0/struct/allocator.h>
#include <libmemcached-1.0/struct/sasl.h>
#include <libmemcached-1.0/struct/memcached.h>
#include <libmemcached-1.0/struct/server.h>
#include <libmemcached-1.0/struct/stat.h>
#include <libmemcached-1.0/basic_string.h>
#include <libmemcached-1.0/error.h>
#include <libmemcached-1.0/stats.h>
// Everything above this line must be in the order specified.
#include <libmemcached-1.0/allocators.h>
#include <libmemcached-1.0/analyze.h>
#include <libmemcached-1.0/auto.h>
#include <libmemcached-1.0/behavior.h>
#include <libmemcached-1.0/callback.h>
#include <libmemcached-1.0/delete.h>
#include <libmemcached-1.0/dump.h>
#include <libmemcached-1.0/exist.h>
#include <libmemcached-1.0/fetch.h>
#include <libmemcached-1.0/flush.h>
#include <libmemcached-1.0/flush_buffers.h>
#include <libmemcached-1.0/get.h>
#include <libmemcached-1.0/hash.h>
#include <libmemcached-1.0/options.h>
#include <libmemcached-1.0/parse.h>
#include <libmemcached-1.0/quit.h>
#include <libmemcached-1.0/result.h>
#include <libmemcached-1.0/server.h>
#include <libmemcached-1.0/server_list.h>
#include <libmemcached-1.0/storage.h>
#include <libmemcached-1.0/strerror.h>
#include <libmemcached-1.0/touch.h>
#include <libmemcached-1.0/verbosity.h>
#include <libmemcached-1.0/version.h>
#include <libmemcached-1.0/sasl.h>
#include <libmemcached-1.0/deprecated_types.h>
#ifdef __cplusplus
extern "C" {
#endif
LIBMEMCACHED_API //LIBMEMCACHED_API此處為空,定義于visibility.h
void memcached_servers_reset(memcached_st *ptr);
LIBMEMCACHED_API
memcached_st *memcached_create(memcached_st *ptr); //這些接口都定義于memcached.cc中,除了memcached_server_count()
LIBMEMCACHED_API
memcached_st *memcached(const char *string, size_t string_length);
LIBMEMCACHED_API
void memcached_free(memcached_st *ptr);
LIBMEMCACHED_API
memcached_return_t memcached_reset(memcached_st *ptr);
LIBMEMCACHED_API
void memcached_reset_last_disconnected_server(memcached_st *ptr);
LIBMEMCACHED_API
memcached_st *memcached_clone(memcached_st *clone, const memcached_st *ptr);
LIBMEMCACHED_API
void *memcached_get_user_data(const memcached_st *ptr);
LIBMEMCACHED_API
void *memcached_set_user_data(memcached_st *ptr, void *data);
LIBMEMCACHED_API
memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source);
LIBMEMCACHED_API
memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key);
LIBMEMCACHED_API
uint32_t memcached_server_count(const memcached_st *); //該接口定義于server.cc中
LIBMEMCACHED_API
uint64_t memcached_query_id(const memcached_st *);
#ifdef __cplusplus
} // extern "C"
#endif
2、libmemcached-1.0/memcached.hpp
#pragma once
#include <libmemcached-1.0/memcached.h> //其中也包含了class Memcache中會調用的函數的聲明,如memcached_create()。
#if 0
#include <libmemcached/exception.hpp>
#endif
#include <string.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
namespace memcache
{
/**
* This is the core memcached library (if later, other objects
* are needed, they will be created from this class).
*/
class Memcache
{
public:
Memcache()
{
memc= memcached("", 0);
}
Memcache(const std::string &config)
{
memc= memcached(config.c_str(), config.size());
}
Memcache(const std::string &hostname, in_port_t port)
{
memc= memcached("", 0);
if (memc)
memcached_server_add(memc, hostname.c_str(), port);
}
Memcache(memcached_st *clone)
{
memc= memcached_clone(NULL, clone);
}
Memcache(const Memcache &rhs)
{
memc= memcached_clone(NULL, rhs.getImpl());
}
Memcache &operator=(const Memcache &rhs)
{
if (this != &rhs)
{
memcached_free(memc);
memc= memcached_clone(NULL, rhs.getImpl());
}
return *this;
}
~Memcache()
{
memcached_free(memc);
}
... //多個成員函數
private:
memcached_st *memc;
};
}
3、libhashkit-1.0/hashkit.hpp
#pragma once
#include <libhashkit-1.0/hashkit.h> //其中也包含了class Hashkit中将要調用的函數的聲明,如hashkit_create()等。
#include <string>
class Hashkit {
public:
Hashkit()
{
hashkit_create(&self);
}
Hashkit(const Hashkit& source)
{
hashkit_clone(&self, &source.self);
}
Hashkit& operator=(const Hashkit& source)
{
hashkit_free(&self);
hashkit_clone(&self, &source.self);
return *this;
}
friend bool operator==(const Hashkit &left, const Hashkit &right)
{
return hashkit_compare(&left.self, &right.self);
}
uint32_t digest(std::string& str)
{
return hashkit_digest(&self, str.c_str(), str.length());
}
uint32_t digest(const char *key, size_t key_length)
{
return hashkit_digest(&self, key, key_length);
}
hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm)
{
return hashkit_set_function(&self, hash_algorithm);
}
hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm)
{
return hashkit_set_function(&self, hash_algorithm);
}
~Hashkit()
{
hashkit_free(&self);
}
private:
hashkit_st self;
};
二、第二部分
common.h都是作為内部頭檔案使用。
4、libmemcached/common.h
被包含于libmemcached/memcached.cc中,基本上将整個架構都包含了。
#pragma once
#include <config.h>
#ifdef __cplusplus
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <libmemcached-1.0/memcached.h> //對外可見的頭檔案
#include <libmemcached/watchpoint.h>
#include <libmemcached/is.h>
#include <libmemcached/server_instance.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#else
#include "poll/poll.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef memcached_return_t (*memcached_server_execute_fn)(memcached_st *ptr, memcached_server_write_instance_st server, void *context);
LIBMEMCACHED_LOCAL
memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key);
LIBMEMCACHED_LOCAL
memcached_return_t memcached_server_execute(memcached_st *ptr,
memcached_server_execute_fn callback,
void *context);
#ifdef __cplusplus
} // extern "C"
#endif
/* These are private not to be installed headers */
#include <libmemcached/error.hpp>
#include <libmemcached/memory.h>
#include <libmemcached/io.h>
#ifdef __cplusplus
#include <libmemcached/string.hpp>
#include <libmemcached/io.hpp>
#include <libmemcached/udp.hpp>
#include <libmemcached/do.hpp>
#include <libmemcached/close_socket.hpp>
#include <libmemcached/connect.hpp>
#include <libmemcached/allocators.hpp>
#include <libmemcached/hash.hpp>
#include <libmemcached/quit.hpp>
#include <libmemcached/server.hpp>
#include <libmemcached/behavior.hpp>
#include <libmemcached/sasl.hpp>
#include <libmemcached/server_list.hpp>
#endif
#include <libmemcached/internal.h>
#include <libmemcached/array.h>
#include <libmemcached/libmemcached_probes.h>
#include <libmemcached/memcached/protocol_binary.h>
#include <libmemcached/byteorder.h>
#include <libmemcached/initialize_query.h>
#ifdef __cplusplus
#include <libmemcached/response.h>
#endif
#include <libmemcached/namespace.h>
#include <libmemcached/virtual_bucket.h>
#ifdef __cplusplus
#include <libmemcached/backtrace.hpp>
#include <libmemcached/assert.hpp>
#include <libmemcached/server.hpp>
#include <libmemcached/key.hpp>
#endif
#include <libmemcached/continuum.hpp>
#if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
#define likely(x) if((x))
#define unlikely(x) if((x))
#else
#define likely(x) if(__builtin_expect((x) != 0, 1))
#define unlikely(x) if(__builtin_expect((x) != 0, 0))
#endif
#define MEMCACHED_BLOCK_SIZE 1024
#define MEMCACHED_DEFAULT_COMMAND_SIZE 350
#define SMALL_STRING_LEN 1024
#define HUGE_STRING_LEN 8196
... //若幹接口
5、libhashkit/common.h
#pragma once
#include <config.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <libhashkit-1.0/hashkit.h> //該檔案包含了檔案夾libhashkit-1.0下的所有頭檔案,并聲明了一些接口
#include <libhashkit/is.h>
#ifdef __cplusplus
extern "C" {
#endif
HASHKIT_LOCAL
void md5_signature(const unsigned char *key, unsigned int length, unsigned char *result);
HASHKIT_LOCAL
int update_continuum(hashkit_st *hashkit);
#ifdef __cplusplus
}
#endif
三、其他
1、libmemcached/util.h(作為對外頭檔案)
該頭檔案隻是包含libmemcachedutil-1.0/util.h,這一點與libmemcached/memcached.h的作用類似。
#pragma once //可以看出util.h這個檔案可以給client提供一些函數接口
#include <libmemcached-1.0/memcached.h>
#include <libmemcachedutil-1.0/pid.h> //這些頭檔案都是包含了一些函數聲明
#include <libmemcachedutil-1.0/flush.h>
#include <libmemcachedutil-1.0/ping.h>
#include <libmemcachedutil-1.0/pool.h>
#include <libmemcachedutil-1.0/version.h>
libmemcachedutil-1.0檔案夾下還有一個ostream.hpp:
#pragma once
static inline std::ostream& operator<<(std::ostream& output, const enum memcached_return_t &arg)
{
output << memcached_strerror(NULL, arg);
return output;
}
2、libmemcachedutil/common.h(内部使用)
#pragma once
#include <config.h>
#include <libmemcachedutil-1.0/util.h>
#include <libmemcached/assert.hpp>
#include <libmemcached/backtrace.hpp>