Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion common/buf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
Size = 8192
)

var ErrBufferFull = errors.New("buffer is full")

var zero = [Size * 10]byte{0}

var pool = bytespool.GetPool(Size)
Expand Down Expand Up @@ -258,13 +260,16 @@ func (b *Buffer) IsFull() bool {
func (b *Buffer) Write(data []byte) (int, error) {
nBytes := copy(b.v[b.end:], data)
b.end += int32(nBytes)
if nBytes < len(data) {
return nBytes, ErrBufferFull
}
return nBytes, nil
}

// WriteByte writes a single byte into the buffer.
func (b *Buffer) WriteByte(v byte) error {
if b.IsFull() {
return errors.New("buffer full")
return ErrBufferFull
}
b.v[b.end] = v
b.end++
Expand Down
19 changes: 10 additions & 9 deletions transport/internet/splithttp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,16 @@ func (w uploadWriter) Write(b []byte) (int, error) {
}
*/

buffer := buf.New()
n, err := buffer.Write(b)
if err != nil {
return 0, err
}
buffer := buf.MultiBufferContainer{}
common.Must2(buffer.Write(b))

err = w.WriteMultiBuffer([]*buf.Buffer{buffer})
if err != nil {
return 0, err
var writed int
for _, buff := range buffer.MultiBuffer {
err := w.WriteMultiBuffer(buf.MultiBuffer{buff})
if err != nil {
return writed, err
}
writed += int(buff.Len())
}
return n, nil
return writed, nil
}
21 changes: 9 additions & 12 deletions transport/internet/splithttp/splithttp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package splithttp_test

import (
"bytes"
"context"
"crypto/rand"
"fmt"
Expand Down Expand Up @@ -421,18 +422,12 @@ func Test_maxUpload(t *testing.T) {
},
}

var uploadSize int
uploadReceived := make([]byte, 10001)
listen, err := ListenXH(context.Background(), net.LocalHostIP, listenPort, streamSettings, func(conn stat.Connection) {
go func(c stat.Connection) {
defer c.Close()
var b [10240]byte
c.SetReadDeadline(time.Now().Add(2 * time.Second))
n, err := c.Read(b[:])
if err != nil {
return
}

uploadSize = n
io.ReadFull(c, uploadReceived)

common.Must2(c.Write([]byte("Response")))
}(conn)
Expand All @@ -441,10 +436,12 @@ func Test_maxUpload(t *testing.T) {
ctx := context.Background()

conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), listenPort), streamSettings)
common.Must(err)

// send a slightly too large upload
var upload [10001]byte
_, err = conn.Write(upload[:])
upload := make([]byte, 10001)
rand.Read(upload)
_, err = conn.Write(upload)
common.Must(err)

var b [10240]byte
Expand All @@ -455,8 +452,8 @@ func Test_maxUpload(t *testing.T) {
}
common.Must(conn.Close())

if uploadSize > 10000 || uploadSize == 0 {
t.Error("incorrect upload size: ", uploadSize)
if !bytes.Equal(upload, uploadReceived) {
t.Error("incorrect upload", upload, uploadReceived)
}

common.Must(listen.Close())
Expand Down
Loading