MCPcopy Create free account
hub / github.com/facebook/time / DecodePacket

Function DecodePacket

ptp/protocol/protocol.go:548–583  ·  view source on GitHub ↗

DecodePacket provides single entry point to try and decode any []bytes to PTPv2 packet. It can be used for easy integration with anything that provides UDP packet payload as bytes. Resulting Packet user can then either switch based on MessageType(), or just with type switch.

(b []byte)

Source from the content-addressed store, hash-verified

546 reader := bytes.NewReader(rawBytes)
547 return binary.Read(reader, binary.BigEndian, p)
548}
549
550// DecodePacket provides single entry point to try and decode any []bytes to PTPv2 packet.
551// It can be used for easy integration with anything that provides UDP packet payload as bytes.
552// Resulting Packet user can then either switch based on MessageType(), or just with type switch.
553func DecodePacket(b []byte) (Packet, error) {
554 r := bytes.NewReader(b)
555 head := &Header{}
556 if err := binary.Read(r, binary.BigEndian, head); err != nil {
557 return nil, err
558 }
559 msgType := head.MessageType()
560 var p Packet
561 switch msgType {
562 case MessageSync, MessageDelayReq:
563 p = &SyncDelayReq{}
564 case MessagePDelayReq:
565 p = &PDelayReq{}
566 case MessagePDelayResp:
567 p = &PDelayResp{}
568 case MessageFollowUp:
569 p = &FollowUp{}
570 case MessageDelayResp:
571 p = &DelayResp{}
572 case MessagePDelayRespFollowUp:
573 p = &PDelayRespFollowUp{}
574 case MessageAnnounce:
575 p = &Announce{}
576 case MessageSignaling:
577 p = &Signaling{}
578 case MessageManagement:
579 return decodeMgmtPacket(b)
580 default:
581 return nil, fmt.Errorf("unsupported type %s", msgType)
582 }
583
584 if err := FromBytes(b, p); err != nil {
585 return nil, err
586 }

Calls 4

MessageTypeMethod · 0.95
decodeMgmtPacketFunction · 0.85
FromBytesFunction · 0.85
ReadMethod · 0.65

Tested by 15

TestParseSyncFunction · 0.68
TestParseFollowupFunction · 0.68
TestParsePDelayReqFunction · 0.68
TestParseAnnounceFunction · 0.68
TestParseDelayRespFunction · 0.68
TestFoundFuzzResultsFunction · 0.68
FuzzDecodePacketFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…