123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
- // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
- // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
- // Copyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.
- //
- // All rights reserved.
- //
- // All use of this code is governed by the MIT license.
- // The complete license is available in the LICENSE file.
- // +build linux
- package lkcp9 // import "go.gridfinity.dev/lkcp9"
- import (
- "net"
- "sync/atomic"
- "golang.org/x/net/ipv4"
- "golang.org/x/net/ipv6"
- )
- const (
- batchSize = 16
- )
- func (
- s *UDPSession,
- ) readLoop() {
- addr, _ := net.ResolveUDPAddr(
- "udp",
- s.conn.LocalAddr().String(),
- )
- if addr.IP.To4() != nil {
- s.readLoopIPv4()
- } else {
- s.readLoopIPv6()
- }
- }
- func (
- s *UDPSession,
- ) readLoopIPv6() {
- var src string
- msgs := make(
- []ipv6.Message,
- batchSize,
- )
- for k := range msgs {
- msgs[k].Buffers = [][]byte{
- make(
- []byte,
- KcpMtuLimit),
- }
- }
- conn := ipv6.NewPacketConn(
- s.conn,
- )
- for {
- if count, err := conn.ReadBatch(
- msgs,
- 0,
- ); err == nil {
- for i := 0; i < count; i++ {
- msg := &msgs[i]
- if src == "" {
- src = msg.Addr.String()
- } else if msg.Addr.String() != src {
- atomic.AddUint64(
- &DefaultSnsi.KcpPreInputErrors,
- 1,
- )
- continue
- }
- if msg.N < s.headerSize+IKCP_OVERHEAD {
- atomic.AddUint64(
- &DefaultSnsi.KcpInputErrors,
- 1,
- )
- continue
- }
- s.packetInput(
- msg.Buffers[0][:msg.N],
- )
- }
- } else {
- s.chReadError <- err
- return
- }
- }
- }
- func (
- s *UDPSession,
- ) readLoopIPv4() {
- var src string
- msgs := make(
- []ipv4.Message,
- batchSize,
- )
- for k := range msgs {
- msgs[k].Buffers = [][]byte{make(
- []byte,
- KcpMtuLimit,
- )}
- }
- conn := ipv4.NewPacketConn(
- s.conn,
- )
- for {
- if count, err := conn.ReadBatch(
- msgs,
- 0,
- ); err == nil {
- for i := 0; i < count; i++ {
- msg := &msgs[i]
- if src == "" {
- src = msg.Addr.String()
- } else if msg.Addr.String() != src {
- atomic.AddUint64(
- &DefaultSnsi.KcpInputErrors,
- 1,
- )
- continue
- }
- if msg.N < s.headerSize+IKCP_OVERHEAD {
- atomic.AddUint64(
- &DefaultSnsi.KcpInputErrors,
- 1,
- )
- continue
- }
- s.packetInput(
- msg.Buffers[0][:msg.N],
- )
- }
- } else {
- s.chReadError <- err
- return
- }
- }
- }
- func (
- l *Listener,
- ) monitor() {
- addr, _ := net.ResolveUDPAddr(
- "udp",
- l.conn.LocalAddr().String(),
- )
- if addr.IP.To4() != nil {
- l.monitorIPv4()
- } else {
- l.monitorIPv6()
- }
- }
- func (
- l *Listener,
- ) monitorIPv4() {
- msgs := make(
- []ipv4.Message,
- batchSize,
- )
- for k := range msgs {
- msgs[k].Buffers = [][]byte{make(
- []byte,
- KcpMtuLimit,
- )}
- }
- conn := ipv4.NewPacketConn(
- l.conn,
- )
- for {
- if count, err := conn.ReadBatch(
- msgs,
- 0,
- ); err == nil {
- for i := 0; i < count; i++ {
- msg := &msgs[i]
- if msg.N >= l.headerSize+IKCP_OVERHEAD {
- l.packetInput(
- msg.Buffers[0][:msg.N],
- msg.Addr,
- )
- } else {
- atomic.AddUint64(
- &DefaultSnsi.KcpInputErrors,
- 1,
- )
- }
- }
- } else {
- return
- }
- }
- }
- func (
- l *Listener,
- ) monitorIPv6() {
- msgs := make(
- []ipv6.Message,
- batchSize,
- )
- for k := range msgs {
- msgs[k].Buffers = [][]byte{make(
- []byte,
- KcpMtuLimit,
- )}
- }
- conn := ipv4.NewPacketConn(
- l.conn,
- )
- for {
- if count, err := conn.ReadBatch(
- msgs,
- 0,
- ); err == nil {
- for i := 0; i < count; i++ {
- msg := &msgs[i]
- if msg.N >= l.headerSize+IKCP_OVERHEAD {
- l.packetInput(
- msg.Buffers[0][:msg.N],
- msg.Addr,
- )
- } else {
- atomic.AddUint64(
- &DefaultSnsi.KcpInputErrors,
- 1,
- )
- }
- }
- } else {
- return
- }
- }
- }
|