4 void *dupClientReplyValue(void *o
) {
5 incrRefCount((robj
*)o
);
9 int listMatchObjects(void *a
, void *b
) {
10 return equalStringObjects(a
,b
);
13 redisClient
*createClient(int fd
) {
14 redisClient
*c
= zmalloc(sizeof(redisClient
));
17 anetNonBlock(NULL
,fd
);
18 anetTcpNoDelay(NULL
,fd
);
20 if (aeCreateFileEvent(server
.el
,fd
,AE_READABLE
,
21 readQueryFromClient
, c
) == AE_ERR
)
30 c
->querybuf
= sdsempty();
38 c
->lastinteraction
= time(NULL
);
40 c
->replstate
= REDIS_REPL_NONE
;
41 c
->reply
= listCreate();
42 listSetFreeMethod(c
->reply
,decrRefCount
);
43 listSetDupMethod(c
->reply
,dupClientReplyValue
);
47 c
->bpop
.target
= NULL
;
48 c
->io_keys
= listCreate();
49 c
->watched_keys
= listCreate();
50 listSetFreeMethod(c
->io_keys
,decrRefCount
);
51 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
52 c
->pubsub_patterns
= listCreate();
53 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
54 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
55 listAddNodeTail(server
.clients
,c
);
56 initClientMultiState(c
);
60 /* Set the event loop to listen for write events on the client's socket.
61 * Typically gets called every time a reply is built. */
62 int _installWriteEvent(redisClient
*c
) {
63 /* When CLOSE_AFTER_REPLY is set, no more replies may be added! */
64 redisAssert(!(c
->flags
& REDIS_CLOSE_AFTER_REPLY
));
66 if (c
->fd
<= 0) return REDIS_ERR
;
67 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0 &&
68 (c
->replstate
== REDIS_REPL_NONE
||
69 c
->replstate
== REDIS_REPL_ONLINE
) &&
70 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
71 sendReplyToClient
, c
) == AE_ERR
) return REDIS_ERR
;
75 /* Create a duplicate of the last object in the reply list when
76 * it is not exclusively owned by the reply list. */
77 robj
*dupLastObjectIfNeeded(list
*reply
) {
80 redisAssert(listLength(reply
) > 0);
82 cur
= listNodeValue(ln
);
83 if (cur
->refcount
> 1) {
84 new = dupStringObject(cur
);
86 listNodeValue(ln
) = new;
88 return listNodeValue(ln
);
91 int _addReplyToBuffer(redisClient
*c
, char *s
, size_t len
) {
92 size_t available
= sizeof(c
->buf
)-c
->bufpos
;
94 /* If there already are entries in the reply list, we cannot
95 * add anything more to the static buffer. */
96 if (listLength(c
->reply
) > 0) return REDIS_ERR
;
98 /* Check that the buffer has enough space available for this string. */
99 if (len
> available
) return REDIS_ERR
;
101 memcpy(c
->buf
+c
->bufpos
,s
,len
);
106 void _addReplyObjectToList(redisClient
*c
, robj
*o
) {
108 if (listLength(c
->reply
) == 0) {
110 listAddNodeTail(c
->reply
,o
);
112 tail
= listNodeValue(listLast(c
->reply
));
114 /* Append to this object when possible. */
115 if (tail
->ptr
!= NULL
&&
116 sdslen(tail
->ptr
)+sdslen(o
->ptr
) <= REDIS_REPLY_CHUNK_BYTES
)
118 tail
= dupLastObjectIfNeeded(c
->reply
);
119 tail
->ptr
= sdscatlen(tail
->ptr
,o
->ptr
,sdslen(o
->ptr
));
122 listAddNodeTail(c
->reply
,o
);
127 /* This method takes responsibility over the sds. When it is no longer
128 * needed it will be free'd, otherwise it ends up in a robj. */
129 void _addReplySdsToList(redisClient
*c
, sds s
) {
131 if (listLength(c
->reply
) == 0) {
132 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
134 tail
= listNodeValue(listLast(c
->reply
));
136 /* Append to this object when possible. */
137 if (tail
->ptr
!= NULL
&&
138 sdslen(tail
->ptr
)+sdslen(s
) <= REDIS_REPLY_CHUNK_BYTES
)
140 tail
= dupLastObjectIfNeeded(c
->reply
);
141 tail
->ptr
= sdscatlen(tail
->ptr
,s
,sdslen(s
));
144 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
149 void _addReplyStringToList(redisClient
*c
, char *s
, size_t len
) {
151 if (listLength(c
->reply
) == 0) {
152 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
154 tail
= listNodeValue(listLast(c
->reply
));
156 /* Append to this object when possible. */
157 if (tail
->ptr
!= NULL
&&
158 sdslen(tail
->ptr
)+len
<= REDIS_REPLY_CHUNK_BYTES
)
160 tail
= dupLastObjectIfNeeded(c
->reply
);
161 tail
->ptr
= sdscatlen(tail
->ptr
,s
,len
);
163 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
168 void addReply(redisClient
*c
, robj
*obj
) {
169 if (_installWriteEvent(c
) != REDIS_OK
) return;
170 redisAssert(!server
.vm_enabled
|| obj
->storage
== REDIS_VM_MEMORY
);
172 /* This is an important place where we can avoid copy-on-write
173 * when there is a saving child running, avoiding touching the
174 * refcount field of the object if it's not needed.
176 * If the encoding is RAW and there is room in the static buffer
177 * we'll be able to send the object to the client without
178 * messing with its page. */
179 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
180 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
181 _addReplyObjectToList(c
,obj
);
183 /* FIXME: convert the long into string and use _addReplyToBuffer()
184 * instead of calling getDecodedObject. As this place in the
185 * code is too performance critical. */
186 obj
= getDecodedObject(obj
);
187 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
188 _addReplyObjectToList(c
,obj
);
193 void addReplySds(redisClient
*c
, sds s
) {
194 if (_installWriteEvent(c
) != REDIS_OK
) {
195 /* The caller expects the sds to be free'd. */
199 if (_addReplyToBuffer(c
,s
,sdslen(s
)) == REDIS_OK
) {
202 /* This method free's the sds when it is no longer needed. */
203 _addReplySdsToList(c
,s
);
207 void addReplyString(redisClient
*c
, char *s
, size_t len
) {
208 if (_installWriteEvent(c
) != REDIS_OK
) return;
209 if (_addReplyToBuffer(c
,s
,len
) != REDIS_OK
)
210 _addReplyStringToList(c
,s
,len
);
213 void _addReplyError(redisClient
*c
, char *s
, size_t len
) {
214 addReplyString(c
,"-ERR ",5);
215 addReplyString(c
,s
,len
);
216 addReplyString(c
,"\r\n",2);
219 void addReplyError(redisClient
*c
, char *err
) {
220 _addReplyError(c
,err
,strlen(err
));
223 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...) {
226 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
228 _addReplyError(c
,s
,sdslen(s
));
232 void _addReplyStatus(redisClient
*c
, char *s
, size_t len
) {
233 addReplyString(c
,"+",1);
234 addReplyString(c
,s
,len
);
235 addReplyString(c
,"\r\n",2);
238 void addReplyStatus(redisClient
*c
, char *status
) {
239 _addReplyStatus(c
,status
,strlen(status
));
242 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...) {
245 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
247 _addReplyStatus(c
,s
,sdslen(s
));
251 /* Adds an empty object to the reply list that will contain the multi bulk
252 * length, which is not known when this function is called. */
253 void *addDeferredMultiBulkLength(redisClient
*c
) {
254 /* Note that we install the write event here even if the object is not
255 * ready to be sent, since we are sure that before returning to the
256 * event loop setDeferredMultiBulkLength() will be called. */
257 if (_installWriteEvent(c
) != REDIS_OK
) return NULL
;
258 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,NULL
));
259 return listLast(c
->reply
);
262 /* Populate the length object and try glueing it to the next chunk. */
263 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
) {
264 listNode
*ln
= (listNode
*)node
;
267 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
268 if (node
== NULL
) return;
270 len
= listNodeValue(ln
);
271 len
->ptr
= sdscatprintf(sdsempty(),"*%ld\r\n",length
);
272 if (ln
->next
!= NULL
) {
273 next
= listNodeValue(ln
->next
);
275 /* Only glue when the next node is non-NULL (an sds in this case) */
276 if (next
->ptr
!= NULL
) {
277 len
->ptr
= sdscatlen(len
->ptr
,next
->ptr
,sdslen(next
->ptr
));
278 listDelNode(c
->reply
,ln
->next
);
283 /* Add a duble as a bulk reply */
284 void addReplyDouble(redisClient
*c
, double d
) {
285 char dbuf
[128], sbuf
[128];
287 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
288 slen
= snprintf(sbuf
,sizeof(sbuf
),"$%d\r\n%s\r\n",dlen
,dbuf
);
289 addReplyString(c
,sbuf
,slen
);
292 /* Add a long long as integer reply or bulk len / multi bulk count.
293 * Basically this is used to output <prefix><long long><crlf>. */
294 void _addReplyLongLong(redisClient
*c
, long long ll
, char prefix
) {
298 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
301 addReplyString(c
,buf
,len
+3);
304 void addReplyLongLong(redisClient
*c
, long long ll
) {
305 _addReplyLongLong(c
,ll
,':');
308 void addReplyMultiBulkLen(redisClient
*c
, long length
) {
309 _addReplyLongLong(c
,length
,'*');
312 /* Create the length prefix of a bulk reply, example: $2234 */
313 void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
316 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
317 len
= sdslen(obj
->ptr
);
319 long n
= (long)obj
->ptr
;
321 /* Compute how many bytes will take this integer as a radix 10 string */
327 while((n
= n
/10) != 0) {
331 _addReplyLongLong(c
,len
,'$');
334 /* Add a Redis Object as a bulk reply */
335 void addReplyBulk(redisClient
*c
, robj
*obj
) {
336 addReplyBulkLen(c
,obj
);
338 addReply(c
,shared
.crlf
);
341 /* Add a C buffer as bulk reply */
342 void addReplyBulkCBuffer(redisClient
*c
, void *p
, size_t len
) {
343 _addReplyLongLong(c
,len
,'$');
344 addReplyString(c
,p
,len
);
345 addReply(c
,shared
.crlf
);
348 /* Add a C nul term string as bulk reply */
349 void addReplyBulkCString(redisClient
*c
, char *s
) {
351 addReply(c
,shared
.nullbulk
);
353 addReplyBulkCBuffer(c
,s
,strlen(s
));
357 /* Add a long long as a bulk reply */
358 void addReplyBulkLongLong(redisClient
*c
, long long ll
) {
362 len
= ll2string(buf
,64,ll
);
363 addReplyBulkCBuffer(c
,buf
,len
);
366 static void acceptCommonHandler(int fd
) {
368 if ((c
= createClient(fd
)) == NULL
) {
369 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
370 close(fd
); /* May be already closed, just ingore errors */
373 /* If maxclient directive is set and this is one client more... close the
374 * connection. Note that we create the client instead to check before
375 * for this condition, since now the socket is already set in nonblocking
376 * mode and we can send an error for free using the Kernel I/O */
377 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
378 char *err
= "-ERR max number of clients reached\r\n";
380 /* That's a best effort error message, don't check write errors */
381 if (write(c
->fd
,err
,strlen(err
)) == -1) {
382 /* Nothing to do, Just to avoid the warning... */
387 server
.stat_numconnections
++;
390 void acceptTcpHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
395 REDIS_NOTUSED(privdata
);
397 cfd
= anetTcpAccept(server
.neterr
, fd
, cip
, &cport
);
399 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
402 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
403 acceptCommonHandler(cfd
);
406 void acceptUnixHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
410 REDIS_NOTUSED(privdata
);
412 cfd
= anetUnixAccept(server
.neterr
, fd
);
414 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
417 redisLog(REDIS_VERBOSE
,"Accepted connection to %s", server
.unixsocket
);
418 acceptCommonHandler(cfd
);
422 static void freeClientArgv(redisClient
*c
) {
424 for (j
= 0; j
< c
->argc
; j
++)
425 decrRefCount(c
->argv
[j
]);
429 void freeClient(redisClient
*c
) {
432 /* Note that if the client we are freeing is blocked into a blocking
433 * call, we have to set querybuf to NULL *before* to call
434 * unblockClientWaitingData() to avoid processInputBuffer() will get
435 * called. Also it is important to remove the file events after
436 * this, because this call adds the READABLE event. */
437 sdsfree(c
->querybuf
);
439 if (c
->flags
& REDIS_BLOCKED
)
440 unblockClientWaitingData(c
);
442 /* UNWATCH all the keys */
444 listRelease(c
->watched_keys
);
445 /* Unsubscribe from all the pubsub channels */
446 pubsubUnsubscribeAllChannels(c
,0);
447 pubsubUnsubscribeAllPatterns(c
,0);
448 dictRelease(c
->pubsub_channels
);
449 listRelease(c
->pubsub_patterns
);
450 /* Obvious cleanup */
451 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
452 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
453 listRelease(c
->reply
);
456 /* Remove from the list of clients */
457 ln
= listSearchKey(server
.clients
,c
);
458 redisAssert(ln
!= NULL
);
459 listDelNode(server
.clients
,ln
);
460 /* Remove from the list of clients waiting for swapped keys, or ready
461 * to be restarted, but not yet woken up again. */
462 if (c
->flags
& REDIS_IO_WAIT
) {
463 redisAssert(server
.vm_enabled
);
464 if (listLength(c
->io_keys
) == 0) {
465 ln
= listSearchKey(server
.io_ready_clients
,c
);
467 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
468 * it should be present in the list io_ready_clients */
469 redisAssert(ln
!= NULL
);
470 listDelNode(server
.io_ready_clients
,ln
);
472 while (listLength(c
->io_keys
)) {
473 ln
= listFirst(c
->io_keys
);
474 dontWaitForSwappedKey(c
,ln
->value
);
477 server
.vm_blocked_clients
--;
479 listRelease(c
->io_keys
);
480 /* Master/slave cleanup.
481 * Case 1: we lost the connection with a slave. */
482 if (c
->flags
& REDIS_SLAVE
) {
483 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
485 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
486 ln
= listSearchKey(l
,c
);
487 redisAssert(ln
!= NULL
);
491 /* Case 2: we lost the connection with the master. */
492 if (c
->flags
& REDIS_MASTER
) {
493 server
.master
= NULL
;
495 server
.replstate
= REDIS_REPL_CONNECT
;
496 /* Since we lost the connection with the master, we should also
497 * close the connection with all our slaves if we have any, so
498 * when we'll resync with the master the other slaves will sync again
499 * with us as well. Note that also when the slave is not connected
500 * to the master it will keep refusing connections by other slaves. */
501 while (listLength(server
.slaves
)) {
502 ln
= listFirst(server
.slaves
);
503 freeClient((redisClient
*)ln
->value
);
508 freeClientMultiState(c
);
512 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
513 redisClient
*c
= privdata
;
514 int nwritten
= 0, totwritten
= 0, objlen
;
519 /* Use writev() if we have enough buffers to send */
520 if (!server
.glueoutputbuf
&&
521 listLength(c
->reply
) > REDIS_WRITEV_THRESHOLD
&&
522 !(c
->flags
& REDIS_MASTER
))
524 sendReplyToClientWritev(el
, fd
, privdata
, mask
);
528 while(c
->bufpos
> 0 || listLength(c
->reply
)) {
530 if (c
->flags
& REDIS_MASTER
) {
531 /* Don't reply to a master */
532 nwritten
= c
->bufpos
- c
->sentlen
;
534 nwritten
= write(fd
,c
->buf
+c
->sentlen
,c
->bufpos
-c
->sentlen
);
535 if (nwritten
<= 0) break;
537 c
->sentlen
+= nwritten
;
538 totwritten
+= nwritten
;
540 /* If the buffer was sent, set bufpos to zero to continue with
541 * the remainder of the reply. */
542 if (c
->sentlen
== c
->bufpos
) {
547 o
= listNodeValue(listFirst(c
->reply
));
548 objlen
= sdslen(o
->ptr
);
551 listDelNode(c
->reply
,listFirst(c
->reply
));
555 if (c
->flags
& REDIS_MASTER
) {
556 /* Don't reply to a master */
557 nwritten
= objlen
- c
->sentlen
;
559 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
,objlen
-c
->sentlen
);
560 if (nwritten
<= 0) break;
562 c
->sentlen
+= nwritten
;
563 totwritten
+= nwritten
;
565 /* If we fully sent the object on head go to the next one */
566 if (c
->sentlen
== objlen
) {
567 listDelNode(c
->reply
,listFirst(c
->reply
));
571 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
572 * bytes, in a single threaded server it's a good idea to serve
573 * other clients as well, even if a very large request comes from
574 * super fast link that is always able to accept data (in real world
575 * scenario think about 'KEYS *' against the loopback interfae) */
576 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
578 if (nwritten
== -1) {
579 if (errno
== EAGAIN
) {
582 redisLog(REDIS_VERBOSE
,
583 "Error writing to client: %s", strerror(errno
));
588 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
589 if (listLength(c
->reply
) == 0) {
591 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
593 /* Close connection after entire reply has been sent. */
594 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) freeClient(c
);
598 void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
600 redisClient
*c
= privdata
;
601 int nwritten
= 0, totwritten
= 0, objlen
, willwrite
;
603 struct iovec iov
[REDIS_WRITEV_IOVEC_COUNT
];
609 while (listLength(c
->reply
)) {
614 /* fill-in the iov[] array */
615 for(node
= listFirst(c
->reply
); node
; node
= listNextNode(node
)) {
616 o
= listNodeValue(node
);
617 objlen
= sdslen(o
->ptr
);
619 if (totwritten
+ objlen
- offset
> REDIS_MAX_WRITE_PER_EVENT
)
622 if(ion
== REDIS_WRITEV_IOVEC_COUNT
)
623 break; /* no more iovecs */
625 iov
[ion
].iov_base
= ((char*)o
->ptr
) + offset
;
626 iov
[ion
].iov_len
= objlen
- offset
;
627 willwrite
+= objlen
- offset
;
628 offset
= 0; /* just for the first item */
635 /* write all collected blocks at once */
636 if((nwritten
= writev(fd
, iov
, ion
)) < 0) {
637 if (errno
!= EAGAIN
) {
638 redisLog(REDIS_VERBOSE
,
639 "Error writing to client: %s", strerror(errno
));
646 totwritten
+= nwritten
;
649 /* remove written robjs from c->reply */
650 while (nwritten
&& listLength(c
->reply
)) {
651 o
= listNodeValue(listFirst(c
->reply
));
652 objlen
= sdslen(o
->ptr
);
654 if(nwritten
>= objlen
- offset
) {
655 listDelNode(c
->reply
, listFirst(c
->reply
));
656 nwritten
-= objlen
- offset
;
660 c
->sentlen
+= nwritten
;
668 c
->lastinteraction
= time(NULL
);
670 if (listLength(c
->reply
) == 0) {
672 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
676 /* resetClient prepare the client to process the next command */
677 void resetClient(redisClient
*c
) {
684 void closeTimedoutClients(void) {
687 time_t now
= time(NULL
);
690 listRewind(server
.clients
,&li
);
691 while ((ln
= listNext(&li
)) != NULL
) {
692 c
= listNodeValue(ln
);
693 if (server
.maxidletime
&&
694 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
695 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
696 !(c
->flags
& REDIS_BLOCKED
) && /* no timeout for BLPOP */
697 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
698 listLength(c
->pubsub_patterns
) == 0 &&
699 (now
- c
->lastinteraction
> server
.maxidletime
))
701 redisLog(REDIS_VERBOSE
,"Closing idle client");
703 } else if (c
->flags
& REDIS_BLOCKED
) {
704 if (c
->bpop
.timeout
!= 0 && c
->bpop
.timeout
< now
) {
705 addReply(c
,shared
.nullmultibulk
);
706 unblockClientWaitingData(c
);
712 int processInlineBuffer(redisClient
*c
) {
713 char *newline
= strstr(c
->querybuf
,"\r\n");
718 /* Nothing to do without a \r\n */
722 /* Split the input buffer up to the \r\n */
723 querylen
= newline
-(c
->querybuf
);
724 argv
= sdssplitlen(c
->querybuf
,querylen
," ",1,&argc
);
726 /* Leave data after the first line of the query in the buffer */
727 c
->querybuf
= sdsrange(c
->querybuf
,querylen
+2,-1);
729 /* Setup argv array on client structure */
730 if (c
->argv
) zfree(c
->argv
);
731 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
733 /* Create redis objects for all arguments. */
734 for (c
->argc
= 0, j
= 0; j
< argc
; j
++) {
735 if (sdslen(argv
[j
])) {
736 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
746 /* Helper function. Trims query buffer to make the function that processes
747 * multi bulk requests idempotent. */
748 static void setProtocolError(redisClient
*c
, int pos
) {
749 c
->flags
|= REDIS_CLOSE_AFTER_REPLY
;
750 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
753 int processMultibulkBuffer(redisClient
*c
) {
754 char *newline
= NULL
;
759 if (c
->multibulklen
== 0) {
760 /* The client should have been reset */
761 redisAssert(c
->argc
== 0);
763 /* Multi bulk length cannot be read without a \r\n */
764 newline
= strstr(c
->querybuf
,"\r\n");
768 /* We know for sure there is a whole line since newline != NULL,
769 * so go ahead and find out the multi bulk length. */
770 redisAssert(c
->querybuf
[0] == '*');
771 c
->multibulklen
= strtol(c
->querybuf
+1,&eptr
,10);
772 pos
= (newline
-c
->querybuf
)+2;
773 if (c
->multibulklen
<= 0) {
774 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
776 } else if (c
->multibulklen
> 1024*1024) {
777 addReplyError(c
,"Protocol error: invalid multibulk length");
778 setProtocolError(c
,pos
);
782 /* Setup argv array on client structure */
783 if (c
->argv
) zfree(c
->argv
);
784 c
->argv
= zmalloc(sizeof(robj
*)*c
->multibulklen
);
786 /* Search new newline */
787 newline
= strstr(c
->querybuf
+pos
,"\r\n");
790 redisAssert(c
->multibulklen
> 0);
791 while(c
->multibulklen
) {
792 /* Read bulk length if unknown */
793 if (c
->bulklen
== -1) {
794 newline
= strstr(c
->querybuf
+pos
,"\r\n");
795 if (newline
!= NULL
) {
796 if (c
->querybuf
[pos
] != '$') {
797 addReplyErrorFormat(c
,
798 "Protocol error: expected '$', got '%c'",
800 setProtocolError(c
,pos
);
804 bulklen
= strtol(c
->querybuf
+pos
+1,&eptr
,10);
805 tolerr
= (eptr
[0] != '\r');
806 if (tolerr
|| bulklen
== LONG_MIN
|| bulklen
== LONG_MAX
||
807 bulklen
< 0 || bulklen
> 512*1024*1024)
809 addReplyError(c
,"Protocol error: invalid bulk length");
810 setProtocolError(c
,pos
);
813 pos
+= eptr
-(c
->querybuf
+pos
)+2;
814 c
->bulklen
= bulklen
;
816 /* No newline in current buffer, so wait for more data */
821 /* Read bulk argument */
822 if (sdslen(c
->querybuf
)-pos
< (unsigned)(c
->bulklen
+2)) {
823 /* Not enough data (+2 == trailing \r\n) */
826 c
->argv
[c
->argc
++] = createStringObject(c
->querybuf
+pos
,c
->bulklen
);
834 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
836 /* We're done when c->multibulk == 0 */
837 if (c
->multibulklen
== 0) {
843 void processInputBuffer(redisClient
*c
) {
844 /* Keep processing while there is something in the input buffer */
845 while(sdslen(c
->querybuf
)) {
846 /* Immediately abort if the client is in the middle of something. */
847 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
849 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
850 * written to the client. Make sure to not let the reply grow after
851 * this flag has been set (i.e. don't process more commands). */
852 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
854 /* Determine request type when unknown. */
856 if (c
->querybuf
[0] == '*') {
857 c
->reqtype
= REDIS_REQ_MULTIBULK
;
859 c
->reqtype
= REDIS_REQ_INLINE
;
863 if (c
->reqtype
== REDIS_REQ_INLINE
) {
864 if (processInlineBuffer(c
) != REDIS_OK
) break;
865 } else if (c
->reqtype
== REDIS_REQ_MULTIBULK
) {
866 if (processMultibulkBuffer(c
) != REDIS_OK
) break;
868 redisPanic("Unknown request type");
871 /* Multibulk processing could see a <= 0 length. */
875 /* Only reset the client when the command was executed. */
876 if (processCommand(c
) == REDIS_OK
)
882 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
883 redisClient
*c
= (redisClient
*) privdata
;
884 char buf
[REDIS_IOBUF_LEN
];
889 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
891 if (errno
== EAGAIN
) {
894 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
898 } else if (nread
== 0) {
899 redisLog(REDIS_VERBOSE
, "Client closed connection");
904 c
->querybuf
= sdscatlen(c
->querybuf
,buf
,nread
);
905 c
->lastinteraction
= time(NULL
);
909 processInputBuffer(c
);