This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
279 lines (236 loc) · 6.21 KB
/
protocol.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package bmemcached
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
)
type (
// Connection is a struct that hold a direct connection to memcached
Connection struct {
conn net.Conn
}
_Request struct {
magic uint8
opcode uint8
keyLength uint16
extrasLength uint8
dataType uint8
reserved uint16
bodyLength uint32
opaque uint32
cas uint64
}
_Response struct {
magic uint8
opcode uint8
keyLength uint16
extrasLength uint8
dataType uint8
status uint16
bodyLength uint32
opaque uint32
cas uint64
}
)
const (
headerSize = 24
typeRequest = 0x80
typeResponse = 0x81
commandGet = 0x00
commandSet = 0x01
commandDelete = 0x04
statusSuccess = 0x00
statusKeyNotFound = 0x01
statusKeyExists = 0x02
statusUnknownCommand = 0x81
)
// New creates a Connection to memcached
func New(addr string) (Connection, error) {
var conn Connection
connection, err := net.Dial("tcp", addr)
if err == nil {
conn = Connection{connection}
} else {
fmt.Println(err)
}
return conn, err
}
func (c *Connection) writeRequest(request _Request, finalPayload []byte) error {
var tempBuffer, // Used to convert bigger integers to []byte
buf []byte
writer := bytes.NewBuffer(buf)
writer.WriteByte(request.magic)
writer.WriteByte(request.opcode)
tempBuffer = make([]byte, 2)
binary.BigEndian.PutUint16(tempBuffer, request.keyLength)
writer.Write(tempBuffer)
writer.WriteByte(request.extrasLength)
writer.WriteByte(request.dataType)
tempBuffer = make([]byte, 2)
binary.BigEndian.PutUint16(tempBuffer, request.reserved)
writer.Write(tempBuffer)
tempBuffer = make([]byte, 4)
binary.BigEndian.PutUint32(tempBuffer, request.bodyLength)
writer.Write(tempBuffer)
tempBuffer = make([]byte, 4)
binary.BigEndian.PutUint32(tempBuffer, request.opaque)
writer.Write(tempBuffer)
tempBuffer = make([]byte, 8)
binary.BigEndian.PutUint64(tempBuffer, request.cas)
writer.Write(tempBuffer)
_, err := c.conn.Write(writer.Bytes())
if err != nil {
return err
}
_, err = c.conn.Write(finalPayload)
if err != nil {
return err
}
return nil
}
func (c *Connection) parseResponse(buf []byte) (_Response, error) {
var magic, opcode, extrasLength, dataType uint8
var keyLength, reserved uint16
var bodyLength, opaque uint32
var cas uint64
var err error
var response _Response
reader := bytes.NewBuffer(buf)
magic, err = reader.ReadByte()
if err != nil {
return response, err
}
opcode, err = reader.ReadByte()
if err != nil {
return response, err
}
err = binary.Read(reader, binary.BigEndian, &keyLength)
if err != nil {
return response, err
}
extrasLength, err = reader.ReadByte()
if err != nil {
return response, err
}
dataType, err = reader.ReadByte()
if err != nil {
return response, err
}
err = binary.Read(reader, binary.BigEndian, &reserved)
if err != nil {
return response, err
}
err = binary.Read(reader, binary.BigEndian, &bodyLength)
if err != nil {
return response, err
}
err = binary.Read(reader, binary.BigEndian, &opaque)
if err != nil {
return response, err
}
err = binary.Read(reader, binary.BigEndian, &cas)
if err != nil {
return response, err
}
response = _Response{magic, opcode, keyLength, extrasLength, dataType,
reserved, bodyLength, opaque, cas}
return response, nil
}
func (c *Connection) getErrorMessage(errorCode uint16) string {
switch errorCode {
case statusKeyExists:
return "Key already exists"
case statusKeyNotFound:
return "Key not found"
case statusUnknownCommand:
return "Unkown command"
default:
return fmt.Sprintf("Unkown error code '%d'", errorCode)
}
}
func (c *Connection) readResponse() (_Response, error) {
var response _Response
buf := make([]byte, headerSize)
c.conn.Read(buf)
if buf[0] != typeRequest && buf[0] != typeResponse {
return response, fmt.Errorf("Server sent an unknown code: %d", buf[0])
}
response, err := c.parseResponse(buf)
if err != nil {
return response, err
}
switch response.status {
case statusSuccess:
break
case statusKeyExists, statusKeyNotFound, statusUnknownCommand:
errorMessage := c.getErrorMessage(response.status)
c.conn.Read(make([]byte, response.bodyLength))
return response, errors.New(errorMessage)
}
return response, nil
}
// Set a value on memcached
func (c *Connection) Set(key string, value string, expirationTime uint32) (int, error) {
var tempBuffer, finalPayload []byte
var err error
extrasLength := 8 // Flags and Expiration Time
bodyLength := uint32(len(key) + len(value) + extrasLength)
request := _Request{typeRequest, commandSet, uint16(len(key)), uint8(extrasLength), 0x00, 0x00,
bodyLength, 0x00, 0x00}
writer := bytes.NewBuffer(finalPayload)
tempBuffer = make([]byte, 4)
binary.BigEndian.PutUint32(tempBuffer, 0x00) // Flags
writer.Write(tempBuffer)
tempBuffer = make([]byte, 4)
binary.BigEndian.PutUint32(tempBuffer, expirationTime)
writer.Write(tempBuffer)
writer.WriteString(key)
writer.WriteString(value)
err = c.writeRequest(request, writer.Bytes())
if err != nil {
return 0, err
}
_, err = c.readResponse()
if err != nil {
return 0, err
}
return len(value), err
}
// Get a key on memcached
func (c *Connection) Get(key string) (string, error) {
var tempBuffer []byte
// TODO: Add cas
request := _Request{typeRequest, commandGet, uint16(len(key)), 0x00, 0x00, 0x00,
uint32(len(key)), 0x00, 0x00}
finalPayload := bytes.NewBuffer(make([]byte, 0))
finalPayload.WriteString(key)
err := c.writeRequest(request, finalPayload.Bytes())
if err != nil {
return "", err
}
response, err := c.readResponse()
if err != nil {
return "", err
}
// TODO: Treat flags
tempBuffer = make([]byte, 4)
c.conn.Read(tempBuffer)
tempBuffer = make([]byte, response.bodyLength-uint32(response.extrasLength))
c.conn.Read(tempBuffer)
return string(tempBuffer), nil
}
// Delete a key
func (c *Connection) Delete(key string) error {
request := _Request{typeRequest, commandDelete, uint16(len(key)), 0x00, 0x00, 0x00,
uint32(len(key)), 0x00, 0x00}
finalPayload := bytes.NewBuffer(make([]byte, 0))
finalPayload.WriteString(key)
err := c.writeRequest(request, finalPayload.Bytes())
if err != nil {
return err
}
_, err = c.readResponse()
return err
}