简体   繁体   中英

Gob decoder throws EOF error for a time then stops

I'm trying to feed []byte over a chan to a gob decoder. It works, but at first the decoder throws a whole bunch of EOF errors then stops. When it stops throwing errors the program behaves exactly like I would expect with it decoding gobs and processing the structs it produces correctly.

This is the calling function, the channel that is being read from is an SSH channel.

log.Println("Reading channel")
dchan := make(chan []byte, 200)
go decoder(dchan)
for {
    buf := make([]byte, 1024)
    //log.Println("Waiting for data")
    numBytes, err := channel.Read(buf)
    if err != nil {
        log.Println(err)
        continue
    }
    dchan <- buf[:numBytes]
}

The decoder function looks like this:

func decoder(dchan chan []byte) error {
  gob.Register(datums.Message{})
  var message datums.Message
  bbuf := bytes.NewBuffer(make([]byte, 512))
  dec := gob.NewDecoder(bbuf)
  for data := range dchan {
    //log.Println("Decoding data")
    bbuf.Write(data)
    err := dec.Decode(&message)
    if err != nil {
        log.Println("Error in decoder")
        log.Println(err)
        continue
    }
    //log.Println(&message)
  }
  return nil
}

I'm not sure if this is even a real issue, does the gob decoder remove the data from the buffer or does it leave it there until sufficient data shows up to generate a value?

The call to channel.Read(buf) can read a partial gob. The decoder will return an error when attempting to decode the partial gob.

Because the channel satisfies the io.Reader interface, the application can create the decoder directly on the channel:

gob.Register(datums.Message{})
dec := gob.NewDecoder(channel)
for {
  var message datums.Message
  err := dec.Decode(&message)
  if err != nil {
     log.Println("Error in decoder", err)
     break
  }
  log.Println(&message)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM