标签:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <thread> #include<functional> #ifdef __cplusplus extern "C"{ #endif #include <hiredis.h> #include <async.h> #include <adapters/ae.h> #ifdef __cplusplus } #endif /* Put event loop in the global scope, so it can be explicitly stopped */ static aeEventLoop *loop; void getCallback(redisAsyncContext *c, void *r, void *privdata) { redisReply *reply = r; if (reply == NULL) return; printf("argv[%s]: %s\n", (char*)privdata, reply->str); } void connectCallback(const redisAsyncContext *c, int status) { if (status != REDIS_OK) { printf("Error: %s\n", c->errstr); aeStop(loop); return; } printf("Connected...\n"); } void disconnectCallback(const redisAsyncContext *c, int status) { if (status != REDIS_OK) { printf("Error: %s\n", c->errstr); aeStop(loop); return; } printf("Disconnected...\n"); aeStop(loop); } void quitConnCallBack(redisAsyncContext *c, void *r, void *privdata) { printf("quit"); redisAsyncDisconnect(c); } void testThreadLoop(void * p) { static int num = 10; char c11[64]; strcpy(c11, "test"); while(1) { std::this_thread::sleep_for(std::chrono::milliseconds(1500)); num--; if (num < 0) { //在这里调用disconnect, 并不能使aeMain退出 redisAsyncDisconnect((redisAsyncContext *)p); //正确做法,应该调用如下 //redisAsyncCommand((redisAsyncContext *)p, quitConnCallBack, c11, "quit"); printf("exit\n"); return; } redisAsyncCommand((redisAsyncContext *)p, getCallback, c11, "ping"); } } int main (int argc, char **argv) { signal(SIGPIPE, SIG_IGN); redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); if (c->err) { /* Let *c leak for now... */ printf("Error: %s\n", c->errstr); return 1; } loop = aeCreateEventLoop(64); redisAeAttach(loop, c); redisAsyncSetConnectCallback(c,connectCallback); redisAsyncSetDisconnectCallback(c,disconnectCallback); std::thread t(testThreadLoop, c); t.detach(); aeMain(loop); return 0; }
void aeMain(aeEventLoop *eventLoop) { eventLoop->stop = 0; while (!eventLoop->stop) { if (eventLoop->beforesleep != NULL) eventLoop->beforesleep(eventLoop); aeProcessEvents(eventLoop, AE_ALL_EVENTS); } } void aeStop(aeEventLoop *eventLoop) { eventLoop->stop = 1; }
int aeProcessEvents(aeEventLoop *eventLoop, int flags) { //... //tvp 会是NULL, 推测阻塞在aeApiPoll, 查看aeApiPoll代码进行证实 numevents = aeApiPoll(eventLoop, tvp); for (j = 0; j < numevents; j++) { aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd]; int mask = eventLoop->fired[j].mask; int fd = eventLoop->fired[j].fd; int rfired = 0; /* note the fe->mask & mask & ... code: maybe an already processed * event removed an element that fired and we still didn‘t * processed, so we check if the event is still valid. */ if (fe->mask & mask & AE_READABLE) { rfired = 1; fe->rfileProc(eventLoop,fd,fe->clientData,mask); } if (fe->mask & mask & AE_WRITABLE) { if (!rfired || fe->wfileProc != fe->rfileProc) fe->wfileProc(eventLoop,fd,fe->clientData,mask); } processed++; } } /* Check time events */ if (flags & AE_TIME_EVENTS) processed += processTimeEvents(eventLoop); return processed; /* return the number of processed file/time events */ } static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) { aeApiState *state = eventLoop->apidata; int retval, numevents = 0; //真相在这边,epoll_wait, 第三个参数为-1, epoll_wait将一直等待下去! retval = epoll_wait(state->epfd,state->events,eventLoop->setsize, tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1); if (retval > 0) { int j; numevents = retval; for (j = 0; j < numevents; j++) { int mask = 0; struct epoll_event *e = state->events+j; if (e->events & EPOLLIN) mask |= AE_READABLE; if (e->events & EPOLLOUT) mask |= AE_WRITABLE; if (e->events & EPOLLERR) mask |= AE_WRITABLE; if (e->events & EPOLLHUP) mask |= AE_WRITABLE; eventLoop->fired[j].fd = e->data.fd; eventLoop->fired[j].mask = mask; } } return numevents; }
整理下aeMain的流程如下图所示,
void quitConnCallBack(redisAsyncContext *c, void *r, void *privdata) { printf("quit"); redisAsyncDisconnect(c); }
hiredis aeStop仅在redis命令的回调函数中生效 分析
标签:
原文地址:http://www.cnblogs.com/europelee/p/5228587.html