/
BufferedConnection.go
41 lines (34 loc) · 973 Bytes
/
BufferedConnection.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
package netutil
import (
"bufio"
"github.com/xiaonanln/goworld/engine/consts"
)
// BufferedConnection provides buffered write to connections
type BufferedConnection struct {
Connection
bufReader *bufio.Reader
bufWriter *bufio.Writer
}
// NewBufferedWriteConnection creates a new connection with buffered write based on underlying connection
func NewBufferedConnection(conn Connection) *BufferedConnection {
brc := &BufferedConnection{
Connection: conn,
}
brc.bufReader = bufio.NewReaderSize(conn, consts.BUFFERED_READ_BUFFSIZE)
brc.bufWriter = bufio.NewWriterSize(conn, consts.BUFFERED_WRITE_BUFFSIZE)
return brc
}
// Read
func (brc *BufferedConnection) Read(p []byte) (int, error) {
return brc.bufReader.Read(p)
}
func (brc *BufferedConnection) Write(p []byte) (int, error) {
return brc.bufWriter.Write(p)
}
func (brc *BufferedConnection) Flush() error {
err := brc.bufWriter.Flush()
if err != nil {
return err
}
return brc.Connection.Flush()
}