FAQ
I have run into a problem with the gob encoder doing some funky things that
I do not understand.

Long story short I have two structures that are something like this:
type testA struct {
         T time.Time
         X net.IP
         D []byte
         M map[string]([]byte)
}

type testB struct {
         X, Y, Z uint64
         Msg []byte
}

A gob encoder and decoder are held by two clients talking over a
ReaderWriter.

Client A is continually sending testA stucts via the encoder, when he is
done he sends one populated as such genc.Encode(testA{time.Time{}, nil,
nil, nil}), after which it sends a populated structure of type testB
Client B is continually decoding testA until it sees one with D set as nil
or having a length of zero, at which point it expects a structure of type
testB.

In this scenario, if the first thing sent by clientA is the mostly nil
testA structure clientB sees it and properly decodes the testB structure.
  However, if clientA has sent a few populated testA structures then sends
the mostly nil testA structure clientB never decodes/sees the nil testA
structure and then fails as it tries to decode the testB structure into a
testA (because clientB doesn't know to stop looking for testA). I have put
together a small code sample that demonstrates this. Anyone have any ideas?

I am about to fire this example up with a TCP connection and wireshark it
to see if the encoder even bothers to send the mostly nil structure, my
next step is to start instrumenting the gob package (but its huge and
nasty). I am hoping I missed something in the docs or am being bone headed.

Thanks for your time

Example Code:
http://pastebin.com/X0k6ey7w



P.S. First time poster so I should say Golang is awesome, it has completely
supplanted all other languages for me other than the really really low
level C/assembly shit.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Carlos Castillo at Mar 7, 2014 at 12:18 pm
    You are decoding in a loop onto the same struct value. With encoding/gob
    null and zero values are not sent over the wire, and thus the fields are
    not being set to zero, they are instead holding the values from last
    iteration. In common usage the value you are decoding into is a freshly
    allocated value, which go zero's by default, so this usually doesn't
    matter. The zero's are not sent because of this assumption, and it saves
    space.

      You should zero the struct as part of the loop by putting between lines 89
    & 90 in reader (just before the decode): t = tester{}
    On Thursday, March 6, 2014 7:58:55 PM UTC-8, traetox wrote:

    I have run into a problem with the gob encoder doing some funky things
    that I do not understand.

    Long story short I have two structures that are something like this:
    type testA struct {
    T time.Time
    X net.IP
    D []byte
    M map[string]([]byte)
    }

    type testB struct {
    X, Y, Z uint64
    Msg []byte
    }

    A gob encoder and decoder are held by two clients talking over a
    ReaderWriter.

    Client A is continually sending testA stucts via the encoder, when he is
    done he sends one populated as such genc.Encode(testA{time.Time{}, nil,
    nil, nil}), after which it sends a populated structure of type testB
    Client B is continually decoding testA until it sees one with D set as nil
    or having a length of zero, at which point it expects a structure of type
    testB.

    In this scenario, if the first thing sent by clientA is the mostly nil
    testA structure clientB sees it and properly decodes the testB structure.
    However, if clientA has sent a few populated testA structures then sends
    the mostly nil testA structure clientB never decodes/sees the nil testA
    structure and then fails as it tries to decode the testB structure into a
    testA (because clientB doesn't know to stop looking for testA). I have put
    together a small code sample that demonstrates this. Anyone have any ideas?

    I am about to fire this example up with a TCP connection and wireshark it
    to see if the encoder even bothers to send the mostly nil structure, my
    next step is to start instrumenting the gob package (but its huge and
    nasty). I am hoping I missed something in the docs or am being bone headed.

    Thanks for your time

    Example Code:
    http://pastebin.com/X0k6ey7w



    P.S. First time poster so I should say Golang is awesome, it has
    completely supplanted all other languages for me other than the really
    really low level C/assembly shit.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Traetox at Mar 8, 2014 at 12:50 am
    That was exactly it. Thank you for the clarification.

    Kris
    On Friday, March 7, 2014 5:18:46 AM UTC-7, Carlos Castillo wrote:

    You are decoding in a loop onto the same struct value. With encoding/gob
    null and zero values are not sent over the wire, and thus the fields are
    not being set to zero, they are instead holding the values from last
    iteration. In common usage the value you are decoding into is a freshly
    allocated value, which go zero's by default, so this usually doesn't
    matter. The zero's are not sent because of this assumption, and it saves
    space.

    You should zero the struct as part of the loop by putting between lines
    89 & 90 in reader (just before the decode): t = tester{}
    On Thursday, March 6, 2014 7:58:55 PM UTC-8, traetox wrote:

    I have run into a problem with the gob encoder doing some funky things
    that I do not understand.

    Long story short I have two structures that are something like this:
    type testA struct {
    T time.Time
    X net.IP
    D []byte
    M map[string]([]byte)
    }

    type testB struct {
    X, Y, Z uint64
    Msg []byte
    }

    A gob encoder and decoder are held by two clients talking over a
    ReaderWriter.

    Client A is continually sending testA stucts via the encoder, when he is
    done he sends one populated as such genc.Encode(testA{time.Time{}, nil,
    nil, nil}), after which it sends a populated structure of type testB
    Client B is continually decoding testA until it sees one with D set as
    nil or having a length of zero, at which point it expects a structure of
    type testB.

    In this scenario, if the first thing sent by clientA is the mostly nil
    testA structure clientB sees it and properly decodes the testB structure.
    However, if clientA has sent a few populated testA structures then sends
    the mostly nil testA structure clientB never decodes/sees the nil testA
    structure and then fails as it tries to decode the testB structure into a
    testA (because clientB doesn't know to stop looking for testA). I have put
    together a small code sample that demonstrates this. Anyone have any ideas?

    I am about to fire this example up with a TCP connection and wireshark it
    to see if the encoder even bothers to send the mostly nil structure, my
    next step is to start instrumenting the gob package (but its huge and
    nasty). I am hoping I missed something in the docs or am being bone headed.

    Thanks for your time

    Example Code:
    http://pastebin.com/X0k6ey7w



    P.S. First time poster so I should say Golang is awesome, it has
    completely supplanted all other languages for me other than the really
    really low level C/assembly shit.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 7, '14 at 6:26a
activeMar 8, '14 at 12:50a
posts3
users2
websitegolang.org

2 users in discussion

Traetox: 2 posts Carlos Castillo: 1 post

People

Translate

site design / logo © 2021 Grokbase