Add bytesrwc

This commit is contained in:
9seconds
2018-07-02 10:33:25 +03:00
parent b74cc8d3ea
commit 2c387cc213
3 changed files with 54 additions and 6 deletions
@@ -1,4 +1,4 @@
package bufferpool package mtproto
import ( import (
"bytes" "bytes"
@@ -9,14 +9,14 @@ const bufferPoolSize = 4 * 1024
var bufferPool sync.Pool var bufferPool sync.Pool
func Get() *bytes.Buffer { func GetBuffer() *bytes.Buffer {
buf := bufferPool.Get().(*bytes.Buffer) buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset() buf.Reset()
return buf return buf
} }
func Return(buf *bytes.Buffer) { func ReturnBuffer(buf *bytes.Buffer) {
bufferPool.Put(buf) bufferPool.Put(buf)
} }
+3 -3
View File
@@ -6,9 +6,9 @@ import (
"encoding/binary" "encoding/binary"
"net" "net"
"github.com/9seconds/mtg/mtproto"
"github.com/9seconds/mtg/mtproto/bufferpool"
"github.com/juju/errors" "github.com/juju/errors"
"github.com/9seconds/mtg/mtproto"
) )
const ( const (
@@ -33,7 +33,7 @@ type RPCProxyRequest struct {
} }
func (r *RPCProxyRequest) Bytes() *bytes.Buffer { func (r *RPCProxyRequest) Bytes() *bytes.Buffer {
buf := bufferpool.Get() buf := mtproto.GetBuffer()
flags := r.Flags flags := r.Flags
if r.Extras.QuickAck { if r.Extras.QuickAck {
+48
View File
@@ -0,0 +1,48 @@
package mtproto
import (
"bytes"
"io"
)
type BytesRWC interface {
Write(*bytes.Buffer) (int, error)
Read([]byte) (int, error)
Close() error
}
type StartBytesRWC struct {
conn BytesRWC
}
func (s *StartBytesRWC) Write(p []byte) (int, error) {
buf := GetBuffer()
buf.Write(p)
defer ReturnBuffer(buf)
return s.conn.Write(buf)
}
func (s *StartBytesRWC) Read(p []byte) (int, error) {
return s.conn.Read(p)
}
func (s *StartBytesRWC) Close() error {
return s.conn.Close()
}
type FinishBytesRWC struct {
conn io.ReadWriteCloser
}
func (f *FinishBytesRWC) Write(buf *bytes.Buffer) (int, error) {
return f.conn.Write(buf.Bytes())
}
func (f *FinishBytesRWC) Read(p []byte) (int, error) {
return f.conn.Read(p)
}
func (f *FinishBytesRWC) Close() error {
return f.conn.Close()
}