- n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
- if (n == RS_HEADER_LEN)
- {
- pkt->hdr_read_flag = 1;
- pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
- if (pkt->rpkt->data_len < 20 /* || len > 4096 */)
- abort (); /* TODO: Read and discard. */
- pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
- if (!pkt->rpkt->data)
- {
- rs_conn_err_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
- NULL);
- abort (); /* FIXME: recovering takes reading of packet */
- }
- memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
- bufferevent_setwatermark (pkt->conn->bev, EV_READ,
- pkt->rpkt->data_len - RS_HEADER_LEN, 0);
-#if defined (DEBUG)
- fprintf (stderr, "%s: packet header read, total pkt len=%d\n",
- __func__, pkt->rpkt->data_len);
-#endif
- }
- else if (n < 0)
- return; /* Buffer frozen. */
- else
- assert (!"short header");
+ int err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
+ pkt->rpkt->length);
+ if (err < 0)
+ return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
+ "bufferevent_write: %s",
+ evutil_gai_strerror (err));