lkcp9_readloop_generic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  2. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  3. // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
  4. // Copyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.
  5. //
  6. // All rights reserved.
  7. //
  8. // All use of this code is governed by the MIT license.
  9. // The complete license is available in the LICENSE file.
  10. // +build !linux
  11. package lkcp9 // import "go.gridfinity.dev/lkcp9"
  12. import (
  13. "sync/atomic"
  14. )
  15. func (
  16. s *UDPSession,
  17. ) readLoop() {
  18. buf := make(
  19. []byte,
  20. KcpMtuLimit,
  21. )
  22. var src string
  23. for {
  24. if n, addr, err := s.conn.ReadFrom(
  25. buf,
  26. ); err == nil {
  27. if src == "" {
  28. src = addr.String()
  29. } else if addr.String() != src {
  30. atomic.AddUint64(
  31. &DefaultSnsi.KcpInputErrors,
  32. 1,
  33. )
  34. continue
  35. }
  36. if n >= s.headerSize+IKCP_OVERHEAD {
  37. s.packetInput(
  38. buf[:n],
  39. )
  40. } else {
  41. atomic.AddUint64(
  42. &DefaultSnsi.KcpInputErrors,
  43. 1,
  44. )
  45. }
  46. } else {
  47. s.chReadError <- err
  48. return
  49. }
  50. }
  51. }
  52. func (
  53. l *Listener,
  54. ) monitor() {
  55. buf := make(
  56. []byte,
  57. KcpMtuLimit,
  58. )
  59. for {
  60. if n, from, err := l.conn.ReadFrom(
  61. buf,
  62. ); err == nil {
  63. if n >= l.headerSize+IKCP_OVERHEAD {
  64. l.packetInput(
  65. buf[:n],
  66. from,
  67. )
  68. } else {
  69. atomic.AddUint64(
  70. &DefaultSnsi.KcpInputErrors,
  71. 1,
  72. )
  73. }
  74. } else {
  75. return
  76. }
  77. }
  78. }