Use Buffer as a writer

pull/40/head
V2Ray 2015-10-10 20:52:13 +02:00
parent 32ab3dcd61
commit a77f62428a
2 changed files with 13 additions and 4 deletions

View File

@ -50,6 +50,11 @@ func (b *Buffer) IsFull() bool {
return len(b.Value) == cap(b.Value)
}
func (b *Buffer) Write(data []byte) (int, error) {
b.Append(data)
return len(data), nil
}
type bufferPool struct {
chain chan []byte
bufferSize int
@ -98,7 +103,10 @@ func (p *bufferPool) cleanup(tick <-chan time.Time) {
continue
}
for delta := p.buffers2Keep - pSize; delta > 0; delta-- {
p.chain <- make([]byte, p.bufferSize)
select {
case p.chain <- make([]byte, p.bufferSize):
default:
}
}
}
}

View File

@ -77,7 +77,8 @@ func (handler *VMessInboundHandler) handlePacket(conn *net.UDPConn, request *pro
responseKey := md5.Sum(request.RequestKey)
responseIV := md5.Sum(request.RequestIV)
buffer := bytes.NewBuffer(make([]byte, 0, bufferSize))
buffer := alloc.NewBuffer().Clear()
defer buffer.Release()
responseWriter, err := v2io.NewAesEncryptWriter(responseKey[:], responseIV[:], buffer)
if err != nil {
@ -95,7 +96,7 @@ func (handler *VMessInboundHandler) handlePacket(conn *net.UDPConn, request *pro
}
if hasData {
conn.WriteToUDP(buffer.Bytes(), clientAddr)
log.Info("VMessIn sending %d bytes to %s", len(buffer.Bytes()), clientAddr.String())
conn.WriteToUDP(buffer.Value, clientAddr)
log.Info("VMessIn sending %d bytes to %s", buffer.Len(), clientAddr.String())
}
}