9seconds 7 лет назад
Родитель
Сommit
2c387cc213
3 измененных файлов: 54 добавлений и 6 удалений
  1. 3
    3
      mtproto/bufferpool.go
  2. 3
    3
      mtproto/rpc/rpc_proxy_request.go
  3. 48
    0
      mtproto/rwc.go

mtproto/bufferpool/bufferpool.go → mtproto/bufferpool.go Просмотреть файл

1
-package bufferpool
1
+package mtproto
2
 
2
 
3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
9
 
9
 
10
 var bufferPool sync.Pool
10
 var bufferPool sync.Pool
11
 
11
 
12
-func Get() *bytes.Buffer {
12
+func GetBuffer() *bytes.Buffer {
13
 	buf := bufferPool.Get().(*bytes.Buffer)
13
 	buf := bufferPool.Get().(*bytes.Buffer)
14
 	buf.Reset()
14
 	buf.Reset()
15
 
15
 
16
 	return buf
16
 	return buf
17
 }
17
 }
18
 
18
 
19
-func Return(buf *bytes.Buffer) {
19
+func ReturnBuffer(buf *bytes.Buffer) {
20
 	bufferPool.Put(buf)
20
 	bufferPool.Put(buf)
21
 }
21
 }
22
 
22
 

+ 3
- 3
mtproto/rpc/rpc_proxy_request.go Просмотреть файл

6
 	"encoding/binary"
6
 	"encoding/binary"
7
 	"net"
7
 	"net"
8
 
8
 
9
-	"github.com/9seconds/mtg/mtproto"
10
-	"github.com/9seconds/mtg/mtproto/bufferpool"
11
 	"github.com/juju/errors"
9
 	"github.com/juju/errors"
10
+
11
+	"github.com/9seconds/mtg/mtproto"
12
 )
12
 )
13
 
13
 
14
 const (
14
 const (
33
 }
33
 }
34
 
34
 
35
 func (r *RPCProxyRequest) Bytes() *bytes.Buffer {
35
 func (r *RPCProxyRequest) Bytes() *bytes.Buffer {
36
-	buf := bufferpool.Get()
36
+	buf := mtproto.GetBuffer()
37
 
37
 
38
 	flags := r.Flags
38
 	flags := r.Flags
39
 	if r.Extras.QuickAck {
39
 	if r.Extras.QuickAck {

+ 48
- 0
mtproto/rwc.go Просмотреть файл

1
+package mtproto
2
+
3
+import (
4
+	"bytes"
5
+	"io"
6
+)
7
+
8
+type BytesRWC interface {
9
+	Write(*bytes.Buffer) (int, error)
10
+	Read([]byte) (int, error)
11
+	Close() error
12
+}
13
+
14
+type StartBytesRWC struct {
15
+	conn BytesRWC
16
+}
17
+
18
+func (s *StartBytesRWC) Write(p []byte) (int, error) {
19
+	buf := GetBuffer()
20
+	buf.Write(p)
21
+	defer ReturnBuffer(buf)
22
+
23
+	return s.conn.Write(buf)
24
+}
25
+
26
+func (s *StartBytesRWC) Read(p []byte) (int, error) {
27
+	return s.conn.Read(p)
28
+}
29
+
30
+func (s *StartBytesRWC) Close() error {
31
+	return s.conn.Close()
32
+}
33
+
34
+type FinishBytesRWC struct {
35
+	conn io.ReadWriteCloser
36
+}
37
+
38
+func (f *FinishBytesRWC) Write(buf *bytes.Buffer) (int, error) {
39
+	return f.conn.Write(buf.Bytes())
40
+}
41
+
42
+func (f *FinishBytesRWC) Read(p []byte) (int, error) {
43
+	return f.conn.Read(p)
44
+}
45
+
46
+func (f *FinishBytesRWC) Close() error {
47
+	return f.conn.Close()
48
+}

Загрузка…
Отмена
Сохранить