Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to TryDeserializeWithLengthPrefix when Stream may not contain complete Object
    primarykey
    data
    text
    <p>I'm working on a new Buffering processor for our application and have had a fresh look at the Protobuf <code>Deserialize*</code> methods and have investigating using the following code, in my former version I had my own TypeIdentifier and LengthPrefix in front and then guaranteeing that I have a full block to Deserialise. For testing I have replaced my reading of a networkStream with reading random length chunks from a File written to disk using repeated calls into <code>SerializeWithLengthPrefix()</code></p> <p>But when I am traversing the mDatabuffer I get the <code>EndOfStreamException</code> being thrown. It's no big deal, just wanted to make sure I'm doing this correctly, as there is no guarantee that I will have a "full" block of data to process perfectly through the Deserialize call. I was initially doing TryReadLengthPrefix, but this seems cleaner.</p> <pre class="lang-vb prettyprint-override"><code>Public Sub New(ByVal maximumPacketSize As Integer) mMaxSize = maximumPacketSize mDataBuffer = New MemoryStream() mTypeResolver = AddressOf PacketTypeResolver End Sub Public Sub ProcessBuffer(ByVal theData() As Byte, offset As Integer, ByVal bytesToRead As Integer) 'append the data we have just received into our StreamBuffer so we can '"move back" if we hit the end of stream whilst Deserializing mDataBuffer.Seek(0, SeekOrigin.End) mDataBuffer.Write(theData, offset, bytesToRead) mDataBuffer.Seek(0, SeekOrigin.Begin) While mDataBuffer.Position &lt; mDataBuffer.Length Dim currentPosition As Integer = CType(mDataBuffer.Position, Integer) Try Dim p As Object = Nothing If Serializer.NonGeneric.TryDeserializeWithLengthPrefix(mDataBuffer, PrefixStyle.Base128, mTypeResolver, p) Then If GetType(BasePacket).IsAssignableFrom(p.GetType) Then Dim bufferedPacket As BasePacket = CType(p, BasePacket) ''''Do stuff with the packet. End If End If Catch ex As EndOfStreamException Dim tmpData As New MemoryStream() tmpData.Write(mDataBuffer.GetBuffer(), currentPosition, CType(mDataBuffer.Length - currentPosition, Integer)) mDataBuffer.Dispose() mDataBuffer = tmpData Exit While Catch ex As Exception Debug.Print(ex.GetType.Name &amp; "---" &amp; ex.Message) Throw End Try End While End Sub </code></pre> <p>(aside) For learning I'm wondering about the API design decision of not catching the exception inside <code>TryDeserializeWithLengthPrefix</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload