-
Notifications
You must be signed in to change notification settings - Fork 8
/
lock.go
360 lines (301 loc) · 13.8 KB
/
lock.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package client
import (
"errors"
"fmt"
"github.com/snower/slock/protocol"
)
var WaitTimeout = errors.New("timeout")
type LockError struct {
Result uint8
CommandResult *protocol.LockResultCommand
Err error
}
func (self *LockError) GetLockData() *protocol.LockResultCommandData {
return self.CommandResult.Data
}
func (self *LockError) GetBytesValue() []byte {
if self.CommandResult != nil && self.CommandResult.Data != nil {
return self.CommandResult.GetLockData().GetBytesValue()
}
return nil
}
func (self *LockError) GetStringValue() string {
if self.CommandResult != nil && self.CommandResult.Data != nil {
return self.CommandResult.GetLockData().GetStringValue()
}
return ""
}
func (self *LockError) Error() string {
if self.Err == nil {
return fmt.Sprintf("error code %d", self.Result)
}
return fmt.Sprintf("%d %s", self.Result, self.Err.Error())
}
type Lock struct {
db *Database
lockId [16]byte
lockKey [16]byte
timeout uint32
expried uint32
count uint16
rcount uint8
}
func NewLock(db *Database, lockKey [16]byte, timeout uint32, expried uint32) *Lock {
return &Lock{db, db.GenLockId(), lockKey, timeout, expried, 0, 0}
}
func (self *Lock) GetLockKey() [16]byte {
return self.lockKey
}
func (self *Lock) GetLockId() [16]byte {
return self.lockId
}
func (self *Lock) GetTimeout() uint16 {
return uint16(self.timeout & 0xffff)
}
func (self *Lock) GetTimeoutFlag() uint16 {
return uint16(self.timeout >> 16)
}
func (self *Lock) SetTimeoutFlag(flag uint16) uint16 {
oflag := self.GetTimeoutFlag()
self.timeout = (self.timeout & 0xffff) | (uint32(flag) << 16)
return oflag
}
func (self *Lock) GetExpried() uint16 {
return uint16(self.expried & 0xffff)
}
func (self *Lock) GetExpriedFlag() uint16 {
return uint16(self.expried >> 16)
}
func (self *Lock) SetExpriedFlag(flag uint16) uint16 {
oflag := self.GetExpriedFlag()
self.expried = (self.expried & 0xffff) | (uint32(flag) << 16)
return oflag
}
func (self *Lock) GetCount() uint16 {
return self.count
}
func (self *Lock) SetCount(count uint16) uint16 {
ocount := self.count
if count > 0 {
self.count = count - 1
} else {
self.count = 0
}
return ocount
}
func (self *Lock) GetRcount() uint8 {
return self.rcount
}
func (self *Lock) SetRcount(rcount uint8) uint8 {
orcount := self.rcount
if rcount > 0 {
self.rcount = rcount - 1
} else {
self.rcount = 0
}
return orcount
}
func (self *Lock) doLock(flag uint8, lockId [16]byte, timeout uint32, expried uint32, count uint16, rcount uint8, data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
command := &protocol.LockCommand{Command: protocol.Command{Magic: protocol.MAGIC, Version: protocol.VERSION, CommandType: protocol.COMMAND_LOCK, RequestId: self.db.GenRequestId()},
Flag: self.buildLockFlag(flag, data), DbId: self.db.dbId, LockId: lockId, LockKey: self.lockKey, TimeoutFlag: uint16(timeout >> 16), Timeout: uint16(timeout),
ExpriedFlag: uint16(expried >> 16), Expried: uint16(expried), Count: count, Rcount: rcount, Data: data}
resultCommand, err := self.db.executeCommand(command, int(command.Timeout+1))
if err != nil {
return nil, err
}
lockResultCommand, ok := resultCommand.(*protocol.LockResultCommand)
if !ok {
return nil, errors.New("unknown command result")
}
return lockResultCommand, nil
}
func (self *Lock) doUnlock(flag uint8, lockId [16]byte, timeout uint32, expried uint32, count uint16, rcount uint8, data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
command := &protocol.LockCommand{Command: protocol.Command{Magic: protocol.MAGIC, Version: protocol.VERSION, CommandType: protocol.COMMAND_UNLOCK, RequestId: self.db.GenRequestId()},
Flag: self.buildUnlockFlag(flag, data), DbId: self.db.dbId, LockId: lockId, LockKey: self.lockKey, TimeoutFlag: uint16(timeout >> 16), Timeout: uint16(timeout),
ExpriedFlag: uint16(expried >> 16), Expried: uint16(expried), Count: count, Rcount: rcount, Data: data}
resultCommand, err := self.db.executeCommand(command, int(command.Timeout+1))
if err != nil {
return nil, err
}
lockResultCommand, ok := resultCommand.(*protocol.LockResultCommand)
if !ok {
return nil, errors.New("unknown command result")
}
return lockResultCommand, nil
}
func (self *Lock) Lock() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doLock(0, self.lockId, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result != 0 {
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, err}
}
return lockResultCommand, nil
}
func (self *Lock) LockWithData(data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doLock(0, self.lockId, self.timeout, self.expried, self.count, self.rcount, data)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result != 0 {
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, err}
}
return lockResultCommand, nil
}
func (self *Lock) Unlock() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(0, self.lockId, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result != 0 {
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, err}
}
return lockResultCommand, nil
}
func (self *Lock) UnlockWithData(data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(0, self.lockId, self.timeout, self.expried, self.count, self.rcount, data)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result != 0 {
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, err}
}
return lockResultCommand, nil
}
func (self *Lock) LockShow() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doLock(protocol.LOCK_FLAG_SHOW_WHEN_LOCKED, [16]byte{}, 0, 0, 0xffff, 0xff, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == protocol.RESULT_UNOWN_ERROR {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("show error")}
}
func (self *Lock) LockUpdate() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doLock(protocol.LOCK_FLAG_UPDATE_WHEN_LOCKED, self.lockId, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 || lockResultCommand.Result == protocol.RESULT_LOCKED_ERROR {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("update error")}
}
func (self *Lock) LockUpdateWithData(data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doLock(protocol.LOCK_FLAG_UPDATE_WHEN_LOCKED, self.lockId, self.timeout, self.expried, self.count, self.rcount, data)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 || lockResultCommand.Result == protocol.RESULT_LOCKED_ERROR {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("update error")}
}
func (self *Lock) UnlockHead() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_UNLOCK_FIRST_LOCK_WHEN_UNLOCKED, [16]byte{}, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("unlock error")}
}
func (self *Lock) UnlockHeadWithData(data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_UNLOCK_FIRST_LOCK_WHEN_UNLOCKED, [16]byte{}, self.timeout, self.expried, self.count, self.rcount, data)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("unlock error")}
}
func (self *Lock) UnlockRetoLockWait() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_SUCCED_TO_LOCK_WAIT, self.lockId, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("unlock error")}
}
func (self *Lock) UnlockRetoLockWaitWithData(data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_SUCCED_TO_LOCK_WAIT, self.lockId, self.timeout, self.expried, self.count, self.rcount, data)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("unlock error")}
}
func (self *Lock) UnlockHeadRetoLockWait() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_UNLOCK_FIRST_LOCK_WHEN_UNLOCKED|protocol.UNLOCK_FLAG_SUCCED_TO_LOCK_WAIT, [16]byte{}, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("unlock error")}
}
func (self *Lock) UnlockHeadRetoLockWaitWithData(data *protocol.LockCommandData) (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_UNLOCK_FIRST_LOCK_WHEN_UNLOCKED|protocol.UNLOCK_FLAG_SUCCED_TO_LOCK_WAIT, [16]byte{}, self.timeout, self.expried, self.count, self.rcount, data)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == 0 {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("unlock error")}
}
func (self *Lock) CancelWait() (*protocol.LockResultCommand, error) {
lockResultCommand, err := self.doUnlock(protocol.UNLOCK_FLAG_CANCEL_WAIT_LOCK_WHEN_UNLOCKED, self.lockId, self.timeout, self.expried, self.count, self.rcount, nil)
if err != nil {
return lockResultCommand, &LockError{0x80, lockResultCommand, err}
}
if lockResultCommand.Result == protocol.RESULT_LOCKED_ERROR {
return lockResultCommand, nil
}
return lockResultCommand, &LockError{lockResultCommand.Result, lockResultCommand, errors.New("cancel error")}
}
func (self *Lock) SendLock() error {
command := &protocol.LockCommand{Command: protocol.Command{Magic: protocol.MAGIC, Version: protocol.VERSION, CommandType: protocol.COMMAND_LOCK, RequestId: self.db.GenRequestId()},
Flag: 0, DbId: self.db.dbId, LockId: self.lockId, LockKey: self.lockKey, TimeoutFlag: uint16(self.timeout >> 16), Timeout: uint16(self.timeout),
ExpriedFlag: uint16(self.expried >> 16), Expried: uint16(self.expried), Count: self.count, Rcount: self.rcount}
return self.db.sendCommand(command)
}
func (self *Lock) SendLockWithData(data *protocol.LockCommandData) error {
command := &protocol.LockCommand{Command: protocol.Command{Magic: protocol.MAGIC, Version: protocol.VERSION, CommandType: protocol.COMMAND_LOCK, RequestId: self.db.GenRequestId()},
Flag: self.buildLockFlag(0, data), DbId: self.db.dbId, LockId: self.lockId, LockKey: self.lockKey, TimeoutFlag: uint16(self.timeout >> 16), Timeout: uint16(self.timeout),
ExpriedFlag: uint16(self.expried >> 16), Expried: uint16(self.expried), Count: self.count, Rcount: self.rcount, Data: data}
return self.db.sendCommand(command)
}
func (self *Lock) SendUnlock() error {
command := &protocol.LockCommand{Command: protocol.Command{Magic: protocol.MAGIC, Version: protocol.VERSION, CommandType: protocol.COMMAND_UNLOCK, RequestId: self.db.GenRequestId()},
Flag: 0, DbId: self.db.dbId, LockId: self.lockId, LockKey: self.lockKey, TimeoutFlag: uint16(self.timeout >> 16), Timeout: uint16(self.timeout),
ExpriedFlag: uint16(self.expried >> 16), Expried: uint16(self.expried), Count: self.count, Rcount: self.rcount}
return self.db.sendCommand(command)
}
func (self *Lock) SendUnlockWithData(data *protocol.LockCommandData) error {
command := &protocol.LockCommand{Command: protocol.Command{Magic: protocol.MAGIC, Version: protocol.VERSION, CommandType: protocol.COMMAND_UNLOCK, RequestId: self.db.GenRequestId()},
Flag: self.buildUnlockFlag(0, data), DbId: self.db.dbId, LockId: self.lockId, LockKey: self.lockKey, TimeoutFlag: uint16(self.timeout >> 16), Timeout: uint16(self.timeout),
ExpriedFlag: uint16(self.expried >> 16), Expried: uint16(self.expried), Count: self.count, Rcount: self.rcount, Data: data}
return self.db.sendCommand(command)
}
func (self *Lock) buildLockFlag(flag uint8, data *protocol.LockCommandData) uint8 {
if data != nil && data.Data != nil {
return flag | protocol.LOCK_FLAG_CONTAINS_DATA
}
return flag
}
func (self *Lock) buildUnlockFlag(flag uint8, data *protocol.LockCommandData) uint8 {
if data != nil && data.Data != nil {
return flag | protocol.UNLOCK_FLAG_CONTAINS_DATA
}
return flag
}