mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 12:44:02 +03:00
Add pools for packetack
This commit is contained in:
@@ -88,7 +88,9 @@ func (w *wrapperClientAbridged) Write(packet conntypes.Packet, acks *conntypes.C
|
||||
return nil
|
||||
case packetLength < clientAbridgedLargePacketLength:
|
||||
length24 := utils.ToUint24(uint32(packetLength))
|
||||
buf := bytes.Buffer{}
|
||||
|
||||
buf := acquireClientBytesBuffer()
|
||||
defer releaseClientBytesBuffer(buf)
|
||||
|
||||
buf.WriteByte(byte(clientAbridgedSmallPacketLength))
|
||||
buf.Write(length24[:])
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package packetack
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
@@ -35,11 +34,13 @@ func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *c
|
||||
return nil
|
||||
}
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
buf := acquireClientBytesBuffer()
|
||||
defer releaseClientBytesBuffer(buf)
|
||||
|
||||
paddingLength := rand.Intn(4)
|
||||
buf.Grow(4 + len(packet) + paddingLength)
|
||||
|
||||
binary.Write(&buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
|
||||
binary.Write(buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
|
||||
buf.Write(packet)
|
||||
buf.Write(make([]byte, paddingLength))
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package packetack
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
poolClientBytesBuffer = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
poolProxyBytesBuffer = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func acquireClientBytesBuffer() *bytes.Buffer {
|
||||
return poolClientBytesBuffer.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func acquireProxyBytesBuffer() *bytes.Buffer {
|
||||
return poolProxyBytesBuffer.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func releaseClientBytesBuffer(buf *bytes.Buffer) {
|
||||
buf.Reset()
|
||||
poolClientBytesBuffer.Put(buf)
|
||||
}
|
||||
|
||||
func releaseProxyBytesBuffer(buf *bytes.Buffer) {
|
||||
buf.Reset()
|
||||
poolProxyBytesBuffer.Put(buf)
|
||||
}
|
||||
@@ -22,7 +22,8 @@ type wrapperProxy struct {
|
||||
}
|
||||
|
||||
func (w *wrapperProxy) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
|
||||
buf := bytes.Buffer{}
|
||||
buf := acquireProxyBytesBuffer()
|
||||
defer releaseProxyBytesBuffer(buf)
|
||||
|
||||
flags := w.flags
|
||||
if acks.Quick {
|
||||
|
||||
Reference in New Issue
Block a user