當前位置: 首頁>>代碼示例>>Golang>>正文


Golang proto.Unmarshal函數代碼示例

本文整理匯總了Golang中code/google/com/p/goprotobuf/proto.Unmarshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unmarshal函數的具體用法?Golang Unmarshal怎麽用?Golang Unmarshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Unmarshal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getProto

// Given a set of bytes and a type, return a protocol buffer object.
func getProto(msgtype int, data []byte) (*riakMessage, error) {
	var ret *riakMessage = nil

	switch msgtype {
	case 0x09:
		obj := &riak.RpbGetReq{}
		err := proto.Unmarshal(data, obj)
		if err != nil {
			return nil, err
		}

		ret = &riakMessage{method: "get", bucket: []byte(obj.Bucket),
			key: []byte(obj.Key)}
	case 0x0a:
		// get response
	case 0x0b:
		obj := &riak.RpbPutReq{}
		err := proto.Unmarshal(data, obj)
		if err != nil {
			return nil, err
		}

		ret = &riakMessage{method: "put", bucket: []byte(obj.Bucket),
			key: []byte(obj.Key)}
	case 0x0c:
		// put response
	}

	return ret, nil
}
開發者ID:zorkian,項目名稱:riak-sniffer,代碼行數:31,代碼來源:riak-sniffer.go

示例2: frameworkMessageCB

//export frameworkMessageCB
func frameworkMessageCB(
	ptr unsafe.Pointer,
	executorIdMessage *C.ProtobufObj,
	slaveIdMessage *C.ProtobufObj,
	dataMessage *C.ProtobufObj) {
	if ptr != nil {
		var driver *SchedulerDriver = (*SchedulerDriver)(ptr)
		if driver.Scheduler.FrameworkMessage == nil {
			return
		}
		executorData := C.GoBytes(executorIdMessage.data, C.int(executorIdMessage.size))
		var executorId ExecutorID
		err := proto.Unmarshal(executorData, &executorId)
		if err != nil {
			return
		}

		slaveData := C.GoBytes(slaveIdMessage.data, C.int(slaveIdMessage.size))
		var slaveId SlaveID
		err = proto.Unmarshal(slaveData, &slaveId)
		if err != nil {
			return
		}

		message := C.GoBytes(dataMessage.data, C.int(dataMessage.size))
		var messageString string = string(message)

		driver.Scheduler.FrameworkMessage(driver, executorId, slaveId, messageString)
	}
}
開發者ID:luffyhwl,項目名稱:gopark,代碼行數:31,代碼來源:scheduler.go

示例3: executorLostCB

//export executorLostCB
func executorLostCB(
	ptr unsafe.Pointer,
	executorIdMessage *C.ProtobufObj,
	slaveIdMessage *C.ProtobufObj,
	status C.int) {
	if ptr != nil {
		var driver *SchedulerDriver = (*SchedulerDriver)(ptr)
		if driver.Scheduler.ExecutorLost == nil {
			return
		}

		executorData := C.GoBytes(executorIdMessage.data, C.int(executorIdMessage.size))
		var executorId ExecutorID
		err := proto.Unmarshal(executorData, &executorId)
		if err != nil {
			return
		}

		slaveData := C.GoBytes(slaveIdMessage.data, C.int(slaveIdMessage.size))
		var slaveId SlaveID
		err = proto.Unmarshal(slaveData, &slaveId)
		if err != nil {
			return
		}

		driver.Scheduler.ExecutorLost(driver, executorId, slaveId, int(status))
	}
}
開發者ID:luffyhwl,項目名稱:gopark,代碼行數:29,代碼來源:scheduler.go

示例4: readReply

func readReply(conn *net.TCPConn) {
	length := int32(0)
	binary.Read(conn, binary.LittleEndian, &length)
	rbuf := make([]byte, length)
	io.ReadFull(conn, rbuf)
	if len(rbuf) < 9 {
		return
	}
	cmd := int(rbuf[8])
	switch cmd {
	case S2C_HelloInfoResult_CMD:
		res := &Auth_S2C.HelloInfoResult{}
		err := proto.Unmarshal(rbuf[9:], res)
		if err != nil {
			log.Fatal(err)
		}
		log.Print(res.GetToken())
	case S2C_LoginUserInfo_CMD:
		res := &Auth_S2C.LoginUserInfo{}
		err := proto.Unmarshal(rbuf[9:], res)
		if err != nil {
			log.Fatal(err)
		}
		log.Print(res.GetMyInfo(), res.GetServerTime())
	case ERROR_ErrorInfo_CMD:
		res := &Auth_S2C.ErrorInfo{}
		err := proto.Unmarshal(rbuf[9:], res)
		if err != nil {
			log.Fatal(err)
		}
		log.Print("error type: ", res.GetType())
	default:
		fmt.Println("Unexpected CMD: ", cmd)
	}
}
開發者ID:botaydotcom,項目名稱:GoMessagingTestFrameWork,代碼行數:35,代碼來源:originalTestCase.go

示例5: registeredCB

//export registeredCB
func registeredCB(
	ptr unsafe.Pointer,
	frameworkMessage *C.ProtobufObj,
	masterMessage *C.ProtobufObj) {
	if ptr != nil {
		var driver *SchedulerDriver = (*SchedulerDriver)(ptr)

		if driver.Scheduler.Registered == nil {
			return
		}

		frameworkData := C.GoBytes(
			frameworkMessage.data,
			C.int(frameworkMessage.size))

		var frameworkId FrameworkID
		err := proto.Unmarshal(frameworkData, &frameworkId)
		if err != nil {
			return
		}

		masterData := C.GoBytes(masterMessage.data, C.int(masterMessage.size))
		var masterInfo MasterInfo
		err = proto.Unmarshal(masterData, &masterInfo)
		if err != nil {
			return
		}

		driver.Scheduler.Registered(driver, frameworkId, masterInfo)
	}
}
開發者ID:luffyhwl,項目名稱:gopark,代碼行數:32,代碼來源:scheduler.go

示例6: deserializeByReader

func deserializeByReader(reader *bufio.Reader) (*pb.MobileSuiteProtobuf, error) {
	buff, _ := reader.Peek(4)
	data := bytes.NewBuffer(buff)
	var length int32
	err := binary.Read(data, binary.BigEndian, &length)
	if err != nil {
		return nil, err
	}
	fmt.Println(length)
	if int32(reader.Buffered()) < length+4 {
		return nil, err
	}

	pack := make([]byte, int(4+length))
	_, err = reader.Read(pack)
	if err != nil {
		return nil, err
	}
	msg := pack[4:]
	var dst pb.MobileSuiteProtobuf
	proto.Unmarshal(msg, &dst)
	fmt.Println(&dst)

	var testMsg pb.TestMessage
	proto.Unmarshal(dst.Message, &testMsg)
	fmt.Println(&testMsg)

	return &dst, nil
}
開發者ID:victoriest,項目名稱:point_and_line,代碼行數:29,代碼來源:protobuf_test_client.go

示例7: Marshal

func Marshal() {
	var Test testdata.GoEnum
	Test.Foo = new(testdata.FOO)
	fmt.Println(Test)

	data, err := proto.Marshal(&Test)
	if checkerr(err) {
		return
	}
	fmt.Println(data)

	var goe testdata.GoEnum
	err = proto.Unmarshal(data, &goe)
	if checkerr(err) {
		return
	}

	fmt.Println(goe)

	var i proto.Message
	err = proto.Unmarshal(data, i)
	if checkerr(err) {
		return
	}

	fmt.Println(i)
}
開發者ID:shaalx,項目名稱:protobuf,代碼行數:27,代碼來源:main.go

示例8: readHelloReply

// read a reply to a buffer based on the expected message type
// return error if reply message has different type of command than expected
func readHelloReply(conn *net.TCPConn) (proto.Message, error) {
	duration := time.Second * 10
	timeNow := time.Now()
	err := conn.SetReadDeadline(timeNow.Add(duration))
	if err != nil {
		TimeEncodedPrint("Cant set read timeout", err.Error())
		return nil, err
	}
	length := int32(0)
	err = binary.Read(conn, binary.LittleEndian, &length)
	if DEBUG_READING_MESSAGE {
		fmt.Println("TRYING TO READ MESSAGE LENGTH => ", length, " ERROR: ", err)
	}

	if err != nil {
		return nil, err
	}

	if BYPASS_CONNECTION_SERVER {
		tempBuf := make([]byte, 8)
		_, err = io.ReadFull(conn, tempBuf)
		if DEBUG_READING_MESSAGE {
			fmt.Println("Trying to read extra 8 bytes:", tempBuf, " PROBLEM: ", err)
		}

		if err != nil {
			return nil, err
		}
		length = length - 8
	}

	rbuf := make([]byte, length)
	n, err := io.ReadFull(conn, rbuf)

	cmd := int(rbuf[0])

	switch cmd {
	case S2C_HelloInfoResult_CMD:
		res := &Auth_S2C.HelloInfoResult{}
		err := proto.Unmarshal(rbuf[1:], res)
		if err != nil {
			log.Fatal(err)
		} else {
			return res, nil
		}

	case ERROR_ErrorInfo_CMD:
		res := &Auth_S2C.ErrorInfo{}
		err := proto.Unmarshal(rbuf[1:], res)
		if err != nil {
			log.Fatal(err)
		}
		msg := fmt.Sprint("Server returns error: ")
		return res, errors.New(msg)
	default:
		log.Fatal("Unexpected CMD: ", cmd, "length is: ", length, " number bytes read: ", n, " error: ", err)
	}
	return nil, nil

}
開發者ID:botaydotcom,項目名稱:GoMessagingTestFrameWork,代碼行數:62,代碼來源:stressTestEngine_hardcode.go

示例9: mp_response

// Deserializes the data from possibly multiple packets,
// currently only for pb.RpbListKeysResp.
func (c *Client) mp_response(conn *net.TCPConn) (response [][]byte, err error) {
	defer c.releaseConn(conn)
	var (
		partial *pb.RpbListKeysResp
		msgcode byte
	)

	for {
		// Read the response from Riak
		msgbuf, err := c.read(conn, 5)
		if err != nil {
			return nil, err
		}
		// Check the length
		if len(msgbuf) < 5 {
			return nil, BadResponseLength
		}
		// Read the message length, read the rest of the message if necessary
		msglen := int(msgbuf[0])<<24 + int(msgbuf[1])<<16 + int(msgbuf[2])<<8 + int(msgbuf[3])
		pbmsg, err := c.read(conn, msglen-1)
		if err != nil {
			return nil, err
		}

		// Deserialize, by default the calling method should provide the expected RbpXXXResp
		msgcode = msgbuf[4]

		if msgcode == messageCodes["RpbListKeysResp"] {
			partial = &pb.RpbListKeysResp{}
			err = proto.Unmarshal(pbmsg, partial)
			if err != nil {
				return nil, err
			}

			response = append(response, partial.Keys...)

			if partial.Done != nil {
				break
			}
		} else if msgcode == messageCodes["RpbErrorResp"] {
			errResp := &pb.RpbErrorResp{}
			err = proto.Unmarshal(pbmsg, errResp)
			if err == nil {
				err = errors.New(string(errResp.Errmsg))
			} else {
				err = fmt.Errorf("Cannot deserialize error response from Riak - %v", err)
			}
			return nil, err
		} else {
			return nil, err
		}
	}

	return
}
開發者ID:keli,項目名稱:goriakpbc,代碼行數:57,代碼來源:client.go

示例10: readReply

// read a reply to a buffer based on the expected message type
// return error if reply message has different type of command than expected
func readReply(conn *net.TCPConn) (proto.Message, error) {
	duration := time.Millisecond * 20
	timeNow := time.Now()
	err := conn.SetReadDeadline(timeNow.Add(duration))
	if err != nil {
		TimeEncodedPrint("Cant set read timeout", err.Error())
		return nil, err
	}
	length := int32(0)
	err = binary.Read(conn, binary.LittleEndian, &length)
	if err != nil {
		return nil, nil
	}

	if DEBUG_STEP {
		fmt.Println("RECEIVE MESSAGE LENGTH: ", length)
	}
	// now wait longer for the message data
	duration = time.Millisecond * 100
	timeNow = time.Now()
	err = conn.SetReadDeadline(timeNow.Add(duration))

	rbuf := make([]byte, length)
	io.ReadFull(conn, rbuf)
	if DEBUG_STEP {
		fmt.Println(rbuf)
	}

	cmd := int(rbuf[0])

	switch cmd {
	case S2C_HelloInfoResult_CMD:
		res := &Auth_S2C.HelloInfoResult{}
		err := proto.Unmarshal(rbuf[1:], res)
		if err != nil {
			log.Fatal(err)
		} else {
			return res, nil
		}

	case ERROR_ErrorInfo_CMD:
		res := &Auth_S2C.ErrorInfo{}
		err := proto.Unmarshal(rbuf[1:], res)
		if err != nil {
			log.Fatal(err)
		}
		msg := fmt.Sprint("Server returns error: ")
		return res, errors.New(msg)
	default:
		log.Fatal("Unexpected CMD: ", cmd)
	}
	return nil, nil

}
開發者ID:botaydotcom,項目名稱:GoMessagingTestFrameWork,代碼行數:56,代碼來源:stressTest_single_routine.go

示例11: processKeyExchange

func (contact *Contact) processKeyExchange(kxsBytes []byte, testing bool) error {
	var kxs pond.SignedKeyExchange
	if err := proto.Unmarshal(kxsBytes, &kxs); err != nil {
		return err
	}

	var sig [64]byte
	if len(kxs.Signature) != len(sig) {
		return errors.New("invalid signature length")
	}
	copy(sig[:], kxs.Signature)

	var kx pond.KeyExchange
	if err := proto.Unmarshal(kxs.Signed, &kx); err != nil {
		return err
	}

	if len(kx.PublicKey) != len(contact.theirPub) {
		return errors.New("invalid public key")
	}
	copy(contact.theirPub[:], kx.PublicKey)

	if !ed25519.Verify(&contact.theirPub, kxs.Signed, &sig) {
		return errors.New("invalid signature")
	}

	contact.theirServer = *kx.Server
	if _, _, err := parseServer(contact.theirServer, testing); err != nil {
		return err
	}

	group, ok := new(bbssig.Group).Unmarshal(kx.Group)
	if !ok {
		return errors.New("invalid group")
	}
	if contact.myGroupKey, ok = new(bbssig.MemberKey).Unmarshal(group, kx.GroupKey); !ok {
		return errors.New("invalid group key")
	}

	if len(kx.IdentityPublic) != len(contact.theirIdentityPublic) {
		return errors.New("invalid public identity")
	}
	copy(contact.theirIdentityPublic[:], kx.IdentityPublic)

	if len(kx.Dh) != len(contact.theirCurrentDHPublic) {
		return errors.New("invalid public DH value")
	}
	copy(contact.theirCurrentDHPublic[:], kx.Dh)

	contact.generation = *kx.Generation

	return nil
}
開發者ID:nico,項目名稱:pond,代碼行數:53,代碼來源:client.go

示例12: Recv

func Recv(conn net.Conn) {
	defer func() { sendChan <- nil }()

	for {
		nethead := [HeadLen]byte{}
		if _, e := io.ReadFull(conn, nethead[:]); e != nil {
			fmt.Println(conn.RemoteAddr().String(), "Error recv head : ", e)
			break
		}
		msglen := *(*uint64)(unsafe.Pointer(&nethead))
		msgbuf := make([]byte, HeadLen+msglen)

		if _, e := io.ReadFull(conn, msgbuf[HeadLen:]); e != nil {
			fmt.Println(conn.RemoteAddr().String(), "Error recv body : ", e)
			break
		}

		mh := (*MessageHead)(unsafe.Pointer(&msgbuf[0]))
		mh.Msglen = msglen
		if uint32(len(msgbuf)) < MsgLen+RecLen {
			continue
		}

		rh := (*RecordHead)(unsafe.Pointer(&msgbuf[MsgLen]))

		if uint32(len(msgbuf)) < MsgLen+RecLen+rh.Length {
			continue
		}

		bodybuf := msgbuf[MsgLen+RecLen : MsgLen+RecLen+rh.Length]
		if rh.HashCode != Common.Hash(bodybuf) {
			continue
		}

		switch rh.RecType {
		case uint32(ConstDefine_E_DepthMarketData):
			dmd := &DepthMarketData{}
			proto.Unmarshal(bodybuf, dmd)
			quoteChan <- dmd

		case uint32(ConstDefine_E_ReqKLine):
			rkl := &ReqKLine{}
			proto.Unmarshal(bodybuf, rkl)
			klineChan <- &KlineReq{mh.Sender, mh.Copyer, mh.SendTime, rkl}

		case uint32(ConstDefine_E_ReqTick):
			rt := &ReqTick{}
			proto.Unmarshal(bodybuf, rt)
			tickChan <- &TickReq{mh.Sender, mh.Copyer, mh.SendTime, rt}
		}
	}
}
開發者ID:6xiao,項目名稱:GoQuant,代碼行數:52,代碼來源:HD.go

示例13: availabilityIntervals

func availabilityIntervals(inputChan, outputChan chan *store.Record) {
	writeRecord := func(firstKey, lastKey *TraceKey, firstTrace, lastTrace []byte) {
		firstTraceDecoded := Trace{}
		if err := proto.Unmarshal(firstTrace, &firstTraceDecoded); err != nil {
			log.Fatalf("Error ummarshaling protocol buffer: %v", err)
		}
		lastTraceDecoded := Trace{}
		if err := proto.Unmarshal(lastTrace, &lastTraceDecoded); err != nil {
			log.Fatalf("Error ummarshaling protocol buffer: %v", err)
		}
		intervalKey := intervalKey{
			NodeId:               firstKey.NodeId,
			AnonymizationContext: firstKey.AnonymizationContext,
			SessionId:            firstKey.SessionId,
			FirstSequenceNumber:  firstKey.SequenceNumber,
			LastSequenceNumber:   lastKey.SequenceNumber,
		}
		outputChan <- &store.Record{
			Key:   encodeIntervalKey(&intervalKey),
			Value: lex.EncodeOrDie(*firstTraceDecoded.TraceCreationTimestamp, *lastTraceDecoded.TraceCreationTimestamp),
		}
	}

	var firstKey, lastKey *TraceKey
	var firstTrace, lastTrace []byte
	var expectedTraceKey []byte
	for record := range inputChan {
		var traceKey TraceKey
		lex.DecodeOrDie(record.Key, &traceKey)
		expectedNextTraceKeyDecoded := TraceKey{
			NodeId:               traceKey.NodeId,
			AnonymizationContext: traceKey.AnonymizationContext,
			SessionId:            traceKey.SessionId,
			SequenceNumber:       traceKey.SequenceNumber + 1,
		}
		expectedNextTraceKey := lex.EncodeOrDie(&expectedNextTraceKeyDecoded)

		if !bytes.Equal(expectedTraceKey, record.Key) {
			if firstTrace != nil {
				writeRecord(firstKey, lastKey, firstTrace, lastTrace)
			}
			firstKey = &traceKey
			firstTrace = record.Value
		}
		lastKey = &traceKey
		lastTrace = record.Value
		expectedTraceKey = expectedNextTraceKey
	}
	if firstTrace != nil {
		writeRecord(firstKey, lastKey, firstTrace, lastTrace)
	}
}
開發者ID:sburnett,項目名稱:bismark-passive-server-go,代碼行數:52,代碼來源:availability.go

示例14: Call

func (c *context) Call(service, method string, in, out proto.Message, opts *internal.CallOptions) error {
	req, err := proto.Marshal(in)
	if err != nil {
		return fmt.Errorf("error marshalling request: %v", err)
	}

	remReq := &pb.Request{
		ServiceName: proto.String(service),
		Method:      proto.String(method),
		Request:     req,
		// NOTE(djd): RequestId is unused in the server.
	}

	req, err = proto.Marshal(remReq)
	if err != nil {
		return fmt.Errorf("proto.Marshal: %v", err)
	}

	// TODO(djd): Respect opts.Timeout?
	resp, err := c.client.Post(c.url, "application/octet-stream", bytes.NewReader(req))
	if err != nil {
		return fmt.Errorf("error sending request: %v", err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("bad response %d; body: %q", resp.StatusCode, body)
	}
	if err != nil {
		return fmt.Errorf("failed reading response: %v", err)
	}
	remResp := &pb.Response{}
	if err := proto.Unmarshal(body, remResp); err != nil {
		return fmt.Errorf("error unmarshalling response: %v", err)
	}

	if ae := remResp.GetApplicationError(); ae != nil {
		return &internal.APIError{
			Code:    ae.GetCode(),
			Detail:  ae.GetDetail(),
			Service: service,
		}
	}

	if remResp.Response == nil {
		return fmt.Errorf("unexpected response: %s", proto.MarshalTextString(remResp))
	}

	return proto.Unmarshal(remResp.Response, out)
}
開發者ID:proppy,項目名稱:appengine,代碼行數:51,代碼來源:client.go

示例15: tryIgnoreReceivedMessage

func tryIgnoreReceivedMessage(buffer []byte) bool {
	if DEBUG_IGNORING_MESSAGE {
		fmt.Println("Trying to ignore received message buffer: ", buffer)
		fmt.Println("Global ignore list:", globalIgnoreMessages)
		fmt.Println("Current ignore list:", currentIgnoreMessages)
	}

	baseCmd := buffer[0]
	// report error
	signature := calculateSignature(0, int(baseCmd))
	if DEBUG_IGNORING_MESSAGE {
		fmt.Println("Signature 1: ", signature)
	}
	if ignoreType, exist := currentIgnoreMessages[signature]; exist {
		newValue := magicVarFunc(ignoreType)
		res := newValue.(proto.Message)
		err := proto.Unmarshal(buffer[1:], res)
		if err == nil {
			// can ignore this message
			if DEBUG_IGNORING_MESSAGE {
				fmt.Println("Ignored!")
			}
			return true
		}
	}

	if len(buffer) >= 2 {
		signature = calculateSignature(int(baseCmd), int(buffer[1]))
		if DEBUG_IGNORING_MESSAGE {
			fmt.Println("Signature 2: ", signature)
		}
		if ignoreType, exist := currentIgnoreMessages[signature]; exist {
			newValue := magicVarFunc(ignoreType)
			res := newValue.(proto.Message)
			err := proto.Unmarshal(buffer[1:], res)
			if err == nil {
				// can ignore this message
				if DEBUG_IGNORING_MESSAGE {
					fmt.Println("Ignored!")
				}
				return true
			}
		}
	}
	if DEBUG_IGNORING_MESSAGE {
		fmt.Println("Not ignored!")
	}
	return false
}
開發者ID:botaydotcom,項目名稱:GoMessagingTestFrameWork,代碼行數:49,代碼來源:testEngine.go


注:本文中的code/google/com/p/goprotobuf/proto.Unmarshal函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。