-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathHorse.Core.Protobuf.Serializer.pas
More file actions
421 lines (381 loc) · 10.7 KB
/
Copy pathHorse.Core.Protobuf.Serializer.pas
File metadata and controls
421 lines (381 loc) · 10.7 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
unit Horse.Core.Protobuf.Serializer;
{$IFDEF FPC}
{$MODE DELPHI}{$H+}
{$ENDIF}
interface
uses
System.SysUtils, System.Classes, System.TypInfo,
System.Generics.Collections,
{$IFNDEF FPC}
System.Rtti,
{$ELSE}
Rtti,
{$ENDIF}
Horse.Grpc.Attributes,
Horse.Core.Protobuf.Rtti;
type
TProtobufWriter = class
private
FStream: TStream;
public
constructor Create(AStream: TStream);
procedure WriteVarint(Value: UInt64);
procedure WriteTag(Tag: Integer; WireType: Integer);
procedure WriteDouble(Tag: Integer; Value: Double);
procedure WriteSingle(Tag: Integer; Value: Single);
procedure WriteInt32(Tag: Integer; Value: Integer);
procedure WriteInt64(Tag: Integer; Value: Int64);
procedure WriteBool(Tag: Integer; Value: Boolean);
procedure WriteString(Tag: Integer; const Value: string);
procedure WriteBytes(Tag: Integer; const Value: TBytes);
procedure WriteMessage(Tag: Integer; const Value: TBytes);
end;
TProtobufReader = class
private
FStream: TStream;
FTag: Integer;
FWireType: Integer;
public
constructor Create(AStream: TStream);
function ReadField: Boolean;
function ReadVarint: UInt64;
function ReadDouble: Double;
function ReadSingle: Single;
function ReadInt32: Integer;
function ReadInt64: Int64;
function ReadBool: Boolean;
function ReadString: string;
function ReadBytes: TBytes;
procedure SkipField;
property Tag: Integer read FTag;
property WireType: Integer read FWireType;
end;
THorseProtobufSerializer = class
private
class function CreateInstance(AClass: TClass): TObject; static;
public
class function Serialize(Obj: TObject): TBytes; static;
class procedure Deserialize(const Bytes: TBytes; Obj: TObject); static;
end;
implementation
{ TProtobufWriter }
constructor TProtobufWriter.Create(AStream: TStream);
begin
inherited Create;
FStream := AStream;
end;
procedure TProtobufWriter.WriteVarint(Value: UInt64);
var
b: Byte;
begin
while Value >= $80 do
begin
b := Byte((Value and $7F) or $80);
FStream.Write(b, 1);
Value := Value shr 7;
end;
b := Byte(Value);
FStream.Write(b, 1);
end;
procedure TProtobufWriter.WriteTag(Tag, WireType: Integer);
begin
WriteVarint((UInt64(Tag) shl 3) or UInt64(WireType));
end;
procedure TProtobufWriter.WriteDouble(Tag: Integer; Value: Double);
begin
WriteTag(Tag, 1);
FStream.Write(Value, SizeOf(Double));
end;
procedure TProtobufWriter.WriteSingle(Tag: Integer; Value: Single);
begin
WriteTag(Tag, 5);
FStream.Write(Value, SizeOf(Single));
end;
procedure TProtobufWriter.WriteInt32(Tag: Integer; Value: Integer);
begin
WriteTag(Tag, 0);
WriteVarint(UInt64(Value));
end;
procedure TProtobufWriter.WriteInt64(Tag: Integer; Value: Int64);
begin
WriteTag(Tag, 0);
WriteVarint(UInt64(Value));
end;
procedure TProtobufWriter.WriteBool(Tag: Integer; Value: Boolean);
begin
WriteTag(Tag, 0);
if Value then
WriteVarint(1)
else
WriteVarint(0);
end;
procedure TProtobufWriter.WriteString(Tag: Integer; const Value: string);
var
Bytes: TBytes;
begin
Bytes := TEncoding.UTF8.GetBytes(Value);
WriteMessage(Tag, Bytes);
end;
procedure TProtobufWriter.WriteBytes(Tag: Integer; const Value: TBytes);
begin
WriteMessage(Tag, Value);
end;
procedure TProtobufWriter.WriteMessage(Tag: Integer; const Value: TBytes);
begin
WriteTag(Tag, 2);
WriteVarint(Length(Value));
if Length(Value) > 0 then
FStream.Write(Value[0], Length(Value));
end;
{ TProtobufReader }
constructor TProtobufReader.Create(AStream: TStream);
begin
inherited Create;
FStream := AStream;
end;
function TProtobufReader.ReadField: Boolean;
var
Header: UInt64;
begin
Result := FStream.Position < FStream.Size;
if not Result then
Exit;
Header := ReadVarint;
FTag := Header shr 3;
FWireType := Header and 7;
end;
function TProtobufReader.ReadVarint: UInt64;
var
b: Byte;
Shift: Integer;
begin
Result := 0;
Shift := 0;
repeat
if FStream.Read(b, 1) <> 1 then
Exit;
Result := Result or (UInt64(b and $7F) shl Shift);
Inc(Shift, 7);
until (b and $80) = 0;
end;
function TProtobufReader.ReadDouble: Double;
begin
Result := 0;
FStream.Read(Result, SizeOf(Double));
end;
function TProtobufReader.ReadSingle: Single;
begin
Result := 0;
FStream.Read(Result, SizeOf(Single));
end;
function TProtobufReader.ReadInt32: Integer;
begin
Result := Integer(ReadVarint);
end;
function TProtobufReader.ReadInt64: Int64;
begin
Result := Int64(ReadVarint);
end;
function TProtobufReader.ReadBool: Boolean;
begin
Result := ReadVarint <> 0;
end;
function TProtobufReader.ReadString: string;
begin
Result := TEncoding.UTF8.GetString(ReadBytes);
end;
function TProtobufReader.ReadBytes: TBytes;
var
Len: UInt64;
begin
Len := ReadVarint;
SetLength(Result, Len);
if Len > 0 then
FStream.Read(Result[0], Len);
end;
procedure TProtobufReader.SkipField;
var
Len: UInt64;
begin
case FWireType of
0: ReadVarint;
1: FStream.Seek(8, TSeekOrigin.soCurrent);
2:
begin
Len := ReadVarint;
FStream.Seek(Len, TSeekOrigin.soCurrent);
end;
5: FStream.Seek(4, TSeekOrigin.soCurrent);
else
raise Exception.CreateFmt('Invalid wire type: %d', [FWireType]);
end;
end;
{ THorseProtobufSerializer }
class function THorseProtobufSerializer.CreateInstance(AClass: TClass): TObject;
begin
Result := THorseProtobufRtti.CreateInstance(AClass);
end;
class function THorseProtobufSerializer.Serialize(Obj: TObject): TBytes;
var
Stream: TBytesStream;
Writer: TProtobufWriter;
Props: TArray<THorseProtobufProp>;
Prop: THorseProtobufProp;
Val: TValue;
SubObj: TObject;
SubBytes: TBytes;
SerializeAddr: Pointer;
StaticMethod: TSerializeMethod;
begin
if not Assigned(Obj) then Exit(nil);
Stream := TBytesStream.Create(nil);
try
SerializeAddr := THorseProtobufRtti.GetSerializeMethod(Obj.ClassType);
if Assigned(SerializeAddr) then
begin
TMethod(StaticMethod).Code := SerializeAddr;
TMethod(StaticMethod).Data := Obj;
StaticMethod(Stream);
end
else
begin
Writer := TProtobufWriter.Create(Stream);
try
Props := THorseProtobufRtti.GetProperties(Obj.ClassType);
for Prop in Props do
begin
Val := THorseProtobufRtti.GetPropValue(Obj, Prop);
if Val.IsEmpty then Continue;
case Prop.PropType of
hptInt32: Writer.WriteInt32(Prop.Tag, Val.AsInteger);
hptInt64: Writer.WriteInt64(Prop.Tag, Val.AsInt64);
hptDouble: Writer.WriteDouble(Prop.Tag, Val.AsType<Double>);
hptSingle: Writer.WriteSingle(Prop.Tag, Val.AsType<Single>);
hptString: Writer.WriteString(Prop.Tag, Val.AsString);
hptBool: Writer.WriteBool(Prop.Tag, Val.AsBoolean);
hptBytes: Writer.WriteBytes(Prop.Tag, Val.AsType<TBytes>);
hptMessage:
begin
SubObj := Val.AsObject;
if Assigned(SubObj) then
begin
SubBytes := Serialize(SubObj);
Writer.WriteMessage(Prop.Tag, SubBytes);
end;
end;
end;
end;
finally
Writer.Free;
end;
end;
Result := Stream.Bytes;
SetLength(Result, Stream.Size);
finally
Stream.Free;
end;
end;
class procedure THorseProtobufSerializer.Deserialize(const Bytes: TBytes; Obj: TObject);
var
Stream: TBytesStream;
Reader: TProtobufReader;
Props: TArray<THorseProtobufProp>;
Prop: THorseProtobufProp;
Found: Boolean;
SubBytes: TBytes;
SubObj: TObject;
PropClass: TClass;
LVal: TValue;
DeserializeAddr: Pointer;
StaticMethod: TDeserializeMethod;
begin
if (Length(Bytes) = 0) or not Assigned(Obj) then Exit;
Stream := TBytesStream.Create(Bytes);
try
DeserializeAddr := THorseProtobufRtti.GetDeserializeMethod(Obj.ClassType);
if Assigned(DeserializeAddr) then
begin
TMethod(StaticMethod).Code := DeserializeAddr;
TMethod(StaticMethod).Data := Obj;
StaticMethod(Stream);
end
else
begin
Props := THorseProtobufRtti.GetProperties(Obj.ClassType);
Reader := TProtobufReader.Create(Stream);
try
while Reader.ReadField do
begin
Found := False;
for Prop in Props do
begin
if Prop.Tag = Reader.Tag then
begin
Found := True;
case Prop.PropType of
hptInt32:
begin
LVal := TValue.From<Integer>(Reader.ReadInt32);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptInt64:
begin
LVal := TValue.From<Int64>(Reader.ReadInt64);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptDouble:
begin
LVal := TValue.From<Double>(Reader.ReadDouble);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptSingle:
begin
LVal := TValue.From<Single>(Reader.ReadSingle);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptString:
begin
LVal := TValue.From<string>(Reader.ReadString);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptBool:
begin
LVal := TValue.From<Boolean>(Reader.ReadBool);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptBytes:
begin
LVal := TValue.From<TBytes>(Reader.ReadBytes);
THorseProtobufRtti.SetPropValue(Obj, Prop, LVal);
end;
hptMessage:
begin
SubBytes := Reader.ReadBytes;
if Length(SubBytes) > 0 then
begin
SubObj := THorseProtobufRtti.GetPropValue(Obj, Prop).AsObject;
if not Assigned(SubObj) then
begin
PropClass := Prop.RttiType.AsInstance.MetaclassType;
SubObj := CreateInstance(PropClass);
THorseProtobufRtti.SetPropValue(Obj, Prop, SubObj);
end;
Deserialize(SubBytes, SubObj);
end;
end;
end;
Break;
end;
end;
if not Found then
Reader.SkipField;
end;
finally
Reader.Free;
end;
end;
finally
Stream.Free;
end;
end;
end.