/* Initialize procthread */ PROCTHREAD *procthread_init(int cond) { PROCTHREAD *pth = NULL; if((pth = (PROCTHREAD *)xmm_mnew(sizeof(PROCTHREAD)))) { if((pth->cond = pth->have_evbase = cond) > 0) { if((pth->evbase = evbase_init(0)) == NULL) { fprintf(stderr, "Initialize evbase failed, %s\n", strerror(errno)); _exit(-1); } } MUTEX_INIT(pth->mutex); pth->message_queue = qmessage_init(); pth->run = procthread_run; pth->set_acceptor = procthread_set_acceptor; pth->pushconn = procthread_pushconn; pth->newconn = procthread_newconn; pth->addconn = procthread_addconn; pth->add_connection = procthread_add_connection; pth->newtask = procthread_newtask; pth->newtransaction = procthread_newtransaction; pth->shut_connection = procthread_shut_connection; pth->over_connection = procthread_over_connection; pth->terminate_connection = procthread_terminate_connection; pth->stop = procthread_stop; pth->wakeup = procthread_wakeup; pth->terminate = procthread_terminate; pth->state = procthread_state; pth->active_heartbeat = procthread_active_heartbeat; pth->clean = procthread_clean; } return pth; }
int main(int argc, char **argv) { char *ip = NULL; int port = 0; int fd = 0; struct sockaddr_in sa, lsa; socklen_t sa_len, lsa_len = -1; int sock_type = 0; if(argc < 5) { fprintf(stderr, "Usage:%s sock_type(0/TCP|1/UDP) ip port domain\n", argv[0]); _exit(-1); } ev_sock_type = atoi(argv[1]); if(ev_sock_type < 0 || ev_sock_type > ev_sock_count) { fprintf(stderr, "sock_type must be 0/TCP OR 1/UDP\n"); _exit(-1); } sock_type = ev_sock_list[ev_sock_type]; ip = argv[2]; port = atoi(argv[3]); domain = argv[4]; /* Set resource limit */ setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, CONN_MAX); /* Initialize global vars */ memset(events, 0, sizeof(EVENT) * CONN_MAX); /* Initialize inet */ memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(ip); sa.sin_port = htons(port); sa_len = sizeof(struct sockaddr); /* set evbase */ if((evbase = evbase_init(0))) { TIMER_INIT(timer); //evbase->set_evops(evbase, EOP_POLL); if((fd = socket(AF_INET, sock_type, 0)) > 0) { /* Connect */ if(connect(fd, (struct sockaddr *)&sa, sa_len) == 0 ) { /* set FD NON-BLOCK */ fcntl(fd, F_SETFL, O_NONBLOCK); if(sock_type == SOCK_STREAM) { event_set(&events[fd], fd, E_READ|E_WRITE|E_PERSIST, (void *)&events[fd], &ev_handler); } else { event_set(&events[fd], fd, E_READ|E_WRITE|E_PERSIST, (void *)&events[fd], &ev_udp_handler); } evbase->add(evbase, &events[fd]); sprintf(buffer[fd], "%d:client message\r\n", fd); lsa_len = sizeof(struct sockaddr); memset(&lsa, 0, lsa_len); getsockname(fd, (struct sockaddr *)&lsa, &lsa_len); SHOW_LOG("Connected to %s:%d via %d port:%d", ip, port, fd, ntohs(lsa.sin_port)); } else { FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno)); _exit(-1); } } while(1) { evbase->loop(evbase, 0, NULL); usleep(10); } //TIMER_CLEAN(timer); } return -1; }
int main(int argc, char **argv) { char *ip = NULL; int port = 0; int fd = 0; struct sockaddr_in sa, lsa; socklen_t sa_len, lsa_len = -1; int opt = 1; int i = 0; int conn_num = 0; EVENT *event = NULL; if(argc < 4) { fprintf(stderr, "Usage:%s ip port connection_number\n", argv[0]); _exit(-1); } ip = argv[1]; port = atoi(argv[2]); conn_num = atoi(argv[3]); /* Set resource limit */ setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, CONN_MAX); /* Initialize global vars */ memset(events, 0, sizeof(EVENT *) * CONN_MAX); /* Initialize inet */ memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(ip); sa.sin_port = htons(port); sa_len = sizeof(struct sockaddr); /* set evbase */ if((evbase = evbase_init(0))) { LOGGER_INIT(evbase->logger, "/tmp/ev_client.log"); evbase->set_evops(evbase, EOP_SELECT); while((fd = socket(AF_INET, SOCK_STREAM, 0)) > 0 && fd < conn_num) { /* Connect */ if(connect(fd, (struct sockaddr *)&sa, sa_len) == 0 ) { /* set FD NON-BLOCK */ fcntl(fd, F_SETFL, O_NONBLOCK); if((events[fd] = ev_init())) { events[fd]->set(events[fd], fd, E_READ|E_WRITE|E_PERSIST, (void *)events[fd], &ev_handler); evbase->add(evbase, events[fd]); sprintf(buffer[fd], "%d:client message", fd); } lsa_len = sizeof(struct sockaddr_in); memset(&lsa, 0, lsa_len); getsockname(fd, (struct sockaddr *)&lsa, &lsa_len); SHOW_LOG("Connected to %s:%d via %d port:%d", ip, port, fd, ntohs(lsa.sin_port)); } else { FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno)); break; } } while(1) { evbase->loop(evbase, 0, NULL); usleep(10); } } }
int main(int argc, char **argv) { char *ip = NULL; int port = 0; int fd = 0; struct sockaddr_in sa, lsa; socklen_t sa_len, lsa_len = -1; int i = 0; int conn_num = 0; int sock_type = 0; int flag = 0; if(argc < 5) { fprintf(stderr, "Usage:%s sock_type(0/TCP|1/UDP) ip port connection_number\n", argv[0]); _exit(-1); } ev_sock_type = atoi(argv[1]); if(ev_sock_type < 0 || ev_sock_type > ev_sock_count) { fprintf(stderr, "sock_type must be 0/TCP OR 1/UDP\n"); _exit(-1); } sock_type = ev_sock_list[ev_sock_type]; ip = argv[2]; port = atoi(argv[3]); conn_num = atoi(argv[4]); /* Set resource limit */ setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, CONN_MAX); /* Initialize global vars */ if((conns = (CONN *)calloc(CONN_MAX, sizeof(CONN)))) { //memset(events, 0, sizeof(EVENT *) * CONN_MAX); /* Initialize inet */ memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_family = AF_INET; sa.sin_addr.s_addr = inet_addr(ip); sa.sin_port = htons(port); sa_len = sizeof(struct sockaddr); /* set evbase */ if((evbase = evbase_init(0))) { LOGGER_INIT(evbase->logger, "/tmp/ev_client.log"); #ifdef USE_SSL SSL_library_init(); OpenSSL_add_all_algorithms(); SSL_load_error_strings(); if((ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) { ERR_print_errors_fp(stdout); _exit(-1); } #endif //evbase->set_evops(evbase, EOP_POLL); while((fd = socket(AF_INET, sock_type, 0)) > 0 && i < conn_num) { conns[fd].fd = fd; /* Connect */ if(connect(fd, (struct sockaddr *)&sa, sa_len) != 0) { FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno)); break; } if(is_use_ssl && sock_type == SOCK_STREAM) { #ifdef USE_SSL conns[fd].ssl = SSL_new(ctx); if(conns[fd].ssl == NULL ) { FATAL_LOG("new SSL with created CTX failed:%s\n", ERR_reason_error_string(ERR_get_error())); break; } if((ret = SSL_set_fd(conns[fd].ssl, fd)) == 0) { FATAL_LOG("add SSL to tcp socket failed:%s\n", ERR_reason_error_string(ERR_get_error())); break; } /* SSL Connect */ if(SSL_connect(conns[fd].ssl) < 0) { FATAL_LOG("SSL connection failed:%s\n", ERR_reason_error_string(ERR_get_error())); break; } #endif } /* set FD NON-BLOCK */ flag = fcntl(fd, F_GETFL, 0); flag |= O_NONBLOCK; fcntl(fd, F_SETFL, flag); if(sock_type == SOCK_STREAM) { event_set(&conns[fd].event, fd, E_READ|E_WRITE|E_PERSIST, (void *)&conns[fd].event, &ev_handler); } else { event_set(&conns[fd].event, fd, E_READ|E_WRITE|E_PERSIST, (void *)&conns[fd].event, &ev_udp_handler); } evbase->add(evbase, &conns[fd].event); sprintf(conns[fd].request, "GET / HTTP/1.0\r\n\r\n"); lsa_len = sizeof(struct sockaddr); memset(&lsa, 0, lsa_len); getsockname(fd, (struct sockaddr *)&lsa, &lsa_len); SHOW_LOG("%d:Connected to %s:%d via %d port:%d", i, ip, port, fd, ntohs(lsa.sin_port)); i++; } while(1) { evbase->loop(evbase, 0, NULL); usleep(1000); } for(i = 0; i < CONN_MAX; i++) { shutdown(conns[i].fd, SHUT_RDWR); close(conns[i].fd); event_destroy(&conns[i].event); #ifdef USE_SSL if(conns[i].ssl) { SSL_shutdown(conns[i].ssl); SSL_free(conns[i].ssl); } #endif } #ifdef USE_SSL ERR_free_strings(); SSL_CTX_free(ctx); #endif } free(conns); } return -1; }
int main(int argc, char **argv) { int i = 0; if(argc < 7) { fprintf(stderr, "Usage:%s sock_type(0/TCP|1/UDP) iskeepalive ip port concurrecy limit\n", argv[0]); _exit(-1); } ev_sock_type = atoi(argv[1]); if(ev_sock_type < 0 || ev_sock_type > ev_sock_count) { fprintf(stderr, "sock_type must be 0/TCP OR 1/UDP\n"); _exit(-1); } sock_type = ev_sock_list[ev_sock_type]; keepalive = atoi(argv[2]); ip = argv[3]; port = atoi(argv[4]); conn_num = atoi(argv[5]); limit = atoi(argv[6]); TIMER_INIT(timer); /* Set resource limit */ setrlimiter("RLIMIT_NOFILE", RLIMIT_NOFILE, CONN_MAX); /* Initialize global vars */ if((conns = (CONN *)calloc(CONN_MAX, sizeof(CONN)))) { //memset(events, 0, sizeof(EVENT *) * CONN_MAX); /* Initialize inet */ memset(&xsa, 0, sizeof(struct sockaddr_in)); xsa.sin_family = AF_INET; xsa.sin_addr.s_addr = inet_addr(ip); xsa.sin_port = htons(port); xsa_len = sizeof(struct sockaddr); /* set evbase */ if((evbase = evbase_init(0))) { if(is_use_ssl) { #ifdef USE_SSL SSL_library_init(); OpenSSL_add_all_algorithms(); SSL_load_error_strings(); if((ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) { ERR_print_errors_fp(stdout); _exit(-1); } #endif } for(i = 0; i < conn_num; i++) { new_request(); i++; } running_status = 1; do { evbase->loop(evbase, 0, NULL); //usleep(1000); }while(running_status); for(i = 0; i < CONN_MAX; i++) { if(conns[i].fd > 0) { event_destroy(&conns[i].event); shutdown(conns[i].fd, SHUT_RDWR); close(conns[i].fd); #ifdef USE_SSL if(conns[i].ssl) { SSL_shutdown(conns[i].ssl); SSL_free(conns[i].ssl); } #endif } } #ifdef USE_SSL ERR_free_strings(); SSL_CTX_free(ctx); #endif } free(conns); TIMER_CLEAN(timer); } return -1; }