-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
54 lines (42 loc) · 851 Bytes
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package net
import (
"bufio"
"io"
"net"
"github.com/dolfly/core/common/bufpool"
)
const (
bufferSize = 64 * 1024
)
func Transport(rw1, rw2 io.ReadWriter) error {
errc := make(chan error, 1)
go func() {
errc <- CopyBuffer(rw1, rw2, bufferSize)
}()
go func() {
errc <- CopyBuffer(rw2, rw1, bufferSize)
}()
if err := <-errc; err != nil && err != io.EOF {
return err
}
return nil
}
func CopyBuffer(dst io.Writer, src io.Reader, bufSize int) error {
buf := bufpool.Get(bufSize)
defer bufpool.Put(buf)
_, err := io.CopyBuffer(dst, src, buf)
return err
}
type bufferReaderConn struct {
net.Conn
br *bufio.Reader
}
func NewBufferReaderConn(conn net.Conn, br *bufio.Reader) net.Conn {
return &bufferReaderConn{
Conn: conn,
br: br,
}
}
func (c *bufferReaderConn) Read(b []byte) (int, error) {
return c.br.Read(b)
}