#pragma once #include #include #include #include "easylogging++.h" #include "util.h" extern "C" { #include #include #include }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// 对关键的Libevent资源实行智能指针的封装 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct EventDeleter { void operator()(struct evbuffer *evbuf) { evbuffer_free(evbuf); } }; struct FileDescriptorDeleter { void operator()(int fd) { close(fd); } void operator()(FILE *fp) { fclose(fp); } }; using evbuffer_unique_ptr_t = std::unique_ptr; using file_unique_ptr_t = std::unique_ptr; using fd_unique_ptr_t = std::unique_ptr; /* Bufferevent托管类 * Bufferevent指针用于构造本Class后,即失去对指针的所有权,指针的释放由本类的析构函数完成 */ class BufferEvent { public: explicit BufferEvent(struct bufferevent *bev) { bev_.reset(bev); } using data_cb_t = std::function; using event_cb_t = std::function; using exception_cb_t = std::function; void SetReadCallback(data_cb_t cb) { read_cb_ = cb; } void SetWriteCallback(data_cb_t cb) { write_cb_ = cb; } void SetEventCallback(event_cb_t cb) { event_cb_ = cb; } void SetExceptionCallback(exception_cb_t cb) { exception_cb_ = cb; } void Enable(short what) { bufferevent_enable(bev_.get(), what); } void Disable(short what) { bufferevent_disable(bev_.get(), what); } struct bufferevent *Raw() { return bev_.get(); } private: data_cb_t read_cb_; data_cb_t write_cb_; event_cb_t event_cb_; exception_cb_t exception_cb_; struct __bufferevent_deleter { void operator()(struct bufferevent *bev) { bufferevent_free(bev); } }; std::unique_ptr bev_; private: static int __wrapper_cb_read(struct bufferevent *bev, void *user) { auto *__this = static_cast(user); std::exception_ptr exp_ptr; try { return __this->read_cb_(bev); } catch (const std::exception & e) { exp_ptr = std::current_exception(); } if(__this->exception_cb_ == nullptr) { std::rethrow_exception(exp_ptr); } if (__this->exception_cb_ != nullptr && __this->exception_cb_(exp_ptr) != 0) { std::rethrow_exception(exp_ptr); } return 0; } static int __wrapper_cb_write(struct bufferevent *bev, void *user) { auto *__this = static_cast(user); std::exception_ptr exp_ptr; try { return __this->write_cb_(bev); } catch (const std::exception & e) { exp_ptr = std::current_exception(); } if(__this->exception_cb_ == nullptr) { std::rethrow_exception(exp_ptr); } if (__this->exception_cb_ != nullptr && __this->exception_cb_(exp_ptr) != 0) { std::rethrow_exception(exp_ptr); } return 0; } static int __wrapper_cb_event(struct bufferevent *bev, short what, void *user) { auto *__this = static_cast(user); std::exception_ptr exp_ptr; try { return __this->event_cb_(bev, what); } catch (const std::exception & e) { exp_ptr = std::current_exception(); } if(__this->exception_cb_ == nullptr) { std::rethrow_exception(exp_ptr); } if (__this->exception_cb_ != nullptr && __this->exception_cb_(exp_ptr) != 0) { std::rethrow_exception(exp_ptr); } return 0; } }; /* Event托管类 * Bufferevent指针用于构造本Class后,即失去对指针的所有权,指针的释放由本类的析构函数完成 */ class Event { public: explicit Event() = default; virtual ~Event() = default; using event_cb_t = std::function; void Init(struct event_base * ev_base, evutil_socket_t fd, short en_event, event_cb_t cb) { ev_.reset(event_new(ev_base, fd, en_event, __wrapper_event, this)); if (ev_ == nullptr) throw std::runtime_error("Failed at create event for fd " + fd); int ret = event_add(ev_.get(), nullptr); if (ret < 0) throw std::runtime_error("Failed at event_add() for fd " + fd); event_cb_ = cb; } private: event_cb_t event_cb_; struct __event_deleter { void operator()(struct event *ev) { if (ev != nullptr) event_free(ev); } }; std::unique_ptr ev_; private: static void __wrapper_event(evutil_socket_t fd, short what, void * user) { auto *__this = static_cast(user); return __this->event_cb_(fd, what); } }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Stream.h内的数据结构转换工具 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include struct SappIpAddrDeleter { void operator()(struct ipaddr *ptr_addr) { if (ptr_addr->addrtype == ADDR_TYPE_IPV4) delete ptr_addr->v4; if (ptr_addr->addrtype == ADDR_TYPE_IPV6) delete ptr_addr->v6; delete ptr_addr; } }; using sapp_ip_addr_ptr_t = std::unique_ptr; sapp_ip_addr_ptr_t sockaddr_to_sapp_ipaddr(const struct sockaddr *sk_addr_src, const struct sockaddr *sk_addr_dst); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// sslsplit logger to easylogger++ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include "easylogging++.h" #include "util.h" #define log_dbg_printf(fmt, ...) do { LOG(DEBUG) << string_format(fmt, ##__VA_ARGS__); } while(0) #define log_err_printf(fmt, ...) do { LOG(ERROR) << string_format(fmt, ##__VA_ARGS__); } while(0) #define log_dbg_print_free(str) do { LOG(DEBUG) << std::string(str); free(str); } while(0)