mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 01:24:02 +03:00
Cleanup pools from tlstypes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package tlstypes
|
package tlstypes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/utils"
|
"github.com/9seconds/mtg/utils"
|
||||||
@@ -15,8 +16,7 @@ type Handshake struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handshake) WriteBytes(writer io.Writer) {
|
func (h *Handshake) WriteBytes(writer io.Writer) {
|
||||||
packetBuf := acquireBytesBuffer()
|
packetBuf := bytes.Buffer{}
|
||||||
defer releaseBytesBuffer(packetBuf)
|
|
||||||
|
|
||||||
writer.Write([]byte{byte(h.Type)}) // nolint: errcheck
|
writer.Write([]byte{byte(h.Type)}) // nolint: errcheck
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ func (h *Handshake) WriteBytes(writer io.Writer) {
|
|||||||
packetBuf.Write(h.Random[:])
|
packetBuf.Write(h.Random[:])
|
||||||
packetBuf.WriteByte(byte(len(h.SessionID)))
|
packetBuf.WriteByte(byte(len(h.SessionID)))
|
||||||
packetBuf.Write(h.SessionID)
|
packetBuf.Write(h.SessionID)
|
||||||
h.Tail.WriteBytes(packetBuf)
|
h.Tail.WriteBytes(&packetBuf)
|
||||||
|
|
||||||
sizeUint24 := utils.ToUint24(uint32(packetBuf.Len()))
|
sizeUint24 := utils.ToUint24(uint32(packetBuf.Len()))
|
||||||
sizeUint24Bytes := sizeUint24[:]
|
sizeUint24Bytes := sizeUint24[:]
|
||||||
@@ -35,10 +35,9 @@ func (h *Handshake) WriteBytes(writer io.Writer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handshake) Len() int {
|
func (h *Handshake) Len() int {
|
||||||
buf := acquireBytesBuffer()
|
buf := bytes.Buffer{}
|
||||||
defer releaseBytesBuffer(buf)
|
|
||||||
|
|
||||||
h.WriteBytes(buf)
|
h.WriteBytes(&buf)
|
||||||
|
|
||||||
return buf.Len()
|
return buf.Len()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
package tlstypes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
poolBytesBuffer = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return &bytes.Buffer{}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func acquireBytesBuffer() *bytes.Buffer {
|
|
||||||
return poolBytesBuffer.Get().(*bytes.Buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func releaseBytesBuffer(buf *bytes.Buffer) {
|
|
||||||
buf.Reset()
|
|
||||||
poolBytesBuffer.Put(buf)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user