// Load the build values from the makefile.inc source file void load_a_makefile(stub_values *sv, char *fn) { FILE *f = fopen(fn, "rb"); if (f == NULL) return; char line[500]; char nm[100]; char val[100]; char *s; while (read_line(f,line)) { s = strstr(line, "="); if (s != 0) { char *c = strstr(line, "#"); if ((c == 0) || (c > s)) { if (c) *c = 0; s = get_str(line,nm); get_str(s+1,val); add_sig(nm, val, &sv->makevals, 0); } } } fclose(f); }
// Load a specified file. // If 'exclude_comments' is non-zero then commented lines are ignored. // Add new entries the list pointer to by 'hdr' static void load_stubs_file(char *name, int exclude_comments, osig **hdr) { FILE *f = fopen(name, "rb"); if (f == NULL) return; char line[500]; char nm[100]; char val[100]; char *s; while (read_line(f,line)) { int typ = TYPE_NHSTUB; int off = 7; s = strstr(line, "NHSTUB("); if (s == 0) { off = 6; s = strstr(line, "NSTUB("); } if (s == 0) { off = 4; s = strstr(line, "DEF("); typ = TYPE_DEF; } if (s != 0) { char *c = strstr(line, "//"); if ((exclude_comments == 0) || (c == 0) || (c > s)) { s = get_str(s+off,nm); get_str(s,val); osig *p = add_sig(nm, val, hdr, ((c != 0) && (c <= s)) ? 1 : 0); p->type = typ; continue; } } } }
http_client scheduler::get_http_client(std::function<void(int, int, std::map<std::string, std::string>&, char*, size_t)> f) { auto sch_impl = std::dynamic_pointer_cast<scheduler_impl>(m_impl); auto& impl = sch_impl->m_util_impls[HTTP_CLI]; if (!impl) { auto http_impl = std::make_shared<http_impl_t<tcp_client_impl>>(NORM_TRANS); http_impl->m_util.m_sch = this; http_impl->m_util.m_app_prt = PRT_HTTP; http_impl->m_util.m_protocol_hook = [this](int fd, char* data, size_t len) { auto this_sch_impl = std::dynamic_pointer_cast<scheduler_impl>(m_impl); if (fd < 0 || fd >= this_sch_impl->m_ev_array.size() || !this_sch_impl->m_ev_array[fd]) return -(int)len; auto conn = std::dynamic_pointer_cast<http_conn_t<tcp_client_conn>>(this_sch_impl->m_ev_array[fd]); return http_parser(true, conn, data, len); }; http_impl->m_util.m_f = [this](int fd, const std::string& ip_addr, uint16_t port, char *data, size_t len) { auto this_sch_impl = std::dynamic_pointer_cast<scheduler_impl>(m_impl); auto& this_http_cli = this_sch_impl->m_util_impls[HTTP_CLI]; auto this_http_impl = std::dynamic_pointer_cast<http_impl_t<tcp_client_impl>>(this_http_cli); tcp_callback_for_http<std::shared_ptr<http_impl_t<tcp_client_impl>>, tcp_client_conn>( true, this_http_impl, fd, data, len); }; http_impl->m_http_cli = std::move(f); //保存回调函数 impl = http_impl; auto ctl = get_sigctl(); //基于tcp_client::connect接口的连接操作在遇到名字解析时将发生协程的切换操作 ctl.add_sig(SIGRTMIN+14, std::bind(&http_impl_t<tcp_client_impl>::name_resolve_callback, http_impl.get(), _1, _2)); } http_client obj; obj.m_impl = impl; return obj; }
// Load a specified file. // Add new entries the list pointer to by 'hdr' void load_funcs(stub_values *sv, char *name) { FILE *f = fopen(name, "rb"); if (f == NULL) return; char line[500]; char nm[100]; char val[100]; char *s; while (read_line(f,line)) { s = get_str(line,val); get_str(s,nm); osig *p = add_sig(nm, val, &sv->stubs, 1); p->type = TYPE_NHSTUB; continue; } }
// Load a specified file. // If 'exclude_comments' is non-zero then commented lines are ignored. // Add new entries the list pointer to by 'hdr' static void load_stubs_file(char *name, int exclude_comments, osig **hdr) { FILE *f = fopen(name, "rb"); if (f == NULL) return; char line[500]; char nm[100]; char val[100]; char *s; while (read_line(f,line)) { int typ = TYPE_NHSTUB; int off = 7; s = strstr(line, "NHSTUB("); if (s == 0) { off = 8; s = strstr(line, "NHSTUB2("); } // note may want to flag dif from NHSTUB if (s == 0) { off = 7; s = strstr(line, "IGNORE("); typ = TYPE_IGNORE; } if (s == 0) { off = 6; s = strstr(line, "NSTUB("); } if (s == 0) { off = 4; s = strstr(line, "DEF("); typ = TYPE_DEF; } if (s == 0) { off = 10; s = strstr(line, "DEF_CONST("); typ = TYPE_CONST; } if (s != 0) { char *c = strstr(line, "//"); if ((exclude_comments == 0) || (c == 0) || (c > s)) { s = get_str(s+off,nm); if (typ != TYPE_IGNORE) get_str(s,val); else val[0] = 0; osig *p = add_sig(nm, val, hdr, ((c != 0) && (c <= s)) ? 1 : 0); p->type = typ; continue; } } } fclose(f); }
// Load the MODEMAP from the shooting.c source file void load_modemap(stub_values *sv) { FILE *f = fopen("../../shooting.c", "rb"); if (f == NULL) return; char line[500]; char nm[100]; char val[12]; int found_modemap = 0; char *s; while (read_line(f,line)) { if (found_modemap) { s = strstr(line, "};"); if (s != 0) return; s = strstr(line, "MODE_"); if (s != 0) { char *c = strstr(line, "//"); if ((c == 0) || (c > s)) { s = get_str(s,nm); get_str(s,val); add_sig(nm, val, &sv->modemap, 0); } } } else { s = strstr(line, "modemap["); if (s != 0) found_modemap = 1; } } }
int main(int argc, char** argv) { if (3 != argc) { log_info("usage: %s ip port \n", argv[0]); return -1; } const char* _ip = argv[1]; const int _port = atoi(argv[2]); struct sockaddr_in _server; _server.sin_family = AF_INET; _server.sin_port = htons(_port); inet_pton(AF_INET, _ip, &_server.sin_addr); int _lisfd = socket(AF_INET, SOCK_STREAM, 0); assert(-1 != _lisfd); socklen_t _server_len = sizeof(_server); int ret = bind(_lisfd, (struct sockaddr*)&_server, _server_len); assert(-1 != ret); ret = listen(_lisfd, 5); assert(-1 != ret); struct epoll_event _events[MAX_EVENT_NUMBER]; int _epofd = epoll_create(5); assert(-1 != _epofd); addfd(_epofd, _lisfd); ret = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd); assert(-1 != ret); set_non_blocking(pipefd[1]); addfd(_epofd, pipefd[0]); add_sig(SIGHUP); add_sig(SIGCHLD); add_sig(SIGTERM); add_sig(SIGINT); typedef int bool; #define false 0 #define true 1 bool stop_server = false; while (!stop_server) { int _number = epoll_wait(_epofd, _events, MAX_EVENT_NUMBER, -1); if ((_number < 0) && (EINTR != errno)) { log_info("epoll_wait fail, errno %s\n", strerror(errno)); break; } int i; for (i=0; i<_number; ++i) { int _sockfd = _events[i].data.fd; if (_lisfd == _sockfd) { struct sockaddr_in _client_addr; socklen_t _client_addr_len = sizeof(_client_addr); int _connfd = accept(_lisfd, (struct sockaddr*)&_client_addr, &_client_addr_len); addfd(_epofd, _connfd); } if ((pipefd[0] == _sockfd) && (_events[i].events & EPOLLIN)) { int sig; char _signals[1024]; int ret = recv(pipefd[0], _signals, sizeof(_signals), 0); if (ret <= 0) { continue; } else { int i; for (i=0; i<ret; ++i) { switch (_signals[i]) { case SIGCHLD: case SIGHUP:continue; case SIGTERM: case SIGINT: stop_server = true; } } } } } } log_info("close fds\n"); close(_lisfd); close(pipefd[0]); close(pipefd[1]); return 0; }