-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
49 lines (42 loc) · 952 Bytes
/
conn.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
package mc
import (
"net"
"time"
"github.com/kinescope/mc/protocol"
)
func openConn(addr string, timeout time.Duration) (*conn, error) {
c, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
return &conn{
nc: c,
addr: addr,
connectedAt: time.Now(),
}, nil
}
type conn struct {
nc net.Conn
addr string
packet protocol.Packet
connectedAt time.Time
}
func (c *conn) sendPacket(opcode protocol.Opcode, key, data, extras []byte, cas uint64) error {
c.packet.Reset()
{
c.packet.CAS = cas
c.packet.Key = key
c.packet.Data = data
c.packet.Extras = extras
c.packet.Opcode = opcode
}
return checkError(c.packet.Write(c.nc))
}
func (c *conn) readPacket() (*protocol.Packet, error) {
c.packet.Reset()
if err := c.packet.Read(c.nc); err != nil {
return nil, checkError(err)
}
return &c.packet, nil
}
func (c *conn) close() error { return c.nc.Close() }