property.go 973 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package protocol
  2. // 消息体属性
  3. type Property uint16
  4. // 启用分包
  5. func (property *Property) enablePacket() {
  6. val := uint16(*property)
  7. *property = Property(val | (1 << 13))
  8. }
  9. // 启用加密
  10. func (property *Property) enableEncrypt() {
  11. val := uint16(*property)
  12. *property = Property(val | (1 << 10))
  13. }
  14. // 是否分包
  15. func (property Property) IsEnablePacket() bool {
  16. val := uint16(property)
  17. return val&(1<<13) > 0
  18. }
  19. // 是否加密
  20. func (property Property) IsEnableEncrypt() bool {
  21. val := uint16(property)
  22. return val&(1<<10) > 0
  23. }
  24. // 获取消息体长度
  25. func (property *Property) GetBodySize() uint16 {
  26. // 前十位表示消息体长度
  27. // 0x3ff == ‭001111111111‬
  28. val := uint16(*property)
  29. return ((val << 6) >> 6) & 0x3ff
  30. }
  31. // 设置消息体长度
  32. func (property *Property) SetBodySize(size uint16) error {
  33. if size > 0x3ff {
  34. return ErrBodyTooLong
  35. }
  36. val := uint16(*property)
  37. *property = Property(((val >> 10) << 10) | size)
  38. return nil
  39. }