utp.go 696 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utp
  2. import (
  3. "context"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/dice"
  6. "v2ray.com/core/common/serial"
  7. )
  8. type UTP struct {
  9. header byte
  10. extension byte
  11. connectionId uint16
  12. }
  13. func (*UTP) Size() int {
  14. return 4
  15. }
  16. func (u *UTP) Write(b []byte) (int, error) {
  17. serial.Uint16ToBytes(u.connectionId, b[:0])
  18. b[2] = u.header
  19. b[3] = u.extension
  20. return 4, nil
  21. }
  22. // NewUTP creates a new UTP header for the given config.
  23. func NewUTP(ctx context.Context, config interface{}) (interface{}, error) {
  24. return &UTP{
  25. header: 1,
  26. extension: 0,
  27. connectionId: dice.RandomUint16(),
  28. }, nil
  29. }
  30. func init() {
  31. common.Must(common.RegisterConfig((*Config)(nil), NewUTP))
  32. }