Skip to content

Commit

Permalink
move udp packet to protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 5, 2019
1 parent 06f9897 commit 21f8bfe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
13 changes: 13 additions & 0 deletions common/protocol/udp/packet.go
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions common/protocol/udp/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package udp
2 changes: 1 addition & 1 deletion transport/internet/kcp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
26 changes: 10 additions & 16 deletions transport/internet/udp/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -123,7 +117,7 @@ func (h *Hub) start() {
case c <- payload:
default:
buffer.Release()
payload.Content = nil
payload.Payload = nil
}

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

0 comments on commit 21f8bfe

Please sign in to comment.