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 if (c
->fd
<= 0) return REDIS_ERR
;
64 if (c
->bufpos
== 0 && listLength(c
->reply
) == 0 &&
65 (c
->replstate
== REDIS_REPL_NONE
||
66 c
->replstate
== REDIS_REPL_ONLINE
) &&
67 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
68 sendReplyToClient
, c
) == AE_ERR
) return REDIS_ERR
;
72 /* Create a duplicate of the last object in the reply list when
73 * it is not exclusively owned by the reply list. */
74 robj
*dupLastObjectIfNeeded(list
*reply
) {
77 redisAssert(listLength(reply
) > 0);
79 cur
= listNodeValue(ln
);
80 if (cur
->refcount
> 1) {
81 new = dupStringObject(cur
);
83 listNodeValue(ln
) = new;
85 return listNodeValue(ln
);
88 /* -----------------------------------------------------------------------------
89 * Low level functions to add more data to output buffers.
90 * -------------------------------------------------------------------------- */
92 int _addReplyToBuffer(redisClient
*c
, char *s
, size_t len
) {
93 size_t available
= sizeof(c
->buf
)-c
->bufpos
;
95 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return REDIS_OK
;
97 /* If there already are entries in the reply list, we cannot
98 * add anything more to the static buffer. */
99 if (listLength(c
->reply
) > 0) return REDIS_ERR
;
101 /* Check that the buffer has enough space available for this string. */
102 if (len
> available
) return REDIS_ERR
;
104 memcpy(c
->buf
+c
->bufpos
,s
,len
);
109 void _addReplyObjectToList(redisClient
*c
, robj
*o
) {
112 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
114 if (listLength(c
->reply
) == 0) {
116 listAddNodeTail(c
->reply
,o
);
118 tail
= listNodeValue(listLast(c
->reply
));
120 /* Append to this object when possible. */
121 if (tail
->ptr
!= NULL
&&
122 sdslen(tail
->ptr
)+sdslen(o
->ptr
) <= REDIS_REPLY_CHUNK_BYTES
)
124 tail
= dupLastObjectIfNeeded(c
->reply
);
125 tail
->ptr
= sdscatlen(tail
->ptr
,o
->ptr
,sdslen(o
->ptr
));
128 listAddNodeTail(c
->reply
,o
);
133 /* This method takes responsibility over the sds. When it is no longer
134 * needed it will be free'd, otherwise it ends up in a robj. */
135 void _addReplySdsToList(redisClient
*c
, sds s
) {
138 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
140 if (listLength(c
->reply
) == 0) {
141 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
143 tail
= listNodeValue(listLast(c
->reply
));
145 /* Append to this object when possible. */
146 if (tail
->ptr
!= NULL
&&
147 sdslen(tail
->ptr
)+sdslen(s
) <= REDIS_REPLY_CHUNK_BYTES
)
149 tail
= dupLastObjectIfNeeded(c
->reply
);
150 tail
->ptr
= sdscatlen(tail
->ptr
,s
,sdslen(s
));
153 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,s
));
158 void _addReplyStringToList(redisClient
*c
, char *s
, size_t len
) {
161 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
163 if (listLength(c
->reply
) == 0) {
164 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
166 tail
= listNodeValue(listLast(c
->reply
));
168 /* Append to this object when possible. */
169 if (tail
->ptr
!= NULL
&&
170 sdslen(tail
->ptr
)+len
<= REDIS_REPLY_CHUNK_BYTES
)
172 tail
= dupLastObjectIfNeeded(c
->reply
);
173 tail
->ptr
= sdscatlen(tail
->ptr
,s
,len
);
175 listAddNodeTail(c
->reply
,createStringObject(s
,len
));
180 /* -----------------------------------------------------------------------------
181 * Higher level functions to queue data on the client output buffer.
182 * The following functions are the ones that commands implementations will call.
183 * -------------------------------------------------------------------------- */
185 void addReply(redisClient
*c
, robj
*obj
) {
186 if (_installWriteEvent(c
) != REDIS_OK
) return;
188 /* This is an important place where we can avoid copy-on-write
189 * when there is a saving child running, avoiding touching the
190 * refcount field of the object if it's not needed.
192 * If the encoding is RAW and there is room in the static buffer
193 * we'll be able to send the object to the client without
194 * messing with its page. */
195 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
196 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
197 _addReplyObjectToList(c
,obj
);
199 /* FIXME: convert the long into string and use _addReplyToBuffer()
200 * instead of calling getDecodedObject. As this place in the
201 * code is too performance critical. */
202 obj
= getDecodedObject(obj
);
203 if (_addReplyToBuffer(c
,obj
->ptr
,sdslen(obj
->ptr
)) != REDIS_OK
)
204 _addReplyObjectToList(c
,obj
);
209 void addReplySds(redisClient
*c
, sds s
) {
210 if (_installWriteEvent(c
) != REDIS_OK
) {
211 /* The caller expects the sds to be free'd. */
215 if (_addReplyToBuffer(c
,s
,sdslen(s
)) == REDIS_OK
) {
218 /* This method free's the sds when it is no longer needed. */
219 _addReplySdsToList(c
,s
);
223 void addReplyString(redisClient
*c
, char *s
, size_t len
) {
224 if (_installWriteEvent(c
) != REDIS_OK
) return;
225 if (_addReplyToBuffer(c
,s
,len
) != REDIS_OK
)
226 _addReplyStringToList(c
,s
,len
);
229 void _addReplyError(redisClient
*c
, char *s
, size_t len
) {
230 addReplyString(c
,"-ERR ",5);
231 addReplyString(c
,s
,len
);
232 addReplyString(c
,"\r\n",2);
235 void addReplyError(redisClient
*c
, char *err
) {
236 _addReplyError(c
,err
,strlen(err
));
239 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...) {
242 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
244 _addReplyError(c
,s
,sdslen(s
));
248 void _addReplyStatus(redisClient
*c
, char *s
, size_t len
) {
249 addReplyString(c
,"+",1);
250 addReplyString(c
,s
,len
);
251 addReplyString(c
,"\r\n",2);
254 void addReplyStatus(redisClient
*c
, char *status
) {
255 _addReplyStatus(c
,status
,strlen(status
));
258 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...) {
261 sds s
= sdscatvprintf(sdsempty(),fmt
,ap
);
263 _addReplyStatus(c
,s
,sdslen(s
));
267 /* Adds an empty object to the reply list that will contain the multi bulk
268 * length, which is not known when this function is called. */
269 void *addDeferredMultiBulkLength(redisClient
*c
) {
270 /* Note that we install the write event here even if the object is not
271 * ready to be sent, since we are sure that before returning to the
272 * event loop setDeferredMultiBulkLength() will be called. */
273 if (_installWriteEvent(c
) != REDIS_OK
) return NULL
;
274 listAddNodeTail(c
->reply
,createObject(REDIS_STRING
,NULL
));
275 return listLast(c
->reply
);
278 /* Populate the length object and try glueing it to the next chunk. */
279 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
) {
280 listNode
*ln
= (listNode
*)node
;
283 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
284 if (node
== NULL
) return;
286 len
= listNodeValue(ln
);
287 len
->ptr
= sdscatprintf(sdsempty(),"*%ld\r\n",length
);
288 if (ln
->next
!= NULL
) {
289 next
= listNodeValue(ln
->next
);
291 /* Only glue when the next node is non-NULL (an sds in this case) */
292 if (next
->ptr
!= NULL
) {
293 len
->ptr
= sdscatlen(len
->ptr
,next
->ptr
,sdslen(next
->ptr
));
294 listDelNode(c
->reply
,ln
->next
);
299 /* Add a duble as a bulk reply */
300 void addReplyDouble(redisClient
*c
, double d
) {
301 char dbuf
[128], sbuf
[128];
303 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
304 slen
= snprintf(sbuf
,sizeof(sbuf
),"$%d\r\n%s\r\n",dlen
,dbuf
);
305 addReplyString(c
,sbuf
,slen
);
308 /* Add a long long as integer reply or bulk len / multi bulk count.
309 * Basically this is used to output <prefix><long long><crlf>. */
310 void _addReplyLongLong(redisClient
*c
, long long ll
, char prefix
) {
314 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
317 addReplyString(c
,buf
,len
+3);
320 void addReplyLongLong(redisClient
*c
, long long ll
) {
321 _addReplyLongLong(c
,ll
,':');
324 void addReplyMultiBulkLen(redisClient
*c
, long length
) {
325 _addReplyLongLong(c
,length
,'*');
328 /* Create the length prefix of a bulk reply, example: $2234 */
329 void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
332 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
333 len
= sdslen(obj
->ptr
);
335 long n
= (long)obj
->ptr
;
337 /* Compute how many bytes will take this integer as a radix 10 string */
343 while((n
= n
/10) != 0) {
347 _addReplyLongLong(c
,len
,'$');
350 /* Add a Redis Object as a bulk reply */
351 void addReplyBulk(redisClient
*c
, robj
*obj
) {
352 addReplyBulkLen(c
,obj
);
354 addReply(c
,shared
.crlf
);
357 /* Add a C buffer as bulk reply */
358 void addReplyBulkCBuffer(redisClient
*c
, void *p
, size_t len
) {
359 _addReplyLongLong(c
,len
,'$');
360 addReplyString(c
,p
,len
);
361 addReply(c
,shared
.crlf
);
364 /* Add a C nul term string as bulk reply */
365 void addReplyBulkCString(redisClient
*c
, char *s
) {
367 addReply(c
,shared
.nullbulk
);
369 addReplyBulkCBuffer(c
,s
,strlen(s
));
373 /* Add a long long as a bulk reply */
374 void addReplyBulkLongLong(redisClient
*c
, long long ll
) {
378 len
= ll2string(buf
,64,ll
);
379 addReplyBulkCBuffer(c
,buf
,len
);
382 static void acceptCommonHandler(int fd
) {
384 if ((c
= createClient(fd
)) == NULL
) {
385 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
386 close(fd
); /* May be already closed, just ingore errors */
389 /* If maxclient directive is set and this is one client more... close the
390 * connection. Note that we create the client instead to check before
391 * for this condition, since now the socket is already set in nonblocking
392 * mode and we can send an error for free using the Kernel I/O */
393 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
394 char *err
= "-ERR max number of clients reached\r\n";
396 /* That's a best effort error message, don't check write errors */
397 if (write(c
->fd
,err
,strlen(err
)) == -1) {
398 /* Nothing to do, Just to avoid the warning... */
403 server
.stat_numconnections
++;
406 void acceptTcpHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
411 REDIS_NOTUSED(privdata
);
413 cfd
= anetTcpAccept(server
.neterr
, fd
, cip
, &cport
);
415 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
418 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
419 acceptCommonHandler(cfd
);
422 void acceptUnixHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
426 REDIS_NOTUSED(privdata
);
428 cfd
= anetUnixAccept(server
.neterr
, fd
);
430 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
433 redisLog(REDIS_VERBOSE
,"Accepted connection to %s", server
.unixsocket
);
434 acceptCommonHandler(cfd
);
438 static void freeClientArgv(redisClient
*c
) {
440 for (j
= 0; j
< c
->argc
; j
++)
441 decrRefCount(c
->argv
[j
]);
445 void freeClient(redisClient
*c
) {
448 /* Note that if the client we are freeing is blocked into a blocking
449 * call, we have to set querybuf to NULL *before* to call
450 * unblockClientWaitingData() to avoid processInputBuffer() will get
451 * called. Also it is important to remove the file events after
452 * this, because this call adds the READABLE event. */
453 sdsfree(c
->querybuf
);
455 if (c
->flags
& REDIS_BLOCKED
)
456 unblockClientWaitingData(c
);
458 /* UNWATCH all the keys */
460 listRelease(c
->watched_keys
);
461 /* Unsubscribe from all the pubsub channels */
462 pubsubUnsubscribeAllChannels(c
,0);
463 pubsubUnsubscribeAllPatterns(c
,0);
464 dictRelease(c
->pubsub_channels
);
465 listRelease(c
->pubsub_patterns
);
466 /* Obvious cleanup */
467 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
468 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
469 listRelease(c
->reply
);
472 /* Remove from the list of clients */
473 ln
= listSearchKey(server
.clients
,c
);
474 redisAssert(ln
!= NULL
);
475 listDelNode(server
.clients
,ln
);
476 /* When client was just unblocked because of a blocking operation,
477 * remove it from the list with unblocked clients. */
478 if (c
->flags
& REDIS_UNBLOCKED
) {
479 ln
= listSearchKey(server
.unblocked_clients
,c
);
480 redisAssert(ln
!= NULL
);
481 listDelNode(server
.unblocked_clients
,ln
);
483 /* Remove from the list of clients waiting for swapped keys, or ready
484 * to be restarted, but not yet woken up again. */
485 if (c
->flags
& REDIS_IO_WAIT
) {
486 redisAssert(server
.ds_enabled
);
487 if (listLength(c
->io_keys
) == 0) {
488 ln
= listSearchKey(server
.io_ready_clients
,c
);
490 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
491 * it should be present in the list io_ready_clients */
492 redisAssert(ln
!= NULL
);
493 listDelNode(server
.io_ready_clients
,ln
);
495 while (listLength(c
->io_keys
)) {
496 ln
= listFirst(c
->io_keys
);
497 dontWaitForSwappedKey(c
,ln
->value
);
500 server
.cache_blocked_clients
--;
502 listRelease(c
->io_keys
);
503 /* Master/slave cleanup.
504 * Case 1: we lost the connection with a slave. */
505 if (c
->flags
& REDIS_SLAVE
) {
506 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
508 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
509 ln
= listSearchKey(l
,c
);
510 redisAssert(ln
!= NULL
);
514 /* Case 2: we lost the connection with the master. */
515 if (c
->flags
& REDIS_MASTER
) {
516 server
.master
= NULL
;
517 server
.replstate
= REDIS_REPL_CONNECT
;
518 /* Since we lost the connection with the master, we should also
519 * close the connection with all our slaves if we have any, so
520 * when we'll resync with the master the other slaves will sync again
521 * with us as well. Note that also when the slave is not connected
522 * to the master it will keep refusing connections by other slaves. */
523 while (listLength(server
.slaves
)) {
524 ln
= listFirst(server
.slaves
);
525 freeClient((redisClient
*)ln
->value
);
530 freeClientMultiState(c
);
534 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
535 redisClient
*c
= privdata
;
536 int nwritten
= 0, totwritten
= 0, objlen
;
541 while(c
->bufpos
> 0 || listLength(c
->reply
)) {
543 if (c
->flags
& REDIS_MASTER
) {
544 /* Don't reply to a master */
545 nwritten
= c
->bufpos
- c
->sentlen
;
547 nwritten
= write(fd
,c
->buf
+c
->sentlen
,c
->bufpos
-c
->sentlen
);
548 if (nwritten
<= 0) break;
550 c
->sentlen
+= nwritten
;
551 totwritten
+= nwritten
;
553 /* If the buffer was sent, set bufpos to zero to continue with
554 * the remainder of the reply. */
555 if (c
->sentlen
== c
->bufpos
) {
560 o
= listNodeValue(listFirst(c
->reply
));
561 objlen
= sdslen(o
->ptr
);
564 listDelNode(c
->reply
,listFirst(c
->reply
));
568 if (c
->flags
& REDIS_MASTER
) {
569 /* Don't reply to a master */
570 nwritten
= objlen
- c
->sentlen
;
572 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
,objlen
-c
->sentlen
);
573 if (nwritten
<= 0) break;
575 c
->sentlen
+= nwritten
;
576 totwritten
+= nwritten
;
578 /* If we fully sent the object on head go to the next one */
579 if (c
->sentlen
== objlen
) {
580 listDelNode(c
->reply
,listFirst(c
->reply
));
584 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
585 * bytes, in a single threaded server it's a good idea to serve
586 * other clients as well, even if a very large request comes from
587 * super fast link that is always able to accept data (in real world
588 * scenario think about 'KEYS *' against the loopback interfae) */
589 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
591 if (nwritten
== -1) {
592 if (errno
== EAGAIN
) {
595 redisLog(REDIS_VERBOSE
,
596 "Error writing to client: %s", strerror(errno
));
601 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
602 if (listLength(c
->reply
) == 0) {
604 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
606 /* Close connection after entire reply has been sent. */
607 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) freeClient(c
);
611 /* resetClient prepare the client to process the next command */
612 void resetClient(redisClient
*c
) {
619 void closeTimedoutClients(void) {
622 time_t now
= time(NULL
);
625 listRewind(server
.clients
,&li
);
626 while ((ln
= listNext(&li
)) != NULL
) {
627 c
= listNodeValue(ln
);
628 if (server
.maxidletime
&&
629 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
630 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
631 !(c
->flags
& REDIS_BLOCKED
) && /* no timeout for BLPOP */
632 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
633 listLength(c
->pubsub_patterns
) == 0 &&
634 (now
- c
->lastinteraction
> server
.maxidletime
))
636 redisLog(REDIS_VERBOSE
,"Closing idle client");
638 } else if (c
->flags
& REDIS_BLOCKED
) {
639 if (c
->bpop
.timeout
!= 0 && c
->bpop
.timeout
< now
) {
640 addReply(c
,shared
.nullmultibulk
);
641 unblockClientWaitingData(c
);
647 int processInlineBuffer(redisClient
*c
) {
648 char *newline
= strstr(c
->querybuf
,"\r\n");
653 /* Nothing to do without a \r\n */
657 /* Split the input buffer up to the \r\n */
658 querylen
= newline
-(c
->querybuf
);
659 argv
= sdssplitlen(c
->querybuf
,querylen
," ",1,&argc
);
661 /* Leave data after the first line of the query in the buffer */
662 c
->querybuf
= sdsrange(c
->querybuf
,querylen
+2,-1);
664 /* Setup argv array on client structure */
665 if (c
->argv
) zfree(c
->argv
);
666 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
668 /* Create redis objects for all arguments. */
669 for (c
->argc
= 0, j
= 0; j
< argc
; j
++) {
670 if (sdslen(argv
[j
])) {
671 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
681 /* Helper function. Trims query buffer to make the function that processes
682 * multi bulk requests idempotent. */
683 static void setProtocolError(redisClient
*c
, int pos
) {
684 c
->flags
|= REDIS_CLOSE_AFTER_REPLY
;
685 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
688 int processMultibulkBuffer(redisClient
*c
) {
689 char *newline
= NULL
;
694 if (c
->multibulklen
== 0) {
695 /* The client should have been reset */
696 redisAssert(c
->argc
== 0);
698 /* Multi bulk length cannot be read without a \r\n */
699 newline
= strstr(c
->querybuf
,"\r\n");
703 /* We know for sure there is a whole line since newline != NULL,
704 * so go ahead and find out the multi bulk length. */
705 redisAssert(c
->querybuf
[0] == '*');
706 c
->multibulklen
= strtol(c
->querybuf
+1,&eptr
,10);
707 pos
= (newline
-c
->querybuf
)+2;
708 if (c
->multibulklen
<= 0) {
709 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
711 } else if (c
->multibulklen
> 1024*1024) {
712 addReplyError(c
,"Protocol error: invalid multibulk length");
713 setProtocolError(c
,pos
);
717 /* Setup argv array on client structure */
718 if (c
->argv
) zfree(c
->argv
);
719 c
->argv
= zmalloc(sizeof(robj
*)*c
->multibulklen
);
721 /* Search new newline */
722 newline
= strstr(c
->querybuf
+pos
,"\r\n");
725 redisAssert(c
->multibulklen
> 0);
726 while(c
->multibulklen
) {
727 /* Read bulk length if unknown */
728 if (c
->bulklen
== -1) {
729 newline
= strstr(c
->querybuf
+pos
,"\r\n");
730 if (newline
!= NULL
) {
731 if (c
->querybuf
[pos
] != '$') {
732 addReplyErrorFormat(c
,
733 "Protocol error: expected '$', got '%c'",
735 setProtocolError(c
,pos
);
739 bulklen
= strtol(c
->querybuf
+pos
+1,&eptr
,10);
740 tolerr
= (eptr
[0] != '\r');
741 if (tolerr
|| bulklen
== LONG_MIN
|| bulklen
== LONG_MAX
||
742 bulklen
< 0 || bulklen
> 512*1024*1024)
744 addReplyError(c
,"Protocol error: invalid bulk length");
745 setProtocolError(c
,pos
);
748 pos
+= eptr
-(c
->querybuf
+pos
)+2;
749 c
->bulklen
= bulklen
;
751 /* No newline in current buffer, so wait for more data */
756 /* Read bulk argument */
757 if (sdslen(c
->querybuf
)-pos
< (unsigned)(c
->bulklen
+2)) {
758 /* Not enough data (+2 == trailing \r\n) */
761 c
->argv
[c
->argc
++] = createStringObject(c
->querybuf
+pos
,c
->bulklen
);
769 c
->querybuf
= sdsrange(c
->querybuf
,pos
,-1);
771 /* We're done when c->multibulk == 0 */
772 if (c
->multibulklen
== 0) {
778 void processInputBuffer(redisClient
*c
) {
779 /* Keep processing while there is something in the input buffer */
780 while(sdslen(c
->querybuf
)) {
781 /* Immediately abort if the client is in the middle of something. */
782 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
784 /* REDIS_CLOSE_AFTER_REPLY closes the connection once the reply is
785 * written to the client. Make sure to not let the reply grow after
786 * this flag has been set (i.e. don't process more commands). */
787 if (c
->flags
& REDIS_CLOSE_AFTER_REPLY
) return;
789 /* Determine request type when unknown. */
791 if (c
->querybuf
[0] == '*') {
792 c
->reqtype
= REDIS_REQ_MULTIBULK
;
794 c
->reqtype
= REDIS_REQ_INLINE
;
798 if (c
->reqtype
== REDIS_REQ_INLINE
) {
799 if (processInlineBuffer(c
) != REDIS_OK
) break;
800 } else if (c
->reqtype
== REDIS_REQ_MULTIBULK
) {
801 if (processMultibulkBuffer(c
) != REDIS_OK
) break;
803 redisPanic("Unknown request type");
806 /* Multibulk processing could see a <= 0 length. */
810 /* Only reset the client when the command was executed. */
811 if (processCommand(c
) == REDIS_OK
)
817 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
818 redisClient
*c
= (redisClient
*) privdata
;
819 char buf
[REDIS_IOBUF_LEN
];
824 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
826 if (errno
== EAGAIN
) {
829 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
833 } else if (nread
== 0) {
834 redisLog(REDIS_VERBOSE
, "Client closed connection");
839 c
->querybuf
= sdscatlen(c
->querybuf
,buf
,nread
);
840 c
->lastinteraction
= time(NULL
);
844 processInputBuffer(c
);
847 void getClientsMaxBuffers(unsigned long *longest_output_list
,
848 unsigned long *biggest_input_buffer
) {
852 unsigned long lol
= 0, bib
= 0;
854 listRewind(server
.clients
,&li
);
855 while ((ln
= listNext(&li
)) != NULL
) {
856 c
= listNodeValue(ln
);
858 if (listLength(c
->reply
) > lol
) lol
= listLength(c
->reply
);
859 if (sdslen(c
->querybuf
) > bib
) bib
= sdslen(c
->querybuf
);
861 *longest_output_list
= lol
;
862 *biggest_input_buffer
= bib
;