github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/protocol/socks/packet_vectorised.go (about)

     1  package socks
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/sagernet/sing/common"
     7  	"github.com/sagernet/sing/common/buf"
     8  	"github.com/sagernet/sing/common/bufio"
     9  	M "github.com/sagernet/sing/common/metadata"
    10  	N "github.com/sagernet/sing/common/network"
    11  )
    12  
    13  var _ N.VectorisedPacketWriter = (*VectorisedAssociatePacketConn)(nil)
    14  
    15  type VectorisedAssociatePacketConn struct {
    16  	AssociatePacketConn
    17  	N.VectorisedPacketWriter
    18  }
    19  
    20  func NewVectorisedAssociateConn(conn net.Conn, writer N.VectorisedWriter, remoteAddr M.Socksaddr, underlying net.Conn) *VectorisedAssociatePacketConn {
    21  	return &VectorisedAssociatePacketConn{
    22  		AssociatePacketConn{
    23  			AbstractConn: conn,
    24  			conn:         bufio.NewExtendedConn(conn),
    25  			remoteAddr:   remoteAddr,
    26  			underlying:   underlying,
    27  		},
    28  		&bufio.UnbindVectorisedPacketWriter{VectorisedWriter: writer},
    29  	}
    30  }
    31  
    32  func (v *VectorisedAssociatePacketConn) WriteVectorisedPacket(buffers []*buf.Buffer, destination M.Socksaddr) error {
    33  	header := buf.NewSize(3 + M.SocksaddrSerializer.AddrPortLen(destination))
    34  	defer header.Release()
    35  	common.Must(header.WriteZeroN(3))
    36  	err := M.SocksaddrSerializer.WriteAddrPort(header, destination)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	return v.VectorisedPacketWriter.WriteVectorisedPacket(append([]*buf.Buffer{header}, buffers...), destination)
    41  }
    42  
    43  func (c *VectorisedAssociatePacketConn) FrontHeadroom() int {
    44  	return 0
    45  }