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
);
44 c
->blocking_keys
= NULL
;
45 c
->blocking_keys_num
= 0;
46 c
->io_keys
= listCreate();
47 c
->watched_keys
= listCreate();
48 listSetFreeMethod(c
->io_keys
,decrRefCount
);
49 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
50 c
->pubsub_patterns
= listCreate();
51 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
52 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
53 listAddNodeTail(server
.clients
,c
);
54 initClientMultiState(c
);
58 int _installWriteEvent(redisClient
*c
) {
59 if (c
->fd
<= 0) return REDIS_ERR
;
60 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0 &&
61 (c
->replstate
== REDIS_REPL_NONE
||
62 c
->replstate
== REDIS_REPL_ONLINE
) &&
63 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
64 sendReplyToClient
, c
) == AE_ERR
) return REDIS_ERR
;
68 /* Create a duplicate of the last object in the reply list when
69 * it is not exclusively owned by the reply list. */
70 robj
*dupLastObjectIfNeeded(list
*reply
) {
73 redisAssert(listLength(reply
) > 0);
75 cur
= listNodeValue(ln
);
76 if (cur
->refcount
> 1) {
77 new = dupStringObject(cur
);
79 listNodeValue(ln
) = new;
81 return listNodeValue(ln
);
84 int _addReplyToBuffer(redisClient
*c
, char *s
, size_t len
) {
85 size_t available
= sizeof(c
->buf
)-c
->bufpos
;
87 /* If there already are entries in the reply list, we cannot
88 * add anything more to the static buffer. */
89 if (listLength(c
->reply
) > 0) return REDIS_ERR
;
91 /* Check that the buffer has enough space available for this string. */
92 if (len
> available
) return REDIS_ERR
;
94 memcpy(c
->buf
+c
->bufpos
,s
,len
);
99 void _addReplyObjectToList(redisClient
*c
, robj
*o
) {
101 if (listLength(c
->reply
) == 0) {
103 listAddNodeTail(c
->reply
,o
);
105 tail
= listNodeValue(listLast(c
->reply
));
107 /* Append to this object when possible. */
108 if (tail
->ptr
!= NULL
&&
109 sdslen(tail
->ptr
)+sdslen(o
->ptr
) <= REDIS_REPLY_CHUNK_BYTES
)
111 tail
= dupLastObjectIfNeeded(c
->reply
);
112 tail
->ptr
= sdscatlen(tail
->ptr
,o
->ptr
,sdslen(o
->ptr
));
115 listAddNodeTail(c
->reply
,o
);
120 /* This method takes responsibility over the sds. When it is no longer
121 * needed it will be free'd, otherwise it ends up in a robj. */
122 void _addReplySdsToList(redisClient
*c
, sds s
) {
124 if (listLength(c
->reply
) == 0) {
125 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
127 tail
= listNodeValue(listLast(c
->reply
));
129 /* Append to this object when possible. */
130 if (tail
->ptr
!= NULL
&&
131 sdslen(tail
->ptr
)+sdslen(s
) <= REDIS_REPLY_CHUNK_BYTES
)
133 tail
= dupLastObjectIfNeeded(c
->reply
);
134 tail
->ptr
= sdscatlen(tail
->ptr
,s
,sdslen(s
));
137 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
142 void _addReplyStringToList(redisClient
*c
, char *s
, size_t len
) {
144 if (listLength(c
->reply
) == 0) {
145 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
147 tail
= listNodeValue(listLast(c
->reply
));
149 /* Append to this object when possible. */
150 if (tail
->ptr
!= NULL
&&
151 sdslen(tail
->ptr
)+len
<= REDIS_REPLY_CHUNK_BYTES
)
153 tail
= dupLastObjectIfNeeded(c
->reply
);
154 tail
->ptr
= sdscatlen(tail
->ptr
,s
,len
);
156 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
161 void addReply(redisClient
*c
, robj
*obj
) {
162 if (_installWriteEvent(c
) != REDIS_OK
) return;
163 redisAssert(!server
.vm_enabled
|| obj
->storage
== REDIS_VM_MEMORY
);
165 /* This is an important place where we can avoid copy-on-write
166 * when there is a saving child running, avoiding touching the
167 * refcount field of the object if it's not needed.
169 * If the encoding is RAW and there is room in the static buffer
170 * we'll be able to send the object to the client without
171 * messing with its page. */
172 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
173 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
174 _addReplyObjectToList(c
,obj
);
176 obj
= getDecodedObject(obj
);
177 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
178 _addReplyObjectToList(c
,obj
);
183 void addReplySds(redisClient
*c
, sds s
) {
184 if (_installWriteEvent(c
) != REDIS_OK
) {
185 /* The caller expects the sds to be free'd. */
189 if (_addReplyToBuffer(c
,s
,sdslen(s
)) == REDIS_OK
) {
192 /* This method free's the sds when it is no longer needed. */
193 _addReplySdsToList(c
,s
);
197 void addReplyString(redisClient
*c
, char *s
, size_t len
) {
198 if (_installWriteEvent(c
) != REDIS_OK
) return;
199 if (_addReplyToBuffer(c
,s
,len
) != REDIS_OK
)
200 _addReplyStringToList(c
,s
,len
);
203 void _addReplyError(redisClient
*c
, char *s
, size_t len
) {
204 addReplyString(c
,"-ERR ",5);
205 addReplyString(c
,s
,len
);
206 addReplyString(c
,"\r\n",2);
209 void addReplyError(redisClient
*c
, char *err
) {
210 _addReplyError(c
,err
,strlen(err
));
213 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...) {
216 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
218 _addReplyError(c
,s
,sdslen(s
));
222 void _addReplyStatus(redisClient
*c
, char *s
, size_t len
) {
223 addReplyString(c
,"+",1);
224 addReplyString(c
,s
,len
);
225 addReplyString(c
,"\r\n",2);
228 void addReplyStatus(redisClient
*c
, char *status
) {
229 _addReplyStatus(c
,status
,strlen(status
));
232 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...) {
235 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
237 _addReplyStatus(c
,s
,sdslen(s
));
241 /* Adds an empty object to the reply list that will contain the multi bulk
242 * length, which is not known when this function is called. */
243 void *addDeferredMultiBulkLength(redisClient
*c
) {
244 /* Note that we install the write event here even if the object is not
245 * ready to be sent, since we are sure that before returning to the
246 * event loop setDeferredMultiBulkLength() will be called. */
247 if (_installWriteEvent(c
) != REDIS_OK
) return NULL
;
248 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,NULL
));
249 return listLast(c
->reply
);
252 /* Populate the length object and try glueing it to the next chunk. */
253 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
) {
254 listNode
*ln
= (listNode
*)node
;
257 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
258 if (node
== NULL
) return;
260 len
= listNodeValue(ln
);
261 len
->ptr
= sdscatprintf(sdsempty(),"*%ld\r\n",length
);
262 if (ln
->next
!= NULL
) {
263 next
= listNodeValue(ln
->next
);
265 /* Only glue when the next node is non-NULL (an sds in this case) */
266 if (next
->ptr
!= NULL
) {
267 len
->ptr
= sdscatlen(len
->ptr
,next
->ptr
,sdslen(next
->ptr
));
268 listDelNode(c
->reply
,ln
->next
);
273 void addReplyDouble(redisClient
*c
, double d
) {
274 char dbuf
[128], sbuf
[128];
276 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
277 slen
= snprintf(sbuf
,sizeof(sbuf
),"$%d\r\n%s\r\n",dlen
,dbuf
);
278 addReplyString(c
,sbuf
,slen
);
281 void _addReplyLongLong(redisClient
*c
, long long ll
, char prefix
) {
285 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
288 addReplyString(c
,buf
,len
+3);
291 void addReplyLongLong(redisClient
*c
, long long ll
) {
292 _addReplyLongLong(c
,ll
,':');
295 void addReplyMultiBulkLen(redisClient
*c
, long length
) {
296 _addReplyLongLong(c
,length
,'*');
299 void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
302 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
303 len
= sdslen(obj
->ptr
);
305 long n
= (long)obj
->ptr
;
307 /* Compute how many bytes will take this integer as a radix 10 string */
313 while((n
= n
/10) != 0) {
317 _addReplyLongLong(c
,len
,'$');
320 void addReplyBulk(redisClient
*c
, robj
*obj
) {
321 addReplyBulkLen(c
,obj
);
323 addReply(c
,shared
.crlf
);
326 /* In the CONFIG command we need to add vanilla C string as bulk replies */
327 void addReplyBulkCString(redisClient
*c
, char *s
) {
329 addReply(c
,shared
.nullbulk
);
331 robj
*o
= createStringObject(s
,strlen(s
));
337 void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
343 REDIS_NOTUSED(privdata
);
345 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
347 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
350 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
351 if ((c
= createClient(cfd
)) == NULL
) {
352 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
353 close(cfd
); /* May be already closed, just ingore errors */
356 /* If maxclient directive is set and this is one client more... close the
357 * connection. Note that we create the client instead to check before
358 * for this condition, since now the socket is already set in nonblocking
359 * mode and we can send an error for free using the Kernel I/O */
360 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
361 char *err
= "-ERR max number of clients reached\r\n";
363 /* That's a best effort error message, don't check write errors */
364 if (write(c
->fd
,err
,strlen(err
)) == -1) {
365 /* Nothing to do, Just to avoid the warning... */
370 server
.stat_numconnections
++;
373 static void freeClientArgv(redisClient
*c
) {
375 for (j
= 0; j
< c
->argc
; j
++)
376 decrRefCount(c
->argv
[j
]);
380 void freeClient(redisClient
*c
) {
383 /* Note that if the client we are freeing is blocked into a blocking
384 * call, we have to set querybuf to NULL *before* to call
385 * unblockClientWaitingData() to avoid processInputBuffer() will get
386 * called. Also it is important to remove the file events after
387 * this, because this call adds the READABLE event. */
388 sdsfree(c
->querybuf
);
390 if (c
->flags
& REDIS_BLOCKED
)
391 unblockClientWaitingData(c
);
393 /* UNWATCH all the keys */
395 listRelease(c
->watched_keys
);
396 /* Unsubscribe from all the pubsub channels */
397 pubsubUnsubscribeAllChannels(c
,0);
398 pubsubUnsubscribeAllPatterns(c
,0);
399 dictRelease(c
->pubsub_channels
);
400 listRelease(c
->pubsub_patterns
);
401 /* Obvious cleanup */
402 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
403 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
404 listRelease(c
->reply
);
407 /* Remove from the list of clients */
408 ln
= listSearchKey(server
.clients
,c
);
409 redisAssert(ln
!= NULL
);
410 listDelNode(server
.clients
,ln
);
411 /* Remove from the list of clients waiting for swapped keys, or ready
412 * to be restarted, but not yet woken up again. */
413 if (c
->flags
& REDIS_IO_WAIT
) {
414 redisAssert(server
.vm_enabled
);
415 if (listLength(c
->io_keys
) == 0) {
416 ln
= listSearchKey(server
.io_ready_clients
,c
);
418 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
419 * it should be present in the list io_ready_clients */
420 redisAssert(ln
!= NULL
);
421 listDelNode(server
.io_ready_clients
,ln
);
423 while (listLength(c
->io_keys
)) {
424 ln
= listFirst(c
->io_keys
);
425 dontWaitForSwappedKey(c
,ln
->value
);
428 server
.vm_blocked_clients
--;
430 listRelease(c
->io_keys
);
431 /* Master/slave cleanup.
432 * Case 1: we lost the connection with a slave. */
433 if (c
->flags
& REDIS_SLAVE
) {
434 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
436 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
437 ln
= listSearchKey(l
,c
);
438 redisAssert(ln
!= NULL
);
442 /* Case 2: we lost the connection with the master. */
443 if (c
->flags
& REDIS_MASTER
) {
444 server
.master
= NULL
;
445 server
.replstate
= REDIS_REPL_CONNECT
;
446 /* Since we lost the connection with the master, we should also
447 * close the connection with all our slaves if we have any, so
448 * when we'll resync with the master the other slaves will sync again
449 * with us as well. Note that also when the slave is not connected
450 * to the master it will keep refusing connections by other slaves. */
451 while (listLength(server
.slaves
)) {
452 ln
= listFirst(server
.slaves
);
453 freeClient((redisClient
*)ln
->value
);
458 freeClientMultiState(c
);
462 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
463 redisClient
*c
= privdata
;
464 int nwritten
= 0, totwritten
= 0, objlen
;
469 /* Use writev() if we have enough buffers to send */
470 if (!server
.glueoutputbuf
&&
471 listLength(c
->reply
) > REDIS_WRITEV_THRESHOLD
&&
472 !(c
->flags
& REDIS_MASTER
))
474 sendReplyToClientWritev(el
, fd
, privdata
, mask
);
478 while(c
->bufpos
> 0 || listLength(c
->reply
)) {
480 if (c
->flags
& REDIS_MASTER
) {
481 /* Don't reply to a master */
482 nwritten
= c
->bufpos
- c
->sentlen
;
484 nwritten
= write(fd
,c
->buf
+c
->sentlen
,c
->bufpos
-c
->sentlen
);
485 if (nwritten
<= 0) break;
487 c
->sentlen
+= nwritten
;
488 totwritten
+= nwritten
;
490 /* If the buffer was sent, set bufpos to zero to continue with
491 * the remainder of the reply. */
492 if (c
->sentlen
== c
->bufpos
) {
497 o
= listNodeValue(listFirst(c
->reply
));
498 objlen
= sdslen(o
->ptr
);
501 listDelNode(c
->reply
,listFirst(c
->reply
));
505 if (c
->flags
& REDIS_MASTER
) {
506 /* Don't reply to a master */
507 nwritten
= objlen
- c
->sentlen
;
509 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
,objlen
-c
->sentlen
);
510 if (nwritten
<= 0) break;
512 c
->sentlen
+= nwritten
;
513 totwritten
+= nwritten
;
515 /* If we fully sent the object on head go to the next one */
516 if (c
->sentlen
== objlen
) {
517 listDelNode(c
->reply
,listFirst(c
->reply
));
521 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
522 * bytes, in a single threaded server it's a good idea to serve
523 * other clients as well, even if a very large request comes from
524 * super fast link that is always able to accept data (in real world
525 * scenario think about 'KEYS *' against the loopback interfae) */
526 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
528 if (nwritten
== -1) {
529 if (errno
== EAGAIN
) {
532 redisLog(REDIS_VERBOSE
,
533 "Error writing to client: %s", strerror(errno
));
538 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
539 if (listLength(c
->reply
) == 0) {
541 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
543 /* Close connection after entire reply has been sent. */
544 if (c
->flags
& REDIS_QUIT
) freeClient(c
);
545 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) freeClient(c
);
549 void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
551 redisClient
*c
= privdata
;
552 int nwritten
= 0, totwritten
= 0, objlen
, willwrite
;
554 struct iovec iov
[REDIS_WRITEV_IOVEC_COUNT
];
560 while (listLength(c
->reply
)) {
565 /* fill-in the iov[] array */
566 for(node
= listFirst(c
->reply
); node
; node
= listNextNode(node
)) {
567 o
= listNodeValue(node
);
568 objlen
= sdslen(o
->ptr
);
570 if (totwritten
+ objlen
- offset
> REDIS_MAX_WRITE_PER_EVENT
)
573 if(ion
== REDIS_WRITEV_IOVEC_COUNT
)
574 break; /* no more iovecs */
576 iov
[ion
].iov_base
= ((char*)o
->ptr
) + offset
;
577 iov
[ion
].iov_len
= objlen
- offset
;
578 willwrite
+= objlen
- offset
;
579 offset
= 0; /* just for the first item */
586 /* write all collected blocks at once */
587 if((nwritten
= writev(fd
, iov
, ion
)) < 0) {
588 if (errno
!= EAGAIN
) {
589 redisLog(REDIS_VERBOSE
,
590 "Error writing to client: %s", strerror(errno
));
597 totwritten
+= nwritten
;
600 /* remove written robjs from c->reply */
601 while (nwritten
&& listLength(c
->reply
)) {
602 o
= listNodeValue(listFirst(c
->reply
));
603 objlen
= sdslen(o
->ptr
);
605 if(nwritten
>= objlen
- offset
) {
606 listDelNode(c
->reply
, listFirst(c
->reply
));
607 nwritten
-= objlen
- offset
;
611 c
->sentlen
+= nwritten
;
619 c
->lastinteraction
= time(NULL
);
621 if (listLength(c
->reply
) == 0) {
623 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
627 /* resetClient prepare the client to process the next command */
628 void resetClient(redisClient
*c
) {
635 void closeTimedoutClients(void) {
638 time_t now
= time(NULL
);
641 listRewind(server
.clients
,&li
);
642 while ((ln
= listNext(&li
)) != NULL
) {
643 c
= listNodeValue(ln
);
644 if (server
.maxidletime
&&
645 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
646 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
647 !(c
->flags
& REDIS_BLOCKED
) && /* no timeout for BLPOP */
648 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
649 listLength(c
->pubsub_patterns
) == 0 &&
650 (now
- c
->lastinteraction
> server
.maxidletime
))
652 redisLog(REDIS_VERBOSE
,"Closing idle client");
654 } else if (c
->flags
& REDIS_BLOCKED
) {
655 if (c
->blockingto
!= 0 && c
->blockingto
< now
) {
656 addReply(c
,shared
.nullmultibulk
);
657 unblockClientWaitingData(c
);
663 int processInlineBuffer(redisClient
*c
) {
664 char *newline
= strstr(c
->querybuf
,"\r\n");
669 /* Nothing to do without a \r\n */
673 /* Split the input buffer up to the \r\n */
674 querylen
= newline
-(c
->querybuf
);
675 argv
= sdssplitlen(c
->querybuf
,querylen
," ",1,&argc
);
677 /* Leave data after the first line of the query in the buffer */
678 c
->querybuf
= sdsrange(c
->querybuf
,querylen
+2,-1);
680 /* Setup argv array on client structure */
681 if (c
->argv
) zfree(c
->argv
);
682 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
684 /* Create redis objects for all arguments. */
685 for (c
->argc
= 0, j
= 0; j
< argc
; j
++) {
686 if (sdslen(argv
[j
])) {
687 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
697 /* Helper function. Trims query buffer to make the function that processes
698 * multi bulk requests idempotent. */
699 static void setProtocolError(redisClient
*c
, int pos
) {
700 c
->flags
|= REDIS_CLOSE_AFTER_REPLY
;
701 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
704 int processMultibulkBuffer(redisClient
*c
) {
705 char *newline
= NULL
;
710 if (c
->multibulklen
== 0) {
711 /* The client should have been reset */
712 redisAssert(c
->argc
== 0);
714 /* Multi bulk length cannot be read without a \r\n */
715 newline
= strstr(c
->querybuf
,"\r\n");
719 /* We know for sure there is a whole line since newline != NULL,
720 * so go ahead and find out the multi bulk length. */
721 redisAssert(c
->querybuf
[0] == '*');
722 c
->multibulklen
= strtol(c
->querybuf
+1,&eptr
,10);
723 pos
= (newline
-c
->querybuf
)+2;
724 if (c
->multibulklen
<= 0) {
725 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
729 /* Setup argv array on client structure */
730 if (c
->argv
) zfree(c
->argv
);
731 c
->argv
= zmalloc(sizeof(robj
*)*c
->multibulklen
);
733 /* Search new newline */
734 newline
= strstr(c
->querybuf
+pos
,"\r\n");
737 redisAssert(c
->multibulklen
> 0);
738 while(c
->multibulklen
) {
739 /* Read bulk length if unknown */
740 if (c
->bulklen
== -1) {
741 newline
= strstr(c
->querybuf
+pos
,"\r\n");
742 if (newline
!= NULL
) {
743 if (c
->querybuf
[pos
] != '$') {
744 addReplyErrorFormat(c
,
745 "Protocol error: expected '$', got '%c'",
747 setProtocolError(c
,pos
);
751 bulklen
= strtol(c
->querybuf
+pos
+1,&eptr
,10);
752 tolerr
= (eptr
[0] != '\r');
753 if (tolerr
|| bulklen
== LONG_MIN
|| bulklen
== LONG_MAX
||
754 bulklen
< 0 || bulklen
> 1024*1024*1024)
756 addReplyError(c
,"Protocol error: invalid bulk length");
757 setProtocolError(c
,pos
);
760 pos
+= eptr
-(c
->querybuf
+pos
)+2;
761 c
->bulklen
= bulklen
;
763 /* No newline in current buffer, so wait for more data */
768 /* Read bulk argument */
769 if (sdslen(c
->querybuf
)-pos
< (unsigned)(c
->bulklen
+2)) {
770 /* Not enough data (+2 == trailing \r\n) */
773 c
->argv
[c
->argc
++] = createStringObject(c
->querybuf
+pos
,c
->bulklen
);
781 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
783 /* We're done when c->multibulk == 0 */
784 if (c
->multibulklen
== 0) {
790 void processInputBuffer(redisClient
*c
) {
791 /* Keep processing while there is something in the input buffer */
792 while(sdslen(c
->querybuf
)) {
793 /* Before to process the input buffer, make sure the client is not
794 * waitig for a blocking operation such as BLPOP. Note that the first
795 * iteration the client is never blocked, otherwise the processInputBuffer
796 * would not be called at all, but after the execution of the first commands
797 * in the input buffer the client may be blocked, and the "goto again"
798 * will try to reiterate. The following line will make it return asap. */
799 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
801 /* Never continue to process the input buffer after QUIT. After the output
802 * buffer is flushed (with the OK), the connection will be dropped. */
803 if (c
->flags
& REDIS_QUIT
) return;
805 /* Determine request type when unknown. */
807 if (c
->querybuf
[0] == '*') {
808 c
->reqtype
= REDIS_REQ_MULTIBULK
;
810 c
->reqtype
= REDIS_REQ_INLINE
;
814 if (c
->reqtype
== REDIS_REQ_INLINE
) {
815 if (processInlineBuffer(c
) != REDIS_OK
) break;
816 } else if (c
->reqtype
== REDIS_REQ_MULTIBULK
) {
817 if (processMultibulkBuffer(c
) != REDIS_OK
) break;
819 redisPanic("Unknown request type");
822 /* Multibulk processing could see a <= 0 length. */
826 /* Only reset the client when the command was executed. */
827 if (processCommand(c
) == REDIS_OK
)
833 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
834 redisClient
*c
= (redisClient
*) privdata
;
835 char buf
[REDIS_IOBUF_LEN
];
840 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
842 if (errno
== EAGAIN
) {
845 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
849 } else if (nread
== 0) {
850 redisLog(REDIS_VERBOSE
, "Client closed connection");
855 c
->querybuf
= sdscatlen(c
->querybuf
,buf
,nread
);
856 c
->lastinteraction
= time(NULL
);
860 processInputBuffer(c
);