Exemplo n.º 1
0
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			&pb.OtherMessage{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			&pb.OtherMessage{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(104<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 104, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(105<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 105, b)

	return msg
}
Exemplo n.º 2
0
// Write an mmn.Line to the connection.
func (c *Conn) WriteLine(line *mmn.Line) (err error) {

	var buf []byte
	buf, err = proto.Marshal(line)
	if err != nil {
		panic("Error marshalling protobuf struct.")
	}

	// Write the length of the line.
	lenBuf := proto.EncodeVarint(uint64(len(buf)))
	_, err = c.conn.Write(lenBuf)
	if err != nil {
		c.Close()
	}

	// Write the line.
	_, err = c.conn.Write(buf)
	if err != nil {
		c.Close()
	}

	return
}