@@ -667,8 +667,13 @@ cdef class ReadBuffer:
667667 self ._finish_message()
668668 return mem
669669
670- cdef redirect_messages(self , WriteBuffer buf, char mtype,
670+ cdef int32_t redirect_messages(self , WriteBuffer buf, char mtype,
671671 int stop_at = 0 ):
672+ # Redirects messages from self into buf until either
673+ # a message with a type different than mtype is encountered, or
674+ # buf contains stop_at bytes.
675+ # Returns the number of messages redirected.
676+
672677 if not self ._current_message_ready:
673678 raise BufferError(
674679 ' consume_full_messages called on a buffer without a '
@@ -687,8 +692,11 @@ cdef class ReadBuffer:
687692 ssize_t new_pos0
688693 ssize_t pos_delta
689694 int32_t done
695+ int32_t count
690696
697+ count = 0
691698 while True :
699+ count += 1
692700 buf.write_byte(mtype)
693701 buf.write_int32(self ._current_message_len)
694702
@@ -701,10 +709,10 @@ cdef class ReadBuffer:
701709 if self ._length > 0 :
702710 self ._ensure_first_buf()
703711 else :
704- return
712+ return count
705713
706714 if stop_at and buf._length >= stop_at:
707- return
715+ return count
708716
709717 # Fast path: exhaust buf0 as efficiently as possible.
710718 if self ._pos0 + 5 <= self ._len0:
@@ -727,14 +735,16 @@ cdef class ReadBuffer:
727735 if new_pos0 + msg_len > cbuf_len:
728736 break
729737 new_pos0 += msg_len
738+ count += 1
730739
731740 if new_pos0 != self ._pos0:
732741 assert self ._pos0 < new_pos0 <= self ._len0
733742
734743 pos_delta = new_pos0 - self ._pos0
735744 buf.write_cstr(
736745 cbuf + self ._pos0,
737- pos_delta)
746+ pos_delta
747+ )
738748
739749 self ._pos0 = new_pos0
740750 self ._length -= pos_delta
@@ -743,11 +753,11 @@ cdef class ReadBuffer:
743753
744754 if done:
745755 # The next message is of a different type.
746- return
756+ return count
747757
748758 # Back to slow path.
749759 if not self .take_message_type(mtype):
750- return
760+ return count
751761
752762 cdef bytearray consume_messages(self , char mtype):
753763 """ Consume consecutive messages of the same type."""
0 commit comments