utp.go 672 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utp
  2. import (
  3. "math/rand"
  4. "v2ray.com/core/common/serial"
  5. "v2ray.com/core/transport/internet"
  6. )
  7. type UTP struct {
  8. header byte
  9. extension byte
  10. connectionId uint16
  11. }
  12. func (v *UTP) Size() int {
  13. return 4
  14. }
  15. func (v *UTP) Write(b []byte) (int, error) {
  16. serial.Uint16ToBytes(v.connectionId, b[:0])
  17. b[2] = v.header
  18. b[3] = v.extension
  19. return 4, nil
  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(serial.GetMessageType(new(Config)), UTPFactory{})
  31. }