+ stream_state = create_stream_state(stream);
+ if (!stream_state) {
+ abort();
+ }
+
+ g_hash_table_insert(iterator->stream_states,
+ (void *) stream, stream_state);
+ stream_state->expected_msg_seq_num++;
+ goto end;
+ }
+
+ if (stream_state->is_ended) {
+ /*
+ * There's a new message which has a reference to a
+ * stream which, from this iterator's point of view, is
+ * ended ("end of stream" message was returned).
+ * This is bad: the API guarantees that it can never
+ * happen.
+ */
+ BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
+ stream);
+ is_valid = false;
+ goto end;
+ }
+
+ if (c_msg->seq_num == -1ULL) {
+ msg->seq_num = stream_state->expected_msg_seq_num;
+ }
+
+ if (c_msg->seq_num != -1ULL &&
+ c_msg->seq_num != stream_state->expected_msg_seq_num) {
+ BT_ASSERT_PRE_MSG("Unexpected message sequence number: "
+ "seq-num=%" PRIu64 ", "
+ "expected-seq-num=%" PRIu64 ", %![stream-]+s",
+ c_msg->seq_num, stream_state->expected_msg_seq_num,
+ stream);
+ is_valid = false;
+ goto end;
+ }
+
+ switch (c_msg->type) {
+ case BT_MESSAGE_TYPE_STREAM_BEGINNING:
+ BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_STREAM_BEGINNING "
+ "message at this point: msg-seq-num=%" PRIu64 ", "
+ "%![stream-]+s", c_msg->seq_num, stream);
+ is_valid = false;
+ goto end;
+ case BT_MESSAGE_TYPE_STREAM_END:
+ if (stream_state->cur_packet) {
+ BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_STREAM_END "
+ "message: missing a "
+ "BT_MESSAGE_TYPE_PACKET_END message "
+ "prior to this message: "
+ "msg-seq-num=%" PRIu64 ", "
+ "%![stream-]+s", c_msg->seq_num, stream);
+ is_valid = false;
+ goto end;
+ }
+ stream_state->expected_msg_seq_num++;
+ stream_state->is_ended = true;
+ goto end;
+ case BT_MESSAGE_TYPE_PACKET_BEGINNING:
+ if (stream_state->cur_packet) {
+ BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_PACKET_BEGINNING "
+ "message at this point: missing a "
+ "BT_MESSAGE_TYPE_PACKET_END message "
+ "prior to this message: "
+ "msg-seq-num=%" PRIu64 ", %![stream-]+s, "
+ "%![packet-]+a", c_msg->seq_num, stream,
+ packet);
+ is_valid = false;
+ goto end;
+ }
+ stream_state->expected_msg_seq_num++;
+ stream_state->cur_packet = packet;
+ bt_object_get_no_null_check(stream_state->cur_packet);
+ goto end;
+ case BT_MESSAGE_TYPE_PACKET_END:
+ if (!stream_state->cur_packet) {
+ BT_ASSERT_PRE_MSG("Unexpected BT_MESSAGE_TYPE_PACKET_END "
+ "message at this point: missing a "
+ "BT_MESSAGE_TYPE_PACKET_BEGINNING message "
+ "prior to this message: "
+ "msg-seq-num=%" PRIu64 ", %![stream-]+s, "
+ "%![packet-]+a", c_msg->seq_num, stream,
+ packet);
+ is_valid = false;