1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-04-07 14:25:26 -04:00

Save 1 allocation in shadowsocks

This commit is contained in:
v2ray
2016-01-31 21:37:00 +01:00
parent 97a85f04ce
commit 6c7a9586d0
2 changed files with 22 additions and 19 deletions

View File

@@ -187,20 +187,18 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
var writeFinish sync.Mutex
writeFinish.Lock()
go func() {
firstChunk := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize())
defer firstChunk.Release()
writer, err := this.config.Cipher.NewEncodingStream(key, firstChunk.Value, conn)
if err != nil {
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
return
}
if payload, ok := <-ray.InboundOutput(); ok {
firstChunk.Append(payload.Value)
payload.Release()
payload.SliceBack(16)
rand.Read(payload.Value[:16])
writer.Write(firstChunk.Value)
writer, err := this.config.Cipher.NewEncodingStream(key, payload.Value[:16], conn)
if err != nil {
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
return
}
writer.Write(payload.Value)
payload.Release()
v2io.ChanToWriter(writer, ray.InboundOutput())
}
writeFinish.Unlock()