deserialization - C# Protobuf-net: how do I deserialize consecutively from a network stream? -


i (gratefully) using marc gravell's excellent protobuf-net protocol buffers library. unfortunately, being unintelligent , having trouble understanding correct way deserialize objects come in on wire.

as basis effort, following advice in these questions:

issue deserializing (protocolbuffer) serialized data using protobuf-net
protobuf: consecutive serialize , deserialize to/from socket

i holding tcp connection open entire session (minutes... hours... knows?), , using single network stream persists long connection does. receiving many pb messages on stream.

given using "trydeserializewithlengthprefix", had expected library sort out waiting on stream have enough data parse whole object. not appear case.

i wrote simple test:

    [test]     public void deserializesimpleobjectinparts ()     {         simpleobject origin = new simpleobject();         byte[] wholeobj = protobufserializer.serializetobytearray ( origin );         int length = wholeobj.length;         int midpoint = length / 2;         int sizea = midpoint;         int sizeb = length - midpoint;         byte[] aaa = new byte[sizea];         byte[] bbb = new byte[sizeb];         ( int = 0; < midpoint; i++ )         {             aaa [ ] = wholeobj [ ];         }         ( int j = midpoint; j < length; j++ )         {             bbb [ j - midpoint ] = wholeobj [ j ];         }          using ( memorystream streama = new memorystream ( aaa ) )         {             using ( memorystream streamb = new memorystream ( bbb ) )             {                 streama.position = 0;                 streamb.position = 0;                 debug.logdebug ( "streama.length = " + streama.length + ", streamb.length = " + streamb.length );                  object resulta = null;                 bool succeededa = false;                 try {                     succeededa = protobufserializer.deserialize ( streama, out resulta );                 }                 catch ( exception e )                 {                     debug.logdebug ( "exception = " + e );                     debug.logdebug ( "streama.position = " + streama.position + ", streama.length = " + streama.length );                 }             }         }     } 

please note that, in above, "protobufserializer.deserialize" call through own service class straight "trydeserializewithlengthprefix". type coding business in there.

when run test, following exception thrown:

system.io.endofstreamexception: failed read past end of stream. 

this advice our friends @ google suggests responsibility take @ length prefix in socket code. how help? c# network streams don't have length property me compare against.

do have intermediate step of running loop on stream, building byte arrays of correct size, making new streams them pass protobuf-net? seems anti-pattern , performance hit.

what missing here?

given using "trydeserializewithlengthprefix", had expected library sort out waiting on stream have enough data parse whole object.

there's no reliable , efficient way "sort out waiting" on abstract stream object.

c# network streams don't have length property me compare against.

count data write socket stream buffer stream. that'll "length" property compare against length prefix in beginning. once have enough data, seek buffer stream prefix , pass deserializer object. seek buffer stream again , reuse next message. repeat.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -