blob: 94e6e1863516b68431cbc072d5fae371d76e1408 [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20{$SCOPEDENUMS ON}
21
22unit Thrift.Protocol;
23
24interface
25
26uses
27 Classes,
28 SysUtils,
29 Contnrs,
Jens Geyer606f1ef2018-04-09 23:09:41 +020030 Thrift.Exception,
Jens Geyerd5436f52014-10-03 19:50:38 +020031 Thrift.Stream,
Jens Geyer8f7487e2019-05-09 22:21:32 +020032 Thrift.Utils,
Jens Geyerd5436f52014-10-03 19:50:38 +020033 Thrift.Collections,
34 Thrift.Transport;
35
36type
37
38 TType = (
39 Stop = 0,
40 Void = 1,
41 Bool_ = 2,
42 Byte_ = 3,
43 Double_ = 4,
44 I16 = 6,
45 I32 = 8,
46 I64 = 10,
47 String_ = 11,
48 Struct = 12,
49 Map = 13,
50 Set_ = 14,
51 List = 15
52 );
53
54 TMessageType = (
55 Call = 1,
56 Reply = 2,
57 Exception = 3,
58 Oneway = 4
59 );
60
Jens Geyerf0e63312015-03-01 18:47:49 +010061const
62 VALID_TTYPES = [
63 TType.Stop, TType.Void,
64 TType.Bool_, TType.Byte_, TType.Double_, TType.I16, TType.I32, TType.I64, TType.String_,
65 TType.Struct, TType.Map, TType.Set_, TType.List
66 ];
67
68 VALID_MESSAGETYPES = [Low(TMessageType)..High(TMessageType)];
69
Jens Geyerd47fcdd2015-07-09 22:05:18 +020070const
71 DEFAULT_RECURSION_LIMIT = 64;
72
Jens Geyerf0e63312015-03-01 18:47:49 +010073type
Jens Geyerd5436f52014-10-03 19:50:38 +020074 IProtocol = interface;
Jens Geyer17c3ad92017-09-05 20:31:27 +020075
76 TThriftMessage = record
77 Name: string;
78 Type_: TMessageType;
79 SeqID: Integer;
80 end;
81
82 TThriftStruct = record
83 Name: string;
84 end;
85
86 TThriftField = record
87 Name: string;
88 Type_: TType;
89 Id: SmallInt;
90 end;
91
92 TThriftList = record
93 ElementType: TType;
94 Count: Integer;
95 end;
96
97 TThriftMap = record
98 KeyType: TType;
99 ValueType: TType;
100 Count: Integer;
101 end;
102
103 TThriftSet = record
104 ElementType: TType;
105 Count: Integer;
106 end;
107
108
Jens Geyerd5436f52014-10-03 19:50:38 +0200109 IProtocolFactory = interface
110 ['{7CD64A10-4E9F-4E99-93BF-708A31F4A67B}']
111 function GetProtocol( const trans: ITransport): IProtocol;
112 end;
113
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100114 TProtocolException = class abstract( TException)
Jens Geyerd5436f52014-10-03 19:50:38 +0200115 public
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100116 type TExceptionType = (
117 UNKNOWN = 0,
118 INVALID_DATA = 1,
119 NEGATIVE_SIZE = 2,
120 SIZE_LIMIT = 3,
121 BAD_VERSION = 4,
122 NOT_IMPLEMENTED = 5,
123 DEPTH_LIMIT = 6
124 );
Jens Geyerfad7fd32019-11-09 23:24:52 +0100125 strict protected
Jens Geyere0e32402016-04-20 21:50:48 +0200126 constructor HiddenCreate(const Msg: string);
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100127 class function GetType: TExceptionType; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200128 public
Jens Geyere0e32402016-04-20 21:50:48 +0200129 // purposefully hide inherited constructor
130 class function Create(const Msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
131 class function Create: TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100132 class function Create( aType: TExceptionType): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
133 class function Create( aType: TExceptionType; const msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
134 property Type_: TExceptionType read GetType;
Jens Geyerd5436f52014-10-03 19:50:38 +0200135 end;
136
Jens Geyere0e32402016-04-20 21:50:48 +0200137 // Needed to remove deprecation warning
138 TProtocolExceptionSpecialized = class abstract (TProtocolException)
139 public
140 constructor Create(const Msg: string);
141 end;
142
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100143 TProtocolExceptionUnknown = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100144 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100145 class function GetType: TProtocolException.TExceptionType; override;
146 end;
147
148 TProtocolExceptionInvalidData = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100149 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100150 class function GetType: TProtocolException.TExceptionType; override;
151 end;
152
153 TProtocolExceptionNegativeSize = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100154 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100155 class function GetType: TProtocolException.TExceptionType; override;
156 end;
157
158 TProtocolExceptionSizeLimit = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100159 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100160 class function GetType: TProtocolException.TExceptionType; override;
161 end;
162
163 TProtocolExceptionBadVersion = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100164 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100165 class function GetType: TProtocolException.TExceptionType; override;
166 end;
167
168 TProtocolExceptionNotImplemented = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100169 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100170 class function GetType: TProtocolException.TExceptionType; override;
171 end;
172
173 TProtocolExceptionDepthLimit = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100174 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100175 class function GetType: TProtocolException.TExceptionType; override;
176 end;
177
Jens Geyere0e32402016-04-20 21:50:48 +0200178
Jens Geyerd5436f52014-10-03 19:50:38 +0200179
180 TProtocolUtil = class
181 public
182 class procedure Skip( prot: IProtocol; type_: TType);
183 end;
184
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200185 IProtocolRecursionTracker = interface
186 ['{29CA033F-BB56-49B1-9EE3-31B1E82FC7A5}']
187 // no members yet
188 end;
189
190 TProtocolRecursionTrackerImpl = class abstract( TInterfacedObject, IProtocolRecursionTracker)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100191 strict protected
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200192 FProtocol : IProtocol;
193 public
194 constructor Create( prot : IProtocol);
195 destructor Destroy; override;
196 end;
197
Jens Geyerd5436f52014-10-03 19:50:38 +0200198 IProtocol = interface
Jens Geyer41f47af2019-11-09 23:24:52 +0100199 ['{7F3640D7-5082-49E7-B562-84202F323C3A}']
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 function GetTransport: ITransport;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200201 procedure WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerd5436f52014-10-03 19:50:38 +0200202 procedure WriteMessageEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200203 procedure WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 procedure WriteStructEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200205 procedure WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +0200206 procedure WriteFieldEnd;
207 procedure WriteFieldStop;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200208 procedure WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +0200209 procedure WriteMapEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200210 procedure WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +0200211 procedure WriteListEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200212 procedure WriteSetBegin( const set_: TThriftSet );
Jens Geyerd5436f52014-10-03 19:50:38 +0200213 procedure WriteSetEnd();
214 procedure WriteBool( b: Boolean);
215 procedure WriteByte( b: ShortInt);
216 procedure WriteI16( i16: SmallInt);
217 procedure WriteI32( i32: Integer);
218 procedure WriteI64( const i64: Int64);
219 procedure WriteDouble( const d: Double);
220 procedure WriteString( const s: string );
221 procedure WriteAnsiString( const s: AnsiString);
222 procedure WriteBinary( const b: TBytes);
223
Jens Geyer17c3ad92017-09-05 20:31:27 +0200224 function ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200225 procedure ReadMessageEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200226 function ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +0200227 procedure ReadStructEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200228 function ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 procedure ReadFieldEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200230 function ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200231 procedure ReadMapEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200232 function ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200233 procedure ReadListEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200234 function ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +0200235 procedure ReadSetEnd();
236 function ReadBool: Boolean;
237 function ReadByte: ShortInt;
238 function ReadI16: SmallInt;
239 function ReadI32: Integer;
240 function ReadI64: Int64;
241 function ReadDouble:Double;
242 function ReadBinary: TBytes;
243 function ReadString: string;
244 function ReadAnsiString: AnsiString;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200245
246 procedure SetRecursionLimit( value : Integer);
247 function GetRecursionLimit : Integer;
248 function NextRecursionLevel : IProtocolRecursionTracker;
249 procedure IncrementRecursionDepth;
250 procedure DecrementRecursionDepth;
Jens Geyer41f47af2019-11-09 23:24:52 +0100251 function GetMinSerializedSize( const aType : TType) : Integer;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200252
Jens Geyerd5436f52014-10-03 19:50:38 +0200253 property Transport: ITransport read GetTransport;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200254 property RecursionLimit : Integer read GetRecursionLimit write SetRecursionLimit;
Jens Geyerd5436f52014-10-03 19:50:38 +0200255 end;
256
257 TProtocolImpl = class abstract( TInterfacedObject, IProtocol)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100258 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 FTrans : ITransport;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200260 FRecursionLimit : Integer;
261 FRecursionDepth : Integer;
262
263 procedure SetRecursionLimit( value : Integer);
264 function GetRecursionLimit : Integer;
265 function NextRecursionLevel : IProtocolRecursionTracker;
266 procedure IncrementRecursionDepth;
267 procedure DecrementRecursionDepth;
268
Jens Geyer41f47af2019-11-09 23:24:52 +0100269 function GetMinSerializedSize( const aType : TType) : Integer; virtual; abstract;
270 procedure CheckReadBytesAvailable( const value : TThriftList); overload; inline;
271 procedure CheckReadBytesAvailable( const value : TThriftSet); overload; inline;
272 procedure CheckReadBytesAvailable( const value : TThriftMap); overload; inline;
273
274 procedure Reset; virtual;
Jens Geyerd5436f52014-10-03 19:50:38 +0200275 function GetTransport: ITransport;
276 public
Jens Geyer17c3ad92017-09-05 20:31:27 +0200277 procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200278 procedure WriteMessageEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200279 procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200280 procedure WriteStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200281 procedure WriteFieldBegin( const field: TThriftField); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200282 procedure WriteFieldEnd; virtual; abstract;
283 procedure WriteFieldStop; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200284 procedure WriteMapBegin( const map: TThriftMap); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200285 procedure WriteMapEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200286 procedure WriteListBegin( const list: TThriftList); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200287 procedure WriteListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200288 procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200289 procedure WriteSetEnd(); virtual; abstract;
290 procedure WriteBool( b: Boolean); virtual; abstract;
291 procedure WriteByte( b: ShortInt); virtual; abstract;
292 procedure WriteI16( i16: SmallInt); virtual; abstract;
293 procedure WriteI32( i32: Integer); virtual; abstract;
294 procedure WriteI64( const i64: Int64); virtual; abstract;
295 procedure WriteDouble( const d: Double); virtual; abstract;
296 procedure WriteString( const s: string ); virtual;
297 procedure WriteAnsiString( const s: AnsiString); virtual;
298 procedure WriteBinary( const b: TBytes); virtual; abstract;
299
Jens Geyer17c3ad92017-09-05 20:31:27 +0200300 function ReadMessageBegin: TThriftMessage; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200301 procedure ReadMessageEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200302 function ReadStructBegin: TThriftStruct; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200303 procedure ReadStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200304 function ReadFieldBegin: TThriftField; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200305 procedure ReadFieldEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200306 function ReadMapBegin: TThriftMap; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200307 procedure ReadMapEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200308 function ReadListBegin: TThriftList; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200309 procedure ReadListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200310 function ReadSetBegin: TThriftSet; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200311 procedure ReadSetEnd(); virtual; abstract;
312 function ReadBool: Boolean; virtual; abstract;
313 function ReadByte: ShortInt; virtual; abstract;
314 function ReadI16: SmallInt; virtual; abstract;
315 function ReadI32: Integer; virtual; abstract;
316 function ReadI64: Int64; virtual; abstract;
317 function ReadDouble:Double; virtual; abstract;
318 function ReadBinary: TBytes; virtual; abstract;
319 function ReadString: string; virtual;
320 function ReadAnsiString: AnsiString; virtual;
321
322 property Transport: ITransport read GetTransport;
323
324 constructor Create( trans: ITransport );
325 end;
326
Jens Geyer8f7487e2019-05-09 22:21:32 +0200327 IBase = interface( ISupportsToString)
328 ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}']
Jens Geyerd5436f52014-10-03 19:50:38 +0200329 procedure Read( const iprot: IProtocol);
330 procedure Write( const iprot: IProtocol);
331 end;
332
Jens Geyerd5436f52014-10-03 19:50:38 +0200333
334 TBinaryProtocolImpl = class( TProtocolImpl )
Jens Geyerfad7fd32019-11-09 23:24:52 +0100335 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200336 const
337 VERSION_MASK : Cardinal = $ffff0000;
338 VERSION_1 : Cardinal = $80010000;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100339 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200340 FStrictRead : Boolean;
341 FStrictWrite : Boolean;
Jens Geyer41f47af2019-11-09 23:24:52 +0100342 function GetMinSerializedSize( const aType : TType) : Integer; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200343
Jens Geyerfad7fd32019-11-09 23:24:52 +0100344 strict private
Jens Geyer17c3ad92017-09-05 20:31:27 +0200345 function ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200346 function ReadStringBody( size: Integer): string;
347
348 public
Jens Geyerd5436f52014-10-03 19:50:38 +0200349 type
350 TFactory = class( TInterfacedObject, IProtocolFactory)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100351 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200352 FStrictRead : Boolean;
353 FStrictWrite : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200354 function GetProtocol( const trans: ITransport): IProtocol;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100355 public
356 constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200357 end;
358
Jens Geyerfad7fd32019-11-09 23:24:52 +0100359 constructor Create( const trans: ITransport; strictRead: Boolean = FALSE; strictWrite: Boolean = TRUE); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200360
Jens Geyer17c3ad92017-09-05 20:31:27 +0200361 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200362 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200363 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200364 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200365 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200366 procedure WriteFieldEnd; override;
367 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200368 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200370 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200371 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200372 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200373 procedure WriteSetEnd(); override;
374 procedure WriteBool( b: Boolean); override;
375 procedure WriteByte( b: ShortInt); override;
376 procedure WriteI16( i16: SmallInt); override;
377 procedure WriteI32( i32: Integer); override;
378 procedure WriteI64( const i64: Int64); override;
379 procedure WriteDouble( const d: Double); override;
380 procedure WriteBinary( const b: TBytes); override;
381
Jens Geyer17c3ad92017-09-05 20:31:27 +0200382 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200383 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200384 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200385 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200386 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200387 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200388 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200389 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200390 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200391 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200392 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200393 procedure ReadSetEnd(); override;
394 function ReadBool: Boolean; override;
395 function ReadByte: ShortInt; override;
396 function ReadI16: SmallInt; override;
397 function ReadI32: Integer; override;
398 function ReadI64: Int64; override;
399 function ReadDouble:Double; override;
400 function ReadBinary: TBytes; override;
401
402 end;
403
404
405 { TProtocolDecorator forwards all requests to an enclosed TProtocol instance,
406 providing a way to author concise concrete decorator subclasses. The decorator
407 does not (and should not) modify the behaviour of the enclosed TProtocol
408
409 See p.175 of Design Patterns (by Gamma et al.)
410 }
411 TProtocolDecorator = class( TProtocolImpl)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100412 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +0200413 FWrappedProtocol : IProtocol;
414
Jens Geyer41f47af2019-11-09 23:24:52 +0100415 strict protected
416 function GetMinSerializedSize( const aType : TType) : Integer; override;
417
Jens Geyerd5436f52014-10-03 19:50:38 +0200418 public
419 // Encloses the specified protocol.
420 // All operations will be forward to the given protocol. Must be non-null.
421 constructor Create( const aProtocol : IProtocol);
422
Jens Geyer17c3ad92017-09-05 20:31:27 +0200423 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200424 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200425 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200427 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200428 procedure WriteFieldEnd; override;
429 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200430 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200431 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200432 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200433 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200434 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200435 procedure WriteSetEnd(); override;
436 procedure WriteBool( b: Boolean); override;
437 procedure WriteByte( b: ShortInt); override;
438 procedure WriteI16( i16: SmallInt); override;
439 procedure WriteI32( i32: Integer); override;
440 procedure WriteI64( const i64: Int64); override;
441 procedure WriteDouble( const d: Double); override;
442 procedure WriteString( const s: string ); override;
443 procedure WriteAnsiString( const s: AnsiString); override;
444 procedure WriteBinary( const b: TBytes); override;
445
Jens Geyer17c3ad92017-09-05 20:31:27 +0200446 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200447 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200448 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200449 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200450 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200451 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200452 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200453 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200454 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200455 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200456 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200457 procedure ReadSetEnd(); override;
458 function ReadBool: Boolean; override;
459 function ReadByte: ShortInt; override;
460 function ReadI16: SmallInt; override;
461 function ReadI32: Integer; override;
462 function ReadI64: Int64; override;
463 function ReadDouble:Double; override;
464 function ReadBinary: TBytes; override;
465 function ReadString: string; override;
466 function ReadAnsiString: AnsiString; override;
467 end;
468
469
470type
471 IRequestEvents = interface
Jens Geyer01640402013-09-25 21:12:21 +0200472 ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}']
473 // Called before reading arguments.
474 procedure PreRead;
475 // Called between reading arguments and calling the handler.
476 procedure PostRead;
477 // Called between calling the handler and writing the response.
478 procedure PreWrite;
479 // Called after writing the response.
480 procedure PostWrite;
481 // Called when an oneway (async) function call completes successfully.
482 procedure OnewayComplete;
483 // Called if the handler throws an undeclared exception.
484 procedure UnhandledError( const e : Exception);
485 // Called when a client has finished request-handling to clean up
486 procedure CleanupContext;
487 end;
488
489
490 IProcessorEvents = interface
491 ['{A8661119-657C-447D-93C5-512E36162A45}']
492 // Called when a client is about to call the processor.
493 procedure Processing( const transport : ITransport);
494 // Called on any service function invocation
495 function CreateRequestContext( const aFunctionName : string) : IRequestEvents;
496 // Called when a client has finished request-handling to clean up
497 procedure CleanupContext;
498 end;
499
500
501 IProcessor = interface
502 ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}']
Jens Geyerd430bbd2013-09-26 23:37:54 +0200503 function Process( const iprot :IProtocol; const oprot: IProtocol; const events : IProcessorEvents = nil): Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200504 end;
505
Jens Geyerd5436f52014-10-03 19:50:38 +0200506
Jens Geyer17c3ad92017-09-05 20:31:27 +0200507procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline;
508procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline;
509procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline;
510procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
511procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
512procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
513
Jens Geyerd5436f52014-10-03 19:50:38 +0200514
515implementation
516
Jens Geyerfad7fd32019-11-09 23:24:52 +0100517function ConvertInt64ToDouble( const n: Int64): Double; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200518begin
519 ASSERT( SizeOf(n) = SizeOf(Result));
520 System.Move( n, Result, SizeOf(Result));
521end;
522
Jens Geyerfad7fd32019-11-09 23:24:52 +0100523function ConvertDoubleToInt64( const d: Double): Int64; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200524begin
525 ASSERT( SizeOf(d) = SizeOf(Result));
526 System.Move( d, Result, SizeOf(Result));
527end;
528
Jens Geyerd5436f52014-10-03 19:50:38 +0200529
Jens Geyerd5436f52014-10-03 19:50:38 +0200530
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200531{ TProtocolRecursionTrackerImpl }
532
533constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol);
534begin
535 inherited Create;
536
537 // storing the pointer *after* the (successful) increment is important here
538 prot.IncrementRecursionDepth;
539 FProtocol := prot;
540end;
541
542destructor TProtocolRecursionTrackerImpl.Destroy;
543begin
544 try
545 // we have to release the reference iff the pointer has been stored
546 if FProtocol <> nil then begin
547 FProtocol.DecrementRecursionDepth;
548 FProtocol := nil;
549 end;
550 finally
551 inherited Destroy;
552 end;
553end;
554
Jens Geyerd5436f52014-10-03 19:50:38 +0200555{ TProtocolImpl }
556
557constructor TProtocolImpl.Create(trans: ITransport);
558begin
559 inherited Create;
560 FTrans := trans;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200561 FRecursionLimit := DEFAULT_RECURSION_LIMIT;
562 FRecursionDepth := 0;
563end;
564
565procedure TProtocolImpl.SetRecursionLimit( value : Integer);
566begin
567 FRecursionLimit := value;
568end;
569
570function TProtocolImpl.GetRecursionLimit : Integer;
571begin
572 result := FRecursionLimit;
573end;
574
575function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker;
576begin
577 result := TProtocolRecursionTrackerImpl.Create(Self);
578end;
579
580procedure TProtocolImpl.IncrementRecursionDepth;
581begin
582 if FRecursionDepth < FRecursionLimit
583 then Inc(FRecursionDepth)
Jens Geyere0e32402016-04-20 21:50:48 +0200584 else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded');
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200585end;
586
587procedure TProtocolImpl.DecrementRecursionDepth;
588begin
589 Dec(FRecursionDepth)
Jens Geyerd5436f52014-10-03 19:50:38 +0200590end;
591
592function TProtocolImpl.GetTransport: ITransport;
593begin
594 Result := FTrans;
595end;
596
Jens Geyer41f47af2019-11-09 23:24:52 +0100597procedure TProtocolImpl.Reset;
598begin
599 if FTrans.TransportControl <> nil
600 then FTrans.TransportControl.ResetConsumedMessageSize;
601end;
602
Jens Geyerd5436f52014-10-03 19:50:38 +0200603function TProtocolImpl.ReadAnsiString: AnsiString;
604var
605 b : TBytes;
606 len : Integer;
607begin
608 Result := '';
609 b := ReadBinary;
610 len := Length( b );
Jens Geyerfad7fd32019-11-09 23:24:52 +0100611 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200612 SetLength( Result, len);
613 System.Move( b[0], Pointer(Result)^, len );
614 end;
615end;
616
617function TProtocolImpl.ReadString: string;
618begin
619 Result := TEncoding.UTF8.GetString( ReadBinary );
620end;
621
622procedure TProtocolImpl.WriteAnsiString(const s: AnsiString);
623var
624 b : TBytes;
625 len : Integer;
626begin
627 len := Length(s);
628 SetLength( b, len);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100629 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200630 System.Move( Pointer(s)^, b[0], len );
631 end;
632 WriteBinary( b );
633end;
634
635procedure TProtocolImpl.WriteString(const s: string);
636var
637 b : TBytes;
638begin
639 b := TEncoding.UTF8.GetBytes(s);
640 WriteBinary( b );
641end;
642
Jens Geyer41f47af2019-11-09 23:24:52 +0100643
644procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList);
645begin
646 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
647end;
648
649
650procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet);
651begin
652 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
653end;
654
655
656procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap);
657var nPairSize : Integer
658;
659begin
660 nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType);
661 FTrans.CheckReadBytesAvailable( value.Count * nPairSize);
662end;
663
664
665
Jens Geyerd5436f52014-10-03 19:50:38 +0200666{ TProtocolUtil }
667
668class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200669var field : TThriftField;
670 map : TThriftMap;
671 set_ : TThriftSet;
672 list : TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200673 i : Integer;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200674 tracker : IProtocolRecursionTracker;
Jens Geyerd5436f52014-10-03 19:50:38 +0200675begin
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200676 tracker := prot.NextRecursionLevel;
Jens Geyerd5436f52014-10-03 19:50:38 +0200677 case type_ of
678 // simple types
679 TType.Bool_ : prot.ReadBool();
680 TType.Byte_ : prot.ReadByte();
681 TType.I16 : prot.ReadI16();
682 TType.I32 : prot.ReadI32();
683 TType.I64 : prot.ReadI64();
684 TType.Double_ : prot.ReadDouble();
685 TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it.
686
687 // structured types
688 TType.Struct : begin
689 prot.ReadStructBegin();
690 while TRUE do begin
691 field := prot.ReadFieldBegin();
692 if (field.Type_ = TType.Stop) then Break;
693 Skip(prot, field.Type_);
694 prot.ReadFieldEnd();
695 end;
696 prot.ReadStructEnd();
697 end;
698
699 TType.Map : begin
700 map := prot.ReadMapBegin();
701 for i := 0 to map.Count-1 do begin
702 Skip(prot, map.KeyType);
703 Skip(prot, map.ValueType);
704 end;
705 prot.ReadMapEnd();
706 end;
707
708 TType.Set_ : begin
709 set_ := prot.ReadSetBegin();
710 for i := 0 to set_.Count-1
711 do Skip( prot, set_.ElementType);
712 prot.ReadSetEnd();
713 end;
714
715 TType.List : begin
716 list := prot.ReadListBegin();
717 for i := 0 to list.Count-1
718 do Skip( prot, list.ElementType);
719 prot.ReadListEnd();
720 end;
721
722 else
Jens Geyer5f723cd2017-01-10 21:57:48 +0100723 raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_)));
Jens Geyerd5436f52014-10-03 19:50:38 +0200724 end;
725end;
726
Jens Geyerd5436f52014-10-03 19:50:38 +0200727
728{ TBinaryProtocolImpl }
729
Jens Geyerfad7fd32019-11-09 23:24:52 +0100730constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +0200731begin
Jens Geyerfad7fd32019-11-09 23:24:52 +0100732 inherited Create( trans);
Jens Geyerd5436f52014-10-03 19:50:38 +0200733 FStrictRead := strictRead;
734 FStrictWrite := strictWrite;
735end;
736
Jens Geyer17c3ad92017-09-05 20:31:27 +0200737function TBinaryProtocolImpl.ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200738begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200739 Result := FTrans.ReadAll( pBuf, buflen, off, len );
Jens Geyerd5436f52014-10-03 19:50:38 +0200740end;
741
742function TBinaryProtocolImpl.ReadBinary: TBytes;
743var
744 size : Integer;
745 buf : TBytes;
746begin
747 size := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100748 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100749 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200750 FTrans.ReadAll( buf, 0, size);
751 Result := buf;
752end;
753
754function TBinaryProtocolImpl.ReadBool: Boolean;
755begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200756 Result := (ReadByte = 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200757end;
758
759function TBinaryProtocolImpl.ReadByte: ShortInt;
Jens Geyerd5436f52014-10-03 19:50:38 +0200760begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200761 ReadAll( @result, SizeOf(result), 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200762end;
763
764function TBinaryProtocolImpl.ReadDouble: Double;
765begin
766 Result := ConvertInt64ToDouble( ReadI64 )
767end;
768
Jens Geyer17c3ad92017-09-05 20:31:27 +0200769function TBinaryProtocolImpl.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200770begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200771 Init( result, '', TType( ReadByte), 0);
772 if ( result.Type_ <> TType.Stop ) then begin
773 result.Id := ReadI16;
Jens Geyerd5436f52014-10-03 19:50:38 +0200774 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200775end;
776
777procedure TBinaryProtocolImpl.ReadFieldEnd;
778begin
779
780end;
781
782function TBinaryProtocolImpl.ReadI16: SmallInt;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200783var i16in : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200784begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200785 ReadAll( @i16in, Sizeof(i16in), 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200786 Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF));
787end;
788
789function TBinaryProtocolImpl.ReadI32: Integer;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200790var i32in : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200791begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200792 ReadAll( @i32in, SizeOf(i32in), 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200793
794 Result := Integer(
795 ((i32in[0] and $FF) shl 24) or
796 ((i32in[1] and $FF) shl 16) or
797 ((i32in[2] and $FF) shl 8) or
798 (i32in[3] and $FF));
799
800end;
801
802function TBinaryProtocolImpl.ReadI64: Int64;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200803var i64in : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200804begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200805 ReadAll( @i64in, SizeOf(i64in), 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200806 Result :=
807 (Int64( i64in[0] and $FF) shl 56) or
808 (Int64( i64in[1] and $FF) shl 48) or
809 (Int64( i64in[2] and $FF) shl 40) or
810 (Int64( i64in[3] and $FF) shl 32) or
811 (Int64( i64in[4] and $FF) shl 24) or
812 (Int64( i64in[5] and $FF) shl 16) or
813 (Int64( i64in[6] and $FF) shl 8) or
814 (Int64( i64in[7] and $FF));
815end;
816
Jens Geyer17c3ad92017-09-05 20:31:27 +0200817function TBinaryProtocolImpl.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200818begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200819 result.ElementType := TType(ReadByte);
820 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100821 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200822end;
823
824procedure TBinaryProtocolImpl.ReadListEnd;
825begin
826
827end;
828
Jens Geyer17c3ad92017-09-05 20:31:27 +0200829function TBinaryProtocolImpl.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200830begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200831 result.KeyType := TType(ReadByte);
832 result.ValueType := TType(ReadByte);
833 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100834 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200835end;
836
837procedure TBinaryProtocolImpl.ReadMapEnd;
838begin
839
840end;
841
Jens Geyer17c3ad92017-09-05 20:31:27 +0200842function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200843var
844 size : Integer;
845 version : Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200846begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100847 Reset;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200848 Init( result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200849 size := ReadI32;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200850 if (size < 0) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200851 version := size and Integer( VERSION_MASK);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200852 if ( version <> Integer( VERSION_1)) then begin
Jens Geyere0e32402016-04-20 21:50:48 +0200853 raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) );
Jens Geyerd5436f52014-10-03 19:50:38 +0200854 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200855 result.Type_ := TMessageType( size and $000000ff);
856 result.Name := ReadString;
857 result.SeqID := ReadI32;
858 end
859 else begin
860 if FStrictRead then begin
Jens Geyere0e32402016-04-20 21:50:48 +0200861 raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' );
Jens Geyerd5436f52014-10-03 19:50:38 +0200862 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200863 result.Name := ReadStringBody( size );
864 result.Type_ := TMessageType( ReadByte );
865 result.SeqID := ReadI32;
Jens Geyerd5436f52014-10-03 19:50:38 +0200866 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200867end;
868
869procedure TBinaryProtocolImpl.ReadMessageEnd;
870begin
871 inherited;
872
873end;
874
Jens Geyer17c3ad92017-09-05 20:31:27 +0200875function TBinaryProtocolImpl.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +0200876begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200877 result.ElementType := TType(ReadByte);
878 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100879 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200880end;
881
882procedure TBinaryProtocolImpl.ReadSetEnd;
883begin
884
885end;
886
887function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100888var buf : TBytes;
Jens Geyerd5436f52014-10-03 19:50:38 +0200889begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100890 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100891 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200892 FTrans.ReadAll( buf, 0, size );
893 Result := TEncoding.UTF8.GetString( buf);
894end;
895
Jens Geyer17c3ad92017-09-05 20:31:27 +0200896function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +0200897begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200898 Init( Result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200899end;
900
901procedure TBinaryProtocolImpl.ReadStructEnd;
902begin
903 inherited;
904
905end;
906
907procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
908var iLen : Integer;
909begin
910 iLen := Length(b);
911 WriteI32( iLen);
912 if iLen > 0 then FTrans.Write(b, 0, iLen);
913end;
914
915procedure TBinaryProtocolImpl.WriteBool(b: Boolean);
916begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200917 if b then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200918 WriteByte( 1 );
Jens Geyer17c3ad92017-09-05 20:31:27 +0200919 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200920 WriteByte( 0 );
921 end;
922end;
923
924procedure TBinaryProtocolImpl.WriteByte(b: ShortInt);
Jens Geyerd5436f52014-10-03 19:50:38 +0200925begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200926 FTrans.Write( @b, 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200927end;
928
929procedure TBinaryProtocolImpl.WriteDouble( const d: Double);
930begin
931 WriteI64(ConvertDoubleToInt64(d));
932end;
933
Jens Geyer17c3ad92017-09-05 20:31:27 +0200934procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +0200935begin
936 WriteByte(ShortInt(field.Type_));
937 WriteI16(field.ID);
938end;
939
940procedure TBinaryProtocolImpl.WriteFieldEnd;
941begin
942
943end;
944
945procedure TBinaryProtocolImpl.WriteFieldStop;
946begin
947 WriteByte(ShortInt(TType.Stop));
948end;
949
950procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200951var i16out : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200952begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200953 i16out[0] := Byte($FF and (i16 shr 8));
954 i16out[1] := Byte($FF and i16);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200955 FTrans.Write( @i16out, 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200956end;
957
958procedure TBinaryProtocolImpl.WriteI32(i32: Integer);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200959var i32out : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200960begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200961 i32out[0] := Byte($FF and (i32 shr 24));
962 i32out[1] := Byte($FF and (i32 shr 16));
963 i32out[2] := Byte($FF and (i32 shr 8));
964 i32out[3] := Byte($FF and i32);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200965 FTrans.Write( @i32out, 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200966end;
967
968procedure TBinaryProtocolImpl.WriteI64( const i64: Int64);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200969var i64out : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200970begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200971 i64out[0] := Byte($FF and (i64 shr 56));
972 i64out[1] := Byte($FF and (i64 shr 48));
973 i64out[2] := Byte($FF and (i64 shr 40));
974 i64out[3] := Byte($FF and (i64 shr 32));
975 i64out[4] := Byte($FF and (i64 shr 24));
976 i64out[5] := Byte($FF and (i64 shr 16));
977 i64out[6] := Byte($FF and (i64 shr 8));
978 i64out[7] := Byte($FF and i64);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200979 FTrans.Write( @i64out, 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200980end;
981
Jens Geyer17c3ad92017-09-05 20:31:27 +0200982procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +0200983begin
984 WriteByte(ShortInt(list.ElementType));
985 WriteI32(list.Count);
986end;
987
988procedure TBinaryProtocolImpl.WriteListEnd;
989begin
990
991end;
992
Jens Geyer17c3ad92017-09-05 20:31:27 +0200993procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +0200994begin
995 WriteByte(ShortInt(map.KeyType));
996 WriteByte(ShortInt(map.ValueType));
997 WriteI32(map.Count);
998end;
999
1000procedure TBinaryProtocolImpl.WriteMapEnd;
1001begin
1002
1003end;
1004
Jens Geyer17c3ad92017-09-05 20:31:27 +02001005procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001006var version : Cardinal;
Jens Geyerd5436f52014-10-03 19:50:38 +02001007begin
Jens Geyer41f47af2019-11-09 23:24:52 +01001008 Reset;
Jens Geyerfad7fd32019-11-09 23:24:52 +01001009 if FStrictWrite then begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001010 version := VERSION_1 or Cardinal( msg.Type_);
1011 WriteI32( Integer( version) );
1012 WriteString( msg.Name);
1013 WriteI32( msg.SeqID);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001014 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001015 WriteString( msg.Name);
1016 WriteByte(ShortInt( msg.Type_));
1017 WriteI32( msg.SeqID);
1018 end;
1019end;
1020
1021procedure TBinaryProtocolImpl.WriteMessageEnd;
1022begin
1023
1024end;
1025
Jens Geyer17c3ad92017-09-05 20:31:27 +02001026procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet);
Jens Geyerd5436f52014-10-03 19:50:38 +02001027begin
1028 WriteByte(ShortInt(set_.ElementType));
1029 WriteI32(set_.Count);
1030end;
1031
1032procedure TBinaryProtocolImpl.WriteSetEnd;
1033begin
1034
1035end;
1036
Jens Geyer17c3ad92017-09-05 20:31:27 +02001037procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001038begin
1039
1040end;
1041
1042procedure TBinaryProtocolImpl.WriteStructEnd;
1043begin
1044
1045end;
1046
Jens Geyer41f47af2019-11-09 23:24:52 +01001047function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer;
1048// Return the minimum number of bytes a type will consume on the wire
1049begin
1050 case aType of
1051 TType.Stop: result := 0;
1052 TType.Void: result := 0;
1053 TType.Bool_: result := SizeOf(Byte);
1054 TType.Byte_: result := SizeOf(Byte);
1055 TType.Double_: result := SizeOf(Double);
1056 TType.I16: result := SizeOf(Int16);
1057 TType.I32: result := SizeOf(Int32);
1058 TType.I64: result := SizeOf(Int64);
1059 TType.String_: result := SizeOf(Int32); // string length
1060 TType.Struct: result := 0; // empty struct
1061 TType.Map: result := SizeOf(Int32); // element count
1062 TType.Set_: result := SizeOf(Int32); // element count
1063 TType.List: result := SizeOf(Int32); // element count
1064 else
1065 raise TTransportExceptionBadArgs.Create('Unhandled type code');
1066 end;
1067end;
1068
1069
Jens Geyerd5436f52014-10-03 19:50:38 +02001070{ TProtocolException }
1071
Jens Geyere0e32402016-04-20 21:50:48 +02001072constructor TProtocolException.HiddenCreate(const Msg: string);
Jens Geyerd5436f52014-10-03 19:50:38 +02001073begin
Jens Geyere0e32402016-04-20 21:50:48 +02001074 inherited Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001075end;
1076
Jens Geyere0e32402016-04-20 21:50:48 +02001077class function TProtocolException.Create(const Msg: string): TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001078begin
Jens Geyere0e32402016-04-20 21:50:48 +02001079 Result := TProtocolExceptionUnknown.Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001080end;
1081
Jens Geyere0e32402016-04-20 21:50:48 +02001082class function TProtocolException.Create: TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001083begin
Jens Geyere0e32402016-04-20 21:50:48 +02001084 Result := TProtocolExceptionUnknown.Create('');
1085end;
1086
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001087class function TProtocolException.Create(aType: TExceptionType): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001088begin
1089{$WARN SYMBOL_DEPRECATED OFF}
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001090 Result := Create(aType, '');
Jens Geyere0e32402016-04-20 21:50:48 +02001091{$WARN SYMBOL_DEPRECATED DEFAULT}
1092end;
1093
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001094class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001095begin
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001096 case aType of
1097 TExceptionType.INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg);
1098 TExceptionType.NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg);
1099 TExceptionType.SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg);
1100 TExceptionType.BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg);
1101 TExceptionType.NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg);
1102 TExceptionType.DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg);
Jens Geyere0e32402016-04-20 21:50:48 +02001103 else
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001104 ASSERT( TExceptionType.UNKNOWN = aType);
Jens Geyere0e32402016-04-20 21:50:48 +02001105 Result := TProtocolExceptionUnknown.Create(msg);
1106 end;
1107end;
1108
1109{ TProtocolExceptionSpecialized }
1110
1111constructor TProtocolExceptionSpecialized.Create(const Msg: string);
1112begin
1113 inherited HiddenCreate(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001114end;
1115
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001116{ specialized TProtocolExceptions }
1117
1118class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType;
1119begin
1120 result := TExceptionType.UNKNOWN;
1121end;
1122
1123class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType;
1124begin
1125 result := TExceptionType.INVALID_DATA;
1126end;
1127
1128class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType;
1129begin
1130 result := TExceptionType.NEGATIVE_SIZE;
1131end;
1132
1133class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType;
1134begin
1135 result := TExceptionType.SIZE_LIMIT;
1136end;
1137
1138class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType;
1139begin
1140 result := TExceptionType.BAD_VERSION;
1141end;
1142
1143class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType;
1144begin
1145 result := TExceptionType.NOT_IMPLEMENTED;
1146end;
1147
1148class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType;
1149begin
1150 result := TExceptionType.DEPTH_LIMIT;
1151end;
1152
Jens Geyerd5436f52014-10-03 19:50:38 +02001153{ TBinaryProtocolImpl.TFactory }
1154
Jens Geyerfad7fd32019-11-09 23:24:52 +01001155constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +02001156begin
1157 inherited Create;
1158 FStrictRead := AStrictRead;
1159 FStrictWrite := AStrictWrite;
1160end;
1161
Jens Geyerd5436f52014-10-03 19:50:38 +02001162function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol;
1163begin
1164 Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite);
1165end;
1166
1167
1168{ TProtocolDecorator }
1169
1170constructor TProtocolDecorator.Create( const aProtocol : IProtocol);
1171begin
1172 ASSERT( aProtocol <> nil);
1173 inherited Create( aProtocol.Transport);
1174 FWrappedProtocol := aProtocol;
1175end;
1176
1177
Jens Geyer17c3ad92017-09-05 20:31:27 +02001178procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerd5436f52014-10-03 19:50:38 +02001179begin
1180 FWrappedProtocol.WriteMessageBegin( msg);
1181end;
1182
1183
1184procedure TProtocolDecorator.WriteMessageEnd;
1185begin
1186 FWrappedProtocol.WriteMessageEnd;
1187end;
1188
1189
Jens Geyer17c3ad92017-09-05 20:31:27 +02001190procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001191begin
1192 FWrappedProtocol.WriteStructBegin( struc);
1193end;
1194
1195
1196procedure TProtocolDecorator.WriteStructEnd;
1197begin
1198 FWrappedProtocol.WriteStructEnd;
1199end;
1200
1201
Jens Geyer17c3ad92017-09-05 20:31:27 +02001202procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +02001203begin
1204 FWrappedProtocol.WriteFieldBegin( field);
1205end;
1206
1207
1208procedure TProtocolDecorator.WriteFieldEnd;
1209begin
1210 FWrappedProtocol.WriteFieldEnd;
1211end;
1212
1213
1214procedure TProtocolDecorator.WriteFieldStop;
1215begin
1216 FWrappedProtocol.WriteFieldStop;
1217end;
1218
1219
Jens Geyer17c3ad92017-09-05 20:31:27 +02001220procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +02001221begin
1222 FWrappedProtocol.WriteMapBegin( map);
1223end;
1224
1225
1226procedure TProtocolDecorator.WriteMapEnd;
1227begin
1228 FWrappedProtocol.WriteMapEnd;
1229end;
1230
1231
Jens Geyer17c3ad92017-09-05 20:31:27 +02001232procedure TProtocolDecorator.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +02001233begin
1234 FWrappedProtocol.WriteListBegin( list);
1235end;
1236
1237
1238procedure TProtocolDecorator.WriteListEnd();
1239begin
1240 FWrappedProtocol.WriteListEnd();
1241end;
1242
1243
Jens Geyer17c3ad92017-09-05 20:31:27 +02001244procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet );
Jens Geyerd5436f52014-10-03 19:50:38 +02001245begin
1246 FWrappedProtocol.WriteSetBegin( set_);
1247end;
1248
1249
1250procedure TProtocolDecorator.WriteSetEnd();
1251begin
1252 FWrappedProtocol.WriteSetEnd();
1253end;
1254
1255
1256procedure TProtocolDecorator.WriteBool( b: Boolean);
1257begin
1258 FWrappedProtocol.WriteBool( b);
1259end;
1260
1261
1262procedure TProtocolDecorator.WriteByte( b: ShortInt);
1263begin
1264 FWrappedProtocol.WriteByte( b);
1265end;
1266
1267
1268procedure TProtocolDecorator.WriteI16( i16: SmallInt);
1269begin
1270 FWrappedProtocol.WriteI16( i16);
1271end;
1272
1273
1274procedure TProtocolDecorator.WriteI32( i32: Integer);
1275begin
1276 FWrappedProtocol.WriteI32( i32);
1277end;
1278
1279
1280procedure TProtocolDecorator.WriteI64( const i64: Int64);
1281begin
1282 FWrappedProtocol.WriteI64( i64);
1283end;
1284
1285
1286procedure TProtocolDecorator.WriteDouble( const d: Double);
1287begin
1288 FWrappedProtocol.WriteDouble( d);
1289end;
1290
1291
1292procedure TProtocolDecorator.WriteString( const s: string );
1293begin
1294 FWrappedProtocol.WriteString( s);
1295end;
1296
1297
1298procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString);
1299begin
1300 FWrappedProtocol.WriteAnsiString( s);
1301end;
1302
1303
1304procedure TProtocolDecorator.WriteBinary( const b: TBytes);
1305begin
1306 FWrappedProtocol.WriteBinary( b);
1307end;
1308
1309
Jens Geyer17c3ad92017-09-05 20:31:27 +02001310function TProtocolDecorator.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +02001311begin
1312 result := FWrappedProtocol.ReadMessageBegin;
1313end;
1314
1315
1316procedure TProtocolDecorator.ReadMessageEnd();
1317begin
1318 FWrappedProtocol.ReadMessageEnd();
1319end;
1320
1321
Jens Geyer17c3ad92017-09-05 20:31:27 +02001322function TProtocolDecorator.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +02001323begin
1324 result := FWrappedProtocol.ReadStructBegin;
1325end;
1326
1327
1328procedure TProtocolDecorator.ReadStructEnd;
1329begin
1330 FWrappedProtocol.ReadStructEnd;
1331end;
1332
1333
Jens Geyer17c3ad92017-09-05 20:31:27 +02001334function TProtocolDecorator.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +02001335begin
1336 result := FWrappedProtocol.ReadFieldBegin;
1337end;
1338
1339
1340procedure TProtocolDecorator.ReadFieldEnd();
1341begin
1342 FWrappedProtocol.ReadFieldEnd();
1343end;
1344
1345
Jens Geyer17c3ad92017-09-05 20:31:27 +02001346function TProtocolDecorator.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +02001347begin
1348 result := FWrappedProtocol.ReadMapBegin;
1349end;
1350
1351
1352procedure TProtocolDecorator.ReadMapEnd();
1353begin
1354 FWrappedProtocol.ReadMapEnd();
1355end;
1356
1357
Jens Geyer17c3ad92017-09-05 20:31:27 +02001358function TProtocolDecorator.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +02001359begin
1360 result := FWrappedProtocol.ReadListBegin;
1361end;
1362
1363
1364procedure TProtocolDecorator.ReadListEnd();
1365begin
1366 FWrappedProtocol.ReadListEnd();
1367end;
1368
1369
Jens Geyer17c3ad92017-09-05 20:31:27 +02001370function TProtocolDecorator.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +02001371begin
1372 result := FWrappedProtocol.ReadSetBegin;
1373end;
1374
1375
1376procedure TProtocolDecorator.ReadSetEnd();
1377begin
1378 FWrappedProtocol.ReadSetEnd();
1379end;
1380
1381
1382function TProtocolDecorator.ReadBool: Boolean;
1383begin
1384 result := FWrappedProtocol.ReadBool;
1385end;
1386
1387
1388function TProtocolDecorator.ReadByte: ShortInt;
1389begin
1390 result := FWrappedProtocol.ReadByte;
1391end;
1392
1393
1394function TProtocolDecorator.ReadI16: SmallInt;
1395begin
1396 result := FWrappedProtocol.ReadI16;
1397end;
1398
1399
1400function TProtocolDecorator.ReadI32: Integer;
1401begin
1402 result := FWrappedProtocol.ReadI32;
1403end;
1404
1405
1406function TProtocolDecorator.ReadI64: Int64;
1407begin
1408 result := FWrappedProtocol.ReadI64;
1409end;
1410
1411
1412function TProtocolDecorator.ReadDouble:Double;
1413begin
1414 result := FWrappedProtocol.ReadDouble;
1415end;
1416
1417
1418function TProtocolDecorator.ReadBinary: TBytes;
1419begin
1420 result := FWrappedProtocol.ReadBinary;
1421end;
1422
1423
1424function TProtocolDecorator.ReadString: string;
1425begin
1426 result := FWrappedProtocol.ReadString;
1427end;
1428
1429
1430function TProtocolDecorator.ReadAnsiString: AnsiString;
1431begin
1432 result := FWrappedProtocol.ReadAnsiString;
1433end;
1434
1435
Jens Geyer41f47af2019-11-09 23:24:52 +01001436function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer;
1437begin
1438 result := FWrappedProtocol.GetMinSerializedSize(aType);
1439end;
1440
1441
Jens Geyer17c3ad92017-09-05 20:31:27 +02001442{ Init helper functions }
1443
1444procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer);
1445begin
1446 rec.Name := AName;
1447 rec.Type_ := AMessageType;
1448 rec.SeqID := ASeqID;
1449end;
1450
1451
1452procedure Init( var rec : TThriftStruct; const AName: string = '');
1453begin
1454 rec.Name := AName;
1455end;
1456
1457
1458procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt);
1459begin
1460 rec.Name := AName;
1461 rec.Type_ := AType;
1462 rec.Id := AId;
1463end;
1464
1465
1466procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer);
1467begin
1468 rec.ValueType := AValueType;
1469 rec.KeyType := AKeyType;
1470 rec.Count := ACount;
1471end;
1472
1473
1474procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer);
1475begin
1476 rec.Count := ACount;
1477 rec.ElementType := AElementType;
1478end;
1479
1480
1481procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer);
1482begin
1483 rec.Count := ACount;
1484 rec.ElementType := AElementType;
1485end;
1486
1487
1488
1489
Jens Geyerd5436f52014-10-03 19:50:38 +02001490
1491end.
1492