-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDMemcached.Message.pas
365 lines (309 loc) · 10.7 KB
/
DMemcached.Message.pas
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
361
362
363
364
365
{***************************************************************************}
{ }
{ DMemCached - Copyright (C) 2014 - Víctor de Souza Faria }
{ }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}
unit DMemcached.Message;
interface
uses
System.Classes,
System.SysUtils,
DMemcached.Trasncoder;
type
opMagic = (Request = $80, Response = $81);
opCodes = (opGet = $00, opSet = $01, opAdd = $02, opReplace = $03, opDelete = $04, opIncrement = $05, opDecrement = $06, opQuit = $07, opFlush = $08, opGetQ = $09, opNoop = $0A, opVersion = $0B , opGetK = $0C, opGetKQ = $0D, opAppend = $0E, opPrepend = $0F, opStat = $10, opSetQ = $11, opAddQ = $12, opReplaceQ = $13, opDeleteQ = $14, opIncrementQ = $15, opDecrementQ = $16, opQuitQ = $17, opFlushQ = $18, opAppendQ = $19, opPrependQ = $1A);
respStatus = (NoError = $00, KeyNotFound = $01, KeyExists = $02, ValueTooLarge = $03, InvalidArguments = $04, ItemNotStored = $05, NonNumericValue = $06);
TMemcaheHeader = class
strict private
FOpcode: opCodes;
FKeyLength: UInt16;
FExtraLength: Byte;
FBodyLength: UInt32;
FOpaque: TBytes;
FCAS: UInt64;
protected
FMagic: opMagic;
function Reserved: TBytes; virtual; abstract;
public
constructor Create;
property Magic: opMagic read FMagic;
property OpCode: opCodes read FOpcode write FOpcode;
property KeyLength: UInt16 read FKeyLength write FKeyLength;
property ExtraLength: Byte read FExtraLength write FExtraLength;
property BodyLength: UInt32 read FBodyLength write FBodyLength;
property Opaque: TBytes read FOpaque write FOpaque;
property CAS: UInt64 read FCAS write FCAS;
function Pack: TBytes;
procedure Unpack(data: TBytes); virtual;
end;
TMemcaheResponseHeader = class(TMemcaheHeader)
strict private
FStatus: respStatus;
protected
function Reserved: TBytes; override;
public
constructor Create;
property Status: respStatus read FStatus write FStatus;
procedure Unpack(data: TBytes); override;
end;
TMemcaheRequestHeader = class(TMemcaheHeader)
protected
function Reserved: TBytes; override;
public
constructor Create;
end;
TMemcachedBaseMessage<T> = class
private
FHeader: TMemcaheHeader;
FKey: String;
FTranscoder: ITranscoder<T>;
FValue: T;
function KeyBytes: TBytes;
protected
function Extra: TBytes; virtual;
procedure SetExtra(extra : TBytes); virtual;
function ValueLength: UInt32;
public
constructor Create(opcode: opCodes; key: String; transcoder: ITranscoder<T>); overload; virtual;
constructor Create(data: TBytes; transcoder: ITranscoder<T>); overload; virtual;
property Key: String read FKey write FKey;
property Value: T read FValue write FValue;
property Header: TMemcaheHeader read FHeader;
function Pack: TBytes;
procedure Unpack(data: TBytes); virtual;
end;
TMemcachedSetMessage<T> = class(TMemcachedBaseMessage<T>)
private
FFlags: UInt32;
FExpire: UInt32;
protected
function Extra: TBytes; override;
procedure SetExtra(extra : TBytes); override;
public
constructor Create(opcode: opCodes; key: String; value: T;
flags: UInt32; expire: UInt32; transcoder: ITranscoder<T>);
end;
TMemcachedGetMessage<T> = class(TMemcachedBaseMessage<T>)
public
constructor Create(opcode: opCodes; key: String; transcoder: ITranscoder<T>); override;
end;
TMemcachedResponseMessage<T> = class(TMemcachedBaseMessage<T>)
public
constructor Create(responseHeader: TBytes; transcoder: ITranscoder<T>); override;
end;
implementation
uses
DMemcached.Util;
const
headerSize = 24;
magicOffset = 0;
opcodOffset = 1;
keyLengthOffset = 2;
extraLengthOffset = 4;
dataTypaOffset = 5;
reservedOffset = 6;
bodyLengthOffset = 8;
opaqueOffset = 12;
casOffset = 16;
{ TMemcachedBaseMessage }
constructor TMemcachedBaseMessage<T>.Create(opcode: opCodes; key: String;
transcoder: ITranscoder<T>);
begin
FHeader := TMemcaheRequestHeader.Create;
FHeader.OpCode := opcode;
FKey := key;
FHeader.KeyLength := Length(key) * SizeOf(Char);
FTranscoder := transcoder;
end;
constructor TMemcachedBaseMessage<T>.Create(data: TBytes; transcoder: ITranscoder<T>);
begin
FHeader := TMemcaheRequestHeader.Create;
Unpack(data);
end;
function TMemcachedBaseMessage<T>.Extra: TBytes;
begin
SetLength(Result, 0);
end;
function TMemcachedBaseMessage<T>.KeyBytes: TBytes;
begin
Result := WideBytesOf(FKey);
end;
function TMemcachedBaseMessage<T>.Pack: TBytes;
var
index: NativeInt;
extra: TBytes;
value: TBytes;
key: TBytes;
begin
Result := FHeader.Pack;
SetLength(Result, headerSize + FHeader.BodyLength);
index := headerSize;
extra := self.Extra;
Move(extra[0], Result[index], Length(extra));
index := index + Length(extra);
key := KeyBytes;
Move(key[0], Result[index], Length(key));
index := index + Length(key);
value := FTranscoder.ToBytes(FValue);
Move(value[0], Result[index], Length(value));
end;
procedure TMemcachedBaseMessage<T>.SetExtra(extra: TBytes);
begin
end;
procedure TMemcachedBaseMessage<T>.Unpack(data: TBytes);
var
index: NativeInt;
begin
FHeader.Unpack(data);
if NativeInt(Length(data)) >= NativeInt(headerSize + FHeader.BodyLength) then begin
index := headerSize;
if (FHeader.ExtraLength > 0) then begin
SetExtra(Copy(data, index, FHeader.ExtraLength));
index := index + FHeader.ExtraLength;
end;
if (FHeader.KeyLength > 0) then begin
FKey := TEncoding.Unicode.GetString(Copy(data, index, FHeader.KeyLength));
index := index + FHeader.KeyLength;
end;
if (FHeader.BodyLength > FHeader.ExtraLength + FHeader.KeyLength) then begin
FValue := FTranscoder.ToObject(Copy(data, index, FHeader.BodyLength - (FHeader.ExtraLength + FHeader.KeyLength)));
end;
end;
end;
function TMemcachedBaseMessage<T>.ValueLength: UInt32;
begin
Result := FTranscoder.BytesSize(FValue);
end;
{ TMemcaheHeader }
constructor TMemcaheHeader.Create;
begin
SetLength(FOpaque, 4);
FCAS := 0;
end;
{ TMemcachedSetMessage<T> }
constructor TMemcachedSetMessage<T>.Create(opcode: opCodes; key: String;
value: T; flags: UInt32; expire: UInt32; transcoder: ITranscoder<T>);
begin
inherited Create(opcode, key, transcoder);
FValue := value;
FHeader.ExtraLength := 8;
FFlags := flags;
FExpire := expire;
FHeader.BodyLength := FHeader.ExtraLength + FHeader.KeyLength + ValueLength;
end;
function TMemcachedSetMessage<T>.Extra: TBytes;
var
flagsBytes: TBytes;
expireBytes: TBytes;
begin
SetLength(Result, 8);
flagsBytes := UInt32ToByte(FFlags);
Move(flagsBytes[0], Result[0], 4);
expireBytes := UInt32ToByte(FExpire);
Move(expireBytes[0], Result[4], 4);
end;
procedure TMemcachedSetMessage<T>.SetExtra(extra: TBytes);
begin
if Length(extra) = 8 then begin
FFlags := ByteToUInt32(Copy(extra, 0, 4));
FExpire := ByteToUInt32(Copy(extra, 4, 4));
end;
end;
function TMemcaheHeader.Pack: TBytes;
var
emptyValue: NativeInt;
reserved: TBytes;
begin
emptyValue := 0;
SetLength(Result, headerSize);
Move(Magic, Result[magicOffset], 1);
Move(Opcode, Result[opcodOffset], 1);
Move(WordToByte(KeyLength)[0], Result[keyLengthOffset], SizeOf(KeyLength));
// Extras
Move(ExtraLength, Result[extraLengthOffset], SizeOf(ExtraLength));
// DataType
Move(emptyValue, Result[dataTypaOffset], 1);
// Reserved
reserved := self.Reserved;
Move(reserved[0], Result[reservedOffset], 2);
// BodyLength
Move(UInt32ToByte(BodyLength)[0], Result[bodyLengthOffset], SizeOf(BodyLength));
// Opaque
Move(Opaque[0], Result[opaqueOffset], 4);
// CAS
Move(UInt64ToByte(CAS)[0], Result[casOffset], SizeOf(CAS));
end;
procedure TMemcaheHeader.Unpack(data: TBytes);
begin
if Length(data) >= headerSize then begin
FMagic := opMagic(data[magicOffset]);
Opcode := opCodes(data[opcodOffset]);
KeyLength := ByteToWord(Copy(data, keyLengthOffset, SizeOf(KeyLength)));
ExtraLength := data[extraLengthOffset];
BodyLength := ByteToUInt32(Copy(data, bodyLengthOffset, SizeOf(BodyLength)));
FOpaque := Copy(data, opaqueOffset, 4);
CAS := ByteToUInt64(Copy(data, casOffset, SizeOf(CAS)));
end;
end;
{ TMemcaheRequestHeader }
constructor TMemcaheRequestHeader.Create;
begin
inherited Create;
FMagic := Request;
end;
function TMemcaheRequestHeader.Reserved: TBytes;
begin
SetLength(Result, 2);
end;
{ TMemcaheResponseHeader }
constructor TMemcaheResponseHeader.Create;
begin
inherited Create;
FMagic := Response;
end;
function TMemcaheResponseHeader.Reserved: TBytes;
var
status: TBytes;
begin
SetLength(Result, 2);
status := WordToByte(Word(FStatus));
Move(status[0], Result[0], 2);
end;
procedure TMemcaheResponseHeader.Unpack(data: TBytes);
begin
inherited Unpack(data);
FStatus := respStatus(ByteToWord(Copy(data, reservedOffset, 2)));
end;
{ TMemcachedResponseMessage<T> }
constructor TMemcachedResponseMessage<T>.Create(responseHeader: TBytes; transcoder: ITranscoder<T>);
begin
FHeader := TMemcaheResponseHeader.Create;
FTranscoder := transcoder;
FHeader.Unpack(responseHeader);
end;
{ TMemcachedGetMessage<T> }
constructor TMemcachedGetMessage<T>.Create(opcode: opCodes; key: String;
transcoder: ITranscoder<T>);
begin
inherited Create(opcode, key, transcoder);
FValue := value;
FHeader.ExtraLength := 0;
FHeader.BodyLength := FHeader.ExtraLength + FHeader.KeyLength + ValueLength;
end;
end.