|
@@ -1,12 +1,13 @@
|
|
|
#include "vendor_base.h"
|
|
|
#include "vendor_global.h"
|
|
|
-#include <event.h>
|
|
|
+#include <errno.h>
|
|
|
+#include <pthread.h>
|
|
|
#include <signal.h>
|
|
|
+#include <string.h>
|
|
|
+#include <utils/logger.h>
|
|
|
|
|
|
namespace vendor {
|
|
|
|
|
|
-struct event_base *eventBase_ = nullptr;
|
|
|
-
|
|
|
LongOpts::LongOpts(const MyOpt opts[], const int size)
|
|
|
{
|
|
|
myOpt = opts;
|
|
@@ -52,90 +53,132 @@ MyOpt LongOpts::GetOption(char ch, int index)
|
|
|
VendorBase::VendorBase()
|
|
|
{
|
|
|
HTELINK_LOG_ENABLE(true);
|
|
|
- eventBase_ = event_base_new();
|
|
|
- if (eventBase_ == nullptr) {
|
|
|
+ uvLoop_ = uv_default_loop();
|
|
|
+ if (uvLoop_ == nullptr) {
|
|
|
HTELINK_LOG_ERR("event new failed, %s", strerror(errno));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
VendorBase::~VendorBase()
|
|
|
{
|
|
|
- if (eventBase_ == nullptr) {
|
|
|
+ if (uvLoop_ == nullptr) {
|
|
|
return;
|
|
|
}
|
|
|
Exit(SIGINT);
|
|
|
- event_base_free(eventBase_);
|
|
|
+ for (auto sig : uvSignals_) {
|
|
|
+ uv_close(reinterpret_cast<uv_handle_t *>(sig), nullptr);
|
|
|
+ delete sig;
|
|
|
+ }
|
|
|
+ uvSignals_.clear();
|
|
|
+ uv_loop_close(uvLoop_);
|
|
|
+}
|
|
|
+
|
|
|
+void VendorBase::ListenSignal(int signal)
|
|
|
+{
|
|
|
+ uv_signal_t *uvSigal = new uv_signal_t;
|
|
|
+ uv_signal_init(uvLoop_, uvSigal);
|
|
|
+ uvSigal->data = this;
|
|
|
+ uv_signal_start(uvSigal, VendorBase::SignalHandler, signal);
|
|
|
+ uvSignals_.push_back(uvSigal);
|
|
|
}
|
|
|
|
|
|
int VendorBase::Exec()
|
|
|
{
|
|
|
HTELINK_LOG_INFO("register event...");
|
|
|
// 注册信号事件
|
|
|
- event_add(evsignal_new(eventBase_, SIGSEGV, VendorBase::SignalHandler, this), NULL);
|
|
|
- event_add(evsignal_new(eventBase_, SIGFPE, VendorBase::SignalHandler, this), NULL);
|
|
|
- event_add(evsignal_new(eventBase_, SIGINT, VendorBase::SignalHandler, this), NULL);
|
|
|
- event_add(evsignal_new(eventBase_, SIGTERM, VendorBase::SignalHandler, this), NULL);
|
|
|
- event_add(evsignal_new(eventBase_, SIGTSTP, VendorBase::SignalHandler, this), NULL);
|
|
|
-
|
|
|
+ ListenSignal(SIGSEGV);
|
|
|
+ ListenSignal(SIGFPE);
|
|
|
+ ListenSignal(SIGINT);
|
|
|
+ ListenSignal(SIGTERM);
|
|
|
+ ListenSignal(SIGTSTP);
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
- HTELINK_LOG_INFO("start to Run");
|
|
|
- // struct event *timer_ev = event_new(
|
|
|
- // eventBase_, -1, EV_PERSIST,
|
|
|
- // [](evutil_socket_t ev, short e, void *arg) {
|
|
|
- // VendorBase *vendorBase = static_cast<VendorBase *>(arg);
|
|
|
- // vendorBase->Run();
|
|
|
- // },
|
|
|
- // this);
|
|
|
- // event_add(timer_ev, NULL);
|
|
|
- std::thread fn([this] {
|
|
|
- Run();
|
|
|
- });
|
|
|
- fn.detach();
|
|
|
- // 其他业务
|
|
|
+ uv_work_t workReq;
|
|
|
+ workReq.data = this;
|
|
|
+ uv_queue_work(
|
|
|
+ uvLoop_, &workReq,
|
|
|
+ [](uv_work_t *req) {
|
|
|
+ VendorBase *vb = reinterpret_cast<VendorBase *>(req->data);
|
|
|
+ HTELINK_LOG_INFO("start to Run");
|
|
|
+ vb->Run();
|
|
|
+ },
|
|
|
+ [](uv_work_t *req, int status) {});
|
|
|
HTELINK_LOG_INFO("start to deamon...");
|
|
|
- return event_base_dispatch(eventBase_);
|
|
|
+ return uv_run(uvLoop_, UV_RUN_DEFAULT);
|
|
|
}
|
|
|
|
|
|
void VendorBase::Exit(int signal)
|
|
|
{
|
|
|
Stop(signal);
|
|
|
- if (eventBase_ == nullptr) {
|
|
|
+ if (uvLoop_ == nullptr) {
|
|
|
return;
|
|
|
}
|
|
|
- char dumpFile[VENDOR_MAX_PATH_SIZE];
|
|
|
- sprintf(dumpFile, "%s/event_stat_%s.txt", VENDOR_LOG_PATH.c_str(), utils::nowtostr().c_str());
|
|
|
- FILE *fp = fopen(dumpFile, "a");
|
|
|
- event_base_dump_events(eventBase_, fp);
|
|
|
- fclose(fp);
|
|
|
- event_base_loopbreak(eventBase_);
|
|
|
+ uv_stop(uvLoop_);
|
|
|
}
|
|
|
|
|
|
-void VendorBase::SignalHandler(int fd, short event, void *arg)
|
|
|
+uv_handle_t *VendorBase::AppendEvent(int fd, int ev, VendorIf *vif)
|
|
|
{
|
|
|
- if (event != EV_SIGNAL) {
|
|
|
- HTELINK_LOG_ERR("fd: %d, event: %d occur", fd, event);
|
|
|
+ uv_async_t *async = new uv_async_t;
|
|
|
+ uv_async_cb asyncCb = [](uv_async_t *handle) {
|
|
|
+ VendorIf *vif = static_cast<VendorIf *>(handle->data);
|
|
|
+ if (vif) {
|
|
|
+ vif->OnEvent(nullptr);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ uv_async_init(uvLoop_, async, asyncCb);
|
|
|
+ async->data = vif;
|
|
|
+ uv_async_send(async);
|
|
|
+ return (uv_handle_t *)async;
|
|
|
+}
|
|
|
+
|
|
|
+uv_handle_t *VendorBase::AppendTimerEvent(int millionsecs, VendorIf *vif)
|
|
|
+{
|
|
|
+ uv_timer_t *timerReq = new uv_timer_t;
|
|
|
+ uv_timer_init(uvLoop_, timerReq);
|
|
|
+ timerReq->data = vif;
|
|
|
+ uv_timer_cb timerCb = [](uv_timer_t *handle) {
|
|
|
+ VendorIf *vif = static_cast<VendorIf *>(handle->data);
|
|
|
+ if (vif) {
|
|
|
+ vif->OnTimer(nullptr);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ uv_timer_start(timerReq, timerCb, millionsecs, 1);
|
|
|
+ return (uv_handle_t *)(timerReq);
|
|
|
+}
|
|
|
+
|
|
|
+void VendorBase::StopEvent(uv_handle_t *handle)
|
|
|
+{
|
|
|
+ if (handle == nullptr) {
|
|
|
return;
|
|
|
}
|
|
|
+ if (handle->type == UV_TIMER) {
|
|
|
+ uv_timer_stop((uv_timer_t *)handle);
|
|
|
+ } else {
|
|
|
+ uv_close(handle, nullptr);
|
|
|
+ }
|
|
|
+ delete handle;
|
|
|
+}
|
|
|
|
|
|
- void *l_buffer[512];
|
|
|
+void VendorBase::SignalHandler(uv_signal_t *handle, int signum)
|
|
|
+{
|
|
|
+ void *l_buffer[1024];
|
|
|
char **l_ptrace;
|
|
|
|
|
|
- int signal = fd;
|
|
|
+ int signal = signum;
|
|
|
|
|
|
- VendorBase *vendorBase = static_cast<VendorBase *>(arg);
|
|
|
+ VendorBase *vendorBase = static_cast<VendorBase *>(handle->data);
|
|
|
if (vendorBase) {
|
|
|
vendorBase->Exit(signal);
|
|
|
}
|
|
|
|
|
|
- HTELINK_LOG_ERR("\r\n=========>>>catch signal %d <<<=========", signal);
|
|
|
+ HTELINK_LOG_ERR("\r\n=========>>>catch signal %d, pid: %ld <<<=========", signal, getpid());
|
|
|
HTELINK_LOG_ERR("Dump stack start...");
|
|
|
|
|
|
- int size = backtrace(l_buffer, 512);
|
|
|
+ int size = backtrace(l_buffer, 1024);
|
|
|
l_ptrace = backtrace_symbols(l_buffer, size);
|
|
|
if (NULL == l_ptrace) {
|
|
|
HTELINK_LOG_ERR("backtrace_symbols");
|
|
|
- exit(1);
|
|
|
+ exit(signal);
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
@@ -143,7 +186,7 @@ void VendorBase::SignalHandler(int fd, short event, void *arg)
|
|
|
}
|
|
|
HTELINK_LOG_ERR("Dump stack end...");
|
|
|
free(l_ptrace);
|
|
|
- exit(signal);
|
|
|
+ abort();
|
|
|
}
|
|
|
|
|
|
int32_t VendorBase::ParseOptionArgs(VendorBase *vendor, int argc, char *argv[])
|