From 47ba054ad5d25e08097260766c55d94f229ce3c8 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 15 Apr 2017 21:49:18 +0200 Subject: [PATCH] optimize ota writer --- proxy/shadowsocks/ota.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index 3f2238638..0c8fbd26f 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -123,19 +123,19 @@ func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter { func (w *ChunkWriter) Write(mb buf.MultiBuffer) error { defer mb.Release() - for _, b := range mb { - if err := w.writeInternal(b); err != nil { + for { + payloadLen, err := mb.Read(w.buffer[2+AuthSize:]) + if err != nil { return err } + serial.Uint16ToBytes(uint16(payloadLen), w.buffer[:0]) + w.auth.Authenticate(w.buffer[2+AuthSize : 2+AuthSize+payloadLen])(w.buffer[2:]) + if _, err := w.writer.Write(w.buffer[:2+AuthSize+payloadLen]); err != nil { + return err + } + if mb.IsEmpty() { + break + } } return nil } - -func (w *ChunkWriter) writeInternal(payload *buf.Buffer) error { - totalLength := payload.Len() - serial.Uint16ToBytes(uint16(totalLength), w.buffer[:0]) - w.auth.Authenticate(payload.Bytes())(w.buffer[2:]) - copy(w.buffer[2+AuthSize:], payload.Bytes()) - _, err := w.writer.Write(w.buffer[:2+AuthSize+payload.Len()]) - return err -}