utp.go 697 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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, error) {
  17. serial.Uint16ToBytes(v.connectionId, b[:0])
  18. b[2] = v.header
  19. b[3] = v.extension
  20. return 4, nil
  21. }
  22. type UTPFactory struct{}
  23. func (v UTPFactory) Create(rawSettings interface{}) internet.PacketHeader {
  24. return &UTP{
  25. header: 1,
  26. extension: 0,
  27. connectionId: uint16(rand.Intn(65536)),
  28. }
  29. }
  30. func init() {
  31. internet.RegisterPacketHeader(loader.GetType(new(Config)), UTPFactory{})
  32. }