void test_mongo_wire_packet_get_set_header_raw (void) { mongo_packet *p; mongo_packet_header ph1, ph2; p = mongo_wire_packet_new (); ok (mongo_wire_packet_get_header_raw (NULL, &ph2) == FALSE, "mongo_wire_packet_get_header_raw() should fail with a NULL packet"); ok (mongo_wire_packet_get_header_raw (p, NULL) == FALSE, "mongo_wire_packet_get_header_raw() should fail with a NULL header"); ok (mongo_wire_packet_set_header_raw (NULL, &ph1) == FALSE, "mongo_wire_packet_set_header_raw() should fail with a NULL packet"); ok (mongo_wire_packet_set_header_raw (p, NULL) == FALSE, "mongo_wire_packet_set_header_raw() should fail with a NULL header"); ok (mongo_wire_packet_get_header_raw (p, &ph2), "mongo_wire_packet_get_header_raw() works on a fresh packet"); /* Need to convert from LE, because _new() sets the length to LE. */ cmp_ok (GINT32_FROM_LE (ph2.length), "==", sizeof (mongo_packet_header), "Initial packet length is the length of the header"); ph1.length = sizeof (mongo_packet_header); ph1.id = 1; ph1.resp_to = 0; ph1.opcode = 1000; memset (&ph2, 0, sizeof (mongo_packet_header)); ok (mongo_wire_packet_set_header_raw (p, &ph1), "mongo_wire_packet_set_header_raw() works"); ok (mongo_wire_packet_get_header_raw (p, &ph2), "mongo_wire_packet_get_header_raw() works"); cmp_ok (ph1.length, "==", ph2.length, "Packet lengths match"); cmp_ok (ph1.id, "==", ph2.id, "Sequence IDs match"); cmp_ok (ph1.resp_to, "==", ph2.resp_to, "Response IDs match"); cmp_ok (ph1.opcode, "==", ph2.opcode, "OPCodes match"); mongo_wire_packet_free (p); }
gboolean mongo_packet_send (mongo_connection *conn, const mongo_packet *p) { const guint8 *data; gint32 data_size; mongo_packet_header h; struct iovec iov[2]; struct msghdr msg; if (!conn) { errno = ENOTCONN; return FALSE; } if (!p) { errno = EINVAL; return FALSE; } if (conn->fd < 0) { errno = EBADF; return FALSE; } if (!mongo_wire_packet_get_header_raw (p, &h)) return FALSE; data_size = mongo_wire_packet_get_data (p, &data); if (data_size == -1) return FALSE; iov[0].iov_base = (void *)&h; iov[0].iov_len = sizeof (h); iov[1].iov_base = (void *)data; iov[1].iov_len = data_size; memset (&msg, 0, sizeof (struct msghdr)); msg.msg_iov = iov; msg.msg_iovlen = 2; if (sendmsg (conn->fd, &msg, MSG_NOSIGNAL) != (gint32)sizeof (h) + data_size) return FALSE; conn->request_id = h.id; return TRUE; }