Beispiel #1
0
        internal bool ConsumerPoll(out Message message, IntPtr millisecondsTimeout)
        {
            // TODO: There is a newer librdkafka interface for this now. Use that.
            IntPtr msgPtr = LibRdKafka.consumer_poll(handle, millisecondsTimeout);

            if (msgPtr == IntPtr.Zero)
            {
                message = default(Message);
                return(false);
            }

            var msg = Marshal.PtrToStructure <rd_kafka_message>(msgPtr);

            byte[] val = null;
            if (msg.val != IntPtr.Zero)
            {
                val = new byte[(int)msg.len];
                Marshal.Copy(msg.val, val, 0, (int)msg.len);
            }
            byte[] key = null;
            if (msg.key != IntPtr.Zero)
            {
                key = new byte[(int)msg.key_len];
                Marshal.Copy(msg.key, key, 0, (int)msg.key_len);
            }

            string topic = null;

            if (msg.rkt != IntPtr.Zero)
            {
                topic = Util.Marshal.PtrToStringUTF8(LibRdKafka.topic_name(msg.rkt));
            }

            IntPtr timestampType;
            long   timestamp = LibRdKafka.message_timestamp(msgPtr, out timestampType) / 1000;
            var    dateTime  = new DateTime(0);

            if ((TimestampType)timestampType != TimestampType.NotAvailable)
            {
                dateTime = Timestamp.UnixTimestampMsToDateTime(timestamp);
            }

            LibRdKafka.message_destroy(msgPtr);

            message = new Message(
                topic,
                msg.partition,
                msg.offset,
                key,
                val,
                new Timestamp(dateTime, (TimestampType)timestampType),
                msg.err
                );

            return(true);
        }
        internal bool ConsumerPoll(out Message message, IntPtr millisecondsTimeout)
        {
            ThrowIfHandleClosed();
            // TODO: There is a newer librdkafka interface for this now. Use that.
            IntPtr msgPtr = LibRdKafka.consumer_poll(handle, millisecondsTimeout);

            if (msgPtr == IntPtr.Zero)
            {
                message = null;
                return(false);
            }

            var msg = Util.Marshal.PtrToStructure <rd_kafka_message>(msgPtr);

            byte[] val = null;
            if (msg.val != IntPtr.Zero)
            {
                val = new byte[(int)msg.len];
                Marshal.Copy(msg.val, val, 0, (int)msg.len);
            }
            byte[] key = null;
            if (msg.key != IntPtr.Zero)
            {
                key = new byte[(int)msg.key_len];
                Marshal.Copy(msg.key, key, 0, (int)msg.key_len);
            }

            string topic = null;

            if (msg.rkt != IntPtr.Zero)
            {
                topic = Util.Marshal.PtrToStringUTF8(LibRdKafka.topic_name(msg.rkt));
            }

            long timestamp = LibRdKafka.message_timestamp(msgPtr, out IntPtr timestampType);

            LibRdKafka.message_destroy(msgPtr);

            message = new Message(
                topic,
                msg.partition,
                msg.offset,
                key,
                val,
                new Timestamp(timestamp, (TimestampType)timestampType),
                msg.err
                );

            return(true);
        }