mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-05 04:47:40 +03:00
Add vectorised interface
This commit is contained in:
parent
13f394e202
commit
05f9836bff
12 changed files with 371 additions and 13 deletions
54
protocol/socks/packet_vectorised.go
Normal file
54
protocol/socks/packet_vectorised.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package socks
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
var _ N.VectorisedPacketWriter = (*VectorisedAssociatePacketConn)(nil)
|
||||
|
||||
type VectorisedAssociatePacketConn struct {
|
||||
AssociatePacketConn
|
||||
N.VectorisedPacketWriter
|
||||
}
|
||||
|
||||
func NewVectorisedAssociatePacketConn(conn net.PacketConn, writer N.VectorisedPacketWriter, remoteAddr M.Socksaddr, underlying net.Conn) *VectorisedAssociatePacketConn {
|
||||
return &VectorisedAssociatePacketConn{
|
||||
AssociatePacketConn{
|
||||
NetPacketConn: bufio.NewPacketConn(conn),
|
||||
remoteAddr: remoteAddr,
|
||||
underlying: underlying,
|
||||
},
|
||||
writer,
|
||||
}
|
||||
}
|
||||
|
||||
func NewVectorisedAssociateConn(conn net.Conn, writer N.VectorisedWriter, remoteAddr M.Socksaddr, underlying net.Conn) *VectorisedAssociatePacketConn {
|
||||
return &VectorisedAssociatePacketConn{
|
||||
AssociatePacketConn{
|
||||
NetPacketConn: bufio.NewUnbindPacketConn(conn),
|
||||
remoteAddr: remoteAddr,
|
||||
underlying: underlying,
|
||||
},
|
||||
&bufio.UnbindVectorisedPacketWriter{VectorisedWriter: writer},
|
||||
}
|
||||
}
|
||||
|
||||
func (v *VectorisedAssociatePacketConn) WriteVectorisedPacket(buffers []*buf.Buffer, destination M.Socksaddr) error {
|
||||
_header := buf.StackNewSize(3 + M.SocksaddrSerializer.AddrPortLen(destination))
|
||||
defer common.KeepAlive(_header)
|
||||
header := common.Dup(_header)
|
||||
defer header.Release()
|
||||
common.Must(header.WriteZeroN(3))
|
||||
common.Must(M.SocksaddrSerializer.WriteAddrPort(header, destination))
|
||||
return v.VectorisedPacketWriter.WriteVectorisedPacket(append([]*buf.Buffer{header}, buffers...), destination)
|
||||
}
|
||||
|
||||
func (c *VectorisedAssociatePacketConn) FrontHeadroom() int {
|
||||
return 0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue