blob: 9f2cac8d107e043064a530a72d225620010a40e2 [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,
Jens Geyera019cda2019-11-09 23:24:52 +010034 Thrift.Configuration,
Jens Geyerd5436f52014-10-03 19:50:38 +020035 Thrift.Transport;
36
37type
38
39 TType = (
40 Stop = 0,
41 Void = 1,
42 Bool_ = 2,
43 Byte_ = 3,
44 Double_ = 4,
45 I16 = 6,
46 I32 = 8,
47 I64 = 10,
48 String_ = 11,
49 Struct = 12,
50 Map = 13,
51 Set_ = 14,
52 List = 15
53 );
54
55 TMessageType = (
56 Call = 1,
57 Reply = 2,
58 Exception = 3,
59 Oneway = 4
60 );
61
Jens Geyerf0e63312015-03-01 18:47:49 +010062const
63 VALID_TTYPES = [
64 TType.Stop, TType.Void,
65 TType.Bool_, TType.Byte_, TType.Double_, TType.I16, TType.I32, TType.I64, TType.String_,
66 TType.Struct, TType.Map, TType.Set_, TType.List
67 ];
68
69 VALID_MESSAGETYPES = [Low(TMessageType)..High(TMessageType)];
70
71type
Jens Geyerd5436f52014-10-03 19:50:38 +020072 IProtocol = interface;
Jens Geyer17c3ad92017-09-05 20:31:27 +020073
74 TThriftMessage = record
75 Name: string;
76 Type_: TMessageType;
77 SeqID: Integer;
78 end;
79
80 TThriftStruct = record
81 Name: string;
82 end;
83
84 TThriftField = record
85 Name: string;
86 Type_: TType;
87 Id: SmallInt;
88 end;
89
90 TThriftList = record
91 ElementType: TType;
92 Count: Integer;
93 end;
94
95 TThriftMap = record
96 KeyType: TType;
97 ValueType: TType;
98 Count: Integer;
99 end;
100
101 TThriftSet = record
102 ElementType: TType;
103 Count: Integer;
104 end;
105
106
Jens Geyerd5436f52014-10-03 19:50:38 +0200107 IProtocolFactory = interface
108 ['{7CD64A10-4E9F-4E99-93BF-708A31F4A67B}']
109 function GetProtocol( const trans: ITransport): IProtocol;
110 end;
111
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100112 TProtocolException = class abstract( TException)
Jens Geyerd5436f52014-10-03 19:50:38 +0200113 public
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100114 type TExceptionType = (
115 UNKNOWN = 0,
116 INVALID_DATA = 1,
117 NEGATIVE_SIZE = 2,
118 SIZE_LIMIT = 3,
119 BAD_VERSION = 4,
120 NOT_IMPLEMENTED = 5,
121 DEPTH_LIMIT = 6
122 );
Jens Geyerfad7fd32019-11-09 23:24:52 +0100123 strict protected
Jens Geyere0e32402016-04-20 21:50:48 +0200124 constructor HiddenCreate(const Msg: string);
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100125 class function GetType: TExceptionType; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200126 public
Jens Geyere0e32402016-04-20 21:50:48 +0200127 // purposefully hide inherited constructor
128 class function Create(const Msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
129 class function Create: TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100130 class function Create( aType: TExceptionType): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
131 class function Create( aType: TExceptionType; const msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
132 property Type_: TExceptionType read GetType;
Jens Geyerd5436f52014-10-03 19:50:38 +0200133 end;
134
Jens Geyere0e32402016-04-20 21:50:48 +0200135 // Needed to remove deprecation warning
136 TProtocolExceptionSpecialized = class abstract (TProtocolException)
137 public
138 constructor Create(const Msg: string);
139 end;
140
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100141 TProtocolExceptionUnknown = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100142 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100143 class function GetType: TProtocolException.TExceptionType; override;
144 end;
145
146 TProtocolExceptionInvalidData = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100147 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100148 class function GetType: TProtocolException.TExceptionType; override;
149 end;
150
151 TProtocolExceptionNegativeSize = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100152 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100153 class function GetType: TProtocolException.TExceptionType; override;
154 end;
155
156 TProtocolExceptionSizeLimit = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100157 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100158 class function GetType: TProtocolException.TExceptionType; override;
159 end;
160
161 TProtocolExceptionBadVersion = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100162 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100163 class function GetType: TProtocolException.TExceptionType; override;
164 end;
165
166 TProtocolExceptionNotImplemented = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100167 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100168 class function GetType: TProtocolException.TExceptionType; override;
169 end;
170
171 TProtocolExceptionDepthLimit = class (TProtocolExceptionSpecialized)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100172 strict protected
Jens Geyer9f11c1e2019-11-09 19:39:20 +0100173 class function GetType: TProtocolException.TExceptionType; override;
174 end;
175
Jens Geyere0e32402016-04-20 21:50:48 +0200176
Jens Geyerd5436f52014-10-03 19:50:38 +0200177
178 TProtocolUtil = class
179 public
180 class procedure Skip( prot: IProtocol; type_: TType);
181 end;
182
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200183 IProtocolRecursionTracker = interface
184 ['{29CA033F-BB56-49B1-9EE3-31B1E82FC7A5}']
185 // no members yet
186 end;
187
188 TProtocolRecursionTrackerImpl = class abstract( TInterfacedObject, IProtocolRecursionTracker)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100189 strict protected
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200190 FProtocol : IProtocol;
191 public
192 constructor Create( prot : IProtocol);
193 destructor Destroy; override;
194 end;
195
Jens Geyer07f4bb52022-09-03 14:50:06 +0200196 IThriftBytes = interface; // forward
197
Jens Geyerd5436f52014-10-03 19:50:38 +0200198 IProtocol = interface
Jens Geyer07f4bb52022-09-03 14:50:06 +0200199 ['{6067A28E-15BF-4C9D-9A6F-D991BB3DCB85}']
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);
Jens Geyer07f4bb52022-09-03 14:50:06 +0200222 procedure WriteBinary( const b: TBytes); overload;
223 procedure WriteBinary( const b: IThriftBytes); overload;
Jens Geyerd5436f52014-10-03 19:50:38 +0200224
Jens Geyer17c3ad92017-09-05 20:31:27 +0200225 function ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200226 procedure ReadMessageEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200227 function ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 procedure ReadStructEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200229 function ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200230 procedure ReadFieldEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200231 function ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200232 procedure ReadMapEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200233 function ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200234 procedure ReadListEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200235 function ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +0200236 procedure ReadSetEnd();
237 function ReadBool: Boolean;
238 function ReadByte: ShortInt;
239 function ReadI16: SmallInt;
240 function ReadI32: Integer;
241 function ReadI64: Int64;
242 function ReadDouble:Double;
Jens Geyer07f4bb52022-09-03 14:50:06 +0200243 function ReadBinary: TBytes; // IMPORTANT: this is NOT safe across module boundaries
244 function ReadBinaryCOM : IThriftBytes;
Jens Geyerd5436f52014-10-03 19:50:38 +0200245 function ReadString: string;
246 function ReadAnsiString: AnsiString;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200247
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200248 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 Geyera019cda2019-11-09 23:24:52 +0100254 function Configuration : IThriftConfiguration;
Jens Geyerd5436f52014-10-03 19:50:38 +0200255 end;
256
Jens Geyer3b686532021-07-01 23:04:08 +0200257 TProtocolImplClass = class of TProtocolImpl;
258
Jens Geyerd5436f52014-10-03 19:50:38 +0200259 TProtocolImpl = class abstract( TInterfacedObject, IProtocol)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100260 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200261 FTrans : ITransport;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200262 FRecursionLimit : Integer;
263 FRecursionDepth : Integer;
264
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200265 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 Geyera019cda2019-11-09 23:24:52 +0100275 function GetTransport: ITransport;
276 function Configuration : IThriftConfiguration;
277
Jens Geyer17c3ad92017-09-05 20:31:27 +0200278 procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200279 procedure WriteMessageEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200280 procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200281 procedure WriteStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200282 procedure WriteFieldBegin( const field: TThriftField); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200283 procedure WriteFieldEnd; virtual; abstract;
284 procedure WriteFieldStop; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200285 procedure WriteMapBegin( const map: TThriftMap); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200286 procedure WriteMapEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200287 procedure WriteListBegin( const list: TThriftList); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200288 procedure WriteListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200289 procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200290 procedure WriteSetEnd(); virtual; abstract;
291 procedure WriteBool( b: Boolean); virtual; abstract;
292 procedure WriteByte( b: ShortInt); virtual; abstract;
293 procedure WriteI16( i16: SmallInt); virtual; abstract;
294 procedure WriteI32( i32: Integer); virtual; abstract;
295 procedure WriteI64( const i64: Int64); virtual; abstract;
296 procedure WriteDouble( const d: Double); virtual; abstract;
297 procedure WriteString( const s: string ); virtual;
298 procedure WriteAnsiString( const s: AnsiString); virtual;
Jens Geyer07f4bb52022-09-03 14:50:06 +0200299 procedure WriteBinary( const b: TBytes); overload; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200300
Jens Geyer17c3ad92017-09-05 20:31:27 +0200301 function ReadMessageBegin: TThriftMessage; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 procedure ReadMessageEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200303 function ReadStructBegin: TThriftStruct; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200304 procedure ReadStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200305 function ReadFieldBegin: TThriftField; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200306 procedure ReadFieldEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200307 function ReadMapBegin: TThriftMap; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200308 procedure ReadMapEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200309 function ReadListBegin: TThriftList; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200310 procedure ReadListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200311 function ReadSetBegin: TThriftSet; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200312 procedure ReadSetEnd(); virtual; abstract;
313 function ReadBool: Boolean; virtual; abstract;
314 function ReadByte: ShortInt; virtual; abstract;
315 function ReadI16: SmallInt; virtual; abstract;
316 function ReadI32: Integer; virtual; abstract;
317 function ReadI64: Int64; virtual; abstract;
318 function ReadDouble:Double; virtual; abstract;
319 function ReadBinary: TBytes; virtual; abstract;
320 function ReadString: string; virtual;
321 function ReadAnsiString: AnsiString; virtual;
322
Jens Geyer07f4bb52022-09-03 14:50:06 +0200323 // provide generic implementation for all derived classes
324 procedure WriteBinary( const bytes : IThriftBytes); overload; virtual;
325 function ReadBinaryCOM : IThriftBytes; virtual;
326
Jens Geyera019cda2019-11-09 23:24:52 +0100327 property Transport: ITransport read GetTransport;
Jens Geyerd5436f52014-10-03 19:50:38 +0200328
Jens Geyera019cda2019-11-09 23:24:52 +0100329 public
Jens Geyer3b686532021-07-01 23:04:08 +0200330 constructor Create( const aTransport : ITransport); virtual;
Jens Geyerd5436f52014-10-03 19:50:38 +0200331 end;
332
Jens Geyer8f7487e2019-05-09 22:21:32 +0200333 IBase = interface( ISupportsToString)
334 ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}']
Jens Geyer07f4bb52022-09-03 14:50:06 +0200335 procedure Read( const prot: IProtocol);
336 procedure Write( const prot: IProtocol);
337 end;
338
339
340 IThriftBytes = interface( ISupportsToString)
341 ['{CDBEF7E8-BEF2-4A0A-983A-F334E3FF0016}']
342 function GetCount : Integer;
343 procedure SetCount(const value : Integer);
344
345 // WARNING: This returns a direct pointer to the underlying data structure
346 function QueryRawDataPtr : Pointer;
347
348 property Count : Integer read GetCount write SetCount;
349 end;
350
351
352 TThriftBytesImpl = class( TInterfacedObject, IThriftBytes, ISupportsToString)
353 strict private
354 FData : TBytes;
355
356 strict protected
357 function GetCount : Integer;
358 procedure SetCount(const value : Integer);
359 function QueryRawDataPtr : Pointer;
360
361 public
362 constructor Create; overload;
363 constructor Create( const bytes : TBytes); overload;
364 constructor Create( var bytes : TBytes; const aTakeOwnership : Boolean = FALSE); overload;
365
366 function ToString : string; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200367 end;
368
Jens Geyerd5436f52014-10-03 19:50:38 +0200369
370 TBinaryProtocolImpl = class( TProtocolImpl )
Jens Geyerfad7fd32019-11-09 23:24:52 +0100371 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200372 const
373 VERSION_MASK : Cardinal = $ffff0000;
374 VERSION_1 : Cardinal = $80010000;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100375 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200376 FStrictRead : Boolean;
377 FStrictWrite : Boolean;
Jens Geyer41f47af2019-11-09 23:24:52 +0100378 function GetMinSerializedSize( const aType : TType) : Integer; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200379
Jens Geyerfad7fd32019-11-09 23:24:52 +0100380 strict private
Jens Geyer17c3ad92017-09-05 20:31:27 +0200381 function ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200382 function ReadStringBody( size: Integer): string;
383
384 public
Jens Geyerd5436f52014-10-03 19:50:38 +0200385 type
386 TFactory = class( TInterfacedObject, IProtocolFactory)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100387 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200388 FStrictRead : Boolean;
389 FStrictWrite : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200390 function GetProtocol( const trans: ITransport): IProtocol;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100391 public
392 constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200393 end;
394
Jens Geyer3b686532021-07-01 23:04:08 +0200395 constructor Create( const trans: ITransport); overload; override;
396 constructor Create( const trans: ITransport; strictRead, strictWrite: Boolean); reintroduce; overload;
Jens Geyerd5436f52014-10-03 19:50:38 +0200397
Jens Geyer17c3ad92017-09-05 20:31:27 +0200398 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200399 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200400 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200401 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200402 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200403 procedure WriteFieldEnd; override;
404 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200405 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200406 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200407 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200408 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200409 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200410 procedure WriteSetEnd(); override;
411 procedure WriteBool( b: Boolean); override;
412 procedure WriteByte( b: ShortInt); override;
413 procedure WriteI16( i16: SmallInt); override;
414 procedure WriteI32( i32: Integer); override;
415 procedure WriteI64( const i64: Int64); override;
416 procedure WriteDouble( const d: Double); override;
417 procedure WriteBinary( const b: TBytes); override;
418
Jens Geyer17c3ad92017-09-05 20:31:27 +0200419 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200420 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200421 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200422 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200423 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200424 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200425 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200426 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200427 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200428 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200429 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200430 procedure ReadSetEnd(); override;
431 function ReadBool: Boolean; override;
432 function ReadByte: ShortInt; override;
433 function ReadI16: SmallInt; override;
434 function ReadI32: Integer; override;
435 function ReadI64: Int64; override;
436 function ReadDouble:Double; override;
437 function ReadBinary: TBytes; override;
438
439 end;
440
441
442 { TProtocolDecorator forwards all requests to an enclosed TProtocol instance,
443 providing a way to author concise concrete decorator subclasses. The decorator
444 does not (and should not) modify the behaviour of the enclosed TProtocol
445
446 See p.175 of Design Patterns (by Gamma et al.)
447 }
448 TProtocolDecorator = class( TProtocolImpl)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100449 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +0200450 FWrappedProtocol : IProtocol;
451
Jens Geyer41f47af2019-11-09 23:24:52 +0100452 strict protected
453 function GetMinSerializedSize( const aType : TType) : Integer; override;
454
Jens Geyerd5436f52014-10-03 19:50:38 +0200455 public
456 // Encloses the specified protocol.
457 // All operations will be forward to the given protocol. Must be non-null.
Jens Geyer3b686532021-07-01 23:04:08 +0200458 constructor Create( const aProtocol : IProtocol); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200459
Jens Geyer17c3ad92017-09-05 20:31:27 +0200460 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200461 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200462 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200463 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200464 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200465 procedure WriteFieldEnd; override;
466 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200467 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200468 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200469 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200470 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200471 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200472 procedure WriteSetEnd(); override;
473 procedure WriteBool( b: Boolean); override;
474 procedure WriteByte( b: ShortInt); override;
475 procedure WriteI16( i16: SmallInt); override;
476 procedure WriteI32( i32: Integer); override;
477 procedure WriteI64( const i64: Int64); override;
478 procedure WriteDouble( const d: Double); override;
479 procedure WriteString( const s: string ); override;
480 procedure WriteAnsiString( const s: AnsiString); override;
481 procedure WriteBinary( const b: TBytes); override;
482
Jens Geyer17c3ad92017-09-05 20:31:27 +0200483 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200484 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200485 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200486 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200487 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200488 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200489 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200490 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200491 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200492 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200493 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200494 procedure ReadSetEnd(); override;
495 function ReadBool: Boolean; override;
496 function ReadByte: ShortInt; override;
497 function ReadI16: SmallInt; override;
498 function ReadI32: Integer; override;
499 function ReadI64: Int64; override;
500 function ReadDouble:Double; override;
501 function ReadBinary: TBytes; override;
502 function ReadString: string; override;
503 function ReadAnsiString: AnsiString; override;
504 end;
505
506
507type
508 IRequestEvents = interface
Jens Geyer01640402013-09-25 21:12:21 +0200509 ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}']
510 // Called before reading arguments.
511 procedure PreRead;
512 // Called between reading arguments and calling the handler.
513 procedure PostRead;
514 // Called between calling the handler and writing the response.
515 procedure PreWrite;
516 // Called after writing the response.
517 procedure PostWrite;
518 // Called when an oneway (async) function call completes successfully.
519 procedure OnewayComplete;
520 // Called if the handler throws an undeclared exception.
521 procedure UnhandledError( const e : Exception);
522 // Called when a client has finished request-handling to clean up
523 procedure CleanupContext;
524 end;
525
526
527 IProcessorEvents = interface
528 ['{A8661119-657C-447D-93C5-512E36162A45}']
529 // Called when a client is about to call the processor.
530 procedure Processing( const transport : ITransport);
531 // Called on any service function invocation
532 function CreateRequestContext( const aFunctionName : string) : IRequestEvents;
533 // Called when a client has finished request-handling to clean up
534 procedure CleanupContext;
535 end;
536
537
538 IProcessor = interface
539 ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}']
Jens Geyerd430bbd2013-09-26 23:37:54 +0200540 function Process( const iprot :IProtocol; const oprot: IProtocol; const events : IProcessorEvents = nil): Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200541 end;
542
Jens Geyerd5436f52014-10-03 19:50:38 +0200543
Jens Geyer17c3ad92017-09-05 20:31:27 +0200544procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline;
545procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline;
546procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline;
547procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
548procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
549procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
550
Jens Geyerd5436f52014-10-03 19:50:38 +0200551
552implementation
553
Jens Geyerfad7fd32019-11-09 23:24:52 +0100554function ConvertInt64ToDouble( const n: Int64): Double; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200555begin
556 ASSERT( SizeOf(n) = SizeOf(Result));
557 System.Move( n, Result, SizeOf(Result));
558end;
559
Jens Geyerfad7fd32019-11-09 23:24:52 +0100560function ConvertDoubleToInt64( const d: Double): Int64; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200561begin
562 ASSERT( SizeOf(d) = SizeOf(Result));
563 System.Move( d, Result, SizeOf(Result));
564end;
565
Jens Geyerd5436f52014-10-03 19:50:38 +0200566
Jens Geyerd5436f52014-10-03 19:50:38 +0200567
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200568{ TProtocolRecursionTrackerImpl }
569
570constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol);
571begin
572 inherited Create;
573
574 // storing the pointer *after* the (successful) increment is important here
575 prot.IncrementRecursionDepth;
576 FProtocol := prot;
577end;
578
579destructor TProtocolRecursionTrackerImpl.Destroy;
580begin
581 try
582 // we have to release the reference iff the pointer has been stored
583 if FProtocol <> nil then begin
584 FProtocol.DecrementRecursionDepth;
585 FProtocol := nil;
586 end;
587 finally
588 inherited Destroy;
589 end;
590end;
591
Jens Geyerd5436f52014-10-03 19:50:38 +0200592{ TProtocolImpl }
593
Jens Geyera019cda2019-11-09 23:24:52 +0100594constructor TProtocolImpl.Create( const aTransport : ITransport);
Jens Geyerd5436f52014-10-03 19:50:38 +0200595begin
596 inherited Create;
Jens Geyera019cda2019-11-09 23:24:52 +0100597 FTrans := aTransport;
598 FRecursionLimit := aTransport.Configuration.RecursionLimit;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200599 FRecursionDepth := 0;
600end;
601
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200602function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker;
603begin
604 result := TProtocolRecursionTrackerImpl.Create(Self);
605end;
606
607procedure TProtocolImpl.IncrementRecursionDepth;
608begin
609 if FRecursionDepth < FRecursionLimit
610 then Inc(FRecursionDepth)
Jens Geyere0e32402016-04-20 21:50:48 +0200611 else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded');
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200612end;
613
614procedure TProtocolImpl.DecrementRecursionDepth;
615begin
616 Dec(FRecursionDepth)
Jens Geyerd5436f52014-10-03 19:50:38 +0200617end;
618
619function TProtocolImpl.GetTransport: ITransport;
620begin
621 Result := FTrans;
622end;
623
Jens Geyera019cda2019-11-09 23:24:52 +0100624function TProtocolImpl.Configuration : IThriftConfiguration;
625begin
626 Result := FTrans.Configuration;
627end;
628
Jens Geyer41f47af2019-11-09 23:24:52 +0100629procedure TProtocolImpl.Reset;
630begin
Jens Geyera019cda2019-11-09 23:24:52 +0100631 FTrans.ResetConsumedMessageSize;
Jens Geyer41f47af2019-11-09 23:24:52 +0100632end;
633
Jens Geyerd5436f52014-10-03 19:50:38 +0200634function TProtocolImpl.ReadAnsiString: AnsiString;
635var
636 b : TBytes;
637 len : Integer;
638begin
639 Result := '';
640 b := ReadBinary;
641 len := Length( b );
Jens Geyerfad7fd32019-11-09 23:24:52 +0100642 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200643 SetLength( Result, len);
644 System.Move( b[0], Pointer(Result)^, len );
645 end;
646end;
647
648function TProtocolImpl.ReadString: string;
649begin
650 Result := TEncoding.UTF8.GetString( ReadBinary );
651end;
652
653procedure TProtocolImpl.WriteAnsiString(const s: AnsiString);
654var
655 b : TBytes;
656 len : Integer;
657begin
658 len := Length(s);
659 SetLength( b, len);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100660 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200661 System.Move( Pointer(s)^, b[0], len );
662 end;
663 WriteBinary( b );
664end;
665
666procedure TProtocolImpl.WriteString(const s: string);
667var
668 b : TBytes;
669begin
670 b := TEncoding.UTF8.GetBytes(s);
671 WriteBinary( b );
672end;
673
Jens Geyer41f47af2019-11-09 23:24:52 +0100674
675procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList);
676begin
677 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
678end;
679
680
681procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet);
682begin
683 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
684end;
685
686
687procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap);
Jens Geyera019cda2019-11-09 23:24:52 +0100688var nPairSize : Integer;
Jens Geyer41f47af2019-11-09 23:24:52 +0100689begin
690 nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType);
691 FTrans.CheckReadBytesAvailable( value.Count * nPairSize);
692end;
693
Jens Geyer07f4bb52022-09-03 14:50:06 +0200694
695procedure TProtocolImpl.WriteBinary( const bytes : IThriftBytes);
696var tmp : TBytes;
697begin
698 SetLength( tmp, bytes.Count);
699 if Length(tmp) > 0
700 then Move( bytes.QueryRawDataPtr^, tmp[0], Length(tmp));
701 WriteBinary( tmp);
702end;
703
704
705function TProtocolImpl.ReadBinaryCOM : IThriftBytes;
706var bytes : TBytes;
707begin
708 bytes := ReadBinary;
709 result := TThriftBytesImpl.Create(bytes,TRUE);
710end;
711
712
713{ TThriftBytesImpl }
714
715constructor TThriftBytesImpl.Create;
716begin
717 inherited Create;
718 ASSERT( Length(FData) = 0);
719end;
720
721
722constructor TThriftBytesImpl.Create( const bytes : TBytes);
723begin
724 FData := bytes; // copies the data
725end;
726
727
728constructor TThriftBytesImpl.Create( var bytes : TBytes; const aTakeOwnership : Boolean);
729
730 procedure SwapPointer( var one, two);
731 var
732 pOne : Pointer absolute one;
733 pTwo : Pointer absolute two;
734 pTmp : Pointer;
735 begin
736 pTmp := pOne;
737 pOne := pTwo;
738 pTwo := pTmp;
739 end;
740
741begin
742 inherited Create;
743 ASSERT( Length(FData) = 0);
744
745 if aTakeOwnership
746 then SwapPointer( FData, bytes)
747 else FData := bytes; // copies the data
748end;
749
750
751function TThriftBytesImpl.ToString : string;
752var sb : TThriftStringBuilder;
753begin
754 sb := TThriftStringBuilder.Create();
755 try
756 sb.Append('Bin: ');
757 sb.Append( FData);
758
759 result := sb.ToString;
760 finally
761 sb.Free;
762 end;
763end;
764
765
766function TThriftBytesImpl.GetCount : Integer;
767begin
768 result := Length(FData);
769end;
770
771
772procedure TThriftBytesImpl.SetCount(const value : Integer);
773begin
774 SetLength( FData, value);
775end;
776
777
778function TThriftBytesImpl.QueryRawDataPtr : Pointer;
779begin
780 result := FData;
781end;
782
Jens Geyerd5436f52014-10-03 19:50:38 +0200783{ TProtocolUtil }
784
785class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200786var field : TThriftField;
787 map : TThriftMap;
788 set_ : TThriftSet;
789 list : TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200790 i : Integer;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200791 tracker : IProtocolRecursionTracker;
Jens Geyerd5436f52014-10-03 19:50:38 +0200792begin
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200793 tracker := prot.NextRecursionLevel;
Jens Geyerd5436f52014-10-03 19:50:38 +0200794 case type_ of
795 // simple types
796 TType.Bool_ : prot.ReadBool();
797 TType.Byte_ : prot.ReadByte();
798 TType.I16 : prot.ReadI16();
799 TType.I32 : prot.ReadI32();
800 TType.I64 : prot.ReadI64();
801 TType.Double_ : prot.ReadDouble();
802 TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it.
803
804 // structured types
805 TType.Struct : begin
806 prot.ReadStructBegin();
807 while TRUE do begin
808 field := prot.ReadFieldBegin();
809 if (field.Type_ = TType.Stop) then Break;
810 Skip(prot, field.Type_);
811 prot.ReadFieldEnd();
812 end;
813 prot.ReadStructEnd();
814 end;
815
816 TType.Map : begin
817 map := prot.ReadMapBegin();
818 for i := 0 to map.Count-1 do begin
819 Skip(prot, map.KeyType);
820 Skip(prot, map.ValueType);
821 end;
822 prot.ReadMapEnd();
823 end;
824
825 TType.Set_ : begin
826 set_ := prot.ReadSetBegin();
827 for i := 0 to set_.Count-1
828 do Skip( prot, set_.ElementType);
829 prot.ReadSetEnd();
830 end;
831
832 TType.List : begin
833 list := prot.ReadListBegin();
834 for i := 0 to list.Count-1
835 do Skip( prot, list.ElementType);
836 prot.ReadListEnd();
837 end;
838
839 else
Jens Geyer5f723cd2017-01-10 21:57:48 +0100840 raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_)));
Jens Geyerd5436f52014-10-03 19:50:38 +0200841 end;
842end;
843
Jens Geyerd5436f52014-10-03 19:50:38 +0200844
845{ TBinaryProtocolImpl }
846
Jens Geyer3b686532021-07-01 23:04:08 +0200847constructor TBinaryProtocolImpl.Create( const trans: ITransport);
848begin
849 // call the real CTOR
850 Self.Create( trans, FALSE, TRUE);
851end;
852
Jens Geyerfad7fd32019-11-09 23:24:52 +0100853constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +0200854begin
Jens Geyerfad7fd32019-11-09 23:24:52 +0100855 inherited Create( trans);
Jens Geyerd5436f52014-10-03 19:50:38 +0200856 FStrictRead := strictRead;
857 FStrictWrite := strictWrite;
858end;
859
Jens Geyer17c3ad92017-09-05 20:31:27 +0200860function TBinaryProtocolImpl.ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200861begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200862 Result := FTrans.ReadAll( pBuf, buflen, off, len );
Jens Geyerd5436f52014-10-03 19:50:38 +0200863end;
864
865function TBinaryProtocolImpl.ReadBinary: TBytes;
866var
867 size : Integer;
868 buf : TBytes;
869begin
870 size := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100871 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100872 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200873 FTrans.ReadAll( buf, 0, size);
874 Result := buf;
875end;
876
877function TBinaryProtocolImpl.ReadBool: Boolean;
878begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200879 Result := (ReadByte = 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200880end;
881
882function TBinaryProtocolImpl.ReadByte: ShortInt;
Jens Geyerd5436f52014-10-03 19:50:38 +0200883begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200884 ReadAll( @result, SizeOf(result), 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200885end;
886
887function TBinaryProtocolImpl.ReadDouble: Double;
888begin
889 Result := ConvertInt64ToDouble( ReadI64 )
890end;
891
Jens Geyer17c3ad92017-09-05 20:31:27 +0200892function TBinaryProtocolImpl.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200893begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200894 Init( result, '', TType( ReadByte), 0);
895 if ( result.Type_ <> TType.Stop ) then begin
896 result.Id := ReadI16;
Jens Geyerd5436f52014-10-03 19:50:38 +0200897 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200898end;
899
900procedure TBinaryProtocolImpl.ReadFieldEnd;
901begin
902
903end;
904
905function TBinaryProtocolImpl.ReadI16: SmallInt;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200906var i16in : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200907begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200908 ReadAll( @i16in, Sizeof(i16in), 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200909 Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF));
910end;
911
912function TBinaryProtocolImpl.ReadI32: Integer;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200913var i32in : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200914begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200915 ReadAll( @i32in, SizeOf(i32in), 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200916
917 Result := Integer(
918 ((i32in[0] and $FF) shl 24) or
919 ((i32in[1] and $FF) shl 16) or
920 ((i32in[2] and $FF) shl 8) or
921 (i32in[3] and $FF));
922
923end;
924
925function TBinaryProtocolImpl.ReadI64: Int64;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200926var i64in : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200927begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200928 ReadAll( @i64in, SizeOf(i64in), 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200929 Result :=
930 (Int64( i64in[0] and $FF) shl 56) or
931 (Int64( i64in[1] and $FF) shl 48) or
932 (Int64( i64in[2] and $FF) shl 40) or
933 (Int64( i64in[3] and $FF) shl 32) or
934 (Int64( i64in[4] and $FF) shl 24) or
935 (Int64( i64in[5] and $FF) shl 16) or
936 (Int64( i64in[6] and $FF) shl 8) or
937 (Int64( i64in[7] and $FF));
938end;
939
Jens Geyer17c3ad92017-09-05 20:31:27 +0200940function TBinaryProtocolImpl.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200941begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200942 result.ElementType := TType(ReadByte);
943 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100944 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200945end;
946
947procedure TBinaryProtocolImpl.ReadListEnd;
948begin
949
950end;
951
Jens Geyer17c3ad92017-09-05 20:31:27 +0200952function TBinaryProtocolImpl.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200953begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200954 result.KeyType := TType(ReadByte);
955 result.ValueType := TType(ReadByte);
956 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100957 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200958end;
959
960procedure TBinaryProtocolImpl.ReadMapEnd;
961begin
962
963end;
964
Jens Geyer17c3ad92017-09-05 20:31:27 +0200965function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200966var
967 size : Integer;
968 version : Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200969begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100970 Reset;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200971 Init( result);
Jens Geyer589ee5b2021-03-29 21:40:55 +0200972
Jens Geyerd5436f52014-10-03 19:50:38 +0200973 size := ReadI32;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200974 if (size < 0) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200975 version := size and Integer( VERSION_MASK);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200976 if ( version <> Integer( VERSION_1)) then begin
Jens Geyere0e32402016-04-20 21:50:48 +0200977 raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) );
Jens Geyerd5436f52014-10-03 19:50:38 +0200978 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200979 result.Type_ := TMessageType( size and $000000ff);
980 result.Name := ReadString;
981 result.SeqID := ReadI32;
Jens Geyer589ee5b2021-03-29 21:40:55 +0200982 Exit;
983 end;
984
985 try
986 if FStrictRead
987 then raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' );
988
Jens Geyer17c3ad92017-09-05 20:31:27 +0200989 result.Name := ReadStringBody( size );
990 result.Type_ := TMessageType( ReadByte );
991 result.SeqID := ReadI32;
Jens Geyer589ee5b2021-03-29 21:40:55 +0200992 except
993 if CharUtils.IsHtmlDoctype(size)
994 then raise TProtocolExceptionInvalidData.Create('Remote end sends HTML instead of data')
995 else raise; // something else
Jens Geyerd5436f52014-10-03 19:50:38 +0200996 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200997end;
998
999procedure TBinaryProtocolImpl.ReadMessageEnd;
1000begin
1001 inherited;
1002
1003end;
1004
Jens Geyer17c3ad92017-09-05 20:31:27 +02001005function TBinaryProtocolImpl.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +02001006begin
Jens Geyer17c3ad92017-09-05 20:31:27 +02001007 result.ElementType := TType(ReadByte);
1008 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +01001009 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +02001010end;
1011
1012procedure TBinaryProtocolImpl.ReadSetEnd;
1013begin
1014
1015end;
1016
1017function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
Jens Geyerfad7fd32019-11-09 23:24:52 +01001018var buf : TBytes;
Jens Geyerd5436f52014-10-03 19:50:38 +02001019begin
Jens Geyer41f47af2019-11-09 23:24:52 +01001020 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001021 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +02001022 FTrans.ReadAll( buf, 0, size );
1023 Result := TEncoding.UTF8.GetString( buf);
1024end;
1025
Jens Geyer17c3ad92017-09-05 20:31:27 +02001026function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +02001027begin
Jens Geyer17c3ad92017-09-05 20:31:27 +02001028 Init( Result);
Jens Geyerd5436f52014-10-03 19:50:38 +02001029end;
1030
1031procedure TBinaryProtocolImpl.ReadStructEnd;
1032begin
1033 inherited;
1034
1035end;
1036
1037procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
1038var iLen : Integer;
1039begin
1040 iLen := Length(b);
1041 WriteI32( iLen);
1042 if iLen > 0 then FTrans.Write(b, 0, iLen);
1043end;
1044
1045procedure TBinaryProtocolImpl.WriteBool(b: Boolean);
1046begin
Jens Geyer17c3ad92017-09-05 20:31:27 +02001047 if b then begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001048 WriteByte( 1 );
Jens Geyer17c3ad92017-09-05 20:31:27 +02001049 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001050 WriteByte( 0 );
1051 end;
1052end;
1053
1054procedure TBinaryProtocolImpl.WriteByte(b: ShortInt);
Jens Geyerd5436f52014-10-03 19:50:38 +02001055begin
Jens Geyer17c3ad92017-09-05 20:31:27 +02001056 FTrans.Write( @b, 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +02001057end;
1058
1059procedure TBinaryProtocolImpl.WriteDouble( const d: Double);
1060begin
1061 WriteI64(ConvertDoubleToInt64(d));
1062end;
1063
Jens Geyer17c3ad92017-09-05 20:31:27 +02001064procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +02001065begin
1066 WriteByte(ShortInt(field.Type_));
1067 WriteI16(field.ID);
1068end;
1069
1070procedure TBinaryProtocolImpl.WriteFieldEnd;
1071begin
1072
1073end;
1074
1075procedure TBinaryProtocolImpl.WriteFieldStop;
1076begin
1077 WriteByte(ShortInt(TType.Stop));
1078end;
1079
1080procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt);
Jens Geyer17c3ad92017-09-05 20:31:27 +02001081var i16out : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +02001082begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001083 i16out[0] := Byte($FF and (i16 shr 8));
1084 i16out[1] := Byte($FF and i16);
Jens Geyer17c3ad92017-09-05 20:31:27 +02001085 FTrans.Write( @i16out, 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +02001086end;
1087
1088procedure TBinaryProtocolImpl.WriteI32(i32: Integer);
Jens Geyer17c3ad92017-09-05 20:31:27 +02001089var i32out : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +02001090begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001091 i32out[0] := Byte($FF and (i32 shr 24));
1092 i32out[1] := Byte($FF and (i32 shr 16));
1093 i32out[2] := Byte($FF and (i32 shr 8));
1094 i32out[3] := Byte($FF and i32);
Jens Geyer17c3ad92017-09-05 20:31:27 +02001095 FTrans.Write( @i32out, 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +02001096end;
1097
1098procedure TBinaryProtocolImpl.WriteI64( const i64: Int64);
Jens Geyer17c3ad92017-09-05 20:31:27 +02001099var i64out : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +02001100begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001101 i64out[0] := Byte($FF and (i64 shr 56));
1102 i64out[1] := Byte($FF and (i64 shr 48));
1103 i64out[2] := Byte($FF and (i64 shr 40));
1104 i64out[3] := Byte($FF and (i64 shr 32));
1105 i64out[4] := Byte($FF and (i64 shr 24));
1106 i64out[5] := Byte($FF and (i64 shr 16));
1107 i64out[6] := Byte($FF and (i64 shr 8));
1108 i64out[7] := Byte($FF and i64);
Jens Geyer17c3ad92017-09-05 20:31:27 +02001109 FTrans.Write( @i64out, 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +02001110end;
1111
Jens Geyer17c3ad92017-09-05 20:31:27 +02001112procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +02001113begin
1114 WriteByte(ShortInt(list.ElementType));
1115 WriteI32(list.Count);
1116end;
1117
1118procedure TBinaryProtocolImpl.WriteListEnd;
1119begin
1120
1121end;
1122
Jens Geyer17c3ad92017-09-05 20:31:27 +02001123procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +02001124begin
1125 WriteByte(ShortInt(map.KeyType));
1126 WriteByte(ShortInt(map.ValueType));
1127 WriteI32(map.Count);
1128end;
1129
1130procedure TBinaryProtocolImpl.WriteMapEnd;
1131begin
1132
1133end;
1134
Jens Geyer17c3ad92017-09-05 20:31:27 +02001135procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001136var version : Cardinal;
Jens Geyerd5436f52014-10-03 19:50:38 +02001137begin
Jens Geyer41f47af2019-11-09 23:24:52 +01001138 Reset;
Jens Geyerfad7fd32019-11-09 23:24:52 +01001139 if FStrictWrite then begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001140 version := VERSION_1 or Cardinal( msg.Type_);
1141 WriteI32( Integer( version) );
1142 WriteString( msg.Name);
1143 WriteI32( msg.SeqID);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001144 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001145 WriteString( msg.Name);
1146 WriteByte(ShortInt( msg.Type_));
1147 WriteI32( msg.SeqID);
1148 end;
1149end;
1150
1151procedure TBinaryProtocolImpl.WriteMessageEnd;
1152begin
1153
1154end;
1155
Jens Geyer17c3ad92017-09-05 20:31:27 +02001156procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet);
Jens Geyerd5436f52014-10-03 19:50:38 +02001157begin
1158 WriteByte(ShortInt(set_.ElementType));
1159 WriteI32(set_.Count);
1160end;
1161
1162procedure TBinaryProtocolImpl.WriteSetEnd;
1163begin
1164
1165end;
1166
Jens Geyer17c3ad92017-09-05 20:31:27 +02001167procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001168begin
1169
1170end;
1171
1172procedure TBinaryProtocolImpl.WriteStructEnd;
1173begin
1174
1175end;
1176
Jens Geyer41f47af2019-11-09 23:24:52 +01001177function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer;
1178// Return the minimum number of bytes a type will consume on the wire
1179begin
1180 case aType of
1181 TType.Stop: result := 0;
1182 TType.Void: result := 0;
1183 TType.Bool_: result := SizeOf(Byte);
1184 TType.Byte_: result := SizeOf(Byte);
1185 TType.Double_: result := SizeOf(Double);
1186 TType.I16: result := SizeOf(Int16);
1187 TType.I32: result := SizeOf(Int32);
1188 TType.I64: result := SizeOf(Int64);
1189 TType.String_: result := SizeOf(Int32); // string length
1190 TType.Struct: result := 0; // empty struct
1191 TType.Map: result := SizeOf(Int32); // element count
1192 TType.Set_: result := SizeOf(Int32); // element count
1193 TType.List: result := SizeOf(Int32); // element count
1194 else
1195 raise TTransportExceptionBadArgs.Create('Unhandled type code');
1196 end;
1197end;
1198
1199
Jens Geyerd5436f52014-10-03 19:50:38 +02001200{ TProtocolException }
1201
Jens Geyere0e32402016-04-20 21:50:48 +02001202constructor TProtocolException.HiddenCreate(const Msg: string);
Jens Geyerd5436f52014-10-03 19:50:38 +02001203begin
Jens Geyere0e32402016-04-20 21:50:48 +02001204 inherited Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001205end;
1206
Jens Geyere0e32402016-04-20 21:50:48 +02001207class function TProtocolException.Create(const Msg: string): TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001208begin
Jens Geyere0e32402016-04-20 21:50:48 +02001209 Result := TProtocolExceptionUnknown.Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001210end;
1211
Jens Geyere0e32402016-04-20 21:50:48 +02001212class function TProtocolException.Create: TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001213begin
Jens Geyere0e32402016-04-20 21:50:48 +02001214 Result := TProtocolExceptionUnknown.Create('');
1215end;
1216
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001217class function TProtocolException.Create(aType: TExceptionType): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001218begin
1219{$WARN SYMBOL_DEPRECATED OFF}
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001220 Result := Create(aType, '');
Jens Geyere0e32402016-04-20 21:50:48 +02001221{$WARN SYMBOL_DEPRECATED DEFAULT}
1222end;
1223
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001224class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001225begin
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001226 case aType of
1227 TExceptionType.INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg);
1228 TExceptionType.NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg);
1229 TExceptionType.SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg);
1230 TExceptionType.BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg);
1231 TExceptionType.NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg);
1232 TExceptionType.DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg);
Jens Geyere0e32402016-04-20 21:50:48 +02001233 else
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001234 ASSERT( TExceptionType.UNKNOWN = aType);
Jens Geyere0e32402016-04-20 21:50:48 +02001235 Result := TProtocolExceptionUnknown.Create(msg);
1236 end;
1237end;
1238
1239{ TProtocolExceptionSpecialized }
1240
1241constructor TProtocolExceptionSpecialized.Create(const Msg: string);
1242begin
1243 inherited HiddenCreate(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001244end;
1245
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001246{ specialized TProtocolExceptions }
1247
1248class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType;
1249begin
1250 result := TExceptionType.UNKNOWN;
1251end;
1252
1253class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType;
1254begin
1255 result := TExceptionType.INVALID_DATA;
1256end;
1257
1258class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType;
1259begin
1260 result := TExceptionType.NEGATIVE_SIZE;
1261end;
1262
1263class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType;
1264begin
1265 result := TExceptionType.SIZE_LIMIT;
1266end;
1267
1268class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType;
1269begin
1270 result := TExceptionType.BAD_VERSION;
1271end;
1272
1273class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType;
1274begin
1275 result := TExceptionType.NOT_IMPLEMENTED;
1276end;
1277
1278class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType;
1279begin
1280 result := TExceptionType.DEPTH_LIMIT;
1281end;
1282
Jens Geyerd5436f52014-10-03 19:50:38 +02001283{ TBinaryProtocolImpl.TFactory }
1284
Jens Geyerfad7fd32019-11-09 23:24:52 +01001285constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +02001286begin
1287 inherited Create;
1288 FStrictRead := AStrictRead;
1289 FStrictWrite := AStrictWrite;
1290end;
1291
Jens Geyerd5436f52014-10-03 19:50:38 +02001292function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol;
1293begin
1294 Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite);
1295end;
1296
1297
1298{ TProtocolDecorator }
1299
1300constructor TProtocolDecorator.Create( const aProtocol : IProtocol);
1301begin
1302 ASSERT( aProtocol <> nil);
1303 inherited Create( aProtocol.Transport);
1304 FWrappedProtocol := aProtocol;
1305end;
1306
1307
Jens Geyer17c3ad92017-09-05 20:31:27 +02001308procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerd5436f52014-10-03 19:50:38 +02001309begin
1310 FWrappedProtocol.WriteMessageBegin( msg);
1311end;
1312
1313
1314procedure TProtocolDecorator.WriteMessageEnd;
1315begin
1316 FWrappedProtocol.WriteMessageEnd;
1317end;
1318
1319
Jens Geyer17c3ad92017-09-05 20:31:27 +02001320procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001321begin
1322 FWrappedProtocol.WriteStructBegin( struc);
1323end;
1324
1325
1326procedure TProtocolDecorator.WriteStructEnd;
1327begin
1328 FWrappedProtocol.WriteStructEnd;
1329end;
1330
1331
Jens Geyer17c3ad92017-09-05 20:31:27 +02001332procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +02001333begin
1334 FWrappedProtocol.WriteFieldBegin( field);
1335end;
1336
1337
1338procedure TProtocolDecorator.WriteFieldEnd;
1339begin
1340 FWrappedProtocol.WriteFieldEnd;
1341end;
1342
1343
1344procedure TProtocolDecorator.WriteFieldStop;
1345begin
1346 FWrappedProtocol.WriteFieldStop;
1347end;
1348
1349
Jens Geyer17c3ad92017-09-05 20:31:27 +02001350procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +02001351begin
1352 FWrappedProtocol.WriteMapBegin( map);
1353end;
1354
1355
1356procedure TProtocolDecorator.WriteMapEnd;
1357begin
1358 FWrappedProtocol.WriteMapEnd;
1359end;
1360
1361
Jens Geyer17c3ad92017-09-05 20:31:27 +02001362procedure TProtocolDecorator.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +02001363begin
1364 FWrappedProtocol.WriteListBegin( list);
1365end;
1366
1367
1368procedure TProtocolDecorator.WriteListEnd();
1369begin
1370 FWrappedProtocol.WriteListEnd();
1371end;
1372
1373
Jens Geyer17c3ad92017-09-05 20:31:27 +02001374procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet );
Jens Geyerd5436f52014-10-03 19:50:38 +02001375begin
1376 FWrappedProtocol.WriteSetBegin( set_);
1377end;
1378
1379
1380procedure TProtocolDecorator.WriteSetEnd();
1381begin
1382 FWrappedProtocol.WriteSetEnd();
1383end;
1384
1385
1386procedure TProtocolDecorator.WriteBool( b: Boolean);
1387begin
1388 FWrappedProtocol.WriteBool( b);
1389end;
1390
1391
1392procedure TProtocolDecorator.WriteByte( b: ShortInt);
1393begin
1394 FWrappedProtocol.WriteByte( b);
1395end;
1396
1397
1398procedure TProtocolDecorator.WriteI16( i16: SmallInt);
1399begin
1400 FWrappedProtocol.WriteI16( i16);
1401end;
1402
1403
1404procedure TProtocolDecorator.WriteI32( i32: Integer);
1405begin
1406 FWrappedProtocol.WriteI32( i32);
1407end;
1408
1409
1410procedure TProtocolDecorator.WriteI64( const i64: Int64);
1411begin
1412 FWrappedProtocol.WriteI64( i64);
1413end;
1414
1415
1416procedure TProtocolDecorator.WriteDouble( const d: Double);
1417begin
1418 FWrappedProtocol.WriteDouble( d);
1419end;
1420
1421
1422procedure TProtocolDecorator.WriteString( const s: string );
1423begin
1424 FWrappedProtocol.WriteString( s);
1425end;
1426
1427
1428procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString);
1429begin
1430 FWrappedProtocol.WriteAnsiString( s);
1431end;
1432
1433
1434procedure TProtocolDecorator.WriteBinary( const b: TBytes);
1435begin
1436 FWrappedProtocol.WriteBinary( b);
1437end;
1438
1439
Jens Geyer17c3ad92017-09-05 20:31:27 +02001440function TProtocolDecorator.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +02001441begin
1442 result := FWrappedProtocol.ReadMessageBegin;
1443end;
1444
1445
1446procedure TProtocolDecorator.ReadMessageEnd();
1447begin
1448 FWrappedProtocol.ReadMessageEnd();
1449end;
1450
1451
Jens Geyer17c3ad92017-09-05 20:31:27 +02001452function TProtocolDecorator.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +02001453begin
1454 result := FWrappedProtocol.ReadStructBegin;
1455end;
1456
1457
1458procedure TProtocolDecorator.ReadStructEnd;
1459begin
1460 FWrappedProtocol.ReadStructEnd;
1461end;
1462
1463
Jens Geyer17c3ad92017-09-05 20:31:27 +02001464function TProtocolDecorator.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +02001465begin
1466 result := FWrappedProtocol.ReadFieldBegin;
1467end;
1468
1469
1470procedure TProtocolDecorator.ReadFieldEnd();
1471begin
1472 FWrappedProtocol.ReadFieldEnd();
1473end;
1474
1475
Jens Geyer17c3ad92017-09-05 20:31:27 +02001476function TProtocolDecorator.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +02001477begin
1478 result := FWrappedProtocol.ReadMapBegin;
1479end;
1480
1481
1482procedure TProtocolDecorator.ReadMapEnd();
1483begin
1484 FWrappedProtocol.ReadMapEnd();
1485end;
1486
1487
Jens Geyer17c3ad92017-09-05 20:31:27 +02001488function TProtocolDecorator.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +02001489begin
1490 result := FWrappedProtocol.ReadListBegin;
1491end;
1492
1493
1494procedure TProtocolDecorator.ReadListEnd();
1495begin
1496 FWrappedProtocol.ReadListEnd();
1497end;
1498
1499
Jens Geyer17c3ad92017-09-05 20:31:27 +02001500function TProtocolDecorator.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +02001501begin
1502 result := FWrappedProtocol.ReadSetBegin;
1503end;
1504
1505
1506procedure TProtocolDecorator.ReadSetEnd();
1507begin
1508 FWrappedProtocol.ReadSetEnd();
1509end;
1510
1511
1512function TProtocolDecorator.ReadBool: Boolean;
1513begin
1514 result := FWrappedProtocol.ReadBool;
1515end;
1516
1517
1518function TProtocolDecorator.ReadByte: ShortInt;
1519begin
1520 result := FWrappedProtocol.ReadByte;
1521end;
1522
1523
1524function TProtocolDecorator.ReadI16: SmallInt;
1525begin
1526 result := FWrappedProtocol.ReadI16;
1527end;
1528
1529
1530function TProtocolDecorator.ReadI32: Integer;
1531begin
1532 result := FWrappedProtocol.ReadI32;
1533end;
1534
1535
1536function TProtocolDecorator.ReadI64: Int64;
1537begin
1538 result := FWrappedProtocol.ReadI64;
1539end;
1540
1541
1542function TProtocolDecorator.ReadDouble:Double;
1543begin
1544 result := FWrappedProtocol.ReadDouble;
1545end;
1546
1547
1548function TProtocolDecorator.ReadBinary: TBytes;
1549begin
1550 result := FWrappedProtocol.ReadBinary;
1551end;
1552
1553
1554function TProtocolDecorator.ReadString: string;
1555begin
1556 result := FWrappedProtocol.ReadString;
1557end;
1558
1559
1560function TProtocolDecorator.ReadAnsiString: AnsiString;
1561begin
1562 result := FWrappedProtocol.ReadAnsiString;
1563end;
1564
1565
Jens Geyer41f47af2019-11-09 23:24:52 +01001566function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer;
1567begin
1568 result := FWrappedProtocol.GetMinSerializedSize(aType);
1569end;
1570
1571
Jens Geyer17c3ad92017-09-05 20:31:27 +02001572{ Init helper functions }
1573
1574procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer);
1575begin
1576 rec.Name := AName;
1577 rec.Type_ := AMessageType;
1578 rec.SeqID := ASeqID;
1579end;
1580
1581
1582procedure Init( var rec : TThriftStruct; const AName: string = '');
1583begin
1584 rec.Name := AName;
1585end;
1586
1587
1588procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt);
1589begin
1590 rec.Name := AName;
1591 rec.Type_ := AType;
1592 rec.Id := AId;
1593end;
1594
1595
1596procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer);
1597begin
1598 rec.ValueType := AValueType;
1599 rec.KeyType := AKeyType;
1600 rec.Count := ACount;
1601end;
1602
1603
1604procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer);
1605begin
1606 rec.Count := ACount;
1607 rec.ElementType := AElementType;
1608end;
1609
1610
1611procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer);
1612begin
1613 rec.Count := ACount;
1614 rec.ElementType := AElementType;
1615end;
1616
1617
1618
Jens Geyerd5436f52014-10-03 19:50:38 +02001619end.
1620