1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

optimize ota writer

This commit is contained in:
Darien Raymond 2017-04-15 21:49:18 +02:00
parent b1dfff2fdd
commit 47ba054ad5
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -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
}