github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/bufio/io.go (about) 1 package bufio 2 3 import ( 4 "io" 5 "net" 6 7 "github.com/sagernet/sing/common" 8 "github.com/sagernet/sing/common/buf" 9 M "github.com/sagernet/sing/common/metadata" 10 N "github.com/sagernet/sing/common/network" 11 ) 12 13 // Deprecated: bad usage 14 func ReadBuffer(reader N.ExtendedReader, buffer *buf.Buffer) (n int, err error) { 15 n, err = reader.Read(buffer.FreeBytes()) 16 buffer.Truncate(n) 17 return 18 } 19 20 // Deprecated: bad usage 21 func ReadPacket(reader N.PacketReader, buffer *buf.Buffer) (n int, addr net.Addr, err error) { 22 startLen := buffer.Len() 23 addr, err = reader.ReadPacket(buffer) 24 n = buffer.Len() - startLen 25 return 26 } 27 28 func Write(writer io.Writer, data []byte) (n int, err error) { 29 if extendedWriter, isExtended := writer.(N.ExtendedWriter); isExtended { 30 return WriteBuffer(extendedWriter, buf.As(data)) 31 } else { 32 return writer.Write(data) 33 } 34 } 35 36 func WriteBuffer(writer N.ExtendedWriter, buffer *buf.Buffer) (n int, err error) { 37 frontHeadroom := N.CalculateFrontHeadroom(writer) 38 rearHeadroom := N.CalculateRearHeadroom(writer) 39 if frontHeadroom > buffer.Start() || rearHeadroom > buffer.FreeLen() { 40 newBuffer := buf.NewSize(buffer.Len() + frontHeadroom + rearHeadroom) 41 newBuffer.Resize(frontHeadroom, 0) 42 common.Must1(newBuffer.Write(buffer.Bytes())) 43 buffer.Release() 44 buffer = newBuffer 45 } 46 dataLen := buffer.Len() 47 err = writer.WriteBuffer(buffer) 48 if err == nil { 49 n = dataLen 50 } 51 return 52 } 53 54 func WritePacket(writer N.NetPacketWriter, data []byte, addr net.Addr) (n int, err error) { 55 if extendedWriter, isExtended := writer.(N.PacketWriter); isExtended { 56 return WritePacketBuffer(extendedWriter, buf.As(data), M.SocksaddrFromNet(addr)) 57 } else { 58 return writer.WriteTo(data, addr) 59 } 60 } 61 62 func WritePacketBuffer(writer N.PacketWriter, buffer *buf.Buffer, destination M.Socksaddr) (n int, err error) { 63 frontHeadroom := N.CalculateFrontHeadroom(writer) 64 rearHeadroom := N.CalculateRearHeadroom(writer) 65 if frontHeadroom > buffer.Start() || rearHeadroom > buffer.FreeLen() { 66 newBuffer := buf.NewSize(buffer.Len() + frontHeadroom + rearHeadroom) 67 newBuffer.Resize(frontHeadroom, 0) 68 common.Must1(newBuffer.Write(buffer.Bytes())) 69 buffer.Release() 70 buffer = newBuffer 71 } 72 dataLen := buffer.Len() 73 err = writer.WritePacket(buffer, destination) 74 if err == nil { 75 n = dataLen 76 } 77 return 78 } 79 80 func WriteVectorised(writer N.VectorisedWriter, data [][]byte) (n int, err error) { 81 var dataLen int 82 buffers := make([]*buf.Buffer, 0, len(data)) 83 for _, p := range data { 84 dataLen += len(p) 85 buffers = append(buffers, buf.As(p)) 86 } 87 err = writer.WriteVectorised(buffers) 88 if err == nil { 89 n = dataLen 90 } 91 return 92 } 93 94 func WriteVectorisedPacket(writer N.VectorisedPacketWriter, data [][]byte, destination M.Socksaddr) (n int, err error) { 95 var dataLen int 96 buffers := make([]*buf.Buffer, 0, len(data)) 97 for _, p := range data { 98 dataLen += len(p) 99 buffers = append(buffers, buf.As(p)) 100 } 101 err = writer.WriteVectorisedPacket(buffers, destination) 102 if err == nil { 103 n = dataLen 104 } 105 return 106 }