1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-16 04:35:24 +00:00

move udp packet to protocol

This commit is contained in:
Darien Raymond 2019-01-05 19:49:21 +01:00
parent 06f989717b
commit 21f8bfe476
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 25 additions and 17 deletions

View File

@ -0,0 +1,13 @@
package udp
import (
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
)
// Packet is a UDP packet together with its source and destination address.
type Packet struct {
Payload *buf.Buffer
Source net.Destination
Target net.Destination
}

View File

@ -0,0 +1 @@
package udp

View File

@ -76,7 +76,7 @@ func NewListener(ctx context.Context, address net.Address, port net.Port, stream
func (l *Listener) handlePackets() {
receive := l.hub.Receive()
for payload := range receive {
l.OnReceive(payload.Content, payload.Source)
l.OnReceive(payload.Payload, payload.Source)
}
}

View File

@ -5,16 +5,10 @@ import (
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol/udp"
"v2ray.com/core/transport/internet"
)
// Payload represents a single UDP payload.
type Payload struct {
Content *buf.Buffer
Source net.Destination
OriginalDestination net.Destination
}
type HubOption func(h *Hub)
func HubCapacity(capacity int) HubOption {
@ -31,7 +25,7 @@ func HubReceiveOriginalDestination(r bool) HubOption {
type Hub struct {
conn *net.UDPConn
cache chan *Payload
cache chan *udp.Packet
capacity int
recvOrigDest bool
}
@ -62,7 +56,7 @@ func ListenUDP(ctx context.Context, address net.Address, port net.Port, streamSe
}
newError("listening UDP on ", address, ":", port).WriteToLog()
hub.conn = udpConn.(*net.UDPConn)
hub.cache = make(chan *Payload, hub.capacity)
hub.cache = make(chan *udp.Packet, hub.capacity)
go hub.start()
return hub, nil
@ -106,14 +100,14 @@ func (h *Hub) start() {
continue
}
payload := &Payload{
Content: buffer,
payload := &udp.Packet{
Payload: buffer,
Source: net.UDPDestination(net.IPAddress(addr.IP), net.Port(addr.Port)),
}
if h.recvOrigDest && noob > 0 {
payload.OriginalDestination = RetrieveOriginalDest(oobBytes[:noob])
if payload.OriginalDestination.IsValid() {
newError("UDP original destination: ", payload.OriginalDestination).AtDebug().WriteToLog()
payload.Target = RetrieveOriginalDest(oobBytes[:noob])
if payload.Target.IsValid() {
newError("UDP original destination: ", payload.Target).AtDebug().WriteToLog()
} else {
newError("failed to read UDP original destination").WriteToLog()
}
@ -123,7 +117,7 @@ func (h *Hub) start() {
case c <- payload:
default:
buffer.Release()
payload.Content = nil
payload.Payload = nil
}
}
@ -134,6 +128,6 @@ func (h *Hub) Addr() net.Addr {
return h.conn.LocalAddr()
}
func (h *Hub) Receive() <-chan *Payload {
func (h *Hub) Receive() <-chan *udp.Packet {
return h.cache
}