priority.go 727 B

123456789101112131415161718192021222324252627282930313233
  1. package ice
  2. import (
  3. "encoding/binary"
  4. "github.com/pion/stun"
  5. )
  6. // PriorityAttr represents PRIORITY attribute.
  7. type PriorityAttr uint32
  8. const prioritySize = 4 // 32 bit
  9. // AddTo adds PRIORITY attribute to message.
  10. func (p PriorityAttr) AddTo(m *stun.Message) error {
  11. v := make([]byte, prioritySize)
  12. binary.BigEndian.PutUint32(v, uint32(p))
  13. m.Add(stun.AttrPriority, v)
  14. return nil
  15. }
  16. // GetFrom decodes PRIORITY attribute from message.
  17. func (p *PriorityAttr) GetFrom(m *stun.Message) error {
  18. v, err := m.Get(stun.AttrPriority)
  19. if err != nil {
  20. return err
  21. }
  22. if err = stun.CheckSize(stun.AttrPriority, len(v), prioritySize); err != nil {
  23. return err
  24. }
  25. *p = PriorityAttr(binary.BigEndian.Uint32(v))
  26. return nil
  27. }