static void drone_event_cb(struct bufferevent *bev, short events, void *arg) { struct session *session = arg; struct job *job = session->job; struct timeval now; if (events & BEV_EVENT_CONNECTED) { evutil_socket_t fd = bufferevent_getfd(bev); set_tcp_no_delay(fd); job->result->total_sessions++; } else if (events & BEV_EVENT_ERROR) { perror("Not connected"); gettimeofday(&now, NULL); if (drone_report_error_ready(job, &now)) { snprintf(job->error_msg, sizeof(job->error_msg), "Connection to the server lost(%zd message skipped)", job->skipped_error_msg); drone_report_error(job, &now); } if (job->result->total_sessions) job->result->total_sessions--; bufferevent_setfd(bev, -1); } }
static void eventcb(struct bufferevent *bev, short events, void *ptr) { if (events & BEV_EVENT_CONNECTED) { evutil_socket_t fd = bufferevent_getfd(bev); set_tcp_no_delay(fd); } else if (events & BEV_EVENT_ERROR) { printf("NOT Connected\n"); } }
static void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd,struct sockaddr*address,int socklen, void*ctx) { /* We got a new connection! Set up a bufferevent for it. */ struct event_base *base=evconnlistener_get_base(listener); struct bufferevent *bev=bufferevent_socket_new(base,fd,BEV_OPT_CLOSE_ON_FREE); set_tcp_no_delay(fd); bufferevent_setcb(bev,echo_read_cb,NULL,echo_event_cb,NULL); bufferevent_enable(bev,EV_READ|EV_WRITE); }
/* 事件回调函数,连接状态改变时回调的函数 */ void event_cb(struct bufferevent *bev, short events, void *ptr) { struct event_base *tbase = (struct event_base*)ptr; if ( events & BEV_EVENT_CONNECTED){ /* We're connected to server. Ordinarily we'd do something here, like start reading or writing. */ evutil_socket_t fd = bufferevent_getfd(bev); set_tcp_no_delay(fd); printf("Server is connected!\n"); }else { //如果连接不成功的消息,就停止事件循环 bufferevent_free(bev); event_base_loopbreak(tbase); printf("Server connect erro!! or The connect have been shutdown: %X\n", events); } }
static void drone_srv_accept_success_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx) { struct event_base *base = evconnlistener_get_base(listener); struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); struct client *client; client = calloc(1, sizeof(struct client)); client->base = base; client->buffer = malloc(sizeof(Drone__JobRequest) + sizeof(u_int32_t)); set_tcp_no_delay(fd); bufferevent_setcb(bev, drone_srv_read_cb, NULL, drone_srv_event_cb, client); bufferevent_enable(bev, EV_READ | EV_WRITE); }
int disable_tcp_no_delay(int fd) { return set_tcp_no_delay(fd, 0); }
int enable_tcp_no_delay(int fd) { return set_tcp_no_delay(fd, 1); }