golang google协议缓冲区中的错误

时间:2016-04-03 03:26:04

标签: go protocol-buffers

我有一个python程序和一个golang程序都从套接字获取数据。我打印从两者接收的字节。

python

Data = 0a300a084a6f686e20446f6510071a126a6f686e2e646f6540676d61696c2e636f6d220e0a0c3131312d3131312d31313130

golang

2016/04/02 23:21:08 50 bytes read from 192.168.0.1:65120
2016/04/02 23:21:08 00000000  0a 30 0a 08 4a 6f 68 6e  20 44 6f 65 10 08 1a 12  |.0..John Doe....|
00000010  6a 6f 68 6e 2e 64 6f 65  40 67 6d 61 69 6c 2e 63  |john.doe@gmail.c|
00000020  6f 6d 22 0e 0a 0c 31 31  31 2d 31 31 31 2d 31 31  |om"...111-111-11|
00000030  31 30                                             |10|

.proto文件在两者中都是相同的。

syntax = "proto3";
package MC_Feed;

message Person {
  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  string name = 1;
  int32 id = 2;  // Unique ID number for this person.
  string email = 3;

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

// Our address book file is just one of these.
message AddressBook {
  repeated Person people = 1;
}

在python(和C#)上,它工作正常。在golang方面,我收到了这个错误:

2016/04/02 23:21:08 proto: MC_Feed.AddressBook: illegal tag 0 (wire type 0)

go代码是我从网上得到的东西并稍加修改:

package main

import (
    "github.com/golang/protobuf/proto"
    "encoding/hex"
    "./Simple"
    "log"
    "net"
)

const (
    srvAddr         = "239.0.0.222:345"
    maxDatagramSize = 8192
)

func main() {
    serveMulticastUDP(srvAddr, msgHandler)
}


func msgHandler(src *net.UDPAddr, n int, b []byte) {
    log.Println(n, "bytes read from", src)
    log.Println(hex.Dump(b[:n]))
    newTest := &MC_Feed.AddressBook{}
    err := proto.Unmarshal(b, newTest)
    log.Println(err)
}

func serveMulticastUDP(a string, h func(*net.UDPAddr, int, []byte)) {
    addr, err := net.ResolveUDPAddr("udp", a)
    if err != nil {
        log.Fatal(err)
    }
    l, err := net.ListenMulticastUDP("udp", nil, addr)
    l.SetReadBuffer(maxDatagramSize)
    for {
        b := make([]byte, maxDatagramSize)
        n, src, err := l.ReadFromUDP(b)
        if err != nil {
            log.Fatal("ReadFromUDP failed:", err)
        }
        h(src, n, b)
    }
}

1 个答案:

答案 0 :(得分:4)

您的缓冲区b的大小适用于最大可能的数据报包,但并非所有这些都是您的消息,只有第一个n字节。有关illegal tag 0wire type 0的消息来自于尝试解析尚未初始化的缓冲区空间中的0字节。将b[:n]传递给Unmarshal进行修复。