NewPacket creates a new Packet object from a set of bytes. The firstLayerDecoder tells it how to interpret the first layer from the bytes, future layers will be generated from that first layer automatically.
(data []byte, firstLayerDecoder Decoder, options DecodeOptions)
| 652 | // firstLayerDecoder tells it how to interpret the first layer from the bytes, |
| 653 | // future layers will be generated from that first layer automatically. |
| 654 | func NewPacket(data []byte, firstLayerDecoder Decoder, options DecodeOptions) Packet { |
| 655 | if !options.NoCopy { |
| 656 | dataCopy := make([]byte, len(data)) |
| 657 | copy(dataCopy, data) |
| 658 | data = dataCopy |
| 659 | } |
| 660 | if options.Lazy { |
| 661 | p := &lazyPacket{ |
| 662 | packet: packet{data: data, decodeOptions: options}, |
| 663 | next: firstLayerDecoder, |
| 664 | } |
| 665 | p.layers = p.initialLayers[:0] |
| 666 | // Crazy craziness: |
| 667 | // If the following return statemet is REMOVED, and Lazy is FALSE, then |
| 668 | // eager packet processing becomes 17% FASTER. No, there is no logical |
| 669 | // explanation for this. However, it's such a hacky micro-optimization that |
| 670 | // we really can't rely on it. It appears to have to do with the size the |
| 671 | // compiler guesses for this function's stack space, since one symptom is |
| 672 | // that with the return statement in place, we more than double calls to |
| 673 | // runtime.morestack/runtime.lessstack. We'll hope the compiler gets better |
| 674 | // over time and we get this optimization for free. Until then, we'll have |
| 675 | // to live with slower packet processing. |
| 676 | return p |
| 677 | } |
| 678 | p := &eagerPacket{ |
| 679 | packet: packet{data: data, decodeOptions: options}, |
| 680 | } |
| 681 | p.layers = p.initialLayers[:0] |
| 682 | p.initialDecode(firstLayerDecoder) |
| 683 | return p |
| 684 | } |
| 685 | |
| 686 | // PacketDataSource is an interface for some source of packet data. Users may |
| 687 | // create their own implementations, or use the existing implementations in |
searching dependent graphs…