mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 12:42:27 +08:00
chore: simplify the code
This commit is contained in:
parent
3922b17067
commit
5812a7bdeb
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/maphash"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -29,11 +28,12 @@ import (
|
|||||||
"github.com/metacubex/mihomo/tunnel/statistic"
|
"github.com/metacubex/mihomo/tunnel/statistic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const queueSize = 200
|
||||||
|
|
||||||
var (
|
var (
|
||||||
status = newAtomicStatus(Suspend)
|
status = newAtomicStatus(Suspend)
|
||||||
tcpQueue = make(chan C.ConnContext, 200)
|
tcpQueue = make(chan C.ConnContext, queueSize)
|
||||||
udpQueues []chan C.PacketAdapter
|
udpQueues []chan C.PacketAdapter
|
||||||
udpHashSeed = maphash.MakeSeed()
|
|
||||||
natTable = nat.New()
|
natTable = nat.New()
|
||||||
rules []C.Rule
|
rules []C.Rule
|
||||||
listeners = make(map[string]C.InboundListener)
|
listeners = make(map[string]C.InboundListener)
|
||||||
@ -73,13 +73,8 @@ func (t tunnel) HandleTCPConn(conn net.Conn, metadata *C.Metadata) {
|
|||||||
func (t tunnel) HandleUDPPacket(packet C.UDPPacket, metadata *C.Metadata) {
|
func (t tunnel) HandleUDPPacket(packet C.UDPPacket, metadata *C.Metadata) {
|
||||||
packetAdapter := C.NewPacketAdapter(packet, metadata)
|
packetAdapter := C.NewPacketAdapter(packet, metadata)
|
||||||
|
|
||||||
var h maphash.Hash
|
hash := utils.MapHash(metadata.SourceAddress() + "-" + metadata.RemoteAddress())
|
||||||
|
queueNo := uint(hash) % uint(len(udpQueues))
|
||||||
h.SetSeed(udpHashSeed)
|
|
||||||
h.WriteString(metadata.SourceAddress())
|
|
||||||
h.WriteString(metadata.RemoteAddress())
|
|
||||||
|
|
||||||
queueNo := uint(h.Sum64()) % uint(len(udpQueues))
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case udpQueues[queueNo] <- packetAdapter:
|
case udpQueues[queueNo] <- packetAdapter:
|
||||||
@ -255,8 +250,7 @@ func isHandle(t C.Type) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// processUDP starts a loop to handle udp packet
|
// processUDP starts a loop to handle udp packet
|
||||||
func processUDP(queueNo int) {
|
func processUDP(queue chan C.PacketAdapter) {
|
||||||
queue := udpQueues[queueNo]
|
|
||||||
for conn := range queue {
|
for conn := range queue {
|
||||||
handleUDPConn(conn)
|
handleUDPConn(conn)
|
||||||
}
|
}
|
||||||
@ -270,8 +264,9 @@ func process() {
|
|||||||
|
|
||||||
udpQueues = make([]chan C.PacketAdapter, numUDPWorkers)
|
udpQueues = make([]chan C.PacketAdapter, numUDPWorkers)
|
||||||
for i := 0; i < numUDPWorkers; i++ {
|
for i := 0; i < numUDPWorkers; i++ {
|
||||||
udpQueues[i] = make(chan C.PacketAdapter, 200)
|
queue := make(chan C.PacketAdapter, queueSize)
|
||||||
go processUDP(i)
|
udpQueues[i] = queue
|
||||||
|
go processUDP(queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
queue := tcpQueue
|
queue := tcpQueue
|
||||||
|
Loading…
Reference in New Issue
Block a user