utp.go 718 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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() int32 {
  14. return 4
  15. }
  16. // Write implements io.Writer.
  17. func (u *UTP) Write(b []byte) (int, error) {
  18. serial.Uint16ToBytes(u.connectionId, b[:0])
  19. b[2] = u.header
  20. b[3] = u.extension
  21. return 4, nil
  22. }
  23. // New creates a new UTP header for the given config.
  24. func New(ctx context.Context, config interface{}) (interface{}, error) {
  25. return &UTP{
  26. header: 1,
  27. extension: 0,
  28. connectionId: dice.RollUint16(),
  29. }, nil
  30. }
  31. func init() {
  32. common.Must(common.RegisterConfig((*Config)(nil), New))
  33. }