1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-11-04 14:27:12 -05:00
Files
v2fly/app/tun/device/linkWriterToWriter.go

36 lines
819 B
Go
Raw Normal View History

package device
import (
2023-10-23 01:44:36 +01:00
"io"
"github.com/v2fly/v2ray-core/v5/common/errors"
2023-10-23 01:44:36 +01:00
"gvisor.dev/gvisor/pkg/buffer"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
func NewLinkWriterToWriter(writer stack.LinkWriter) io.Writer {
return &linkWriterToWriter{writer: writer}
}
type linkWriterToWriter struct {
writer stack.LinkWriter
}
func (l linkWriterToWriter) Write(p []byte) (n int, err error) {
buffer := buffer.MakeWithData(p)
packetBufferPtr := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: buffer,
OnRelease: func() {
buffer.Release()
},
})
packetList := stack.PacketBufferList{}
packetList.PushBack(packetBufferPtr)
_, terr := l.writer.WritePackets(packetList)
if terr != nil {
return 0, newError("failed to write packet").Base(errors.New(terr.String())).AtError()
}
return len(p), nil
}