Browse Source

move udp packet to protocol

pull/1869/head^2
Darien Raymond 6 years ago
parent
commit
21f8bfe476
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 13
      common/protocol/udp/packet.go
  2. 1
      common/protocol/udp/udp.go
  3. 2
      transport/internet/kcp/listener.go
  4. 26
      transport/internet/udp/hub.go

13
common/protocol/udp/packet.go

@ -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
}

1
common/protocol/udp/udp.go

@ -0,0 +1 @@
package udp

2
transport/internet/kcp/listener.go

@ -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)
}
}

26
transport/internet/udp/hub.go

@ -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
}

Loading…
Cancel
Save