func (server *Server) updateCodecVersions() { codecusers := map[int32]int{} var winner int32 var count int for _, client := range server.clients { for _, codec := range client.codecs { codecusers[codec] += 1 } } for codec, users := range codecusers { if users > count { count = users winner = codec } if users == count && codec > winner { winner = codec } } var current int32 if server.PreferAlphaCodec { current = server.AlphaCodec } else { current = server.BetaCodec } if winner == current { return } if winner == CeltCompatBitstream { server.PreferAlphaCodec = true } else { server.PreferAlphaCodec = !server.PreferAlphaCodec } if server.PreferAlphaCodec { server.AlphaCodec = winner } else { server.BetaCodec = winner } err := server.broadcastProtoMessage(MessageCodecVersion, &mumbleproto.CodecVersion{ Alpha: proto.Int32(server.AlphaCodec), Beta: proto.Int32(server.BetaCodec), PreferAlpha: proto.Bool(server.PreferAlphaCodec), }) if err != nil { log.Printf("Unable to broadcast.") return } log.Printf("CELT codec switch %#x %#x (PreferAlpha %v)", uint32(server.AlphaCodec), uint32(server.BetaCodec), server.PreferAlphaCodec) return }
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 }
// toProto converts the query to a protocol buffer. func (q *Query) toProto(dst *pb.Query, appID string, zlp zeroLimitPolicy) error { if q.kind == "" { return errors.New("datastore: empty query kind") } dst.Reset() dst.App = proto.String(appID) dst.Kind = proto.String(q.kind) if q.ancestor != nil { dst.Ancestor = keyToProto(appID, q.ancestor) } if q.keysOnly { dst.KeysOnly = proto.Bool(true) dst.RequirePerfectPlan = proto.Bool(true) } for _, qf := range q.filter { if qf.FieldName == "" { return errors.New("datastore: empty query filter field name") } p, errStr := valueToProto(appID, qf.FieldName, reflect.ValueOf(qf.Value), false) if errStr != "" { return errors.New("datastore: bad query filter value type: " + errStr) } xf := &pb.Query_Filter{ Op: operatorToProto[qf.Op], Property: []*pb.Property{p}, } if xf.Op == nil { return errors.New("datastore: unknown query filter operator") } dst.Filter = append(dst.Filter, xf) } for _, qo := range q.order { if qo.FieldName == "" { return errors.New("datastore: empty query order field name") } xo := &pb.Query_Order{ Property: proto.String(qo.FieldName), Direction: sortDirectionToProto[qo.Direction], } if xo.Direction == nil { return errors.New("datastore: unknown query order direction") } dst.Order = append(dst.Order, xo) } if q.limit != 0 || zlp == zeroLimitMeansZero { dst.Limit = proto.Int32(q.limit) } if q.offset != 0 { dst.Offset = proto.Int32(q.offset) } return nil }
func (this *PushProtoHandler) UpdatePlayerCoordinate(x, y int) { fmt.Printf("Updating player coordinate\n") m := NewUpdatePlayerCoord() m.Coord = new(Coordinate) m.Coord.X = proto.Int32(int32(x)) m.Coord.Y = proto.Int32(int32(y)) data, err := proto.Marshal(m) if err != nil { fmt.Printf("E: %s", err) return } this.Proxy.SendMsg(data, PORT_PUSH, Server_UPDATELOCATION, false) }
// Run starts a query for log records, which contain request and application // level log information. func (params *Query) Run(c appengine.Context) *Result { req := &log_proto.LogReadRequest{} appId := c.FullyQualifiedAppID() req.AppId = &appId if params.StartTime != 0 { req.StartTime = ¶ms.StartTime } if params.EndTime != 0 { req.EndTime = ¶ms.EndTime } if params.Incomplete { req.IncludeIncomplete = ¶ms.Incomplete } if params.AppLogs { req.IncludeAppLogs = ¶ms.AppLogs } if params.ApplyMinLevel { req.MinimumLogLevel = proto.Int32(int32(params.MinLevel)) } if params.Versions == nil { // If no versions were specified, default to the major version // used by this app. versionID := appengine.VersionID(c) if i := strings.Index(versionID, "."); i >= 0 { versionID = versionID[:i] } req.VersionId = []string{versionID} } else { req.VersionId = params.Versions } return &Result{context: c, request: req} }
func TestPBS(t *testing.T) { memStore := NewMemoryStore() stores := [1]DataStore{memStore} ds := NewPBSDataStore(&stores) test := &Test{ Label: proto.String("hello"), Type: proto.Int32(17), Optionalgroup: &Test_OptionalGroup{ RequiredField: proto.String("good bye"), }, } key, err := ds.Put(test) if err != nil { t.Fatalf("Put failed: %q", err.String()) } t.Logf("Key: %q", key) test2 := NewTest() err2 := ds.Get(key, test2) if err2 != nil { t.Fatalf("Get failed: %q", err.String()) } t.Logf("Value: %q", *test2.Label) }
// Walk reads up to lim entries matching glob, in revision rev, into an array. // Entries are read in lexicographical order, starting at position off. // A negative lim means to read until the end. // Conn.Walk will be removed in a future release. Use Walk instead. func (c *Conn) Walk(glob string, rev int64, off, lim int) (info []Event, err os.Error) { for lim != 0 { var t txn t.req.Verb = newRequest_Verb(request_WALK) t.req.Rev = &rev t.req.Path = &glob t.req.Offset = proto.Int32(int32(off)) err = c.call(&t) if err, ok := err.(*Error); ok && err.Err == ErrRange { return info, nil } if err != nil { return nil, err } info = append(info, Event{ *t.resp.Rev, *t.resp.Path, t.resp.Value, *t.resp.Flags, }) off++ lim-- } return }
// callNext issues a datastore_v3/Next RPC to advance a cursor, such as that // returned by a query with more results. func callNext(c appengine.Context, res *pb.QueryResult, offset, limit int32, zlp zeroLimitPolicy) error { if res.Cursor == nil { return errors.New("datastore: internal error: server did not return a cursor") } // TODO: should I eventually call datastore_v3/DeleteCursor on the cursor? req := &pb.NextRequest{ Cursor: res.Cursor, Offset: proto.Int32(offset), } if limit != 0 || zlp == zeroLimitMeansZero { req.Count = proto.Int32(limit) } if res.CompiledCursor != nil { req.Compile = proto.Bool(true) } res.Reset() return c.Call("datastore_v3", "Next", req, res, nil) }
func (c *conn) respond(t *T, flag int32, cc chan bool, r *R) { r.Tag = t.Tag r.Flags = pb.Int32(flag) tag := pb.GetInt32(t.Tag) if flag&Done != 0 { c.closeTxn(tag) } if c.poisoned { select { case cc <- true: default: } return } buf, err := pb.Marshal(r) c.wl.Lock() defer c.wl.Unlock() if err != nil { c.poisoned = true select { case cc <- true: default: } log.Println(err) return } err = binary.Write(c.c, binary.BigEndian, int32(len(buf))) if err != nil { c.poisoned = true select { case cc <- true: default: } log.Println(err) return } for len(buf) > 0 { n, err := c.c.Write(buf) if err != nil { c.poisoned = true select { case cc <- true: default: } log.Println(err) return } buf = buf[n:] } }
func TestSetNilFields(t *testing.T) { c := &conn{ c: &bytes.Buffer{}, s: &Server{}, cal: true, tx: make(map[int32]txn), } c.set(&T{Tag: proto.Int32(1)}, newTxn()) assertResponse(t, missingArg, c) }
func main() { m1 := &test.Member{ Name: proto.String("Linus"), Age: proto.Int32(99), } m1.Skills = []test.Skill{test.Skill_ASM, test.Skill_C} m1_data, err := proto.Marshal(m1) if err != nil { log.Fatal("marshaling error: ", err) os.Exit(1) } fmt.Println("m1: ", m1) fmt.Println("m1.Skills: ", m1.Skills) fmt.Println("actual data: ", m1_data) println("=== unmarshalling m1 into m2 ===") m2 := &test.Member{} proto.Unmarshal(m1_data, m2) fmt.Println("m2: ", m2) port := 9090 fmt.Println("=== END EXAMPLES / SERVER STARTING at %d ===", port) service := fmt.Sprintf(":%d", port) tcpAddr, err := net.ResolveTCPAddr("ip4", service) checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) for { conn, err := listener.Accept() checkError(err) input := make([]byte, 1024) n, err := conn.Read(input) input = input[:n] fmt.Println("input: ", input) club := &test.Club{} err = proto.Unmarshal(input, club) checkError(err) sum := int32(0) fmt.Println("len: ", len(club.Member)) for _, m := range club.Member { sum += *m.Age } avg := float64(sum) / float64(len(club.Member)) fmt.Printf("avg age: %f (sum: %d)\n", avg, sum) reply := &test.Reply{Average: proto.Float64(avg)} out_data, err := proto.Marshal(reply) conn.Write(out_data) // don't care about return value conn.Close() // we're finished with this client } }
func makeRemoveEntity(id int32, name string) (msg *protocol.Message) { removeEntity := &protocol.RemoveEntity{ Id: proto.Int32(id), Name: proto.String(name), } return &protocol.Message{ RemoveEntity: removeEntity, Type: protocol.NewMessage_Type(protocol.Message_REMOVEENTITY), } }
func makeAddEntity(id int32, name string) (msg *protocol.Message) { addEntity := &protocol.AddEntity{ Id: proto.Int32(id), Name: proto.String(name), } return &protocol.Message{ AddEntity: addEntity, Type: protocol.NewMessage_Type(protocol.Message_ADDENTITY), } }
func makeUpdateState(id int32, stateId string, value *protocol.StateValue) (msg *protocol.Message) { updateState := &protocol.UpdateState{ Id: proto.Int32(id), StateId: proto.String(stateId), Value: value, } return &protocol.Message{ UpdateState: updateState, Type: protocol.NewMessage_Type(protocol.Message_UPDATESTATE), } }
func (p *parser) readEnum(e *EnumDescriptorProto) *parseError { if err := p.readToken("enum"); err != nil { return err } tok := p.next() if tok.err != nil { return tok.err } // TODO: check that the name is acceptable. e.Name = proto.String(tok.value) if err := p.readToken("{"); err != nil { return err } // Parse enum values for !p.done { tok := p.next() if tok.err != nil { return tok.err } if tok.value == "}" { // end of enum return nil } // TODO: verify tok.value is a valid enum value name. ev := new(EnumValueDescriptorProto) e.Value = append(e.Value, ev) ev.Name = proto.String(tok.value) if err := p.readToken("="); err != nil { return err } tok = p.next() if tok.err != nil { return tok.err } // TODO: check that tok.value is a valid enum value number. num, err := atoi32(tok.value) if err != nil { return p.error("bad enum number %q: %v", tok.value, err) } ev.Number = proto.Int32(num) if err := p.readToken(";"); err != nil { return err } } return p.error("unexpected end while parsing enum") }
func TestSetNilFields(t *testing.T) { c := &conn{ c: &bytes.Buffer{}, canWrite: true, waccess: true, } tx := &txn{ c: c, req: request{Tag: proto.Int32(1)}, } tx.set() assertResponseErrCode(t, response_MISSING_ARG, c) }
func (t *txn) wait() { if t.c.access == false { t.respondOsError(os.EACCES) return } if t.req.Path == nil || t.req.Rev == nil { t.respondErrCode(response_MISSING_ARG) return } glob, err := store.CompileGlob(*t.req.Path) if err != nil { t.respondOsError(err) return } ch, err := t.c.st.Wait(glob, *t.req.Rev) if err != nil { t.respondOsError(err) return } go func() { ev := <-ch t.resp.Path = &ev.Path t.resp.Value = []byte(ev.Body) t.resp.Rev = &ev.Seqn switch { case ev.IsSet(): t.resp.Flags = proto.Int32(set) case ev.IsDel(): t.resp.Flags = proto.Int32(del) default: t.resp.Flags = proto.Int32(0) } t.respond() }() }
/* func SubMsg(data []byte, t int32, encap bool) []byte { header := NewSubHeader() header.Type = proto.Int32(t) header.Encap = proto.Bool(encap) hdrdata,_ := proto.Marhal(header) hdrlen := uint32(len(hdrdata)) datalen := uint32(len(data)) buf := make([]byte, 8+hdrlen+datalen) binary.Write(this.SBuffer, binary.BigEndian, [2]uint32{hdrlen,datalen}) copy(buf[8:8+len(hdrdata)],hdrdata) copy(buf[8+len(hdrdata):-1], data) return buf } func UnSubMsg(data []byte) (*SubHeader, []byte) { var hdrlen uint32 var datalen uint32 hdr,err := data[0:8] if err != nil { return nil,err } fmt.Printf("len(data) = %d\n", len(data)) err = binary.Read(data[0:4], binary.BigEndian, &hdrlen) if err != nil { return nil,nil,err } err = binary.Read(data[4:8], binary.BigEndian, &datalen) if err != nil { return nil,nil,err } fmt.Printf("hdrlen=%d, datalen=%d\n", hdrlen, datalen) if !(hdrlen < 96 && datalen < 16000 ) { return nil,nil,os.ENOMEM } hdrdata := make(Buf, hdrlen) newdata := make(Buf, datalen) tmp,err := this.Read(this.Buffer[0:hdrlen]) if err != nil { //this.Conn.Close() return nil,nil,err } copy(hdrdata,tmp) header := NewHeader() err = proto.Unmarshal(hdrdata, header) if err != nil { return nil,nil,err } tmp,err = this.Read(this.Buffer[0:datalen]) if err != nil { return nil,nil,err } copy(newdata,tmp) return header, newdata,nil } */ func (this *ProtoProxy) SendMsg(data []byte, port int32, t int32, encap bool) { h := NewHot(func(shared map[string]interface{}) { //self := shared["self"].(*GenericHot) header := NewHeader() header.Type = proto.Int32(t) header.Port = proto.Int32(port) header.Encap = proto.Bool(encap) hdrdata, err := proto.Marshal(header) if err != nil { fmt.Printf("%s\n", err) return } hdrlen := uint32(len(hdrdata)) datalen := uint32(len(data)) binary.Write(this.SBuffer, binary.BigEndian, [2]uint32{hdrlen, datalen}) copy(this.SBuffer[8:8+len(hdrdata)], hdrdata) copy(this.SBuffer[8+len(hdrdata):8+len(hdrdata)+len(data)], data) //fmt.Printf("Writing this: [%d]%s\n", len(this.SBuffer[0:hdrlen+len(data)]), this.SBuffer[0:hdrlen+len(data)]) this.Conn.Write(this.SBuffer[0 : 8+len(hdrdata)+len(data)]) }) this.QueryHot(h) }
func TestServerCancel(t *testing.T) { var buf bytes.Buffer c := &conn{ c: &buf, s: &Server{}, cal: true, tx: make(map[int32]txn), } fakeTx := newTxn() c.tx[1] = fakeTx cancelTx := newTxn() c.tx[2] = cancelTx done := make(chan bool, 1) go func() { c.cancel(&T{Tag: proto.Int32(2), OtherTag: proto.Int32(1)}, cancelTx) done <- true }() // fake verb <-fakeTx.cancel c.closeTxn(1) // standard thing to do for every verb <-done b := buf.Bytes() assert.NotEqual(t, []byte{}, b) assert.Equal(t, []byte{0, 0, 0, 4}, b[0:4]) exp := &R{ Tag: proto.Int32(2), Flags: proto.Int32(Valid | Done), } assert.Equal(t, exp, mustUnmarshal(b[4:])) }
func (t *txn) walk() { if t.c.access == false { t.respondOsError(os.EACCES) return } if t.req.Path == nil || t.req.Offset == nil { t.respondErrCode(response_MISSING_ARG) return } glob, err := store.CompileGlob(*t.req.Path) if err != nil { t.respondOsError(err) return } offset := *t.req.Offset if offset < 0 { t.respondErrCode(response_RANGE) return } go func() { g, err := t.getter() if err != nil { t.respondOsError(err) return } f := func(path, body string, rev int64) (stop bool) { if offset == 0 { t.resp.Path = &path t.resp.Value = []byte(body) t.resp.Rev = &rev t.resp.Flags = proto.Int32(set) t.respond() return true } offset-- return false } if !store.Walk(g, glob, f) { t.respondErrCode(response_RANGE) } }() }
func (client *Client) sendChannelTree(channel *Channel) { chanstate := &mumbleproto.ChannelState{ ChannelId: proto.Uint32(uint32(channel.Id)), Name: proto.String(channel.Name), } if channel.parent != nil { chanstate.Parent = proto.Uint32(uint32(channel.parent.Id)) } if channel.HasDescription() { if client.Version >= 0x10202 { chanstate.DescriptionHash = channel.DescriptionBlobHashBytes() } else { buf, err := globalBlobstore.Get(channel.DescriptionBlob) if err != nil { panic("Blobstore error.") } chanstate.Description = proto.String(string(buf)) } } if channel.Temporary { chanstate.Temporary = proto.Bool(true) } chanstate.Position = proto.Int32(int32(channel.Position)) links := []uint32{} for cid, _ := range channel.Links { links = append(links, uint32(cid)) } chanstate.Links = links err := client.sendProtoMessage(MessageChannelState, chanstate) if err != nil { client.Panic(err.String()) } for _, subchannel := range channel.children { client.sendChannelTree(subchannel) } }
// Getdir reads up to lim names from dir, at revision rev, into an array. // Names are read in lexicographical order, starting at position off. // A negative lim means to read until the end. func (c *Conn) Getdir(dir string, rev int64, off, lim int) (names []string, err os.Error) { for lim != 0 { var t txn t.req.Verb = newRequest_Verb(request_GETDIR) t.req.Rev = &rev t.req.Path = &dir t.req.Offset = proto.Int32(int32(off)) err = c.call(&t) if err, ok := err.(*Error); ok && err.Err == ErrRange { return names, nil } if err != nil { return nil, err } names = append(names, *t.resp.Path) off++ lim-- } return }
func TestServerValidAccessToken(t *testing.T) { b := make(bchan, 2) c := &conn{ c: b, canWrite: true, st: store.New(), secret: "abc", } tx := &txn{ c: c, req: request{Tag: proto.Int32(1), Value: []byte(c.secret)}, } tx.access() assert.Equal(t, 4, len(<-b)) assert.Equal(t, (*response_Err)(nil), mustUnmarshal(<-b).ErrCode) assert.T(t, c.access) }
func main() { p := example.Person{ Name: proto.String("Taro Yamada"), Age: proto.Int32(8), } pet := example.Pet{Name: proto.String("Mike")} p.Pet = append(p.Pet, &pet) fmt.Println("-- p.String() --") fmt.Println(p.String()) fmt.Println("-- MarshalText --") fmt.Print(PrintToString(&p)) // not compact fmt.Println("-- Marshal --") m, _ := proto.Marshal(&p) fmt.Println(m) fmt.Println("-- CompactTextString --") fmt.Println(proto.CompactTextString(&p)) }
func TestServerInvalidAccessToken(t *testing.T) { b := make(bchan, 2) c := &conn{ c: b, canWrite: true, st: store.New(), secret: "abc", } tx := &txn{ c: c, req: request{Tag: proto.Int32(1), Value: []byte("bad")}, } tx.access() var exp response_Err = response_OTHER assert.Equal(t, 4, len(<-b)) assert.Equal(t, &exp, mustUnmarshal(<-b).ErrCode) assert.T(t, !c.access) }
func TestServerNoAccess(t *testing.T) { b := make(bchan, 2) c := &conn{ c: b, canWrite: true, st: store.New(), } tx := &txn{ c: c, req: request{Tag: proto.Int32(1)}, } for i, op := range ops { if i != int32(request_ACCESS) { op(tx) var exp response_Err = response_OTHER assert.Equal(t, 4, len(<-b), request_Verb_name[i]) assert.Equal(t, &exp, mustUnmarshal(<-b).ErrCode, request_Verb_name[i]) } } }
func TestServerRo(t *testing.T) { b := make(bchan, 2) c := &conn{ c: b, canWrite: true, st: store.New(), } tx := &txn{ c: c, req: request{Tag: proto.Int32(1)}, } wops := []int32{int32(request_DEL), int32(request_NOP), int32(request_SET)} for _, i := range wops { op := ops[i] op(tx) var exp response_Err = response_OTHER assert.Equal(t, 4, len(<-b), request_Verb_name[i]) assert.Equal(t, &exp, mustUnmarshal(<-b).ErrCode, request_Verb_name[i]) } }
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package proto_test import ( "log" "testing" "goprotobuf.googlecode.com/hg/proto" pb "./testdata/_obj/test_proto" ) var cloneTestMessage = &pb.MyMessage{ Count: proto.Int32(42), Name: proto.String("Dave"), Pet: []string{"bunny", "kitty", "horsey"}, Inner: &pb.InnerMessage{ Host: proto.String("niles"), Port: proto.Int32(9099), Connected: proto.Bool(true), }, Others: []*pb.OtherMessage{ &pb.OtherMessage{ Value: []byte("some bytes"), }, }, RepBytes: [][]byte{[]byte("sham"), []byte("wow")}, }