vendor_base.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "vendor_base.h"
  2. #include "vendor_global.h"
  3. #include <errno.h>
  4. #include <pthread.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <utils/logger.h>
  8. namespace vendor {
  9. LongOpts::LongOpts(const MyOpt opts[], const int size)
  10. {
  11. myOpt = opts;
  12. sizeOpt = size;
  13. }
  14. void LongOpts::GetOptions(option *opts, std::string &shorts)
  15. {
  16. for (int i = 0; i < sizeOpt; i++) {
  17. // printf("val: %d(%c) ", myOpt[i].val, myOpt[i].val);
  18. opts[i].name = strdup(myOpt[i].name);
  19. opts[i].has_arg = myOpt[i].has_arg;
  20. opts[i].flag = NULL;
  21. opts[i].val = myOpt[i].val;
  22. if (myOpt[i].val == 0) {
  23. continue;
  24. }
  25. if (myOpt[i].has_arg == no_argument) {
  26. shorts.push_back(static_cast<char>(myOpt[i].val));
  27. } else if (myOpt[i].has_arg == required_argument) {
  28. shorts.push_back(static_cast<char>(myOpt[i].val));
  29. shorts.push_back(':');
  30. } else {
  31. shorts.push_back(static_cast<char>(myOpt[i].val));
  32. }
  33. }
  34. }
  35. MyOpt LongOpts::GetOption(char ch, int index)
  36. {
  37. if (ch == 0) {
  38. return myOpt[index];
  39. }
  40. for (int i = 0; i < sizeOpt; i++) {
  41. if (myOpt[i].val == ch) {
  42. return myOpt[i];
  43. }
  44. }
  45. return myOpt[0];
  46. }
  47. VendorBase::VendorBase()
  48. {
  49. HTELINK_LOG_ENABLE(true);
  50. uvLoop_ = uv_default_loop();
  51. if (uvLoop_ == nullptr) {
  52. HTELINK_LOG_ERR("event new failed, %s", strerror(errno));
  53. }
  54. }
  55. VendorBase::~VendorBase()
  56. {
  57. if (uvLoop_ == nullptr) {
  58. return;
  59. }
  60. Exit(SIGINT);
  61. for (auto sig : uvSignals_) {
  62. uv_close(reinterpret_cast<uv_handle_t *>(sig), nullptr);
  63. delete sig;
  64. }
  65. uvSignals_.clear();
  66. uv_loop_close(uvLoop_);
  67. }
  68. void VendorBase::ListenSignal(int signal)
  69. {
  70. uv_signal_t *uvSigal = new uv_signal_t;
  71. uv_signal_init(uvLoop_, uvSigal);
  72. uvSigal->data = this;
  73. uv_signal_start(uvSigal, VendorBase::SignalHandler, signal);
  74. uvSignals_.push_back(uvSigal);
  75. }
  76. int VendorBase::Exec()
  77. {
  78. HTELINK_LOG_INFO("register event...");
  79. // 注册信号事件
  80. ListenSignal(SIGSEGV);
  81. ListenSignal(SIGFPE);
  82. ListenSignal(SIGINT);
  83. ListenSignal(SIGTERM);
  84. ListenSignal(SIGTSTP);
  85. signal(SIGPIPE, SIG_IGN);
  86. uv_work_t workReq;
  87. workReq.data = this;
  88. uv_queue_work(
  89. uvLoop_, &workReq,
  90. [](uv_work_t *req) {
  91. VendorBase *vb = reinterpret_cast<VendorBase *>(req->data);
  92. HTELINK_LOG_INFO("start to Run");
  93. vb->Run();
  94. },
  95. [](uv_work_t *req, int status) {});
  96. HTELINK_LOG_INFO("start to deamon...");
  97. return uv_run(uvLoop_, UV_RUN_DEFAULT);
  98. }
  99. void VendorBase::Exit(int signal)
  100. {
  101. Stop(signal);
  102. if (uvLoop_ == nullptr) {
  103. return;
  104. }
  105. uv_stop(uvLoop_);
  106. }
  107. uv_handle_t *VendorBase::AppendEvent(int fd, int ev, VendorIf *vif)
  108. {
  109. uv_async_t *async = new uv_async_t;
  110. uv_async_cb asyncCb = [](uv_async_t *handle) {
  111. VendorIf *vif = static_cast<VendorIf *>(handle->data);
  112. if (vif) {
  113. vif->OnEvent(nullptr);
  114. }
  115. };
  116. uv_async_init(uvLoop_, async, asyncCb);
  117. async->data = vif;
  118. uv_async_send(async);
  119. return (uv_handle_t *)async;
  120. }
  121. uv_handle_t *VendorBase::AppendTimerEvent(int millionsecs, VendorIf *vif)
  122. {
  123. uv_timer_t *timerReq = new uv_timer_t;
  124. uv_timer_init(uvLoop_, timerReq);
  125. timerReq->data = vif;
  126. uv_timer_cb timerCb = [](uv_timer_t *handle) {
  127. VendorIf *vif = static_cast<VendorIf *>(handle->data);
  128. if (vif) {
  129. vif->OnTimer(nullptr);
  130. }
  131. };
  132. uv_timer_start(timerReq, timerCb, millionsecs, 1);
  133. return (uv_handle_t *)(timerReq);
  134. }
  135. void VendorBase::StopEvent(uv_handle_t *handle)
  136. {
  137. if (handle == nullptr) {
  138. return;
  139. }
  140. if (handle->type == UV_TIMER) {
  141. uv_timer_stop((uv_timer_t *)handle);
  142. } else {
  143. uv_close(handle, nullptr);
  144. }
  145. delete handle;
  146. }
  147. void VendorBase::SignalHandler(uv_signal_t *handle, int signum)
  148. {
  149. void *l_buffer[1024];
  150. char **l_ptrace;
  151. int signal = signum;
  152. VendorBase *vendorBase = static_cast<VendorBase *>(handle->data);
  153. if (vendorBase) {
  154. vendorBase->Exit(signal);
  155. }
  156. HTELINK_LOG_ERR("\r\n=========>>>catch signal %d, pid: %ld <<<=========", signal, getpid());
  157. HTELINK_LOG_ERR("Dump stack start...");
  158. int size = backtrace(l_buffer, 1024);
  159. l_ptrace = backtrace_symbols(l_buffer, size);
  160. if (NULL == l_ptrace) {
  161. HTELINK_LOG_ERR("backtrace_symbols");
  162. exit(signal);
  163. }
  164. for (int i = 0; i < size; i++) {
  165. HTELINK_LOG_ERR(" [%02d] %s", i, l_ptrace[i]);
  166. }
  167. HTELINK_LOG_ERR("Dump stack end...");
  168. free(l_ptrace);
  169. abort();
  170. }
  171. int32_t VendorBase::ParseOptionArgs(VendorBase *vendor, int argc, char *argv[])
  172. {
  173. int longindex = 0;
  174. std::string shorts;
  175. struct option longopts[vendor->GetOptSize()];
  176. LongOpts longOpts(vendor->GetOpts(), vendor->GetOptSize());
  177. longOpts.GetOptions(longopts, shorts);
  178. int ch;
  179. while ((ch = getopt_long(argc, argv, shorts.c_str(), longopts, &longindex)) != -1) {
  180. MyOpt myOpt = longOpts.GetOption(ch, longindex);
  181. printf("longindex :%d, opt %d(%c), name:%s , optarg :%s\n", longindex, myOpt.val, myOpt.val, myOpt.name,
  182. optarg == nullptr ? "nullptr" : optarg);
  183. if (strcmp(myOpt.name, "help") == 0) {
  184. MyOpt *myOpts = vendor->GetOpts();
  185. printf("Usage: %s [OPTION] -l URL\n\nOPTIONS:\n", vendor->Name().c_str());
  186. for (int i = 1; i < vendor->GetOptSize(); i++) {
  187. if (myOpts[i].val == 0) {
  188. printf("--%s: %s\n", myOpts[i].name, myOpts[i].desc);
  189. } else {
  190. printf("-%c, --%s: %s\n", myOpts[i].val, myOpts[i].name, myOpts[i].desc);
  191. }
  192. }
  193. return -1;
  194. }
  195. if (vendor->ParseCmdline(myOpt.name, optarg == nullptr ? "" : optarg) != 0) {
  196. printf("parse failed!!!\n");
  197. return -1;
  198. }
  199. }
  200. return 0;
  201. }
  202. }; // namespace vendor