├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── dstore ├── CPPLINT.cfg ├── common ├── CMakeLists.txt ├── errno_define.h ├── log.cpp ├── log.h ├── memory.cpp ├── memory.h ├── time_operator.cpp └── time_operator.h ├── cpplint.py ├── main ├── CMakeLists.txt └── main.cpp ├── network ├── CMakeLists.txt ├── buffer.cpp ├── buffer.h ├── connection.cpp ├── connection.h ├── endian_operator.cpp ├── endian_operator.h ├── epoll.cpp ├── epoll.h ├── event_loop.cpp ├── event_loop.h ├── event_poll_api.h ├── inet_addr.cpp ├── inet_addr.h ├── message.cpp ├── message.h ├── socket.cpp ├── socket.h ├── socket_operator.cpp ├── socket_operator.h ├── tcp_client.cpp ├── tcp_client.h ├── tcp_connector.cpp ├── tcp_connector.h ├── tcp_listener.cpp ├── tcp_listener.h ├── tcp_server.cpp └── tcp_server.h ├── run_cpplint.sh └── test ├── CMakeLists.txt ├── simple_packet_client_test.cpp ├── simple_packet_server_test.cpp └── timer_test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | # Vim files 31 | *.swp 32 | 33 | build/* 34 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(dstore C CXX) 4 | 5 | set(CXX_FLAGS 6 | -g 7 | -Wall 8 | -Wextra 9 | -Werror 10 | -Wconversion 11 | -Wno-unused-parameter 12 | -Wold-style-cast 13 | -Woverloaded-virtual 14 | -Wpointer-arith 15 | -Wshadow 16 | -Wwrite-strings 17 | -std=c++0x 18 | ) 19 | 20 | set(CMAKE_CXX_COMPILER "g++") 21 | 22 | string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}") 23 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) 24 | 25 | include_directories("${PROJECT_SOURCE_DIR}/dstore/common") 26 | include_directories("${PROJECT_SOURCE_DIR}/dstore/network") 27 | include_directories("${PROJECT_SOURCE_DIR}/dstore/main") 28 | 29 | add_subdirectory(dstore/common) 30 | add_subdirectory(dstore/network) 31 | add_subdirectory(dstore/main) 32 | add_subdirectory(dstore/test) 33 | 34 | foreach(src_file ${COMMON_SRCS}) 35 | set(SRCS ${SRCS} "dstore/common/${src_file}") 36 | endforeach(src_file) 37 | 38 | foreach(src_file ${NETWORK_SRCS}) 39 | set(SRCS ${SRCS} "dstore/network/${src_file}") 40 | endforeach(src_file) 41 | 42 | foreach(src_file ${TEST_SRCS}) 43 | set (EXECUTE_TEST_SRCS ${SRCS} "dstore/test/${src_file}") 44 | string(REPLACE "." ";" src_list ${src_file}) 45 | list(GET src_list 0 EXECUTABLE_NAME) 46 | message(STATUS "${EXECUTE_TEST_SRCS}") 47 | add_executable(${EXECUTABLE_NAME} ${EXECUTE_TEST_SRCS}) 48 | endforeach(src_file) 49 | 50 | foreach(src_file ${MAIN_SRCS}) 51 | set(SRCS ${SRCS} "dstore/main/${src_file}") 52 | endforeach(src_file) 53 | 54 | #message(STATUS "${SRCS}") 55 | #add_executable(dstore ${SRCS}) 56 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Charles 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | DSTORE is a distributed key-value storage engine. 4 | 5 | # How to use 6 | 7 | ``` 8 | git clone https://github.com/Charles0429/dstore 9 | cd dstore 10 | mkdir build 11 | cd build 12 | cmake ../ 13 | make 14 | ``` 15 | 16 | # Todo 17 | 18 | - server-side network framework[done] 19 | - rpc lib -------------------------------------------------------------------------------- /dstore/CPPLINT.cfg: -------------------------------------------------------------------------------- 1 | linelength=120 2 | -------------------------------------------------------------------------------- /dstore/common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(COMMON_SRCS 2 | log.cpp 3 | memory.cpp 4 | time_operator.cpp 5 | PARENT_SCOPE 6 | ) 7 | -------------------------------------------------------------------------------- /dstore/common/errno_define.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_COMMON_ERRNO_DEFINE_H_ 2 | #define DSTORE_COMMON_ERRNO_DEFINE_H_ 3 | 4 | namespace dstore 5 | { 6 | namespace common 7 | { 8 | #define DSTORE_SUCCESS 0 9 | #define DSTORE_MEMORY_ALLOCATION_FAILED -1 10 | #define DSTORE_EPOLL_ERROR -2 11 | #define DSTORE_BIND_ERROR -3 12 | #define DSTORE_LISTEN_ERROR -4 13 | #define DSTORE_ACCEPT_ERROR -5 14 | #define DSTORE_PARSE_ADDR_ERROR -6 15 | #define DSTORE_PARSE_ADDR_EMPTY -7 16 | #define DSTORE_SET_SOCKET_ERROR -8 17 | #define DSTORE_EAGAIN -9 18 | #define DSTORE_INVALID_ARGUMENT -10 19 | #define DSTORE_CONNECT_ERROR -11 20 | #define DSTORE_CONNECT_IN_PROGRESS -12 21 | } // end namespace common 22 | } // end namespace dstore 23 | 24 | #endif // DSTORE_COMMON_ERRNO_DEFINE_H_ 25 | -------------------------------------------------------------------------------- /dstore/common/log.cpp: -------------------------------------------------------------------------------- 1 | #include "log.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace dstore 9 | { 10 | namespace common 11 | { 12 | Log &Log::get_instance(void) 13 | { 14 | static Log log; 15 | return log; 16 | } 17 | 18 | void Log::log(const char *level, const char *filename, const int line, const char *format, ...) 19 | { 20 | char time_buffer[80]; 21 | char buffer[1024]; 22 | va_list args; 23 | timeval cur_time; 24 | size_t len = 0; 25 | va_start(args, format); 26 | vsnprintf(buffer, sizeof(buffer), format, args); 27 | va_end(args); 28 | gettimeofday(&cur_time, NULL); 29 | len = strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", localtime(&cur_time.tv_sec)); 30 | snprintf(time_buffer + len, sizeof(time_buffer), "%ld", cur_time.tv_usec); 31 | std::cout << level << ": " << time_buffer << " " << filename << ":" << line << " " << buffer << std::endl; 32 | } 33 | } // end namespace common 34 | } // end namespace dstore 35 | -------------------------------------------------------------------------------- /dstore/common/log.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_COMMON_LOG_H_ 2 | #define DSTORE_COMMON_LOG_H_ 3 | 4 | #include 5 | 6 | namespace dstore 7 | { 8 | namespace common 9 | { 10 | class Log 11 | { 12 | public: 13 | void log(const char *level, const char *filename, const int line, const char *format, ...); 14 | static Log &get_instance(void); 15 | }; 16 | #define SHORT_FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ 17 | #define LOG_INSTANCE (::dstore::common::Log::get_instance()) 18 | #define LOG_DEBUG(message, args...) (LOG_INSTANCE.log("DEBUG", SHORT_FILE, __LINE__, message, ##args)) 19 | #define LOG_INFO(message, args...) (LOG_INSTANCE.log("INFO", SHORT_FILE, __LINE__, message, ##args)) 20 | #define LOG_WARN(message, args...) (LOG_INSTANCE.log("WARN", SHORT_FILE, __LINE__, message, ##args)) 21 | #define LOG_ERROR(message, args...) (LOG_INSTANCE.log("ERROR", SHORT_FILE, __LINE__, message, ##args)) 22 | } // end namespace common 23 | } // end namespace dstore 24 | 25 | #endif // DSTORE_COMMON_LOG_H_ 26 | -------------------------------------------------------------------------------- /dstore/common/memory.cpp: -------------------------------------------------------------------------------- 1 | #include "memory.h" 2 | #include 3 | 4 | namespace dstore 5 | { 6 | namespace common 7 | { 8 | void *dstore_malloc(size_t size) 9 | { 10 | return ::malloc(size); 11 | } 12 | 13 | void dstore_free(void *ptr) 14 | { 15 | return ::free(ptr); 16 | } 17 | 18 | void *dstore_realloc(void *ptr, size_t size) 19 | { 20 | return ::realloc(ptr, size); 21 | } 22 | 23 | } // end namespace common 24 | } // end namespace dstore 25 | -------------------------------------------------------------------------------- /dstore/common/memory.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_COMMON_MEMORY_H_ 2 | #define DSTORE_COMMON_MEMORY_H_ 3 | 4 | #include 5 | 6 | namespace dstore 7 | { 8 | namespace common 9 | { 10 | void *dstore_malloc(size_t size); 11 | void dstore_free(void *ptr); 12 | void *dstore_realloc(void *ptr, size_t size); 13 | } // end namespace common 14 | } // end namespace dstore 15 | #endif // DSTORE_COMMON_MEMORY_H_ 16 | -------------------------------------------------------------------------------- /dstore/common/time_operator.cpp: -------------------------------------------------------------------------------- 1 | #include "time_operator.h" 2 | #include 3 | 4 | namespace dstore 5 | { 6 | namespace common 7 | { 8 | int64_t get_milliseconds(void) 9 | { 10 | struct timeval tp; 11 | gettimeofday(&tp, nullptr); 12 | return tp.tv_sec * 1000 + tp.tv_usec / 1000; 13 | } 14 | } // end namespace common 15 | } // end namespace dstore 16 | -------------------------------------------------------------------------------- /dstore/common/time_operator.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_COMMON_TIME_OPERATOR_H_ 2 | #define DSTORE_COMMON_TIME_OPERATOR_H_ 3 | 4 | #include 5 | 6 | namespace dstore 7 | { 8 | namespace common 9 | { 10 | int64_t get_milliseconds(void); 11 | } // end namespace common 12 | } // end namespace dstore 13 | #endif // DSTORE_COMMON_TIME_OPERATOR_H_ 14 | -------------------------------------------------------------------------------- /dstore/main/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(MAIN_SRCS 2 | main.cpp 3 | PARENT_SCOPE 4 | ) 5 | -------------------------------------------------------------------------------- /dstore/main/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Charles0429/dstore/a825e6c4cc30d853f10b7a56f5a9286c2e57cc75/dstore/main/main.cpp -------------------------------------------------------------------------------- /dstore/network/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(NETWORK_SRCS 2 | endian_operator.cpp 3 | buffer.cpp 4 | connection.cpp 5 | epoll.cpp 6 | event_loop.cpp 7 | inet_addr.cpp 8 | message.cpp 9 | socket.cpp 10 | socket_operator.cpp 11 | tcp_listener.cpp 12 | tcp_server.cpp 13 | tcp_connector.cpp 14 | tcp_client.cpp 15 | PARENT_SCOPE 16 | ) 17 | MESSAGE(STATUS "${NETWORK_SRCS}") 18 | -------------------------------------------------------------------------------- /dstore/network/buffer.cpp: -------------------------------------------------------------------------------- 1 | #include "buffer.h" 2 | #include 3 | #include 4 | #include "socket_operator.h" 5 | #include "endian_operator.h" 6 | 7 | using namespace dstore::network; 8 | 9 | Buffer::Buffer(void) 10 | : buffer_(), read_pos_(0), write_pos_(0) 11 | { 12 | } 13 | 14 | Buffer::Buffer(int size) 15 | : buffer_(), read_pos_(0), write_pos_(0) 16 | { 17 | buffer_.reserve(size); 18 | } 19 | 20 | ssize_t Buffer::read_fd(int fd, size_t nbytes) 21 | { 22 | ssize_t bytes_read = 0; 23 | resize(nbytes); 24 | bytes_read = recv(fd, buffer_.data() + write_pos_, nbytes); 25 | write_pos_ += (bytes_read > 0) ? bytes_read : 0; 26 | return bytes_read; 27 | } 28 | 29 | ssize_t Buffer::write_fd(int fd) 30 | { 31 | ssize_t bytes_writen = 0; 32 | bytes_writen = send(fd, buffer_.data() + read_pos_, get_need_write_bytes()); 33 | read_pos_ += (bytes_writen > 0) ? bytes_writen : 0; 34 | return bytes_writen; 35 | } 36 | 37 | void Buffer::append(const char *buf, size_t nbytes) 38 | { 39 | resize(nbytes); 40 | ::memcpy(buffer_.data() + write_pos_, buf, nbytes); 41 | write_pos_ += nbytes; 42 | } 43 | 44 | void Buffer::append(const void *buf, size_t nbytes) 45 | { 46 | return append(static_cast(buf), nbytes); 47 | } 48 | 49 | void Buffer::append_int16(int16_t data) 50 | { 51 | int16_t network = dstore::network::host_to_network16(data); 52 | append(&network, sizeof(int16_t)); 53 | } 54 | 55 | void Buffer::append_int32(int32_t data) 56 | { 57 | int32_t network = host_to_network32(data); 58 | append(&network, sizeof(int32_t)); 59 | } 60 | 61 | void Buffer::append_int64(int64_t data) 62 | { 63 | int64_t network = host_to_network64(data); 64 | append(&network, sizeof(int64_t)); 65 | } 66 | 67 | void *Buffer::peek(void) 68 | { 69 | return buffer_.data(); 70 | } 71 | 72 | void Buffer::consume(size_t nbytes) 73 | { 74 | read_pos_ += nbytes; 75 | } 76 | 77 | int16_t Buffer::peek_int16(void) 78 | { 79 | int16_t ret = -1; 80 | ::memcpy(&ret, peek(), sizeof(ret)); 81 | return network_to_host16(ret); 82 | } 83 | 84 | int16_t Buffer::get_int16(void) 85 | { 86 | assert(get_read_bytes() >= sizeof(int16_t)); 87 | int16_t ret = peek_int16(); 88 | consume(sizeof(int16_t)); 89 | return ret; 90 | } 91 | 92 | int32_t Buffer::peek_int32(void) 93 | { 94 | int32_t ret = -1; 95 | ::memcpy(&ret, peek(), sizeof(ret)); 96 | return network_to_host32(ret); 97 | } 98 | 99 | int32_t Buffer::get_int32(void) 100 | { 101 | assert(get_read_bytes() >= sizeof(int32_t)); 102 | int32_t ret = peek_int32(); 103 | consume(sizeof(int32_t)); 104 | return ret; 105 | } 106 | 107 | int64_t Buffer::peek_int64(void) 108 | { 109 | int64_t ret = -1; 110 | ::memcpy(&ret, peek(), sizeof(ret)); 111 | return network_to_host64(ret); 112 | } 113 | 114 | int64_t Buffer::get_int64(void) 115 | { 116 | assert(get_read_bytes() >= sizeof(int64_t)); 117 | int64_t ret = peek_int64(); 118 | consume(sizeof(int64_t)); 119 | return ret; 120 | } 121 | 122 | size_t Buffer::get_read_pos(void) 123 | { 124 | return read_pos_; 125 | } 126 | 127 | size_t Buffer::get_write_pos(void) 128 | { 129 | return write_pos_; 130 | } 131 | 132 | void Buffer::set_read_pos(size_t read_pos) 133 | { 134 | read_pos_ = read_pos; 135 | } 136 | 137 | void Buffer::set_write_pos(size_t write_pos) 138 | { 139 | write_pos_ = write_pos; 140 | } 141 | 142 | char *Buffer::get_data(void) 143 | { 144 | return buffer_.data(); 145 | } 146 | 147 | size_t Buffer::get_read_bytes(void) 148 | { 149 | return write_pos_ - read_pos_; 150 | } 151 | 152 | size_t Buffer::get_need_write_bytes(void) 153 | { 154 | return write_pos_ - read_pos_; 155 | } 156 | 157 | size_t Buffer::left(void) 158 | { 159 | return buffer_.capacity() - write_pos_; 160 | } 161 | 162 | void Buffer::resize(size_t nbytes) 163 | { 164 | size_t left_bytes = left(); 165 | if (nbytes > left_bytes) { 166 | size_t expand = buffer_.capacity() + 2 * (nbytes - left_bytes); 167 | buffer_.resize(expand); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /dstore/network/buffer.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_BUFFER_H_ 2 | #define DSTORE_NETWORK_BUFFER_H_ 3 | 4 | #include 5 | #include 6 | 7 | namespace dstore 8 | { 9 | namespace network 10 | { 11 | class Buffer 12 | { 13 | public: 14 | Buffer(void); 15 | explicit Buffer(int size); 16 | ssize_t read_fd(int fd, size_t nbytes); 17 | ssize_t write_fd(int fd); 18 | void append(const char *buf, size_t nbytes); 19 | void append(const void *buf, size_t nbytes); 20 | void append_int16(int16_t data); 21 | void append_int32(int32_t data); 22 | void append_int64(int64_t data); 23 | int16_t get_int16(void); 24 | int32_t get_int32(void); 25 | int64_t get_int64(void); 26 | int16_t peek_int16(void); 27 | int32_t peek_int32(void); 28 | int64_t peek_int64(void); 29 | void *peek(void); 30 | void consume(size_t nbytes); 31 | size_t get_read_bytes(void); 32 | size_t get_need_write_bytes(void); 33 | size_t get_read_pos(void); 34 | size_t get_write_pos(void); 35 | char *get_data(void); 36 | void set_read_pos(size_t read_pos); 37 | void set_write_pos(size_t write_pos); 38 | Buffer &operator=(const Buffer &) = delete; 39 | Buffer(const Buffer &) = delete; 40 | private: 41 | size_t left(void); 42 | void resize(size_t nbytes); 43 | private: 44 | std::vector buffer_; 45 | size_t read_pos_; 46 | size_t write_pos_; 47 | }; 48 | } // end namespace network 49 | } // end namespace dstore 50 | 51 | #endif // DSTORE_NETWORK_BUFFER_H_ 52 | -------------------------------------------------------------------------------- /dstore/network/connection.cpp: -------------------------------------------------------------------------------- 1 | #include "connection.h" 2 | #include "event_loop.h" 3 | #include "errno_define.h" 4 | #include "log.h" 5 | 6 | using namespace dstore::network; 7 | 8 | Connection::Connection(const InetAddr &peer, std::shared_ptr e) 9 | : status_(Connection::INVALID), e_(e), loop_(nullptr), socket_(e->fd), 10 | peer_(peer), message_list_(), read_buffer_(), write_buffer_() 11 | { 12 | } 13 | 14 | int Connection::init(EventLoop *loop) 15 | { 16 | int ret = DSTORE_SUCCESS; 17 | if (DSTORE_SUCCESS != (ret = socket_.set_nonblocking())) { 18 | LOG_INFO("set nonblocking failed, ret=%d", ret); 19 | return ret; 20 | } 21 | loop_ = loop; 22 | return ret; 23 | } 24 | 25 | void Connection::set_event(const Event &e) 26 | { 27 | *e_ = e; 28 | } 29 | 30 | Event &Connection::get_event(void) 31 | { 32 | return *e_; 33 | } 34 | 35 | Socket &Connection::get_socket(void) 36 | { 37 | return socket_; 38 | } 39 | 40 | Buffer &Connection::get_read_buffer(void) 41 | { 42 | return read_buffer_; 43 | } 44 | 45 | Buffer &Connection::get_write_buffer(void) 46 | { 47 | return write_buffer_; 48 | } 49 | 50 | int Connection::remove_write(void) 51 | { 52 | int ret = DSTORE_SUCCESS; 53 | e_->type &= ~Event::kEventWrite; 54 | if (DSTORE_SUCCESS != (ret = loop_->modify_event(e_))) { 55 | LOG_INFO("modify event failed, fd=%d, type=%d, ret=%d", e_->fd, e_->type, ret); 56 | return ret; 57 | } 58 | return ret; 59 | } 60 | 61 | int Connection::add_write(void) 62 | { 63 | int ret = DSTORE_SUCCESS; 64 | e_->type |= Event::kEventWrite; 65 | if (DSTORE_SUCCESS != (ret = loop_->modify_event(e_))) { 66 | LOG_INFO("modify event failed, fd=%d, type=%d, ret=%d", e_->fd, e_->type, ret); 67 | return ret; 68 | } 69 | return ret; 70 | } 71 | 72 | int Connection::remove_read(void) 73 | { 74 | int ret = DSTORE_SUCCESS; 75 | e_->type &= ~Event::kEventRead; 76 | if (DSTORE_SUCCESS != (ret = loop_->modify_event(e_))) { 77 | LOG_INFO("modify event failed, fd=%d, type=%d, ret=%d", e_->fd, e_->type, ret); 78 | return ret; 79 | } 80 | return ret; 81 | } 82 | 83 | int Connection::add_read(void) 84 | { 85 | int ret = DSTORE_SUCCESS; 86 | e_->type |= Event::kEventRead; 87 | if (DSTORE_SUCCESS != (ret = loop_->modify_event(e_))) { 88 | LOG_INFO("modify event failed, fd=%d, type=%d, ret=%d", e_->fd, e_->type, ret); 89 | return ret; 90 | } 91 | return ret; 92 | } 93 | 94 | void Connection::add_message(Message *message) 95 | { 96 | message_list_.push_back(message); 97 | } 98 | 99 | void Connection::remove_message(Message *message) 100 | { 101 | message_list_.remove(message); 102 | } 103 | 104 | std::list &Connection::get_message_list(void) 105 | { 106 | return message_list_; 107 | } 108 | 109 | void Connection::close(void) 110 | { 111 | socket_.close(); 112 | } 113 | 114 | void Connection::set_status(const Connection::Status status) 115 | { 116 | status_ = status; 117 | } 118 | 119 | Connection::Status Connection::get_status(void) 120 | { 121 | return status_; 122 | } 123 | 124 | bool Connection::pending_write(void) 125 | { 126 | return write_buffer_.get_need_write_bytes() > 0; 127 | } 128 | 129 | bool Connection::is_connect_ok(void) 130 | { 131 | return socket_.is_connect_ok(); 132 | } 133 | -------------------------------------------------------------------------------- /dstore/network/connection.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_CONNECTION_H_ 2 | #define DSTORE_NETWORK_CONNECTION_H_ 3 | 4 | #include 5 | #include 6 | #include "event_loop.h" 7 | #include "socket.h" 8 | #include "inet_addr.h" 9 | #include "buffer.h" 10 | 11 | namespace dstore 12 | { 13 | namespace network 14 | { 15 | class InetAddr; 16 | class Message; 17 | class Buffer; 18 | class Connection 19 | { 20 | public: 21 | enum Status 22 | { 23 | INVALID = 0, 24 | ACTIVE, 25 | CONNECTING, 26 | SHUTDOWN_READ, 27 | SHUTDOWN_WRITE, 28 | }; 29 | Connection(const InetAddr &peer, std::shared_ptr e); 30 | int init(EventLoop *loop); 31 | void set_event(const Event &e); 32 | Event &get_event(void); 33 | Buffer &get_read_buffer(void); 34 | Buffer &get_write_buffer(void); 35 | Socket &get_socket(void); 36 | int remove_write(void); 37 | int add_write(void); 38 | int remove_read(void); 39 | int add_read(void); 40 | void add_message(Message *); 41 | void remove_message(Message *); 42 | void set_status(const Connection::Status s); 43 | Connection::Status get_status(void); 44 | void close(void); 45 | bool pending_write(void); 46 | bool is_connect_ok(void); 47 | std::list &get_message_list(void); 48 | void clear_message_list(void); 49 | Connection &operator=(const Connection &) = delete; 50 | Connection(const Connection &) = delete; 51 | private: 52 | Status status_; 53 | std::shared_ptr e_; 54 | EventLoop *loop_; 55 | Socket socket_; 56 | InetAddr peer_; 57 | std::list message_list_; 58 | Buffer read_buffer_; 59 | Buffer write_buffer_; 60 | }; 61 | } // end namespace network 62 | } // end namespace dstore 63 | #endif // DSTORE_NETWORK_CONNECTION_H_ 64 | -------------------------------------------------------------------------------- /dstore/network/endian_operator.cpp: -------------------------------------------------------------------------------- 1 | #include "endian_operator.h" 2 | #include 3 | 4 | namespace dstore 5 | { 6 | namespace network 7 | { 8 | uint16_t host_to_network16(uint16_t host) 9 | { 10 | return htobe16(host); 11 | } 12 | 13 | uint16_t network_to_host16(uint16_t net) 14 | { 15 | return be16toh(net); 16 | } 17 | 18 | uint32_t host_to_network32(uint32_t host) 19 | { 20 | return htobe32(host); 21 | } 22 | 23 | uint32_t network_to_host32(uint32_t net) 24 | { 25 | return be32toh(net); 26 | } 27 | 28 | uint64_t host_to_network64(uint64_t host) 29 | { 30 | return htobe64(host); 31 | } 32 | 33 | uint64_t network_to_host64(uint64_t net) 34 | { 35 | return be64toh(net); 36 | } 37 | } // end namespace network 38 | } // end namespace dstore 39 | -------------------------------------------------------------------------------- /dstore/network/endian_operator.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_ENDIAN_OPERATOR_H_ 2 | #define DSTORE_NETWORK_ENDIAN_OPERATOR_H_ 3 | 4 | #include 5 | 6 | namespace dstore 7 | { 8 | namespace network 9 | { 10 | uint16_t network_to_host16(uint16_t network); 11 | uint16_t host_to_network16(uint16_t host); 12 | uint16_t network_to_host16(uint16_t net); 13 | uint32_t host_to_network32(uint32_t host); 14 | uint32_t network_to_host32(uint32_t net); 15 | uint64_t host_to_network64(uint64_t host); 16 | uint64_t network_to_host64(uint64_t net); 17 | } // end namespace network 18 | } // end namespace dstore 19 | 20 | #endif // DSTORE_NETWORK_ENDIAN_OPERATOR_H_ 21 | -------------------------------------------------------------------------------- /dstore/network/epoll.cpp: -------------------------------------------------------------------------------- 1 | #include "epoll.h" 2 | #include 3 | #include "errno_define.h" 4 | #include "log.h" 5 | #include "memory.h" 6 | #include "event_loop.h" 7 | 8 | using namespace dstore::common; 9 | using namespace dstore::network; 10 | 11 | EpollAPI::EpollAPI(EventLoop *loop) 12 | : epfd_(0), events_(nullptr), loop_size_(0), loop_(loop) 13 | { 14 | } 15 | 16 | EpollAPI::~EpollAPI() 17 | { 18 | destory(); 19 | } 20 | 21 | int EpollAPI::init(void) 22 | { 23 | int ret = DSTORE_SUCCESS; 24 | events_ = static_cast(dstore_malloc(sizeof(struct epoll_event) * LOOP_INIT_SIZE)); 25 | if (nullptr == events_) { 26 | ret = DSTORE_MEMORY_ALLOCATION_FAILED; 27 | LOG_ERROR("allocate memory failed for epoll events, ret=%d", ret); 28 | return ret; 29 | } 30 | loop_size_ = LOOP_INIT_SIZE; 31 | epfd_ = epoll_create(1024); 32 | if (-1 == epfd_) { 33 | ret = DSTORE_EPOLL_ERROR; 34 | LOG_ERROR("epoll_create failed, errno=%d, ret=%d", errno, ret); 35 | dstore_free(events_); 36 | return ret; 37 | } 38 | 39 | return ret; 40 | } 41 | 42 | int EpollAPI::register_event(int fd, int type) 43 | { 44 | int ret = DSTORE_SUCCESS; 45 | struct epoll_event event; 46 | int sys_ret = 0; 47 | 48 | event.events = 0; 49 | event.data.fd = fd; 50 | if (type & Event::kEventRead) { 51 | event.events |= EPOLLIN; 52 | } 53 | if (type & Event::kEventWrite) { 54 | event.events |= EPOLLOUT; 55 | } 56 | 57 | sys_ret = epoll_ctl(epfd_, EPOLL_CTL_ADD, fd, &event); 58 | if (-1 == sys_ret) { 59 | ret = DSTORE_EPOLL_ERROR; 60 | LOG_WARN("epoll_ctl failed, epfd=%d, fd=%d, event=%d, errno=%d", epfd_, fd, event.events, errno); 61 | } 62 | return ret; 63 | } 64 | 65 | int EpollAPI::unregister_event(int fd, int type) 66 | { 67 | int ret = DSTORE_SUCCESS; 68 | struct epoll_event event; 69 | int sys_ret = 0; 70 | 71 | event.events = 0; 72 | event.data.fd = fd; 73 | if (type & Event::kEventRead) { 74 | event.events |= EPOLLIN; 75 | } 76 | if (type & Event::kEventWrite) { 77 | event.events |= EPOLLOUT; 78 | } 79 | sys_ret = epoll_ctl(epfd_, EPOLL_CTL_DEL, fd, &event); 80 | if (-1 == sys_ret) { 81 | ret = DSTORE_EPOLL_ERROR; 82 | LOG_WARN("epoll_ctl failed, epfd=%d, fd=%d, event=%d, errno=%d", epfd_, fd, event.events, errno); 83 | } 84 | return ret; 85 | } 86 | 87 | int EpollAPI::modify_event(int fd, int type) 88 | { 89 | struct epoll_event event; 90 | int sys_ret = 0; 91 | int ret = DSTORE_SUCCESS; 92 | 93 | event.events = 0; 94 | event.data.fd = fd; 95 | 96 | if (type & Event::kEventRead) { 97 | event.events |= EPOLLIN; 98 | } 99 | if (type & Event::kEventWrite) { 100 | event.events |= EPOLLOUT; 101 | } 102 | 103 | sys_ret = epoll_ctl(epfd_, EPOLL_CTL_MOD, fd, &event); 104 | if (-1 == sys_ret) { 105 | ret = DSTORE_EPOLL_ERROR; 106 | LOG_WARN("epoll_ctl failed, epfd=%d, fd=%d, event=%d, errno=%d", epfd_, fd, event.events, errno); 107 | } 108 | return ret; 109 | } 110 | 111 | int EpollAPI::resize(int set_size) 112 | { 113 | int ret = DSTORE_SUCCESS; 114 | struct epoll_event *events_ptr = nullptr; 115 | 116 | events_ptr = static_cast(dstore_realloc(events_, sizeof(struct epoll_event) * set_size)); 117 | if (nullptr == events_ptr) { 118 | ret = DSTORE_MEMORY_ALLOCATION_FAILED; 119 | LOG_WARN("allocate memory failed, ret=%d", ret); 120 | return ret; 121 | } 122 | events_ = events_ptr; 123 | loop_size_ = set_size; 124 | return ret; 125 | } 126 | 127 | int EpollAPI::loop(int timeout) 128 | { 129 | int ret = DSTORE_SUCCESS; 130 | int events_num = 0; 131 | 132 | events_num = epoll_wait(epfd_, events_, loop_size_, timeout); 133 | if (-1 == events_num) { 134 | ret = DSTORE_EPOLL_ERROR; 135 | LOG_WARN("epoll wait failed, epfd_=%d, loop_size=%d, errno = %d, ret=%d", 136 | epfd_, loop_size_, errno, ret); 137 | return ret; 138 | } 139 | 140 | for (int64_t i = 0; i < events_num; ++i) { 141 | int event_type = 0; 142 | struct epoll_event *e = events_ + i; 143 | 144 | if (e->events & EPOLLERR) { 145 | event_type |= Event::kEventError; 146 | } 147 | if (e->events & (EPOLLIN | EPOLLHUP)) { 148 | event_type |= Event::kEventRead; 149 | } 150 | if (e->events & EPOLLOUT) { 151 | event_type |= Event::kEventWrite; 152 | } 153 | loop_->add_ready_event(e->data.fd, event_type); 154 | } 155 | if (events_num == loop_size_) { 156 | int tmp_ret = DSTORE_SUCCESS; 157 | if (DSTORE_SUCCESS != (tmp_ret = resize(loop_size_ * 2))) { 158 | LOG_WARN("epoll resize failed, loop_size_=%d, expand_to=%d, ret=%d", loop_size_, loop_size_ * 2, ret); 159 | } 160 | } 161 | return ret; 162 | } 163 | 164 | void EpollAPI::destory(void) 165 | { 166 | if (nullptr != events_) { 167 | dstore_free(events_); 168 | events_ = nullptr; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /dstore/network/epoll.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_EPOLL_H_ 2 | #define DSTORE_NETWORK_EPOLL_H_ 3 | 4 | #include 5 | #include 6 | #include "event_poll_api.h" 7 | 8 | namespace dstore 9 | { 10 | namespace network 11 | { 12 | class EventLoop; 13 | class EpollAPI : public EventPollAPI 14 | { 15 | public: 16 | explicit EpollAPI(EventLoop *loop); 17 | virtual ~EpollAPI(); 18 | virtual int init(void); 19 | virtual int register_event(int fd, int type); 20 | virtual int unregister_event(int fd, int type); 21 | virtual int modify_event(int fd, int type); 22 | virtual int resize(int set_size); 23 | virtual int loop(int timeout); 24 | virtual void destory(void); 25 | EpollAPI &operator=(const EpollAPI &) = delete; 26 | EpollAPI(const EpollAPI &) = delete; 27 | private: 28 | static const int LOOP_INIT_SIZE = 1024; 29 | int epfd_; 30 | struct epoll_event *events_; 31 | int loop_size_; 32 | EventLoop *loop_; 33 | }; 34 | } // end namespace network 35 | } // end namespace dstore 36 | 37 | #endif // DSTORE_NETWORK_EPOLL_H_ 38 | -------------------------------------------------------------------------------- /dstore/network/event_loop.cpp: -------------------------------------------------------------------------------- 1 | #include "event_loop.h" 2 | #include 3 | #include 4 | #include 5 | #include "errno_define.h" 6 | #include "log.h" 7 | #include "event_poll_api.h" 8 | #include "time_operator.h" 9 | 10 | using namespace dstore::common; 11 | using namespace dstore::network; 12 | 13 | EventLoop::EventLoop(void) 14 | : poll_api_(nullptr), registered_events_(), 15 | ready_events_(), timer_heap_(), stop_(false) 16 | { 17 | } 18 | 19 | EventLoop::~EventLoop(void) 20 | { 21 | } 22 | 23 | int EventLoop::init(EventPollAPI *poll_api) 24 | { 25 | int ret = DSTORE_SUCCESS; 26 | if (DSTORE_SUCCESS != (ret = poll_api->init())) { 27 | LOG_ERROR("init poll api failed, ret=%d", ret); 28 | return ret; 29 | } 30 | poll_api_.reset(poll_api); 31 | return ret; 32 | } 33 | 34 | int EventLoop::register_event(std::shared_ptr e) 35 | { 36 | int ret = DSTORE_SUCCESS; 37 | const int type = e->type; 38 | if ((type & Event::kEventRead) || (type & Event::kEventWrite)) { 39 | if (DSTORE_SUCCESS != (ret = poll_api_->register_event(e->fd, e->type))) { 40 | LOG_WARN("register event failed, fd=%d, type=%d, ret=%d", e->fd, e->type, ret); 41 | return ret; 42 | } else { 43 | registered_events_[e->fd] = e; 44 | } 45 | } 46 | if (type & Event::kEventTimer) { 47 | if (DSTORE_SUCCESS != (ret = register_timer_event(e))) { 48 | LOG_WARN("register timer event failed"); 49 | return ret; 50 | } 51 | } 52 | return ret; 53 | } 54 | 55 | int EventLoop::unregister_event(std::shared_ptr e) 56 | { 57 | int ret = DSTORE_SUCCESS; 58 | const int type = e->type; 59 | if ((type & Event::kEventRead) || (type & Event::kEventWrite)) { 60 | ret = poll_api_->unregister_event(e->fd, e->type); 61 | if (DSTORE_SUCCESS != ret) { 62 | LOG_WARN("unregister event failed, fd=%d, type=%d, ret=%d", e->fd, e->type, ret); 63 | } else { 64 | registered_events_.erase(e->fd); 65 | } 66 | } 67 | if (type & Event::kEventTimer) { 68 | if (DSTORE_SUCCESS != (ret = unregister_timer_event(e))) { 69 | LOG_WARN("unregister timer event failed"); 70 | return ret; 71 | } 72 | } 73 | return ret; 74 | } 75 | 76 | int EventLoop::modify_event(std::shared_ptr e) 77 | { 78 | int ret = DSTORE_SUCCESS; 79 | const int type = e->type; 80 | if ((type & Event::kEventRead) || (type & Event::kEventWrite)) { 81 | ret = poll_api_->modify_event(e->fd, e->type); 82 | if (DSTORE_SUCCESS != ret) { 83 | LOG_WARN("modify event failed, ret=%d", ret); 84 | } else { 85 | registered_events_[e->fd] = e; 86 | } 87 | } 88 | if (type & Event::kEventTimer) { 89 | e->timeout += get_milliseconds(); 90 | if (DSTORE_SUCCESS != (ret = modify_timer_event(e))) { 91 | LOG_WARN("modify timer event failed"); 92 | return ret; 93 | } 94 | } 95 | return ret; 96 | } 97 | 98 | int EventLoop::loop(void) 99 | { 100 | int ret = DSTORE_SUCCESS; 101 | while (!stop_) { 102 | if (DSTORE_SUCCESS != (ret = poll_api_->loop(get_timeout()))) { 103 | LOG_ERROR("poll error, ret=%d", ret); 104 | return ret; 105 | } 106 | process_timeout_events(); 107 | for (auto e = ready_events_.cbegin(); e != ready_events_.cend(); ++e) { 108 | const std::shared_ptr r = registered_events_[e->fd]; 109 | if (e->type & r->type & Event::kEventRead) { 110 | e->read_cb(e->fd, Event::kEventRead, e->args); 111 | } 112 | if (e->type & r->type & Event::kEventWrite) { 113 | e->write_cb(e->fd, Event::kEventWrite, e->args); 114 | } 115 | } 116 | clear_ready_events(); 117 | } 118 | return ret; 119 | } 120 | 121 | void EventLoop::destroy(void) 122 | { 123 | poll_api_->destory(); 124 | } 125 | 126 | void EventLoop::add_ready_event(int fd, int event_type) 127 | { 128 | Event e = *registered_events_[fd]; 129 | e.type = event_type; 130 | ready_events_.push_back(e); 131 | } 132 | 133 | void EventLoop::clear_ready_events(void) 134 | { 135 | ready_events_.clear(); 136 | } 137 | 138 | int EventLoop::register_timer_event(std::shared_ptr e) 139 | { 140 | int ret = DSTORE_SUCCESS; 141 | if ((e->timeout <= 0) || (e->timeout > std::numeric_limits::max())) { 142 | ret = DSTORE_INVALID_ARGUMENT; 143 | LOG_WARN("Invalid timeout=%ld", e->timeout); 144 | return ret; 145 | } 146 | push_timer_heap(e); 147 | return ret; 148 | } 149 | 150 | int EventLoop::unregister_timer_event(std::shared_ptr e) 151 | { 152 | int ret = DSTORE_SUCCESS; 153 | if (e->index >= timer_heap_.size()) { 154 | ret = DSTORE_INVALID_ARGUMENT; 155 | LOG_WARN("Invalid event index=%d", e->index); 156 | return ret; 157 | } 158 | remove_timer_heap(e); 159 | return ret; 160 | } 161 | 162 | int EventLoop::modify_timer_event(std::shared_ptr e) 163 | { 164 | int ret = DSTORE_SUCCESS; 165 | if (e->index >= timer_heap_.size()) { 166 | ret = DSTORE_INVALID_ARGUMENT; 167 | LOG_WARN("Invalid event index=%d", e->index); 168 | return ret; 169 | } 170 | update_timer_heap(e); 171 | return ret; 172 | } 173 | 174 | void EventLoop::swap_timer_event(const Index i, const Index j) 175 | { 176 | std::swap(timer_heap_[i], timer_heap_[j]); 177 | std::swap(timer_heap_[i]->index, timer_heap_[j]->index); 178 | } 179 | 180 | void EventLoop::down_timer_heap(const Index i, const Index size, std::vector> &heap) 181 | { 182 | Index p = i; 183 | for (Index child = 2 * p + 1; child < size; child = 2 * p + 1) { 184 | if (child + 1 < size && heap[child]->timeout > heap[child+1]->timeout) { 185 | ++child; 186 | } 187 | if (heap[p]->timeout > heap[child]->timeout) { 188 | swap_timer_event(p, child); 189 | p = child; 190 | } else { 191 | break; 192 | } 193 | } 194 | } 195 | 196 | void EventLoop::up_timer_heap(const Index i, std::vector> &heap) 197 | { 198 | int64_t c = static_cast(i); 199 | if (0 == i) { 200 | return; 201 | } 202 | for (int64_t parent = (c - 1) / 2; parent >= 0; parent = (parent - 1) / 2) { 203 | if (parent == c || heap[parent]->timeout < heap[c]->timeout) { 204 | break; 205 | } else { 206 | swap_timer_event(parent, c); 207 | c = parent; 208 | } 209 | } 210 | } 211 | 212 | void EventLoop::adjust_timer_heap(const Index i, const Index size, std::vector> &heap) 213 | { 214 | if (i > 0 && heap[i]->timeout <= heap[(i-1)/2]->timeout) { 215 | up_timer_heap(i, heap); 216 | } else { 217 | down_timer_heap(i, size, heap); 218 | } 219 | } 220 | 221 | void EventLoop::push_timer_heap(std::shared_ptr e) 222 | { 223 | e->index = timer_heap_.size(); 224 | e->timeout += get_milliseconds(); 225 | timer_heap_.push_back(e); 226 | adjust_timer_heap(e->index, timer_heap_.size(), timer_heap_); 227 | } 228 | 229 | void EventLoop::pop_timer_heap(void) 230 | { 231 | swap_timer_event(0, timer_heap_.size() - 1); 232 | adjust_timer_heap(0, timer_heap_.size() - 1, timer_heap_); 233 | timer_heap_.pop_back(); 234 | } 235 | 236 | std::shared_ptr EventLoop::top_timer_heap(void) 237 | { 238 | return timer_heap_.size() == 0 ? nullptr : timer_heap_.front(); 239 | } 240 | 241 | void EventLoop::remove_timer_heap(const std::shared_ptr e) 242 | { 243 | timer_heap_[e->index]->timeout = -1; 244 | adjust_timer_heap(e->index, timer_heap_.size(), timer_heap_); 245 | pop_timer_heap(); 246 | } 247 | 248 | void EventLoop::update_timer_heap(const std::shared_ptr e) 249 | { 250 | adjust_timer_heap(e->index, timer_heap_.size(), timer_heap_); 251 | } 252 | 253 | int EventLoop::get_timeout(void) 254 | { 255 | const int64_t now = get_milliseconds(); 256 | return timer_heap_.size() == 0 ? -1 : static_cast(timer_heap_.front()->timeout - now); 257 | } 258 | 259 | void EventLoop::process_timeout_events(void) 260 | { 261 | const int64_t now = get_milliseconds(); 262 | std::shared_ptr e = top_timer_heap(); 263 | while (e != nullptr && e->timeout <= now) { 264 | e->timer_cb(e->fd, Event::kEventTimer, e->args); 265 | pop_timer_heap(); 266 | e = top_timer_heap(); 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /dstore/network/event_loop.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_EVENT_LOOP_H_ 2 | #define DSTORE_NETWORK_EVENT_LOOP_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace dstore 11 | { 12 | namespace network 13 | { 14 | class EventPollAPI; 15 | 16 | struct Event 17 | { 18 | public: 19 | typedef std::function EventCallback; 20 | static const int kEventRead = 1 << 0; 21 | static const int kEventWrite = 1 << 1; 22 | static const int kEventTimer = 1 << 2; 23 | static const int kEventError = 1 << 3; 24 | int fd; 25 | int type; 26 | void *args; 27 | int64_t timeout; 28 | std::vector::size_type index; 29 | EventCallback read_cb; 30 | EventCallback write_cb; 31 | EventCallback timer_cb; 32 | }; 33 | 34 | class EventLoop 35 | { 36 | public: 37 | EventLoop(void); 38 | ~EventLoop(void); 39 | int init(EventPollAPI *api); 40 | int register_event(std::shared_ptr e); 41 | int unregister_event(std::shared_ptr e); 42 | int modify_event(std::shared_ptr e); 43 | int loop(void); 44 | void destroy(void); 45 | int get_loop_size(void); 46 | void add_ready_event(int fd, int event_type); 47 | EventLoop &operator=(const EventLoop &) = delete; 48 | EventLoop(const EventLoop &) = delete; 49 | private: 50 | typedef std::vector>::size_type Index; 51 | void clear_ready_events(void); 52 | int register_timer_event(std::shared_ptr e); 53 | int unregister_timer_event(const std::shared_ptr e); 54 | int modify_timer_event(std::shared_ptr e); 55 | void swap_timer_event(const Index i, const Index j); 56 | void down_timer_heap(const Index i, const Index size, std::vector> &heap); 57 | void up_timer_heap(const Index i, std::vector> &heap); 58 | void adjust_timer_heap(const Index i, const Index size, std::vector> &heap); 59 | void push_timer_heap(std::shared_ptr e); 60 | void pop_timer_heap(void); 61 | std::shared_ptr top_timer_heap(void); 62 | void remove_timer_heap(const std::shared_ptr e); 63 | void update_timer_heap(const std::shared_ptr e); 64 | int get_timeout(void); 65 | void process_timeout_events(void); 66 | private: 67 | std::unique_ptr poll_api_; 68 | std::unordered_map> registered_events_; 69 | std::list ready_events_; 70 | std::vector> timer_heap_; 71 | bool stop_; 72 | }; 73 | } // end namespace network 74 | } // end namespace dstore 75 | 76 | #endif // DSTORE_NETWORK_EVENT_LOOP_H_ 77 | -------------------------------------------------------------------------------- /dstore/network/event_poll_api.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_EVENT_POLL_API_H_ 2 | #define DSTORE_NETWORK_EVENT_POLL_API_H_ 3 | 4 | #include 5 | 6 | namespace dstore 7 | { 8 | namespace network 9 | { 10 | class EventPollAPI 11 | { 12 | public: 13 | EventPollAPI() {} 14 | virtual ~EventPollAPI() {} 15 | virtual int init(void) = 0; 16 | virtual int register_event(int fd, int type) = 0; 17 | virtual int unregister_event(int fd, int type) = 0; 18 | virtual int modify_event(int fd, int type) = 0; 19 | virtual int resize(int set_size) = 0; 20 | virtual int loop(int timeout) = 0; 21 | virtual void destory(void) = 0; 22 | EventPollAPI &operator=(const EventPollAPI &) = delete; 23 | EventPollAPI(const EventPollAPI &) = delete; 24 | }; 25 | } // end namespace network 26 | } // end namespace dstore 27 | 28 | #endif // DSTORE_NETWORK_EVENT_POLL_API_H_ 29 | -------------------------------------------------------------------------------- /dstore/network/inet_addr.cpp: -------------------------------------------------------------------------------- 1 | #include "inet_addr.h" 2 | #include 3 | #include 4 | #include "errno_define.h" 5 | #include "log.h" 6 | 7 | using namespace dstore::common; 8 | using namespace dstore::network; 9 | 10 | InetAddr::InetAddr(void) 11 | : host_(nullptr), port_(nullptr), addr_(), addr_len_(sizeof(addr_)), 12 | ai_family_(-1), ai_socktype_(-1), ai_protocol_(-1), is_ipv6_(false), 13 | is_ai_passive_(false), is_resolved_(false) 14 | { 15 | } 16 | 17 | InetAddr::~InetAddr(void) 18 | { 19 | } 20 | 21 | int InetAddr::set_addr(const char *host, const char *port, const bool is_ipv6, const bool is_ai_passive) 22 | { 23 | int ret = DSTORE_SUCCESS; 24 | host_ = host; 25 | port_ = port; 26 | is_ipv6_ = is_ipv6; 27 | is_ai_passive_ = is_ai_passive; 28 | is_resolved_ = false; 29 | if (DSTORE_SUCCESS != (ret = resolve())) { 30 | LOG_WARN("resolve address failed, host=%s, port=%s, is_ipv6=%d, is_ai_passive=%d", 31 | host, port, is_ipv6, is_ai_passive); 32 | return ret; 33 | } 34 | return ret; 35 | } 36 | 37 | const struct sockaddr* InetAddr::get_addr(void) const 38 | { 39 | return &addr_; 40 | } 41 | 42 | struct sockaddr* InetAddr::get_addr(void) 43 | { 44 | return &addr_; 45 | } 46 | 47 | const int *InetAddr::get_addr_len(void) const 48 | { 49 | return &addr_len_; 50 | } 51 | 52 | int *InetAddr::get_addr_len(void) 53 | { 54 | return &addr_len_; 55 | } 56 | 57 | int InetAddr::resolve(void) 58 | { 59 | int ret = DSTORE_SUCCESS; 60 | struct addrinfo hints; 61 | struct addrinfo *res = nullptr; 62 | 63 | ::memset(&hints, 0, sizeof(struct addrinfo)); 64 | if (is_ai_passive_) { 65 | hints.ai_flags = AI_PASSIVE; 66 | } 67 | if (is_ipv6_) { 68 | hints.ai_family = AF_INET6; 69 | } else { 70 | hints.ai_family = AF_INET; 71 | } 72 | hints.ai_socktype = SOCK_STREAM; 73 | 74 | ret = ::getaddrinfo(host_, port_, &hints, &res); 75 | if (0 != ret) { 76 | LOG_WARN("getaddrinfo failed, host=%s, port=%s, ret=%d, error=%s", host_, port_, ret, gai_strerror(ret)); 77 | ret = DSTORE_PARSE_ADDR_ERROR; 78 | return ret; 79 | } else if (nullptr == res) { 80 | LOG_WARN("getaddrinfo failed, got an empty address, host=%s, port=%s", host_, port_); 81 | ret = DSTORE_PARSE_ADDR_EMPTY; 82 | return ret; 83 | } 84 | addr_ = *res->ai_addr; 85 | addr_len_ = res->ai_addrlen; 86 | ai_family_ = res->ai_family; 87 | ai_socktype_ = res->ai_socktype; 88 | ai_protocol_ = res->ai_protocol; 89 | ::freeaddrinfo(res); 90 | is_resolved_ = true; 91 | return ret; 92 | } 93 | -------------------------------------------------------------------------------- /dstore/network/inet_addr.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_INET_ADDR_H_ 2 | #define DSTORE_NETWORK_INET_ADDR_H_ 3 | 4 | #include 5 | 6 | namespace dstore 7 | { 8 | namespace network 9 | { 10 | class InetAddr 11 | { 12 | public: 13 | InetAddr(void); 14 | ~InetAddr(void); 15 | int set_addr(const char *host, const char *port, const bool is_ipv6, const bool is_ai_passive); 16 | const struct sockaddr *get_addr(void) const; 17 | struct sockaddr *get_addr(void); 18 | const int *get_addr_len(void) const; 19 | int *get_addr_len(void); 20 | int get_family(void) const { return ai_family_; } 21 | int get_socket_type(void) const { return ai_socktype_; } 22 | int get_protocol(void) const { return ai_protocol_; } 23 | private: 24 | int resolve(); 25 | private: 26 | const char *host_; 27 | const char *port_; 28 | struct sockaddr addr_; 29 | int addr_len_; 30 | int ai_family_; 31 | int ai_socktype_; 32 | int ai_protocol_; 33 | bool is_ipv6_; 34 | bool is_ai_passive_; 35 | bool is_resolved_; 36 | }; 37 | } // end namespace network 38 | } // end namespace dstore 39 | #endif // DSTORE_NETWORK_INET_ADDR_H_ 40 | -------------------------------------------------------------------------------- /dstore/network/message.cpp: -------------------------------------------------------------------------------- 1 | #include "message.h" 2 | 3 | using namespace dstore::network; 4 | 5 | Message::Message(void *request) 6 | : request_(request) 7 | { 8 | } 9 | -------------------------------------------------------------------------------- /dstore/network/message.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_MESSAGE_H_ 2 | #define DSTORE_NETWORK_MESSAGE_H_ 3 | 4 | namespace dstore 5 | { 6 | namespace network 7 | { 8 | class Message 9 | { 10 | public: 11 | explicit Message(void *request); 12 | void *get_request(void) { return request_; } 13 | void set_request(void *request) { request_ = request; } 14 | private: 15 | void *request_; 16 | }; 17 | } // end namespace network 18 | } // end namespace dstore 19 | 20 | #endif // DSTORE_NETWORK_MESSAGE_H_ 21 | -------------------------------------------------------------------------------- /dstore/network/socket.cpp: -------------------------------------------------------------------------------- 1 | #include "socket.h" 2 | #include 3 | #include "errno_define.h" 4 | #include "socket_operator.h" 5 | #include "log.h" 6 | 7 | using namespace dstore::network; 8 | 9 | ListenSocket::ListenSocket(void) 10 | : listen_fd_(-1) 11 | { 12 | } 13 | 14 | ListenSocket::ListenSocket(int listen_fd) 15 | : listen_fd_(listen_fd) 16 | { 17 | } 18 | 19 | ListenSocket::~ListenSocket(void) 20 | { 21 | } 22 | 23 | void ListenSocket::set_listen_fd(int listen_fd) 24 | { 25 | listen_fd_ = listen_fd; 26 | } 27 | 28 | int ListenSocket::get_listen_fd(void) 29 | { 30 | return listen_fd_; 31 | } 32 | 33 | int ListenSocket::socket(const InetAddr &addr) 34 | { 35 | int ret = DSTORE_SUCCESS; 36 | int listen_fd = dstore::network::socket(addr.get_family(), addr.get_socket_type(), addr.get_protocol()); 37 | if (-1 == listen_fd) { 38 | LOG_WARN("create listen socket failed"); 39 | ret = DSTORE_LISTEN_ERROR; 40 | return ret; 41 | } 42 | listen_fd_ = listen_fd; 43 | return ret; 44 | } 45 | 46 | int ListenSocket::bind(const InetAddr &addr) 47 | { 48 | int ret = DSTORE_SUCCESS; 49 | ret = dstore::network::bind(listen_fd_, addr.get_addr(), *addr.get_addr_len()); 50 | if (-1 == ret) { 51 | LOG_WARN("bind listen socket failed, fd=%d", listen_fd_); 52 | ret = DSTORE_BIND_ERROR; 53 | return ret; 54 | } 55 | return ret; 56 | } 57 | 58 | int ListenSocket::listen(int backlog) 59 | { 60 | int ret = DSTORE_SUCCESS; 61 | ret = dstore::network::listen(listen_fd_, backlog); 62 | if (-1 == ret) { 63 | LOG_WARN("listen socket failed, fd=%d", listen_fd_); 64 | ret = DSTORE_LISTEN_ERROR; 65 | return ret; 66 | } 67 | return ret; 68 | } 69 | 70 | int ListenSocket::accept(int *fd, InetAddr *addr) 71 | { 72 | int ret = DSTORE_SUCCESS; 73 | *fd = dstore::network::accept(listen_fd_, addr->get_addr(), addr->get_addr_len()); 74 | if (-1 == *fd) { 75 | LOG_WARN("accept failed, listen_fd=%d", listen_fd_); 76 | ret = DSTORE_ACCEPT_ERROR; 77 | return ret; 78 | } 79 | return ret; 80 | } 81 | 82 | int ListenSocket::set_reuseaddr(void) 83 | { 84 | int ret = DSTORE_SUCCESS; 85 | ret = dstore::network::set_reuseaddr(listen_fd_); 86 | if (-1 == ret) { 87 | LOG_WARN("set reuseaddr failed, listen_fd=%d", listen_fd_); 88 | ret = DSTORE_SET_SOCKET_ERROR; 89 | return ret; 90 | } 91 | return ret; 92 | } 93 | 94 | int ListenSocket::set_nonblocking(void) 95 | { 96 | int ret = DSTORE_SUCCESS; 97 | ret = dstore::network::set_nonblocking(listen_fd_); 98 | if (-1 == ret) { 99 | LOG_WARN("set nonblocking failed, listen_fd=%d", listen_fd_); 100 | ret = DSTORE_SET_SOCKET_ERROR; 101 | return ret; 102 | } 103 | return ret; 104 | } 105 | 106 | Socket::Socket(void) 107 | : fd_(-1) 108 | { 109 | } 110 | 111 | Socket::Socket(int fd) 112 | : fd_(fd) 113 | { 114 | } 115 | 116 | Socket::~Socket(void) 117 | { 118 | } 119 | 120 | void Socket::set_fd(int fd) 121 | { 122 | fd_ = fd; 123 | } 124 | 125 | int Socket::get_fd(void) 126 | { 127 | return fd_; 128 | } 129 | 130 | int Socket::socket(const InetAddr &addr) 131 | { 132 | int ret = DSTORE_SUCCESS; 133 | int fd = dstore::network::socket(addr.get_family(), addr.get_socket_type(), addr.get_protocol()); 134 | if (-1 == fd) { 135 | LOG_WARN("create listen socket failed"); 136 | ret = DSTORE_LISTEN_ERROR; 137 | return ret; 138 | } 139 | fd_ = fd; 140 | return ret; 141 | } 142 | 143 | int Socket::connect(const InetAddr &addr) 144 | { 145 | int ret = DSTORE_SUCCESS; 146 | ret = dstore::network::connect(fd_, addr.get_addr(), *addr.get_addr_len()); 147 | if (-1 == ret) { 148 | if (errno == EINPROGRESS) { 149 | ret = DSTORE_CONNECT_IN_PROGRESS; 150 | } else { 151 | ret = DSTORE_CONNECT_ERROR; 152 | } 153 | } 154 | return ret; 155 | } 156 | 157 | int Socket::set_nonblocking(void) 158 | { 159 | int ret = DSTORE_SUCCESS; 160 | ret = dstore::network::set_nonblocking(fd_); 161 | if (-1 == ret) { 162 | LOG_WARN("set non blocking failed, fd_=%d", fd_); 163 | ret = DSTORE_SET_SOCKET_ERROR; 164 | return ret; 165 | } 166 | return ret; 167 | } 168 | 169 | void Socket::close(void) 170 | { 171 | dstore::network::close(fd_); 172 | } 173 | 174 | bool Socket::is_connect_ok(void) 175 | { 176 | return dstore::network::is_connect_ok(fd_); 177 | } 178 | -------------------------------------------------------------------------------- /dstore/network/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_SOCKET_H_ 2 | #define DSTORE_NETWORK_SOCKET_H_ 3 | 4 | #include "inet_addr.h" 5 | 6 | namespace dstore 7 | { 8 | namespace network 9 | { 10 | class ListenSocket 11 | { 12 | public: 13 | ListenSocket(void); 14 | explicit ListenSocket(int listen_fd); 15 | ~ListenSocket(void); 16 | void set_listen_fd(int listen_fd); 17 | int get_listen_fd(void); 18 | int socket(const InetAddr &addr); 19 | int bind(const InetAddr &addr); 20 | int listen(int backlog); 21 | int accept(int *fd, InetAddr *addr); 22 | int set_reuseaddr(void); 23 | int set_nonblocking(void); 24 | ListenSocket &operator=(const ListenSocket &) = delete; 25 | ListenSocket(const ListenSocket &) = delete; 26 | private: 27 | int listen_fd_; 28 | }; 29 | 30 | class Socket 31 | { 32 | public: 33 | Socket(void); 34 | explicit Socket(int fd); 35 | ~Socket(void); 36 | void set_fd(int fd); 37 | int get_fd(void); 38 | int set_nonblocking(void); 39 | void close(void); 40 | int socket(const InetAddr &addr); 41 | int connect(const InetAddr &addr); 42 | bool is_connect_ok(void); 43 | Socket &operator=(const Socket &) = delete; 44 | Socket(const Socket &) = delete; 45 | private: 46 | int fd_; 47 | }; 48 | } // end namespace network 49 | } // end namespace dstore 50 | 51 | #endif // DSTORE_NETWORK_SOCKET_H_ 52 | -------------------------------------------------------------------------------- /dstore/network/socket_operator.cpp: -------------------------------------------------------------------------------- 1 | #include "socket_operator.h" 2 | #include 3 | #include 4 | #include 5 | #include "log.h" 6 | 7 | namespace dstore 8 | { 9 | namespace network 10 | { 11 | 12 | int socket(int family, int socket_type, int protocol) 13 | { 14 | int ret = ::socket(family, socket_type, protocol); 15 | if (-1 == ret) { 16 | LOG_WARN("create socket failed, family=%d, socket_type=%d, protocol=%d, errno=%d", 17 | family, socket_type, protocol, errno); 18 | return ret; 19 | } 20 | return ret; 21 | } 22 | 23 | int bind(int fd, const struct sockaddr *addr, int addr_len) 24 | { 25 | int ret = ::bind(fd, addr, addr_len); 26 | if (-1 == ret) { 27 | LOG_WARN("bind fd failed, fd=%d, addrlen=%d, errno=%d", fd, addr_len, errno); 28 | return ret; 29 | } 30 | return ret; 31 | } 32 | 33 | int listen(int fd, int backlog) 34 | { 35 | int ret = ::listen(fd, backlog); 36 | if (-1 == ret) { 37 | LOG_WARN("listen failed, fd=%d, backlog=%d, errno=%d", fd, backlog, errno); 38 | return ret; 39 | } 40 | return ret; 41 | } 42 | int accept(int fd, struct sockaddr *addr, int *addr_len) 43 | { 44 | int ret = -1; 45 | socklen_t sock_len = static_cast(*addr_len); 46 | ret = ::accept(fd, addr, &sock_len); 47 | if (-1 == ret) { 48 | LOG_WARN("fd=%d, addrlen=%d, ret= %d", fd, *addr_len, errno); 49 | return ret; 50 | } 51 | *addr_len = static_cast(sock_len); 52 | return ret; 53 | } 54 | 55 | int set_reuseaddr(int fd) 56 | { 57 | int on = 1; 58 | int ret = ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); 59 | if (-1 == ret) { 60 | LOG_WARN("set_reuseaddr failed, fd=%d, errno=%d", fd, errno); 61 | return ret; 62 | } 63 | return ret; 64 | } 65 | 66 | int set_nonblocking(int fd) 67 | { 68 | int ret = ::fcntl(fd, F_SETFL, O_NONBLOCK); 69 | if (-1 == ret) { 70 | LOG_WARN("set nonblocking failed, fd=%d, errno=%d", fd, errno); 71 | return ret; 72 | } 73 | return ret; 74 | } 75 | 76 | ssize_t recv(int fd, void *buf, size_t nbytes) 77 | { 78 | ssize_t nread = 0; 79 | size_t total_read = 0; 80 | char *ptr = static_cast(buf); 81 | while (total_read != nbytes) { 82 | nread = ::read(fd, ptr, nbytes - total_read); 83 | if (0 == nread) { 84 | return total_read; 85 | } else if (-1 == nread) { 86 | return total_read == 0 ? -1 : total_read; 87 | } else { 88 | total_read += nread; 89 | ptr += static_cast(nread); 90 | } 91 | } 92 | return total_read; 93 | } 94 | 95 | ssize_t send(int fd, const void *buf, size_t nbytes) 96 | { 97 | ssize_t nwrite = 0; 98 | size_t total_write = 0; 99 | const char *ptr = static_cast(buf); 100 | 101 | while (total_write != nbytes) { 102 | nwrite = ::write(fd, ptr, nbytes - total_write); 103 | if (0 == nwrite) { 104 | return total_write; 105 | } else if (-1 == nwrite) { 106 | return nwrite == 0 ? -1 : nwrite; 107 | } else { 108 | total_write += nwrite; 109 | ptr += nwrite; 110 | } 111 | } 112 | return total_write; 113 | } 114 | 115 | int close(int fd) 116 | { 117 | return ::close(fd); 118 | } 119 | 120 | int connect(int fd, const struct sockaddr *addr, int addrlen) 121 | { 122 | return ::connect(fd, addr, addrlen); 123 | } 124 | 125 | bool is_connect_ok(int fd) 126 | { 127 | int error = 0; 128 | unsigned int sz = sizeof(error); 129 | int ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, static_cast(&error), &sz); 130 | return !((ret < 0) || error); 131 | } 132 | 133 | } // end namespace network 134 | } // end namespace dstore 135 | -------------------------------------------------------------------------------- /dstore/network/socket_operator.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_SOCKET_OPERATOR_H_ 2 | #define DSTORE_NETWORK_SOCKET_OPERATOR_H_ 3 | 4 | #include 5 | #include 6 | 7 | namespace dstore 8 | { 9 | namespace network 10 | { 11 | int socket(int ai_family, int socket_type, int protocol); 12 | int bind(int fd, const struct sockaddr *addr, int addr_len); 13 | int listen(int fd, int backlog); 14 | int accept(int fd, struct sockaddr *addr, int *addr_len); 15 | int set_reuseaddr(int fd); 16 | int set_nonblocking(int fd); 17 | ssize_t recv(int fd, void *buf, size_t size); 18 | ssize_t send(int fd, const void *buf, size_t size); 19 | int close(int fd); 20 | int connect(int fd, const struct sockaddr *addr, int addr_len); 21 | bool is_connect_ok(int fd); 22 | } // end namespace network 23 | } // end namespace dstore 24 | 25 | #endif // DSTORE_NETWORK_SOCKET_OPERATOR_H_ 26 | -------------------------------------------------------------------------------- /dstore/network/tcp_client.cpp: -------------------------------------------------------------------------------- 1 | #include "tcp_client.h" 2 | #include 3 | #include "epoll.h" 4 | #include "errno_define.h" 5 | #include "log.h" 6 | 7 | using namespace std::placeholders; 8 | using namespace dstore::network; 9 | 10 | TCPClient::TCPClient(void) 11 | : loop_(), connection_map_(), next_connection_id_(0), message_decoder_(), 12 | new_message_callback_(), connect_complete_() 13 | { 14 | } 15 | 16 | int TCPClient::start(void) 17 | { 18 | int ret = DSTORE_SUCCESS; 19 | EventPollAPI *poll_api = new EpollAPI(&loop_); 20 | if (DSTORE_SUCCESS != (ret = loop_.init(poll_api))) { 21 | LOG_ERROR("init loop failed, ret=%d", ret); 22 | return ret; 23 | } 24 | return ret; 25 | } 26 | 27 | int TCPClient::connect(const char *host, const char *port, const bool is_ipv6) 28 | { 29 | int ret = DSTORE_SUCCESS; 30 | if (DSTORE_SUCCESS != (ret = connector_.set_addr(host, port, is_ipv6))) { 31 | LOG_WARN("connector set addr failed, host=%s, port=%s, is_ipv6=%d, ret=%d", 32 | host, port, is_ipv6, ret); 33 | return ret; 34 | } 35 | 36 | if (DSTORE_SUCCESS != (ret = connector_.start())) { 37 | LOG_WARN("connector start failed, ret=%d", ret); 38 | return ret; 39 | } 40 | 41 | if (DSTORE_SUCCESS != (ret = connector_.connect())) { 42 | if (DSTORE_CONNECT_IN_PROGRESS != ret) { 43 | LOG_WARN("connect failed, ret=%d", ret); 44 | return ret; 45 | } 46 | } 47 | 48 | std::shared_ptr e(new Event()); 49 | Connection::Status status = DSTORE_SUCCESS == ret ? Connection::ACTIVE : Connection::CONNECTING; 50 | e->fd = connector_.get_socket_fd(); 51 | e->type = DSTORE_SUCCESS == ret ? Event::kEventRead : Event::kEventRead | Event::kEventWrite; 52 | e->args = reinterpret_cast(next_connection_id_); 53 | e->read_cb = std::bind(&TCPClient::on_read, this, _1, _2, _3); 54 | e->write_cb = std::bind(&TCPClient::on_write, this, _1, _2, _3); 55 | 56 | if (DSTORE_SUCCESS != (ret = loop_.register_event(e))) { 57 | LOG_WARN("register event failed, ret=%d", ret); 58 | return ret; 59 | } 60 | 61 | std::shared_ptr conn(new Connection(connector_.get_server_addr(), e)); 62 | if (DSTORE_SUCCESS != (ret = conn->init(&loop_))) { 63 | LOG_WARN("init connection failed, ret=%d", ret); 64 | return ret; 65 | } 66 | conn->set_status(status); 67 | connection_map_[next_connection_id_++] = conn; 68 | return ret; 69 | } 70 | 71 | int TCPClient::loop(void) 72 | { 73 | int ret = DSTORE_SUCCESS; 74 | if (DSTORE_SUCCESS != (ret = loop_.loop())) { 75 | LOG_WARN("loop failed, ret=%d", ret); 76 | return ret; 77 | } 78 | return ret; 79 | } 80 | 81 | void TCPClient::set_message_decode_callback(const MessageDecodeCallback &message_decode) 82 | { 83 | message_decoder_ = message_decode; 84 | } 85 | 86 | void TCPClient::set_new_message_callback(const NewMessageCallback &new_message) 87 | { 88 | new_message_callback_ = new_message; 89 | } 90 | 91 | void TCPClient::set_connect_complete_callback(const ConnectCompeleteCallback &connect_complete) 92 | { 93 | connect_complete_ = connect_complete; 94 | } 95 | 96 | int TCPClient::on_read(int fd, int type, void *args) 97 | { 98 | int ret = DSTORE_SUCCESS; 99 | const int64_t connection_id = reinterpret_cast(args); 100 | std::shared_ptr conn = connection_map_[connection_id]; 101 | Buffer &read_buffer = conn->get_read_buffer(); 102 | const int nbytes = 1024; 103 | ssize_t read_bytes = read_buffer.read_fd(fd, nbytes); 104 | if (0 == read_bytes) { 105 | if (DSTORE_SUCCESS != (ret = on_close(connection_id))) { 106 | LOG_WARN("handle close failed, ret=%d", ret); 107 | return ret; 108 | } 109 | return ret; 110 | } 111 | if (DSTORE_SUCCESS != (ret = message_decoder_(conn))) { 112 | LOG_WARN("decode message failed, ret=%d", ret); 113 | return ret; 114 | } 115 | return new_message_callback_(conn); 116 | } 117 | 118 | int TCPClient::on_write(int fd, int type, void *args) 119 | { 120 | int ret = DSTORE_SUCCESS; 121 | const int64_t connection_id = reinterpret_cast(args); 122 | std::shared_ptr conn = connection_map_[connection_id]; 123 | if (Connection::ACTIVE == conn->get_status()) { 124 | Buffer &write_buffer = conn->get_write_buffer(); 125 | ssize_t write_bytes = write_buffer.write_fd(fd); 126 | if (-1 == write_bytes) { 127 | remove_connection(connection_id); 128 | return ret; 129 | } 130 | size_t need_write_bytes = write_buffer.get_need_write_bytes(); 131 | if (0 == need_write_bytes) { 132 | if (DSTORE_SUCCESS != (ret = conn->remove_write())) { 133 | LOG_WARN("remove write event from connection_id=%d failed, ret=%d", connection_id, ret); 134 | return ret; 135 | } 136 | if (Connection::SHUTDOWN_READ == conn->get_status()) { 137 | if (DSTORE_SUCCESS != (ret = on_close(connection_id))) { 138 | LOG_WARN("handle close connection failed, ret=%d", ret); 139 | return ret; 140 | } 141 | } 142 | } else if (need_write_bytes > 0) { 143 | // do not modify the event 144 | // just let it in loop again 145 | } 146 | } else { 147 | bool ok = conn->is_connect_ok(); 148 | if (ok) { 149 | conn->set_status(Connection::ACTIVE); 150 | if (DSTORE_SUCCESS != (ret = conn->remove_write())) { 151 | LOG_WARN("remove write event from connection=%d failed, ret=%d", connection_id, ret); 152 | return ret; 153 | } 154 | connect_complete_(conn); 155 | } else { 156 | remove_connection(connection_id); 157 | return ret; 158 | } 159 | } 160 | ret = DSTORE_CONNECT_IN_PROGRESS == ret ? DSTORE_SUCCESS : ret; 161 | return ret; 162 | } 163 | 164 | void TCPClient::remove_connection(const int64_t connection_id) 165 | { 166 | std::shared_ptr conn = connection_map_[connection_id]; 167 | conn->close(); 168 | connection_map_.erase(connection_id); 169 | } 170 | 171 | int TCPClient::on_close(const int64_t connection_id) 172 | { 173 | int ret = DSTORE_SUCCESS; 174 | std::shared_ptr conn = connection_map_[connection_id]; 175 | assert(conn != nullptr); 176 | bool has_data_to_write = conn->pending_write(); 177 | if (!has_data_to_write) { 178 | remove_connection(connection_id); 179 | } else { 180 | set_connection_status(connection_id, Connection::SHUTDOWN_READ); 181 | } 182 | return ret; 183 | } 184 | 185 | int TCPClient::set_connection_status(const int64_t connection_id, const Connection::Status status) 186 | { 187 | int ret = DSTORE_SUCCESS; 188 | std::shared_ptr conn = connection_map_[connection_id]; 189 | if (status == Connection::SHUTDOWN_READ) { 190 | if (DSTORE_SUCCESS != (ret = conn->remove_read())) { 191 | LOG_WARN("remove connection read event failed, ret=%d", ret); 192 | return ret; 193 | } 194 | conn->set_status(Connection::SHUTDOWN_READ); 195 | } else if (status == Connection::SHUTDOWN_WRITE) { 196 | if (DSTORE_SUCCESS != (ret = conn->remove_write())) { 197 | LOG_WARN("remove connection write event failed, ret=%d", ret); 198 | return ret; 199 | } 200 | conn->set_status(Connection::SHUTDOWN_WRITE); 201 | } 202 | return ret; 203 | } 204 | -------------------------------------------------------------------------------- /dstore/network/tcp_client.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_TCP_CLIENT_H_ 2 | #define DSTORE_NETWORK_TCP_CLIENT_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include "event_loop.h" 8 | #include "connection.h" 9 | #include "tcp_connector.h" 10 | 11 | namespace dstore 12 | { 13 | namespace network 14 | { 15 | class TCPClient 16 | { 17 | public: 18 | typedef std::function)> MessageDecodeCallback; 19 | typedef std::function)> NewMessageCallback; 20 | typedef std::function)> ConnectCompeleteCallback; 21 | 22 | TCPClient(void); 23 | int start(void); 24 | int connect(const char *host, const char *port, const bool is_ipv6); 25 | int loop(void); 26 | void set_message_decode_callback(const MessageDecodeCallback &message_decode); 27 | void set_new_message_callback(const NewMessageCallback &new_message); 28 | void set_connect_complete_callback(const ConnectCompeleteCallback &connect_complete); 29 | private: 30 | int on_read(int fd, int type, void *args); 31 | int on_write(int fd, int type, void *args); 32 | void remove_connection(const int64_t connection_id); 33 | int on_close(const int64_t connection_id); 34 | int set_connection_status(const int64_t connection_id, const Connection::Status status); 35 | private: 36 | EventLoop loop_; 37 | TCPConnector connector_; 38 | std::unordered_map> connection_map_; 39 | int64_t next_connection_id_; 40 | MessageDecodeCallback message_decoder_; 41 | NewMessageCallback new_message_callback_; 42 | ConnectCompeleteCallback connect_complete_; 43 | }; 44 | } // namespace network 45 | } // namespace dstore 46 | 47 | #endif // DSTORE_NETWORK_TCP_CLIENT_H_ 48 | -------------------------------------------------------------------------------- /dstore/network/tcp_connector.cpp: -------------------------------------------------------------------------------- 1 | #include "tcp_connector.h" 2 | #include "errno_define.h" 3 | #include "log.h" 4 | 5 | using namespace std::placeholders; 6 | using namespace dstore::common; 7 | using namespace dstore::network; 8 | 9 | TCPConnector::TCPConnector(void) 10 | : addr_(), socket_() 11 | { 12 | } 13 | 14 | int TCPConnector::set_addr(const char *host, const char *port, const bool is_ipv6) 15 | { 16 | int ret = DSTORE_SUCCESS; 17 | const bool is_ai_passive = false; 18 | if (DSTORE_SUCCESS != (ret = addr_.set_addr(host, port, is_ipv6, is_ai_passive))) { 19 | LOG_WARN("set addr failed, host=%s, port=%s, is_ipv6=%d, ret=%d", host, port, is_ipv6, ret); 20 | return ret; 21 | } 22 | return ret; 23 | } 24 | 25 | int TCPConnector::start(void) 26 | { 27 | int ret = DSTORE_SUCCESS; 28 | if (DSTORE_SUCCESS != (ret = socket_.socket(addr_))) { 29 | LOG_WARN("create socket failed, ret=%d", ret); 30 | return ret; 31 | } 32 | if (DSTORE_SUCCESS != (ret = socket_.set_nonblocking())) { 33 | LOG_WARN("set socket nonblocking failed, ret=%d", ret); 34 | return ret; 35 | } 36 | return ret; 37 | } 38 | 39 | int TCPConnector::connect(void) 40 | { 41 | int ret = DSTORE_SUCCESS; 42 | if (DSTORE_SUCCESS != (ret = socket_.connect(addr_))) { 43 | if (DSTORE_CONNECT_IN_PROGRESS != ret) { 44 | LOG_WARN("connect failed, ret=%d", ret); 45 | } 46 | return ret; 47 | } 48 | return ret; 49 | } 50 | 51 | int TCPConnector::get_socket_fd(void) 52 | { 53 | return socket_.get_fd(); 54 | } 55 | 56 | InetAddr &TCPConnector::get_server_addr(void) 57 | { 58 | return addr_; 59 | } 60 | -------------------------------------------------------------------------------- /dstore/network/tcp_connector.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_TCP_CONNECTOR_H_ 2 | #define DSTORE_NETWORK_TCP_CONNECTOR_H_ 3 | 4 | #include "inet_addr.h" 5 | #include "socket.h" 6 | #include "event_loop.h" 7 | 8 | namespace dstore 9 | { 10 | namespace network 11 | { 12 | class TCPConnector 13 | { 14 | public: 15 | TCPConnector(void); 16 | int set_addr(const char *host, const char *port, const bool is_ipv6); 17 | Event &get_event(void); 18 | int start(void); 19 | int connect(void); 20 | int get_socket_fd(void); 21 | InetAddr &get_server_addr(void); 22 | TCPConnector &operator=(const TCPConnector &) = delete; 23 | TCPConnector(const TCPConnector &) = delete; 24 | private: 25 | InetAddr addr_; 26 | Socket socket_; 27 | }; 28 | } // namespace network 29 | } // namespace dstore 30 | #endif // DSTORE_NETWORK_TCP_CONNECTOR_H_ 31 | -------------------------------------------------------------------------------- /dstore/network/tcp_listener.cpp: -------------------------------------------------------------------------------- 1 | #include "tcp_listener.h" 2 | #include 3 | #include "errno_define.h" 4 | #include "log.h" 5 | 6 | using namespace std::placeholders; 7 | using namespace dstore::common; 8 | using namespace dstore::network; 9 | 10 | TCPListener::TCPListener(void) 11 | : addr_(), socket_(), e_(new Event()), on_connect_() 12 | { 13 | } 14 | 15 | TCPListener::~TCPListener(void) 16 | { 17 | } 18 | 19 | int TCPListener::set_addr(const char *host, const char *port, const bool is_ipv6) 20 | { 21 | int ret = DSTORE_SUCCESS; 22 | const bool is_ai_passive = true; 23 | ret = addr_.set_addr(host, port, is_ipv6, is_ai_passive); 24 | if (DSTORE_SUCCESS != ret) { 25 | LOG_WARN("set addr failed, host=%s, port=%s, is_ipv6=%d, ret=%d", host, port, is_ipv6, ret); 26 | return ret; 27 | } 28 | return ret; 29 | } 30 | 31 | void TCPListener::set_new_connection_callback(NewConnectionCallback on_connect) 32 | { 33 | on_connect_ = on_connect; 34 | } 35 | 36 | std::shared_ptr TCPListener::get_listen_event(void) 37 | { 38 | return e_; 39 | } 40 | 41 | int TCPListener::start(void) 42 | { 43 | int ret = DSTORE_SUCCESS; 44 | if (DSTORE_SUCCESS != (ret = socket_.socket(addr_))) { 45 | LOG_WARN("create socket failed, ret=%d", ret); 46 | return ret; 47 | } 48 | if (DSTORE_SUCCESS != (ret = socket_.set_reuseaddr())) { 49 | LOG_WARN("set reuseaddr failed, ret=%d", ret); 50 | return ret; 51 | } 52 | if (DSTORE_SUCCESS != (ret = socket_.set_nonblocking())) { 53 | LOG_WARN("set non blocking failed, ret=%d", ret); 54 | return ret; 55 | } 56 | if (DSTORE_SUCCESS != (ret = socket_.bind(addr_))) { 57 | LOG_WARN("bind failed, ret=%d", ret); 58 | return ret; 59 | } 60 | if (DSTORE_SUCCESS != (ret = socket_.listen(1024))) { 61 | LOG_WARN("listen failed, ret=%d", ret); 62 | return ret; 63 | } 64 | e_->fd = socket_.get_listen_fd(); 65 | e_->type = Event::kEventRead; 66 | e_->args = nullptr; 67 | e_->read_cb = std::bind(&TCPListener::accept, this, _1, _2, _3); 68 | LOG_INFO("start listening, listen_fd=%d", e_->fd, e_->type); 69 | return ret; 70 | } 71 | 72 | int TCPListener::accept(int fd, int type, void *args) 73 | { 74 | int ret = DSTORE_SUCCESS; 75 | int new_fd = -1; 76 | InetAddr addr; 77 | if (DSTORE_SUCCESS != (ret = socket_.accept(&new_fd, &addr))) { 78 | LOG_WARN("accept failed, ret=%d", ret); 79 | return ret; 80 | } 81 | return on_connect_(new_fd, &addr); 82 | } 83 | -------------------------------------------------------------------------------- /dstore/network/tcp_listener.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_TCP_LISTENER_H_ 2 | #define DSTORE_NETWORK_TCP_LISTENER_H_ 3 | 4 | #include 5 | #include 6 | #include "socket.h" 7 | #include "event_loop.h" 8 | #include "inet_addr.h" 9 | 10 | namespace dstore 11 | { 12 | namespace network 13 | { 14 | class TCPListener 15 | { 16 | public: 17 | typedef std::function NewConnectionCallback; 18 | TCPListener(void); 19 | ~TCPListener(void); 20 | int set_addr(const char *host, const char *port, const bool is_ipv6); 21 | void set_new_connection_callback(NewConnectionCallback on_connect); 22 | std::shared_ptr get_listen_event(void); 23 | int start(void); 24 | int accept(int fd, int type, void *args); 25 | TCPListener &operator=(const TCPListener &) = delete; 26 | TCPListener(const TCPListener &) = delete; 27 | private: 28 | InetAddr addr_; 29 | ListenSocket socket_; 30 | std::shared_ptr e_; 31 | NewConnectionCallback on_connect_; 32 | }; 33 | } // end namespace network 34 | } // end namespace dstore 35 | #endif // DSTORE_NETWORK_TCP_LISTENER_H_ 36 | -------------------------------------------------------------------------------- /dstore/network/tcp_server.cpp: -------------------------------------------------------------------------------- 1 | #include "tcp_server.h" 2 | #include 3 | #include "errno_define.h" 4 | #include "log.h" 5 | #include "epoll.h" 6 | #include "connection.h" 7 | #include "buffer.h" 8 | 9 | using namespace std::placeholders; 10 | using namespace dstore::network; 11 | TCPServer::TCPServer(void) 12 | : loop_(), listener_(), connection_map_(), 13 | next_connection_id_(0) 14 | { 15 | } 16 | 17 | TCPServer::TCPServer(const char *host, const char *port, const bool is_ipv6) 18 | : host_(host), port_(port), is_ipv6_(is_ipv6), loop_(), listener_(), 19 | connection_map_(), next_connection_id_(0), message_decoder_(), 20 | new_message_callback_() 21 | { 22 | } 23 | 24 | TCPServer::~TCPServer(void) 25 | { 26 | } 27 | 28 | int TCPServer::start(void) 29 | { 30 | int ret = DSTORE_SUCCESS; 31 | if (DSTORE_SUCCESS != (ret = listener_.set_addr(host_.c_str(), port_.c_str(), is_ipv6_))) { 32 | LOG_ERROR("listener set addr failed, host=%s, port=%s, is_ipv6=%d, ret=%d", 33 | host_.c_str(), port_.c_str(), is_ipv6_, ret); 34 | return ret; 35 | } 36 | if (DSTORE_SUCCESS != (ret = listener_.start())) { 37 | LOG_ERROR("listener start failed, ret=%d", ret); 38 | return ret; 39 | } 40 | // init event loop 41 | EventPollAPI *poll_api = new EpollAPI(&loop_); 42 | if (DSTORE_SUCCESS != (ret = loop_.init(poll_api))) { 43 | LOG_ERROR("init event loop failed, ret=%d", ret); 44 | return ret; 45 | } 46 | // register the event 47 | std::shared_ptr e = listener_.get_listen_event(); 48 | if (DSTORE_SUCCESS != (ret = loop_.register_event(e))) { 49 | LOG_ERROR("register listen event failed, ret=%d", ret); 50 | return ret; 51 | } 52 | 53 | LOG_INFO("listener started"); 54 | // set connection callback 55 | listener_.set_new_connection_callback(std::bind(&TCPServer::on_connect, this, _1, _2)); 56 | return ret; 57 | } 58 | 59 | int TCPServer::loop(void) 60 | { 61 | int ret = DSTORE_SUCCESS; 62 | if (DSTORE_SUCCESS != (ret = loop_.loop())) { 63 | LOG_WARN("loop failed, ret=%d", ret); 64 | return ret; 65 | } 66 | return ret; 67 | } 68 | 69 | void TCPServer::set_message_decode_callback(const MessageDecodeCallback &message_decode) 70 | { 71 | message_decoder_ = message_decode; 72 | } 73 | 74 | void TCPServer::set_new_message_callback(const NewMessageCallback &new_message) 75 | { 76 | new_message_callback_ = new_message; 77 | } 78 | 79 | int TCPServer::on_connect(int fd, InetAddr *addr) 80 | { 81 | int ret = DSTORE_SUCCESS; 82 | std::shared_ptr e(new Event()); 83 | e->fd = fd; 84 | e->type = Event::kEventRead; 85 | e->args = reinterpret_cast(next_connection_id_); 86 | e->read_cb = std::bind(&TCPServer::on_read, this, _1, _2, _3); 87 | e->write_cb = std::bind(&TCPServer::on_write, this, _1, _2, _3); 88 | if (DSTORE_SUCCESS != (ret = loop_.register_event(e))) { 89 | LOG_WARN("register event failed, fd=%d, type=%d, ret=%d", e->fd, e->type, ret); 90 | return ret; 91 | } 92 | std::shared_ptr connection(new Connection(*addr, e)); 93 | connection_map_[next_connection_id_++] = connection; 94 | if (DSTORE_SUCCESS != (ret = connection->init(&loop_))) { 95 | LOG_WARN("init connection failed, ret=%d", ret); 96 | return ret; 97 | } 98 | return ret; 99 | } 100 | 101 | int TCPServer::on_read(int fd, int type, void *args) 102 | { 103 | int ret = DSTORE_SUCCESS; 104 | int64_t connection_id = reinterpret_cast(args); 105 | std::shared_ptr connection = connection_map_[connection_id]; 106 | Buffer &read_buffer = connection->get_read_buffer(); 107 | const int nbytes = 1024; 108 | ssize_t read_bytes = read_buffer.read_fd(fd, nbytes); 109 | if (0 == read_bytes) { 110 | if (DSTORE_SUCCESS != (ret = on_close(connection_id))) { 111 | LOG_WARN("handle close failed, ret=%d", ret); 112 | return ret; 113 | } 114 | return ret; 115 | } 116 | if (DSTORE_SUCCESS != (ret = message_decoder_(connection))) { 117 | LOG_WARN("decode message failed, ret=%d", ret); 118 | return ret; 119 | } 120 | return new_message_callback_(connection); 121 | } 122 | 123 | int TCPServer::on_write(int fd, int type, void *args) 124 | { 125 | int ret = DSTORE_SUCCESS; 126 | int64_t connection_id = reinterpret_cast(args); 127 | std::shared_ptr connection = connection_map_[connection_id]; 128 | Buffer &write_buffer = connection->get_write_buffer(); 129 | ssize_t write_bytes = write_buffer.write_fd(fd); 130 | if (-1 == write_bytes) { 131 | remove_connection(connection_id); 132 | return ret; 133 | } 134 | size_t need_write_bytes = write_buffer.get_need_write_bytes(); 135 | if (0 == need_write_bytes) { 136 | if (DSTORE_SUCCESS != (ret = connection->remove_write())) { 137 | LOG_WARN("remove write event from connection_id=%d failed, ret=%d", connection_id, ret); 138 | return ret; 139 | } 140 | if (Connection::SHUTDOWN_READ == connection->get_status()) { 141 | if (DSTORE_SUCCESS != (ret = on_close(connection_id))) { 142 | LOG_WARN("handle close connection failed, ret=%d", ret); 143 | return ret; 144 | } 145 | } 146 | } else if (need_write_bytes > 0) { 147 | // do not modify the event 148 | // just let it in loop again 149 | } 150 | return ret; 151 | } 152 | 153 | void TCPServer::remove_connection(const int64_t connection_id) 154 | { 155 | std::shared_ptr conn = connection_map_[connection_id]; 156 | conn->close(); 157 | connection_map_.erase(connection_id); 158 | } 159 | 160 | int TCPServer::set_connection_status(const int64_t connection_id, const Connection::Status status) 161 | { 162 | int ret = DSTORE_SUCCESS; 163 | std::shared_ptr conn = connection_map_[connection_id]; 164 | if (status == Connection::SHUTDOWN_READ) { 165 | if (DSTORE_SUCCESS != (ret = conn->remove_read())) { 166 | LOG_WARN("remove connection read event failed, ret=%d", ret); 167 | return ret; 168 | } 169 | conn->set_status(Connection::SHUTDOWN_READ); 170 | } else if (status == Connection::SHUTDOWN_WRITE) { 171 | if (DSTORE_SUCCESS != (ret = conn->remove_write())) { 172 | LOG_WARN("remove connection write event failed, ret=%d", ret); 173 | return ret; 174 | } 175 | conn->set_status(Connection::SHUTDOWN_WRITE); 176 | } 177 | return ret; 178 | } 179 | 180 | int TCPServer::on_close(const int64_t connection_id) 181 | { 182 | int ret = DSTORE_SUCCESS; 183 | std::shared_ptr conn = connection_map_[connection_id]; 184 | assert(conn != nullptr); 185 | bool has_data_to_write = conn->pending_write(); 186 | if (!has_data_to_write) { 187 | remove_connection(connection_id); 188 | } else { 189 | set_connection_status(connection_id, Connection::SHUTDOWN_READ); 190 | } 191 | return ret; 192 | } 193 | -------------------------------------------------------------------------------- /dstore/network/tcp_server.h: -------------------------------------------------------------------------------- 1 | #ifndef DSTORE_NETWORK_TCP_SERVER_H_ 2 | #define DSTORE_NETWORK_TCP_SERVER_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "tcp_listener.h" 9 | #include "event_loop.h" 10 | #include "connection.h" 11 | 12 | namespace dstore 13 | { 14 | namespace network 15 | { 16 | class TCPListener; 17 | class TCPServer; 18 | class EventLoop; 19 | class TCPServer 20 | { 21 | public: 22 | typedef std::function)> MessageDecodeCallback; 23 | typedef std::function)> NewMessageCallback; 24 | TCPServer(void); 25 | TCPServer(const char *host, const char *port, const bool is_ipv6); 26 | ~TCPServer(void); 27 | int start(void); 28 | int loop(void); 29 | void set_message_decode_callback(const MessageDecodeCallback &message_decode); 30 | void set_new_message_callback(const NewMessageCallback &new_message); 31 | TCPServer &operator=(const TCPServer &) = delete; 32 | TCPServer(const TCPServer &) = delete; 33 | private: 34 | int on_connect(int fd, InetAddr *addr); 35 | int on_read(int fd, int type, void *args); 36 | int on_write(int fd, int type, void *args); 37 | int on_close(const int64_t connection_id); 38 | void remove_connection(const int64_t connection_id); 39 | int set_connection_status(const int64_t connection_id, const Connection::Status status); 40 | private: 41 | std::string host_; 42 | std::string port_; 43 | bool is_ipv6_; 44 | EventLoop loop_; 45 | TCPListener listener_; 46 | std::unordered_map> connection_map_; 47 | int64_t next_connection_id_; 48 | MessageDecodeCallback message_decoder_; 49 | NewMessageCallback new_message_callback_; 50 | }; 51 | } // end namespace network 52 | } // end namespace dstore 53 | #endif // DSTORE_NETWORK_TCP_SERVER_H_ 54 | -------------------------------------------------------------------------------- /dstore/run_cpplint.sh: -------------------------------------------------------------------------------- 1 | ./cpplint.py $(find . -name *.h -or -name *.cpp | grep -vE "^./build/") 2>&1 | grep -v "should almost always be" | grep -v "Include the directory when naming" | grep -v "Do not use namespace using-directives" | grep -v "No copyright message found" 2 | -------------------------------------------------------------------------------- /dstore/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(TEST_SRCS 2 | simple_packet_server_test.cpp 3 | simple_packet_client_test.cpp 4 | timer_test.cpp 5 | PARENT_SCOPE 6 | ) 7 | -------------------------------------------------------------------------------- /dstore/test/simple_packet_client_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "tcp_client.h" 4 | #include "connection.h" 5 | #include "message.h" 6 | #include "errno_define.h" 7 | #include "log.h" 8 | 9 | using namespace dstore::common; 10 | using namespace dstore::network; 11 | 12 | struct simple_packet 13 | { 14 | int32_t data_len; 15 | char *data; 16 | }; 17 | 18 | int decode_message(std::shared_ptr conn) 19 | { 20 | int ret = DSTORE_SUCCESS; 21 | const size_t header_len = sizeof(int32_t); 22 | Buffer &read_buffer = conn->get_read_buffer(); 23 | size_t readable_bytes = read_buffer.get_read_bytes(); 24 | if (readable_bytes < header_len) { 25 | ret = DSTORE_EAGAIN; 26 | return ret; 27 | } 28 | LOG_INFO("header enough, readable_bytes = %d", readable_bytes); 29 | char *data = read_buffer.get_data(); 30 | int32_t data_len = read_buffer.peek_int32(); 31 | LOG_INFO("data len=%d\n", data_len); 32 | if (readable_bytes < data_len + header_len) { 33 | ret = DSTORE_EAGAIN; 34 | return ret; 35 | } 36 | simple_packet *packet = new simple_packet(); 37 | packet->data_len = data_len; 38 | packet->data = data + header_len; 39 | Message *message = new Message(static_cast(packet)); 40 | conn->add_message(message); 41 | read_buffer.consume(data_len + header_len); 42 | return ret; 43 | } 44 | 45 | int process_message(std::shared_ptr conn) 46 | { 47 | int ret = DSTORE_SUCCESS; 48 | LOG_INFO("start processing message"); 49 | std::list &message_list = conn->get_message_list(); 50 | for (auto iter = message_list.begin(); iter != message_list.end();) { 51 | LOG_INFO("start dealing with message"); 52 | simple_packet *packet = static_cast((*iter)->get_request()); 53 | LOG_INFO("data_len=%d, data=%s", packet->data_len, packet->data); 54 | delete packet; 55 | delete *iter; 56 | iter = message_list.erase(iter); 57 | } 58 | return ret; 59 | } 60 | 61 | int connected(std::shared_ptr conn) 62 | { 63 | int ret = DSTORE_SUCCESS; 64 | char buffer[1024]; 65 | snprintf(buffer, sizeof(buffer), "hello world from client"); 66 | Buffer &write_buffer = conn->get_write_buffer(); 67 | write_buffer.append_int32(static_cast(strlen(buffer))); 68 | write_buffer.append(buffer, strlen(buffer)); 69 | LOG_INFO("writetable_bytes=%d", write_buffer.get_need_write_bytes()); 70 | if (DSTORE_SUCCESS != (ret = conn->add_write())) { 71 | LOG_INFO("add write event failed, ret=%d", ret); 72 | return ret; 73 | } 74 | return ret; 75 | } 76 | 77 | int main(int argc, char **argv) 78 | { 79 | int ret = DSTORE_SUCCESS; 80 | TCPClient client; 81 | client.set_message_decode_callback(decode_message); 82 | client.set_new_message_callback(process_message); 83 | client.set_connect_complete_callback(connected); 84 | if (DSTORE_SUCCESS != (ret = client.start())) { 85 | LOG_WARN("client start failed, ret=%d", ret); 86 | return ret; 87 | } 88 | if (DSTORE_SUCCESS != (ret = client.connect("127.0.0.1", "8900", false))) { 89 | LOG_WARN("connect failed, ret=%d", ret); 90 | return ret; 91 | } 92 | 93 | if (DSTORE_SUCCESS != (ret = client.loop())) { 94 | LOG_WARN("loop failed, ret=%d", ret); 95 | return ret; 96 | } 97 | return ret; 98 | } 99 | -------------------------------------------------------------------------------- /dstore/test/simple_packet_server_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "tcp_server.h" 5 | #include "connection.h" 6 | #include "message.h" 7 | #include "errno_define.h" 8 | #include "log.h" 9 | 10 | using namespace dstore::common; 11 | using namespace dstore::network; 12 | 13 | struct simple_packet 14 | { 15 | int32_t data_len; 16 | char *data; 17 | }; 18 | 19 | int decode_message(std::shared_ptr conn) 20 | { 21 | int ret = DSTORE_SUCCESS; 22 | const size_t header_len = sizeof(int32_t); 23 | Buffer &read_buffer = conn->get_read_buffer(); 24 | size_t readable_bytes = read_buffer.get_read_bytes(); 25 | if (readable_bytes < header_len) { 26 | ret = DSTORE_EAGAIN; 27 | return ret; 28 | } 29 | LOG_INFO("header enough, readable_bytes = %d", readable_bytes); 30 | char *data = read_buffer.get_data(); 31 | int32_t data_len = read_buffer.peek_int32(); 32 | LOG_INFO("data len=%d\n", data_len); 33 | if (readable_bytes < data_len + header_len) { 34 | ret = DSTORE_EAGAIN; 35 | return ret; 36 | } 37 | simple_packet *packet = new simple_packet(); 38 | packet->data_len = data_len; 39 | packet->data = data + header_len; 40 | Message *message = new Message(static_cast(packet)); 41 | conn->add_message(message); 42 | read_buffer.consume(data_len + header_len); 43 | return ret; 44 | } 45 | 46 | int process_message(std::shared_ptr conn) 47 | { 48 | int ret = DSTORE_SUCCESS; 49 | LOG_INFO("start processing message"); 50 | std::list &message_list = conn->get_message_list(); 51 | for (auto iter = message_list.begin(); iter != message_list.end();) { 52 | LOG_INFO("start dealing with message"); 53 | simple_packet *packet = static_cast((*iter)->get_request()); 54 | LOG_INFO("message from client, len=%d, data=%s", packet->data_len, packet->data); 55 | char buffer[1024]; 56 | snprintf(buffer, sizeof(buffer), "hello world"); 57 | Buffer &write_buffer = conn->get_write_buffer(); 58 | write_buffer.append_int32(static_cast(strlen(buffer))); 59 | write_buffer.append(buffer, strlen(buffer)); 60 | LOG_INFO("writetable_bytes=%d", write_buffer.get_need_write_bytes()); 61 | if (DSTORE_SUCCESS != (ret = conn->add_write())) { 62 | LOG_INFO("add write event failed, ret=%d", ret); 63 | return ret; 64 | } 65 | delete packet; 66 | delete *iter; 67 | iter = message_list.erase(iter); 68 | } 69 | return ret; 70 | } 71 | 72 | int main(int argc, char **argv) 73 | { 74 | TCPServer server("0.0.0.0", "8900", false); 75 | server.set_message_decode_callback(decode_message); 76 | server.set_new_message_callback(process_message); 77 | int ret = DSTORE_SUCCESS; 78 | if (DSTORE_SUCCESS != (ret = server.start())) { 79 | LOG_ERROR("can not start server, ret=%d", ret); 80 | return ret; 81 | } 82 | if (DSTORE_SUCCESS != (ret = server.loop())) { 83 | LOG_ERROR("loop failed, ret=%d", ret); 84 | return ret; 85 | } 86 | return ret; 87 | } 88 | -------------------------------------------------------------------------------- /dstore/test/timer_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "event_loop.h" 4 | #include "epoll.h" 5 | #include "time_operator.h" 6 | #include "log.h" 7 | #include "errno_define.h" 8 | 9 | using namespace dstore::common; 10 | using namespace dstore::network; 11 | using namespace std::placeholders; 12 | 13 | int timer_func(int fd, int type, void *args) 14 | { 15 | int ret = DSTORE_SUCCESS; 16 | LOG_INFO("time %d: now %ld", fd, get_milliseconds()); 17 | return ret; 18 | } 19 | 20 | int main(void) 21 | { 22 | int ret = DSTORE_SUCCESS; 23 | EventLoop loop; 24 | EventPollAPI *poll_api = new EpollAPI(&loop); 25 | if (DSTORE_SUCCESS != (ret = loop.init(poll_api))) { 26 | LOG_WARN("init event loop failed"); 27 | return ret; 28 | } 29 | 30 | std::vector> events; 31 | for (int i = 0; i < 100; i++) { 32 | std::shared_ptr e(new Event()); 33 | e->type = Event::kEventTimer; 34 | e->timeout = 1000 * (i+1); 35 | e->timer_cb = std::bind(timer_func, _1, _2, _3); 36 | e->fd = i; 37 | LOG_INFO("try registering timer=%ld, timeout=%ld", i, e->timeout); 38 | if (DSTORE_SUCCESS != (ret = loop.register_event(e))) { 39 | LOG_WARN("register event failed, i = %d", i); 40 | return ret; 41 | } 42 | events.push_back(e); 43 | } 44 | for (int i = 10; i < 100; i += 10) { 45 | LOG_INFO("modify timer to 10S"); 46 | events[i]->timeout = 100 * 1000; 47 | if (DSTORE_SUCCESS != (ret = loop.modify_event(events[i]))) { 48 | LOG_WARN("modify event failed, ret=%d", ret); 49 | return ret; 50 | } 51 | } 52 | for (int i = 9; i < 100; i += 9) { 53 | LOG_INFO("unregistering timers"); 54 | std::shared_ptr e = events[i]; 55 | if (DSTORE_SUCCESS != (ret = loop.unregister_event(e))) { 56 | LOG_WARN("unregister event failed"); 57 | return ret; 58 | } 59 | } 60 | if (DSTORE_SUCCESS != (ret = loop.loop())) { 61 | LOG_WARN("loop failed, ret=%d", ret); 62 | return ret; 63 | } 64 | return ret; 65 | } 66 | --------------------------------------------------------------------------------