MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / clusterReadHandler

Function clusterReadHandler

src/cluster.cpp:2370–2440  ·  view source on GitHub ↗

Read data. Try to read the first field of the header first to check the * full length of the packet. When a whole packet is in memory this function * will call the function to process the packet. And so forth. */

Source from the content-addressed store, hash-verified

2368 * full length of the packet. When a whole packet is in memory this function
2369 * will call the function to process the packet. And so forth. */
2370void clusterReadHandler(connection *conn) {
2371 clusterMsg buf[1];
2372 ssize_t nread;
2373 clusterMsg *hdr;
2374 clusterLink *link = (clusterLink*)connGetPrivateData(conn);
2375 unsigned int readlen, rcvbuflen;
2376
2377 while(1) { /* Read as long as there is data to read. */
2378 rcvbuflen = link->rcvbuf_len;
2379 if (rcvbuflen < 8) {
2380 /* First, obtain the first 8 bytes to get the full message
2381 * length. */
2382 readlen = 8 - rcvbuflen;
2383 } else {
2384 /* Finally read the full message. */
2385 hdr = (clusterMsg*) link->rcvbuf;
2386 if (rcvbuflen == 8) {
2387 /* Perform some sanity check on the message signature
2388 * and length. */
2389 if (memcmp(hdr->sig,"RCmb",4) != 0 ||
2390 ntohl(hdr->totlen) < CLUSTERMSG_MIN_LEN)
2391 {
2392 serverLog(LL_WARNING,
2393 "Bad message length or signature received "
2394 "from Cluster bus.");
2395 handleLinkIOError(link);
2396 return;
2397 }
2398 }
2399 readlen = ntohl(hdr->totlen) - rcvbuflen;
2400 if (readlen > sizeof(buf)) readlen = sizeof(buf);
2401 }
2402
2403 nread = connRead(conn,buf,readlen);
2404 if (nread == -1 && (connGetState(conn) == CONN_STATE_CONNECTED)) return; /* No more data ready. */
2405
2406 if (nread <= 0) {
2407 /* I/O error... */
2408 serverLog(LL_DEBUG,"I/O error reading from node link: %s",
2409 (nread == 0) ? "connection closed" : connGetLastError(conn));
2410 handleLinkIOError(link);
2411 return;
2412 } else {
2413 /* Read data and recast the pointer to the new buffer. */
2414 size_t unused = link->rcvbuf_alloc - link->rcvbuf_len;
2415 if ((size_t)nread > unused) {
2416 size_t required = link->rcvbuf_len + nread;
2417 /* If less than 1mb, grow to twice the needed size, if larger grow by 1mb. */
2418 link->rcvbuf_alloc = required < RCVBUF_MAX_PREALLOC ? required * 2: required + RCVBUF_MAX_PREALLOC;
2419 link->rcvbuf = (char*)zrealloc(link->rcvbuf, link->rcvbuf_alloc);
2420 }
2421 memcpy(link->rcvbuf + link->rcvbuf_len, buf, nread);
2422 link->rcvbuf_len += nread;
2423 hdr = (clusterMsg*) link->rcvbuf;
2424 rcvbuflen += nread;
2425 }
2426
2427 /* Total length obtained? Process this packet. */

Callers

nothing calls this directly

Calls 10

connGetPrivateDataFunction · 0.85
serverLogFunction · 0.85
handleLinkIOErrorFunction · 0.85
connReadFunction · 0.85
connGetStateFunction · 0.85
connGetLastErrorFunction · 0.85
zreallocFunction · 0.85
clusterProcessPacketFunction · 0.85
zfreeFunction · 0.85
zmallocFunction · 0.85

Tested by

no test coverage detected