- assert (pkt);
- assert (pkt->conn);
- if (!pkt->hdr_read_flag)
- {
- n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN;
- if (n == RS_HEADER_LEN)
- {
- uint16_t len = (pkt->hdr[2] << 8) + pkt->hdr[3];
- uint8_t *buf = rs_malloc (pkt->conn->ctx, len);
-
- pkt->hdr_read_flag = 1;
- if (!buf)
- {
- rs_conn_err_push_fl (pkt->conn, RSE_NOMEM, __FILE__,
- __LINE__, NULL);
- abort (); /* FIXME: recovering takes reading of packet */
- }
- pkt->rpkt->data = buf;
- pkt->rpkt->data_len = len;
- bufferevent_setwatermark (pkt->conn->bev, EV_READ,
- len - RS_HEADER_LEN, 0);
-#if defined (DEBUG)
- fprintf (stderr, "%s: packet header read, pkt len=%d\n", __func__,
- len);
-#endif
- }
- else if (n < 0)
- return; /* Buffer frozen, i suppose. Let's hope it thaws. */
- else
- {
- assert (n < RS_HEADER_LEN);
- return; /* Need more to complete header. */
- }
- }