mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-02 00:11:56 +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
|
return nil
|
||||||
case packetLength < clientAbridgedLargePacketLength:
|
case packetLength < clientAbridgedLargePacketLength:
|
||||||
length24 := utils.ToUint24(uint32(packetLength))
|
length24 := utils.ToUint24(uint32(packetLength))
|
||||||
buf := bytes.Buffer{}
|
|
||||||
|
buf := acquireClientBytesBuffer()
|
||||||
|
defer releaseClientBytesBuffer(buf)
|
||||||
|
|
||||||
buf.WriteByte(byte(clientAbridgedSmallPacketLength))
|
buf.WriteByte(byte(clientAbridgedSmallPacketLength))
|
||||||
buf.Write(length24[:])
|
buf.Write(length24[:])
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package packetack
|
package packetack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@@ -35,11 +34,13 @@ func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *c
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.Buffer{}
|
buf := acquireClientBytesBuffer()
|
||||||
|
defer releaseClientBytesBuffer(buf)
|
||||||
|
|
||||||
paddingLength := rand.Intn(4)
|
paddingLength := rand.Intn(4)
|
||||||
buf.Grow(4 + len(packet) + paddingLength)
|
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(packet)
|
||||||
buf.Write(make([]byte, paddingLength))
|
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 {
|
func (w *wrapperProxy) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
|
||||||
buf := bytes.Buffer{}
|
buf := acquireProxyBytesBuffer()
|
||||||
|
defer releaseProxyBytesBuffer(buf)
|
||||||
|
|
||||||
flags := w.flags
|
flags := w.flags
|
||||||
if acks.Quick {
|
if acks.Quick {
|
||||||
|
|||||||
Reference in New Issue
Block a user