utp.go 722 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utp
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "github.com/v2fly/v2ray-core/v5/common"
  6. "github.com/v2fly/v2ray-core/v5/common/dice"
  7. )
  8. type UTP struct {
  9. header byte
  10. extension byte
  11. connectionID uint16
  12. }
  13. func (*UTP) Size() int32 {
  14. return 4
  15. }
  16. // Serialize implements PacketHeader.
  17. func (u *UTP) Serialize(b []byte) {
  18. binary.BigEndian.PutUint16(b, u.connectionID)
  19. b[2] = u.header
  20. b[3] = u.extension
  21. }
  22. // New creates a new UTP header for the given config.
  23. func New(ctx context.Context, config interface{}) (interface{}, error) {
  24. return &UTP{
  25. header: 1,
  26. extension: 0,
  27. connectionID: dice.RollUint16(),
  28. }, nil
  29. }
  30. func init() {
  31. common.Must(common.RegisterConfig((*Config)(nil), New))
  32. }