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{ { Key: proto.Int64(0xdeadbeef), Value: []byte{1, 65, 7, 12}, }, { Weight: proto.Float32(6.022), Inner: &pb.InnerMessage{ Host: proto.String("lesha.mtv"), Port: proto.Int32(8002), }, }, }, Bikeshed: pb.MyMessage_BLUE.Enum(), 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) } greetings := []string{"adg", "easy", "cow"} if err := proto.SetExtension(msg, pb.E_Greeting, greetings); 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(201<<3|proto.WireBytes), b...) proto.SetRawExtension(msg, 201, b) // Extensions can be plain fields, too, so let's test that. b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19) proto.SetRawExtension(msg, 202, b) return msg }
func TestNewID(t *testing.T) { guid := NewGuid() test = make(map[uint64]int) for i := 0; i < 1; i++ { go func() { // id := guid.NewID(uint16(random.RandomInt31n(4095))) id := guid.NewID(1) // fmt.Println(id) addID(id) }() } // fmt.Println(int8(byte(128))) // fmt.Println(uint8(byte(128))) // var a []byte = make([]byte, 8) // a[0] = 128 // a[1] = 128 // a[2] = 128 // a[3] = 128 // a[4] = 128 // a[5] = 128 // a[6] = 128 // a[7] = 128 // fmt.Println(binary.BigEndian.Uint64(a)) // b_buf := bytes.NewBuffer(a) // var x int64 // binary.Read(b_buf, binary.BigEndian, &x) // fmt.Println(x) fmt.Println(proto.EncodeVarint(322590098456576)) }
func varint_encoder(L *lua.LState) int { l_value := L.CheckNumber(2) value := uint64(l_value) b := proto.EncodeVarint(value) L.SetTop(1) L.Push(lua.LString(string(b))) L.Call(1, 0) return 0 }
func writeDelimitedBytes(conn *connection, data []byte) error { if _, err := conn.con.Write(proto.EncodeVarint(uint64(len(data)))); err != nil { log.Fatal("conn.con.Write(proto.EncodeVarint(uint64(len(data))))", err) return err } if _, err := conn.con.Write(data); err != nil { log.Fatal("conn.con.Write(data)", err) return err } return nil }
func signed_varint_encoder(L *lua.LState) int { l_value := L.CheckNumber(2) value := int64(l_value) var b []byte b = proto.EncodeVarint(uint64(value)) L.SetTop(1) L.Push(lua.LString(string(b))) L.Call(1, 0) return 0 }
// Writes a message to the underlying connection. func (b *BaseConn) writeMsg(msg *baseproto.Message) error { msgBuffer, err := proto.Marshal(msg) if err != nil { log.Print("shared/connect: error marshalling msg ", err) return err } prefix := proto.EncodeVarint(uint64(len(msgBuffer))) _, err = b.conn.Write(prefix) if err != nil { return err } _, err = b.conn.Write(msgBuffer) return err }
// Sends the specified message and waits for a response // This function blocks till it gets a response // Each message gets a unique id and messages are prefixed with it's length // encoded using protobuf'd varint format func getResponse(conn net.Conn, message *Message) (*Message, error) { responseChan := make(chan *Message) messageId := getUniqueId() message.MessageId = &messageId pendingRequests[*message.MessageId] = responseChan data, err := proto.Marshal(message) if err != nil { delete(pendingRequests, *message.MessageId) return nil, err } data = append(proto.EncodeVarint(uint64(len(data))), data...) _, err = conn.Write(data) if err != nil { delete(pendingRequests, *message.MessageId) return nil, err } select { case response := <-responseChan: return response, nil } }