github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/transport/internet/udp/monodest.go (about) 1 package udp 2 3 import ( 4 "github.com/v2fly/v2ray-core/v5/common/buf" 5 "github.com/v2fly/v2ray-core/v5/common/net" 6 "github.com/v2fly/v2ray-core/v5/transport/internet" 7 ) 8 9 func NewMonoDestUDPConn(conn internet.AbstractPacketConn, addr net.Addr) *MonoDestUDPConn { 10 return &MonoDestUDPConn{ 11 AbstractPacketConn: conn, 12 dest: addr, 13 } 14 } 15 16 type MonoDestUDPConn struct { 17 internet.AbstractPacketConn 18 dest net.Addr 19 } 20 21 func (m *MonoDestUDPConn) ReadMultiBuffer() (buf.MultiBuffer, error) { 22 buffer := buf.New() 23 buffer.Extend(2048) 24 nBytes, _, err := m.AbstractPacketConn.ReadFrom(buffer.Bytes()) 25 if err != nil { 26 buffer.Release() 27 return nil, err 28 } 29 buffer.Resize(0, int32(nBytes)) 30 return buf.MultiBuffer{buffer}, nil 31 } 32 33 func (m *MonoDestUDPConn) WriteMultiBuffer(buffer buf.MultiBuffer) error { 34 for _, b := range buffer { 35 _, err := m.AbstractPacketConn.WriteTo(b.Bytes(), m.dest) 36 if err != nil { 37 return err 38 } 39 } 40 buf.ReleaseMulti(buffer) 41 return nil 42 } 43 44 func (m *MonoDestUDPConn) Read(p []byte) (n int, err error) { 45 n, _, err = m.AbstractPacketConn.ReadFrom(p) 46 return 47 } 48 49 func (m *MonoDestUDPConn) Write(p []byte) (n int, err error) { 50 return m.AbstractPacketConn.WriteTo(p, m.dest) 51 }