kcp.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package hub
  2. import (
  3. "errors"
  4. "net"
  5. "time"
  6. "github.com/v2ray/v2ray-core/common/log"
  7. "github.com/v2ray/v2ray-core/transport/hub/kcpv"
  8. "github.com/xtaci/kcp-go"
  9. )
  10. type KCPVlistener struct {
  11. lst *kcp.Listener
  12. conf *kcpv.Config
  13. }
  14. func (kvl *KCPVlistener) Accept() (*KCPVconn, error) {
  15. conn, err := kvl.lst.Accept()
  16. if err != nil {
  17. return nil, err
  18. }
  19. nodelay, interval, resend, nc := 0, 40, 0, 0
  20. if kvl.conf.Mode != "manual" {
  21. switch kvl.conf.Mode {
  22. case "normal":
  23. nodelay, interval, resend, nc = 0, 30, 2, 1
  24. case "fast":
  25. nodelay, interval, resend, nc = 0, 20, 2, 1
  26. case "fast2":
  27. nodelay, interval, resend, nc = 1, 20, 2, 1
  28. case "fast3":
  29. nodelay, interval, resend, nc = 1, 10, 2, 1
  30. }
  31. } else {
  32. log.Error("kcp: Accepted Unsuccessfully: Manual mode is not supported.(yet!)")
  33. return nil, errors.New("kcp: Manual Not Implemented")
  34. }
  35. conn.SetNoDelay(nodelay, interval, resend, nc)
  36. conn.SetWindowSize(kvl.conf.AdvancedConfigs.Sndwnd, kvl.conf.AdvancedConfigs.Rcvwnd)
  37. conn.SetMtu(kvl.conf.AdvancedConfigs.Mtu)
  38. conn.SetACKNoDelay(kvl.conf.AdvancedConfigs.Acknodelay)
  39. conn.SetDSCP(kvl.conf.AdvancedConfigs.Dscp)
  40. kcv := &KCPVconn{hc: conn}
  41. kcv.conf = kvl.conf
  42. return kcv, nil
  43. }
  44. func (kvl *KCPVlistener) Close() error {
  45. return kvl.lst.Close()
  46. }
  47. func (kvl *KCPVlistener) Addr() net.Addr {
  48. return kvl.lst.Addr()
  49. }
  50. type KCPVconn struct {
  51. hc *kcp.UDPSession
  52. conf *kcpv.Config
  53. conntokeep time.Time
  54. }
  55. func (kcpvc *KCPVconn) Read(b []byte) (int, error) {
  56. ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.ReadTimeout) * time.Second)
  57. if ifb.After(kcpvc.conntokeep) {
  58. kcpvc.conntokeep = ifb
  59. }
  60. kcpvc.hc.SetDeadline(kcpvc.conntokeep)
  61. return kcpvc.hc.Read(b)
  62. }
  63. func (kcpvc *KCPVconn) Write(b []byte) (int, error) {
  64. ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.WriteTimeout) * time.Second)
  65. if ifb.After(kcpvc.conntokeep) {
  66. kcpvc.conntokeep = ifb
  67. }
  68. kcpvc.hc.SetDeadline(kcpvc.conntokeep)
  69. return kcpvc.hc.Write(b)
  70. }
  71. func (kcpvc *KCPVconn) Close() error {
  72. return kcpvc.hc.Close()
  73. }
  74. func (kcpvc *KCPVconn) LocalAddr() net.Addr {
  75. return kcpvc.hc.LocalAddr()
  76. }
  77. func (kcpvc *KCPVconn) RemoteAddr() net.Addr {
  78. return kcpvc.hc.RemoteAddr()
  79. }
  80. func (kcpvc *KCPVconn) SetDeadline(t time.Time) error {
  81. return kcpvc.hc.SetDeadline(t)
  82. }
  83. func (kcpvc *KCPVconn) SetReadDeadline(t time.Time) error {
  84. return kcpvc.hc.SetReadDeadline(t)
  85. }
  86. func (kcpvc *KCPVconn) SetWriteDeadline(t time.Time) error {
  87. return kcpvc.hc.SetWriteDeadline(t)
  88. }