utp.go 688 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utp
  2. import (
  3. "math/rand"
  4. "v2ray.com/core/common/loader"
  5. "v2ray.com/core/common/serial"
  6. "v2ray.com/core/transport/internet"
  7. )
  8. type UTP struct {
  9. header byte
  10. extension byte
  11. connectionId uint16
  12. }
  13. func (v *UTP) Size() int {
  14. return 4
  15. }
  16. func (v *UTP) Write(b []byte) int {
  17. b = serial.Uint16ToBytes(v.connectionId, b[:0])
  18. b = append(b, v.header, v.extension)
  19. return 4
  20. }
  21. type UTPFactory struct{}
  22. func (v UTPFactory) Create(rawSettings interface{}) internet.PacketHeader {
  23. return &UTP{
  24. header: 1,
  25. extension: 0,
  26. connectionId: uint16(rand.Intn(65536)),
  27. }
  28. }
  29. func init() {
  30. internet.RegisterPacketHeader(loader.GetType(new(Config)), UTPFactory{})
  31. }