1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-21 16:56:27 -05:00

safe release buffer

This commit is contained in:
Darien Raymond 2017-11-07 11:58:43 +01:00
parent ccf1fb6616
commit c1eccb3a45
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -82,23 +82,21 @@ func (w *Writer) writeData(mb buf.MultiBuffer) error {
// Write implements buf.MultiBufferWriter.
func (w *Writer) Write(mb buf.MultiBuffer) error {
defer mb.Release()
if mb.IsEmpty() {
return w.writeMetaOnly()
}
if w.transferType == protocol.TransferTypeStream {
const chunkSize = 8 * 1024
for !mb.IsEmpty() {
slice := mb.SliceBySize(chunkSize)
if err := w.writeData(slice); err != nil {
return err
}
for !mb.IsEmpty() {
var chunk buf.MultiBuffer
if w.transferType == protocol.TransferTypeStream {
chunk = mb.SliceBySize(8 * 1024)
} else {
chunk = buf.NewMultiBufferValue(mb.SplitFirst())
}
} else {
for _, b := range mb {
if err := w.writeData(buf.NewMultiBufferValue(b)); err != nil {
return err
}
if err := w.writeData(chunk); err != nil {
return err
}
}