1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 10:26:01 -05:00

write by slice when buffer is too large

This commit is contained in:
Darien Raymond 2017-04-19 10:14:52 +02:00
parent 5bfab82c41
commit 31a0951bd2
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 32 additions and 5 deletions

View File

@ -28,7 +28,7 @@ func (r *Reader) ReadMetadata() (*FrameMetadata, error) {
} }
metaLen := serial.BytesToUint16(b.Bytes()) metaLen := serial.BytesToUint16(b.Bytes())
if metaLen > 512 { if metaLen > 512 {
return nil, newError("invalid metalen ", metaLen) return nil, newError("invalid metalen ", metaLen).AtWarning()
} }
b.Clear() b.Clear()
if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(metaLen))); err != nil { if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(metaLen))); err != nil {

View File

@ -18,6 +18,7 @@ func NewWriter(id uint16, dest net.Destination, writer buf.Writer) *Writer {
id: id, id: id,
dest: dest, dest: dest,
writer: writer, writer: writer,
followup: false,
} }
} }
@ -29,7 +30,7 @@ func NewResponseWriter(id uint16, writer buf.Writer) *Writer {
} }
} }
func (w *Writer) Write(mb buf.MultiBuffer) error { func (w *Writer) writeInternal(mb buf.MultiBuffer) error {
meta := FrameMetadata{ meta := FrameMetadata{
SessionID: w.id, SessionID: w.id,
Target: w.dest, Target: w.dest,
@ -58,6 +59,17 @@ func (w *Writer) Write(mb buf.MultiBuffer) error {
return w.writer.Write(mb2) return w.writer.Write(mb2)
} }
func (w *Writer) Write(mb buf.MultiBuffer) error {
const chunkSize = 8 * 1024
for !mb.IsEmpty() {
slice := mb.SliceBySize(chunkSize)
if err := w.writeInternal(slice); err != nil {
return err
}
}
return nil
}
func (w *Writer) Close() { func (w *Writer) Close() {
meta := FrameMetadata{ meta := FrameMetadata{
SessionID: w.id, SessionID: w.id,

View File

@ -86,3 +86,18 @@ func (mb MultiBuffer) ToNetBuffers() net.Buffers {
} }
return bs return bs
} }
func (mb *MultiBuffer) SliceBySize(size int) MultiBuffer {
slice := NewMultiBuffer()
sliceSize := 0
endIndex := len(*mb)
for i, b := range *mb {
if b.Len()+sliceSize > size {
endIndex = i
break
}
slice.Append(b)
}
*mb = (*mb)[endIndex:]
return slice
}