void redisAsyncHandleWrite(redisAsyncContext *ac) { redisContext *c = &(ac->c); int done = 0; // 连接创建失败??? if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ if (__redisAsyncHandleConnect(ac) != REDIS_OK) return; // 再尝试检测 flag /* Try again later when the context is still not connected. */ if (!(c->flags & REDIS_CONNECTED)) return; } if (redisBufferWrite(c,&done) == REDIS_ERR) { __redisAsyncDisconnect(ac); } else { /* Continue writing when not done, stop writing otherwise */ // 当缓冲区中还有数据待发送时,会再次注册写事件 if (!done) _EL_ADD_WRITE(ac); // 当缓冲区中没有数据待发送时,会再次注册写事件 else _EL_DEL_WRITE(ac); /* Always schedule reads after writes */ _EL_ADD_READ(ac); } }
/* This function should be called when the socket is readable. * It processes all replies that can be read and executes their callbacks. */ void redisAsyncHandleRead(redisAsyncContext *ac) { redisContext *c = &(ac->c); // 如果连接已经断开,尝试连接 if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ if (__redisAsyncHandleConnect(ac) != REDIS_OK) return; /* Try again later when the context is still not connected. */ if (!(c->flags & REDIS_CONNECTED)) return; } // 读取数据 if (redisBufferRead(c) == REDIS_ERR) { // 读取错误,断开连接并做一些清理工作 __redisAsyncDisconnect(ac); } else { // 读取数据成功,再次注册读事件, /* Always re-schedule reads */ _EL_ADD_READ(ac); // 并处理回调函数 redisProcessCallbacks(ac); } }
void redisAsyncHandleWrite(redisAsyncContext *ac) { redisContext *c = &(ac->c); int done = 0; int status; if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ if (__redisAsyncHandleConnect(ac) != REDIS_OK) return; /* Try again later when the context is still not connected. */ if (!(c->flags & REDIS_CONNECTED)) return; } pthread_mutex_lock(&ac->lock); status = redisBufferWrite(c,&done); pthread_mutex_unlock(&ac->lock); if (status == REDIS_ERR) { __redisAsyncDisconnect(ac); } else { /* Continue writing when not done, stop writing otherwise */ if (!done) _EL_ADD_WRITE(ac); else _EL_DEL_WRITE(ac); /* Always schedule reads after writes */ _EL_ADD_READ(ac); } }
/* This function should be called when the socket is readable. * It processes all replies that can be read and executes their callbacks. */ void redisAsyncHandleRead(redisAsyncContext *ac) { redisContext *c = &(ac->c); if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ if (__redisAsyncHandleConnect(ac) != REDIS_OK) return; /* Try again later when the context is still not connected. */ if (!(c->flags & REDIS_CONNECTED)) return; } if (redisBufferRead(c) == REDIS_ERR) { __redisAsyncDisconnect(ac); } else { /* Always re-schedule reads */ #ifdef _WIN32 // There appears to be a bug in the Linux version of _EL_ADD_READ which will not reschedule // the read if already reading. This is a problem if there is a large number of async GET // operations. If the receive buffer is exhausted with the data returned, the read would // not be rescheduled, and the async operations would cease. This forces the read to recur. _EL_FORCE_ADD_READ(ac); #else _EL_ADD_READ(ac); #endif redisProcessCallbacks(ac); } }
/* This function should be called when the socket is readable. * It processes all replies that can be read and executes their callbacks. */ void redisAsyncHandleRead(redisAsyncContext *ac) { redisContext *c = &(ac->c); if (!(c->flags & REDIS_CONNECTED)) { /* Abort connect was not successful. */ if (__redisAsyncHandleConnect(ac) != REDIS_OK) return; /* Try again later when the context is still not connected. */ if (!(c->flags & REDIS_CONNECTED)) return; } if (redisBufferRead(c) == REDIS_ERR) { __redisAsyncDisconnect(ac); } else { /* Always re-schedule reads */ _EL_ADD_READ(ac); redisProcessCallbacks(ac); } }
int redisAsyncHandleWriteComplete(redisAsyncContext *ac, int written) { redisContext *c = &(ac->c); int done = 0; int rc; rc = redisBufferWriteDone(c, written, &done); if (rc == REDIS_ERR) { __redisAsyncDisconnect(ac); } else { /* Continue writing when not done, stop writing otherwise */ if (!done) _EL_ADD_WRITE(ac); else _EL_DEL_WRITE(ac); /* Always schedule reads after writes */ _EL_ADD_READ(ac); } return REDIS_OK; }