1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-10 04:56:15 -04:00

change ToPacketAddrConnWrapper to fused connection

This commit is contained in:
Shelikhoo 2021-12-27 15:54:41 +00:00 committed by Xiaokang Wang (Shelikhoo)
parent 409c4e9c98
commit 5b536a7d02

View File

@ -2,7 +2,6 @@ package packetaddr
import (
"context"
"io"
gonet "net"
"sync"
"time"
@ -107,18 +106,27 @@ func (c packetConnectionAdaptor) SetWriteDeadline(t time.Time) error {
return nil
}
func ToPacketAddrReadWriteCloser(conn net.PacketConn, isStream bool) io.ReadWriteCloser {
func ToPacketAddrConnWrapper(conn net.PacketConn, isStream bool) FusedConnection {
return &packetConnWrapper{conn}
}
type packetConnWrapper struct {
conn net.PacketConn
net.PacketConn
}
func (pc *packetConnWrapper) RemoteAddr() gonet.Addr {
return nil
}
type FusedConnection interface {
net.PacketConn
net.Conn
}
func (pc *packetConnWrapper) Read(p []byte) (n int, err error) {
recbuf := buf.StackNew()
recbuf.Extend(2048)
n, addr, err := pc.conn.ReadFrom(recbuf.Bytes())
n, addr, err := pc.PacketConn.ReadFrom(recbuf.Bytes())
if err != nil {
return 0, nil
}
@ -129,7 +137,7 @@ func (pc *packetConnWrapper) Read(p []byte) (n int, err error) {
func (pc *packetConnWrapper) Write(p []byte) (n int, err error) {
data, addr := ExtractAddressFromPacket(p)
_, err = pc.conn.WriteTo(data, addr)
_, err = pc.PacketConn.WriteTo(data, addr)
if err != nil {
return 0, err
}