mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 19:14:01 +03:00
Add bytesrwc
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user