1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 07:26:24 -05:00

remove buffer.WriteBytes

This commit is contained in:
Darien Raymond 2018-11-14 22:55:20 +01:00
parent ff7e5a7cdb
commit 6c7dcc35ab
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
12 changed files with 89 additions and 32 deletions

View File

@ -139,11 +139,6 @@ func (b *Buffer) Write(data []byte) (int, error) {
return nBytes, nil
}
// WriteBytes appends one or more bytes to the end of the buffer.
func (b *Buffer) WriteBytes(bytes ...byte) (int, error) {
return b.Write(bytes)
}
// WriteByte writes a single byte into the buffer.
func (b *Buffer) WriteByte(v byte) error {
if b.IsFull() {

View File

@ -96,3 +96,63 @@ func BenchmarkNewBuffer(b *testing.B) {
buffer.Release()
}
}
func BenchmarkWrite2(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = buffer.Write([]byte{'a', 'b'})
buffer.Clear()
}
}
func BenchmarkWrite8(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = buffer.Write([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'})
buffer.Clear()
}
}
func BenchmarkWrite32(b *testing.B) {
buffer := New()
payload := make([]byte, 32)
rand.Read(payload)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = buffer.Write(payload)
buffer.Clear()
}
}
func BenchmarkWriteByte2(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = buffer.WriteByte('a')
_ = buffer.WriteByte('b')
buffer.Clear()
}
}
func BenchmarkWriteByte8(b *testing.B) {
buffer := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = buffer.WriteByte('a')
_ = buffer.WriteByte('b')
_ = buffer.WriteByte('c')
_ = buffer.WriteByte('d')
_ = buffer.WriteByte('e')
_ = buffer.WriteByte('f')
_ = buffer.WriteByte('g')
_ = buffer.WriteByte('h')
buffer.Clear()
}
}

View File

@ -14,10 +14,10 @@ func TestMultiBufferRead(t *testing.T) {
assert := With(t)
b1 := New()
b1.WriteBytes('a', 'b')
b1.WriteString("ab")
b2 := New()
b2.WriteBytes('c', 'd')
b2.WriteString("cd")
mb := NewMultiBufferValue(b1, b2)
bs := make([]byte, 32)
@ -32,7 +32,7 @@ func TestMultiBufferAppend(t *testing.T) {
var mb MultiBuffer
b := New()
b.WriteBytes('a', 'b')
b.WriteString("ab")
mb.Append(b)
assert(mb.Len(), Equals, int32(2))
}

View File

@ -17,9 +17,9 @@ func TestBytesReaderWriteTo(t *testing.T) {
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
reader := &BufferedReader{Reader: pReader}
b1 := New()
b1.WriteBytes('a', 'b', 'c')
b1.WriteString("abc")
b2 := New()
b2.WriteBytes('e', 'f', 'g')
b2.WriteString("efg")
assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
pWriter.Close()
@ -44,9 +44,9 @@ func TestBytesReaderMultiBuffer(t *testing.T) {
pReader, pWriter := pipe.New(pipe.WithSizeLimit(1024))
reader := &BufferedReader{Reader: pReader}
b1 := New()
b1.WriteBytes('a', 'b', 'c')
b1.WriteString("abc")
b2 := New()
b2.WriteBytes('e', 'f', 'g')
b2.WriteString("efg")
assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil)
pWriter.Close()

View File

@ -20,11 +20,11 @@ func TestChunkStreamIO(t *testing.T) {
reader := NewChunkStreamReader(PlainChunkSizeParser{}, cache)
b := buf.New()
b.WriteBytes('a', 'b', 'c', 'd')
b.WriteString("abcd")
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
b = buf.New()
b.WriteBytes('e', 'f', 'g')
b.WriteString("efg")
common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)))
common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{}))

View File

@ -27,7 +27,7 @@ func (*NoneResponse) WriteTo(buf.Writer) int32 { return 0 }
// WriteTo implements ResponseConfig.WriteTo().
func (*HTTPResponse) WriteTo(writer buf.Writer) int32 {
b := buf.New()
common.Must2(b.Write([]byte(http403response)))
common.Must2(b.WriteString(http403response))
n := b.Len()
writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))
return n

View File

@ -12,8 +12,7 @@ func TestNormalChunkReading(t *testing.T) {
assert := With(t)
buffer := buf.New()
buffer.WriteBytes(
0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18)
buffer.Write([]byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18})
reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator(
[]byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36})))
payload, err := reader.ReadMultiBuffer()

View File

@ -255,7 +255,7 @@ func writeSocks5Response(writer io.Writer, errCode byte, address net.Address, po
buffer := buf.New()
defer buffer.Release()
common.Must2(buffer.WriteBytes(socks5Version, errCode, 0x00 /* reserved */))
common.Must2(buffer.Write([]byte{socks5Version, errCode, 0x00 /* reserved */}))
if err := addrParser.WriteAddressPort(buffer, address, port); err != nil {
return err
}
@ -267,7 +267,8 @@ func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, po
buffer := buf.New()
defer buffer.Release()
common.Must2(buffer.WriteBytes(0x00, errCode))
common.Must(buffer.WriteByte(0x00))
common.Must(buffer.WriteByte(errCode))
common.Must2(serial.WriteUint16(buffer, port.Value()))
common.Must2(buffer.Write(address.IP()))
return buf.WriteAllBytes(writer, buffer.Bytes())
@ -300,7 +301,7 @@ func DecodeUDPPacket(packet *buf.Buffer) (*protocol.RequestHeader, error) {
func EncodeUDPPacket(request *protocol.RequestHeader, data []byte) (*buf.Buffer, error) {
b := buf.New()
common.Must2(b.WriteBytes(0, 0, 0 /* Fragment */))
common.Must2(b.Write([]byte{0, 0, 0 /* Fragment */}))
if err := addrParser.WriteAddressPort(b, request.Address, request.Port); err != nil {
b.Release()
return nil, err
@ -362,14 +363,15 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
b := buf.New()
defer b.Release()
common.Must2(b.WriteBytes(socks5Version, 0x01, authByte))
common.Must2(b.Write([]byte{socks5Version, 0x01, authByte}))
if authByte == authPassword {
account := request.User.Account.(*Account)
common.Must2(b.WriteBytes(0x01, byte(len(account.Username))))
common.Must2(b.Write([]byte(account.Username)))
common.Must2(b.WriteBytes(byte(len(account.Password))))
common.Must2(b.Write([]byte(account.Password)))
common.Must(b.WriteByte(0x01))
common.Must(b.WriteByte(byte(len(account.Username))))
common.Must2(b.WriteString(account.Username))
common.Must(b.WriteByte(byte(len(account.Password))))
common.Must2(b.WriteString(account.Password))
}
if err := buf.WriteAllBytes(writer, b.Bytes()); err != nil {
@ -404,7 +406,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
if request.Command == protocol.RequestCommandUDP {
command = byte(cmdUDPPort)
}
common.Must2(b.WriteBytes(socks5Version, command, 0x00 /* reserved */))
common.Must2(b.Write([]byte{socks5Version, command, 0x00 /* reserved */}))
if err := addrParser.WriteAddressPort(b, request.Address, request.Port); err != nil {
return nil, err
}

View File

@ -65,14 +65,15 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
buffer := buf.New()
defer buffer.Release()
common.Must2(buffer.WriteBytes(Version))
common.Must(buffer.WriteByte(Version))
common.Must2(buffer.Write(c.requestBodyIV[:]))
common.Must2(buffer.Write(c.requestBodyKey[:]))
common.Must2(buffer.WriteBytes(c.responseHeader, byte(header.Option)))
common.Must(buffer.WriteByte(c.responseHeader))
common.Must(buffer.WriteByte(byte(header.Option)))
padingLen := dice.Roll(16)
security := byte(padingLen<<4) | byte(header.Security)
common.Must2(buffer.WriteBytes(security, byte(0), byte(header.Command)))
common.Must2(buffer.Write([]byte{security, byte(0), byte(header.Command)}))
if header.Command != protocol.RequestCommandMux {
if err := addrParser.WriteAddressPort(buffer, header.Address, header.Port); err != nil {

View File

@ -50,7 +50,7 @@ func Test1ByteDataSegment(t *testing.T) {
Number: 4,
SendingNext: 5,
}
seg.Data().WriteBytes('a')
seg.Data().WriteByte('a')
nBytes := seg.ByteSize()
bytes := make([]byte, nBytes)

View File

@ -63,7 +63,7 @@ func TestSameDestinationDispatching(t *testing.T) {
dest := net.UDPDestination(net.LocalHostIP, 53)
b := buf.New()
b.WriteBytes('a', 'b', 'c', 'd')
b.WriteString("abcd")
var msgCount uint32
dispatcher := NewDispatcher(td, func(ctx context.Context, payload *buf.Buffer) {

View File

@ -103,7 +103,7 @@ func TestPipeWriteMultiThread(t *testing.T) {
wg.Add(1)
go func() {
b := buf.New()
b.WriteBytes('a', 'b', 'c', 'd')
b.WriteString("abcd")
pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b))
wg.Done()
}()