Skip to content

Commit 640c927

Browse files
committed
Add MessageDecoder.get_buffered_data to retrieve final contents
This completes the API abstraction of MessageDecoder; since the ReceiveBuffer is internal, the user shouldn't have to care about it. (But we're leaving it in a public attribute, just in case.)
1 parent 69946c2 commit 640c927

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

unpythonic/net/msg.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ class MessageDecoder:
275275
break
276276
...
277277
278-
A `ReceiveBuffer` is created internally. If you need to access it (e.g.
279-
when you're done receiving messages and would like to switch to a raw
280-
stream), it's the `buffer` attribute.
278+
If you need to access data remaining in the internal buffer (e.g. when
279+
you're done receiving messages and would like to switch to a raw stream),
280+
call `get_buffered_data`.
281281
"""
282282
def __init__(self, source):
283283
"""The `source` is a message source in the sense of `decodemsg`."""
@@ -287,3 +287,16 @@ def __init__(self, source):
287287
def decode(self):
288288
"""Decode next message from source, and update receive buffer."""
289289
return decodemsg(self.buffer, self.source)
290+
291+
def get_buffered_data(self):
292+
"""Return data currently in the receive buffer.
293+
294+
Usually this is not needed; an application may need this if you intend
295+
to switch over from messages back to raw data on an existing stream
296+
transport.
297+
298+
When you're done receiving messages, any remaining data already in the
299+
receive buffer should be processed first, before you resume reading and
300+
processing any more data from your original stream.
301+
"""
302+
return self.buffer.getvalue()

unpythonic/net/test/test_msg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test():
3535
# whole point of having this protocol).
3636
# - Note this means that should you wish to stop receiving messages on a particular
3737
# source, and resume reading a raw stream from it instead, you must manually prepend
38-
# the final contents of the receive buffer (`decoder.buffer.getvalue()`) to whatever
38+
# the final contents of the receive buffer (`decoder.get_buffered_data()`) to whatever
3939
# data you later receive from that source (since that data has already been placed
4040
# into the receive buffer, so it is no longer available at the source).
4141
# - So it's recommended to have a dedicated channel to communicate using messages,

0 commit comments

Comments
 (0)