blob: aa12ad34fe22245e986aeae9b53a8070f42a9521 [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 Geyerd5436f52014-10-03 19:50:38 +0200196 IProtocol = interface
Jens Geyera019cda2019-11-09 23:24:52 +0100197 ['{F0040D99-937F-400D-9932-AF04F665899F}']
Jens Geyerd5436f52014-10-03 19:50:38 +0200198 function GetTransport: ITransport;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200199 procedure WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 procedure WriteMessageEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200201 procedure WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +0200202 procedure WriteStructEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200203 procedure WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 procedure WriteFieldEnd;
205 procedure WriteFieldStop;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200206 procedure WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +0200207 procedure WriteMapEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200208 procedure WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +0200209 procedure WriteListEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200210 procedure WriteSetBegin( const set_: TThriftSet );
Jens Geyerd5436f52014-10-03 19:50:38 +0200211 procedure WriteSetEnd();
212 procedure WriteBool( b: Boolean);
213 procedure WriteByte( b: ShortInt);
214 procedure WriteI16( i16: SmallInt);
215 procedure WriteI32( i32: Integer);
216 procedure WriteI64( const i64: Int64);
217 procedure WriteDouble( const d: Double);
218 procedure WriteString( const s: string );
219 procedure WriteAnsiString( const s: AnsiString);
220 procedure WriteBinary( const b: TBytes);
221
Jens Geyer17c3ad92017-09-05 20:31:27 +0200222 function ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200223 procedure ReadMessageEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200224 function ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +0200225 procedure ReadStructEnd;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200226 function ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200227 procedure ReadFieldEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200228 function ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 procedure ReadMapEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200230 function ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200231 procedure ReadListEnd();
Jens Geyer17c3ad92017-09-05 20:31:27 +0200232 function ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +0200233 procedure ReadSetEnd();
234 function ReadBool: Boolean;
235 function ReadByte: ShortInt;
236 function ReadI16: SmallInt;
237 function ReadI32: Integer;
238 function ReadI64: Int64;
239 function ReadDouble:Double;
240 function ReadBinary: TBytes;
241 function ReadString: string;
242 function ReadAnsiString: AnsiString;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200243
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200244 function NextRecursionLevel : IProtocolRecursionTracker;
245 procedure IncrementRecursionDepth;
246 procedure DecrementRecursionDepth;
Jens Geyer41f47af2019-11-09 23:24:52 +0100247 function GetMinSerializedSize( const aType : TType) : Integer;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200248
Jens Geyerd5436f52014-10-03 19:50:38 +0200249 property Transport: ITransport read GetTransport;
Jens Geyera019cda2019-11-09 23:24:52 +0100250 function Configuration : IThriftConfiguration;
Jens Geyerd5436f52014-10-03 19:50:38 +0200251 end;
252
253 TProtocolImpl = class abstract( TInterfacedObject, IProtocol)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100254 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200255 FTrans : ITransport;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200256 FRecursionLimit : Integer;
257 FRecursionDepth : Integer;
258
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200259 function NextRecursionLevel : IProtocolRecursionTracker;
260 procedure IncrementRecursionDepth;
261 procedure DecrementRecursionDepth;
262
Jens Geyer41f47af2019-11-09 23:24:52 +0100263 function GetMinSerializedSize( const aType : TType) : Integer; virtual; abstract;
264 procedure CheckReadBytesAvailable( const value : TThriftList); overload; inline;
265 procedure CheckReadBytesAvailable( const value : TThriftSet); overload; inline;
266 procedure CheckReadBytesAvailable( const value : TThriftMap); overload; inline;
267
268 procedure Reset; virtual;
Jens Geyera019cda2019-11-09 23:24:52 +0100269 function GetTransport: ITransport;
270 function Configuration : IThriftConfiguration;
271
Jens Geyer17c3ad92017-09-05 20:31:27 +0200272 procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200273 procedure WriteMessageEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200274 procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200275 procedure WriteStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200276 procedure WriteFieldBegin( const field: TThriftField); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200277 procedure WriteFieldEnd; virtual; abstract;
278 procedure WriteFieldStop; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200279 procedure WriteMapBegin( const map: TThriftMap); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200280 procedure WriteMapEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200281 procedure WriteListBegin( const list: TThriftList); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200282 procedure WriteListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200283 procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200284 procedure WriteSetEnd(); virtual; abstract;
285 procedure WriteBool( b: Boolean); virtual; abstract;
286 procedure WriteByte( b: ShortInt); virtual; abstract;
287 procedure WriteI16( i16: SmallInt); virtual; abstract;
288 procedure WriteI32( i32: Integer); virtual; abstract;
289 procedure WriteI64( const i64: Int64); virtual; abstract;
290 procedure WriteDouble( const d: Double); virtual; abstract;
291 procedure WriteString( const s: string ); virtual;
292 procedure WriteAnsiString( const s: AnsiString); virtual;
293 procedure WriteBinary( const b: TBytes); virtual; abstract;
294
Jens Geyer17c3ad92017-09-05 20:31:27 +0200295 function ReadMessageBegin: TThriftMessage; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200296 procedure ReadMessageEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200297 function ReadStructBegin: TThriftStruct; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200298 procedure ReadStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200299 function ReadFieldBegin: TThriftField; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 procedure ReadFieldEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200301 function ReadMapBegin: TThriftMap; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 procedure ReadMapEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200303 function ReadListBegin: TThriftList; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200304 procedure ReadListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200305 function ReadSetBegin: TThriftSet; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200306 procedure ReadSetEnd(); virtual; abstract;
307 function ReadBool: Boolean; virtual; abstract;
308 function ReadByte: ShortInt; virtual; abstract;
309 function ReadI16: SmallInt; virtual; abstract;
310 function ReadI32: Integer; virtual; abstract;
311 function ReadI64: Int64; virtual; abstract;
312 function ReadDouble:Double; virtual; abstract;
313 function ReadBinary: TBytes; virtual; abstract;
314 function ReadString: string; virtual;
315 function ReadAnsiString: AnsiString; virtual;
316
Jens Geyera019cda2019-11-09 23:24:52 +0100317 property Transport: ITransport read GetTransport;
Jens Geyerd5436f52014-10-03 19:50:38 +0200318
Jens Geyera019cda2019-11-09 23:24:52 +0100319 public
320 constructor Create( const aTransport : ITransport);
Jens Geyerd5436f52014-10-03 19:50:38 +0200321 end;
322
Jens Geyer8f7487e2019-05-09 22:21:32 +0200323 IBase = interface( ISupportsToString)
324 ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}']
Jens Geyerd5436f52014-10-03 19:50:38 +0200325 procedure Read( const iprot: IProtocol);
326 procedure Write( const iprot: IProtocol);
327 end;
328
Jens Geyerd5436f52014-10-03 19:50:38 +0200329
330 TBinaryProtocolImpl = class( TProtocolImpl )
Jens Geyerfad7fd32019-11-09 23:24:52 +0100331 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200332 const
333 VERSION_MASK : Cardinal = $ffff0000;
334 VERSION_1 : Cardinal = $80010000;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100335 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200336 FStrictRead : Boolean;
337 FStrictWrite : Boolean;
Jens Geyer41f47af2019-11-09 23:24:52 +0100338 function GetMinSerializedSize( const aType : TType) : Integer; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200339
Jens Geyerfad7fd32019-11-09 23:24:52 +0100340 strict private
Jens Geyer17c3ad92017-09-05 20:31:27 +0200341 function ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200342 function ReadStringBody( size: Integer): string;
343
344 public
Jens Geyerd5436f52014-10-03 19:50:38 +0200345 type
346 TFactory = class( TInterfacedObject, IProtocolFactory)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100347 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200348 FStrictRead : Boolean;
349 FStrictWrite : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 function GetProtocol( const trans: ITransport): IProtocol;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100351 public
352 constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200353 end;
354
Jens Geyerfad7fd32019-11-09 23:24:52 +0100355 constructor Create( const trans: ITransport; strictRead: Boolean = FALSE; strictWrite: Boolean = TRUE); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200356
Jens Geyer17c3ad92017-09-05 20:31:27 +0200357 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200358 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200359 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200360 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200361 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200362 procedure WriteFieldEnd; override;
363 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200364 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200365 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200366 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200367 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200368 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200369 procedure WriteSetEnd(); override;
370 procedure WriteBool( b: Boolean); override;
371 procedure WriteByte( b: ShortInt); override;
372 procedure WriteI16( i16: SmallInt); override;
373 procedure WriteI32( i32: Integer); override;
374 procedure WriteI64( const i64: Int64); override;
375 procedure WriteDouble( const d: Double); override;
376 procedure WriteBinary( const b: TBytes); override;
377
Jens Geyer17c3ad92017-09-05 20:31:27 +0200378 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200379 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200380 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200381 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200382 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200383 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200384 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200385 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200386 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200387 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200388 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200389 procedure ReadSetEnd(); override;
390 function ReadBool: Boolean; override;
391 function ReadByte: ShortInt; override;
392 function ReadI16: SmallInt; override;
393 function ReadI32: Integer; override;
394 function ReadI64: Int64; override;
395 function ReadDouble:Double; override;
396 function ReadBinary: TBytes; override;
397
398 end;
399
400
401 { TProtocolDecorator forwards all requests to an enclosed TProtocol instance,
402 providing a way to author concise concrete decorator subclasses. The decorator
403 does not (and should not) modify the behaviour of the enclosed TProtocol
404
405 See p.175 of Design Patterns (by Gamma et al.)
406 }
407 TProtocolDecorator = class( TProtocolImpl)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100408 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +0200409 FWrappedProtocol : IProtocol;
410
Jens Geyer41f47af2019-11-09 23:24:52 +0100411 strict protected
412 function GetMinSerializedSize( const aType : TType) : Integer; override;
413
Jens Geyerd5436f52014-10-03 19:50:38 +0200414 public
415 // Encloses the specified protocol.
416 // All operations will be forward to the given protocol. Must be non-null.
417 constructor Create( const aProtocol : IProtocol);
418
Jens Geyer17c3ad92017-09-05 20:31:27 +0200419 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200420 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200421 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200422 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200423 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200424 procedure WriteFieldEnd; override;
425 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200426 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200427 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200428 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200429 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200430 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200431 procedure WriteSetEnd(); override;
432 procedure WriteBool( b: Boolean); override;
433 procedure WriteByte( b: ShortInt); override;
434 procedure WriteI16( i16: SmallInt); override;
435 procedure WriteI32( i32: Integer); override;
436 procedure WriteI64( const i64: Int64); override;
437 procedure WriteDouble( const d: Double); override;
438 procedure WriteString( const s: string ); override;
439 procedure WriteAnsiString( const s: AnsiString); override;
440 procedure WriteBinary( const b: TBytes); override;
441
Jens Geyer17c3ad92017-09-05 20:31:27 +0200442 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200443 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200444 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200445 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200446 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200447 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200448 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200449 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200450 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200451 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200452 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200453 procedure ReadSetEnd(); override;
454 function ReadBool: Boolean; override;
455 function ReadByte: ShortInt; override;
456 function ReadI16: SmallInt; override;
457 function ReadI32: Integer; override;
458 function ReadI64: Int64; override;
459 function ReadDouble:Double; override;
460 function ReadBinary: TBytes; override;
461 function ReadString: string; override;
462 function ReadAnsiString: AnsiString; override;
463 end;
464
465
466type
467 IRequestEvents = interface
Jens Geyer01640402013-09-25 21:12:21 +0200468 ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}']
469 // Called before reading arguments.
470 procedure PreRead;
471 // Called between reading arguments and calling the handler.
472 procedure PostRead;
473 // Called between calling the handler and writing the response.
474 procedure PreWrite;
475 // Called after writing the response.
476 procedure PostWrite;
477 // Called when an oneway (async) function call completes successfully.
478 procedure OnewayComplete;
479 // Called if the handler throws an undeclared exception.
480 procedure UnhandledError( const e : Exception);
481 // Called when a client has finished request-handling to clean up
482 procedure CleanupContext;
483 end;
484
485
486 IProcessorEvents = interface
487 ['{A8661119-657C-447D-93C5-512E36162A45}']
488 // Called when a client is about to call the processor.
489 procedure Processing( const transport : ITransport);
490 // Called on any service function invocation
491 function CreateRequestContext( const aFunctionName : string) : IRequestEvents;
492 // Called when a client has finished request-handling to clean up
493 procedure CleanupContext;
494 end;
495
496
497 IProcessor = interface
498 ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}']
Jens Geyerd430bbd2013-09-26 23:37:54 +0200499 function Process( const iprot :IProtocol; const oprot: IProtocol; const events : IProcessorEvents = nil): Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200500 end;
501
Jens Geyerd5436f52014-10-03 19:50:38 +0200502
Jens Geyer17c3ad92017-09-05 20:31:27 +0200503procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline;
504procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline;
505procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline;
506procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
507procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
508procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
509
Jens Geyerd5436f52014-10-03 19:50:38 +0200510
511implementation
512
Jens Geyerfad7fd32019-11-09 23:24:52 +0100513function ConvertInt64ToDouble( const n: Int64): Double; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200514begin
515 ASSERT( SizeOf(n) = SizeOf(Result));
516 System.Move( n, Result, SizeOf(Result));
517end;
518
Jens Geyerfad7fd32019-11-09 23:24:52 +0100519function ConvertDoubleToInt64( const d: Double): Int64; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200520begin
521 ASSERT( SizeOf(d) = SizeOf(Result));
522 System.Move( d, Result, SizeOf(Result));
523end;
524
Jens Geyerd5436f52014-10-03 19:50:38 +0200525
Jens Geyerd5436f52014-10-03 19:50:38 +0200526
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200527{ TProtocolRecursionTrackerImpl }
528
529constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol);
530begin
531 inherited Create;
532
533 // storing the pointer *after* the (successful) increment is important here
534 prot.IncrementRecursionDepth;
535 FProtocol := prot;
536end;
537
538destructor TProtocolRecursionTrackerImpl.Destroy;
539begin
540 try
541 // we have to release the reference iff the pointer has been stored
542 if FProtocol <> nil then begin
543 FProtocol.DecrementRecursionDepth;
544 FProtocol := nil;
545 end;
546 finally
547 inherited Destroy;
548 end;
549end;
550
Jens Geyerd5436f52014-10-03 19:50:38 +0200551{ TProtocolImpl }
552
Jens Geyera019cda2019-11-09 23:24:52 +0100553constructor TProtocolImpl.Create( const aTransport : ITransport);
Jens Geyerd5436f52014-10-03 19:50:38 +0200554begin
555 inherited Create;
Jens Geyera019cda2019-11-09 23:24:52 +0100556 FTrans := aTransport;
557 FRecursionLimit := aTransport.Configuration.RecursionLimit;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200558 FRecursionDepth := 0;
559end;
560
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200561function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker;
562begin
563 result := TProtocolRecursionTrackerImpl.Create(Self);
564end;
565
566procedure TProtocolImpl.IncrementRecursionDepth;
567begin
568 if FRecursionDepth < FRecursionLimit
569 then Inc(FRecursionDepth)
Jens Geyere0e32402016-04-20 21:50:48 +0200570 else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded');
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200571end;
572
573procedure TProtocolImpl.DecrementRecursionDepth;
574begin
575 Dec(FRecursionDepth)
Jens Geyerd5436f52014-10-03 19:50:38 +0200576end;
577
578function TProtocolImpl.GetTransport: ITransport;
579begin
580 Result := FTrans;
581end;
582
Jens Geyera019cda2019-11-09 23:24:52 +0100583function TProtocolImpl.Configuration : IThriftConfiguration;
584begin
585 Result := FTrans.Configuration;
586end;
587
Jens Geyer41f47af2019-11-09 23:24:52 +0100588procedure TProtocolImpl.Reset;
589begin
Jens Geyera019cda2019-11-09 23:24:52 +0100590 FTrans.ResetConsumedMessageSize;
Jens Geyer41f47af2019-11-09 23:24:52 +0100591end;
592
Jens Geyerd5436f52014-10-03 19:50:38 +0200593function TProtocolImpl.ReadAnsiString: AnsiString;
594var
595 b : TBytes;
596 len : Integer;
597begin
598 Result := '';
599 b := ReadBinary;
600 len := Length( b );
Jens Geyerfad7fd32019-11-09 23:24:52 +0100601 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200602 SetLength( Result, len);
603 System.Move( b[0], Pointer(Result)^, len );
604 end;
605end;
606
607function TProtocolImpl.ReadString: string;
608begin
609 Result := TEncoding.UTF8.GetString( ReadBinary );
610end;
611
612procedure TProtocolImpl.WriteAnsiString(const s: AnsiString);
613var
614 b : TBytes;
615 len : Integer;
616begin
617 len := Length(s);
618 SetLength( b, len);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100619 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200620 System.Move( Pointer(s)^, b[0], len );
621 end;
622 WriteBinary( b );
623end;
624
625procedure TProtocolImpl.WriteString(const s: string);
626var
627 b : TBytes;
628begin
629 b := TEncoding.UTF8.GetBytes(s);
630 WriteBinary( b );
631end;
632
Jens Geyer41f47af2019-11-09 23:24:52 +0100633
634procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList);
635begin
636 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
637end;
638
639
640procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet);
641begin
642 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
643end;
644
645
646procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap);
Jens Geyera019cda2019-11-09 23:24:52 +0100647var nPairSize : Integer;
Jens Geyer41f47af2019-11-09 23:24:52 +0100648begin
649 nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType);
650 FTrans.CheckReadBytesAvailable( value.Count * nPairSize);
651end;
652
Jens Geyerd5436f52014-10-03 19:50:38 +0200653{ TProtocolUtil }
654
655class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200656var field : TThriftField;
657 map : TThriftMap;
658 set_ : TThriftSet;
659 list : TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200660 i : Integer;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200661 tracker : IProtocolRecursionTracker;
Jens Geyerd5436f52014-10-03 19:50:38 +0200662begin
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200663 tracker := prot.NextRecursionLevel;
Jens Geyerd5436f52014-10-03 19:50:38 +0200664 case type_ of
665 // simple types
666 TType.Bool_ : prot.ReadBool();
667 TType.Byte_ : prot.ReadByte();
668 TType.I16 : prot.ReadI16();
669 TType.I32 : prot.ReadI32();
670 TType.I64 : prot.ReadI64();
671 TType.Double_ : prot.ReadDouble();
672 TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it.
673
674 // structured types
675 TType.Struct : begin
676 prot.ReadStructBegin();
677 while TRUE do begin
678 field := prot.ReadFieldBegin();
679 if (field.Type_ = TType.Stop) then Break;
680 Skip(prot, field.Type_);
681 prot.ReadFieldEnd();
682 end;
683 prot.ReadStructEnd();
684 end;
685
686 TType.Map : begin
687 map := prot.ReadMapBegin();
688 for i := 0 to map.Count-1 do begin
689 Skip(prot, map.KeyType);
690 Skip(prot, map.ValueType);
691 end;
692 prot.ReadMapEnd();
693 end;
694
695 TType.Set_ : begin
696 set_ := prot.ReadSetBegin();
697 for i := 0 to set_.Count-1
698 do Skip( prot, set_.ElementType);
699 prot.ReadSetEnd();
700 end;
701
702 TType.List : begin
703 list := prot.ReadListBegin();
704 for i := 0 to list.Count-1
705 do Skip( prot, list.ElementType);
706 prot.ReadListEnd();
707 end;
708
709 else
Jens Geyer5f723cd2017-01-10 21:57:48 +0100710 raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_)));
Jens Geyerd5436f52014-10-03 19:50:38 +0200711 end;
712end;
713
Jens Geyerd5436f52014-10-03 19:50:38 +0200714
715{ TBinaryProtocolImpl }
716
Jens Geyerfad7fd32019-11-09 23:24:52 +0100717constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +0200718begin
Jens Geyerfad7fd32019-11-09 23:24:52 +0100719 inherited Create( trans);
Jens Geyerd5436f52014-10-03 19:50:38 +0200720 FStrictRead := strictRead;
721 FStrictWrite := strictWrite;
722end;
723
Jens Geyer17c3ad92017-09-05 20:31:27 +0200724function TBinaryProtocolImpl.ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200725begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200726 Result := FTrans.ReadAll( pBuf, buflen, off, len );
Jens Geyerd5436f52014-10-03 19:50:38 +0200727end;
728
729function TBinaryProtocolImpl.ReadBinary: TBytes;
730var
731 size : Integer;
732 buf : TBytes;
733begin
734 size := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100735 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100736 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200737 FTrans.ReadAll( buf, 0, size);
738 Result := buf;
739end;
740
741function TBinaryProtocolImpl.ReadBool: Boolean;
742begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200743 Result := (ReadByte = 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200744end;
745
746function TBinaryProtocolImpl.ReadByte: ShortInt;
Jens Geyerd5436f52014-10-03 19:50:38 +0200747begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200748 ReadAll( @result, SizeOf(result), 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200749end;
750
751function TBinaryProtocolImpl.ReadDouble: Double;
752begin
753 Result := ConvertInt64ToDouble( ReadI64 )
754end;
755
Jens Geyer17c3ad92017-09-05 20:31:27 +0200756function TBinaryProtocolImpl.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200757begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200758 Init( result, '', TType( ReadByte), 0);
759 if ( result.Type_ <> TType.Stop ) then begin
760 result.Id := ReadI16;
Jens Geyerd5436f52014-10-03 19:50:38 +0200761 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200762end;
763
764procedure TBinaryProtocolImpl.ReadFieldEnd;
765begin
766
767end;
768
769function TBinaryProtocolImpl.ReadI16: SmallInt;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200770var i16in : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200771begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200772 ReadAll( @i16in, Sizeof(i16in), 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200773 Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF));
774end;
775
776function TBinaryProtocolImpl.ReadI32: Integer;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200777var i32in : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200778begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200779 ReadAll( @i32in, SizeOf(i32in), 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200780
781 Result := Integer(
782 ((i32in[0] and $FF) shl 24) or
783 ((i32in[1] and $FF) shl 16) or
784 ((i32in[2] and $FF) shl 8) or
785 (i32in[3] and $FF));
786
787end;
788
789function TBinaryProtocolImpl.ReadI64: Int64;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200790var i64in : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200791begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200792 ReadAll( @i64in, SizeOf(i64in), 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200793 Result :=
794 (Int64( i64in[0] and $FF) shl 56) or
795 (Int64( i64in[1] and $FF) shl 48) or
796 (Int64( i64in[2] and $FF) shl 40) or
797 (Int64( i64in[3] and $FF) shl 32) or
798 (Int64( i64in[4] and $FF) shl 24) or
799 (Int64( i64in[5] and $FF) shl 16) or
800 (Int64( i64in[6] and $FF) shl 8) or
801 (Int64( i64in[7] and $FF));
802end;
803
Jens Geyer17c3ad92017-09-05 20:31:27 +0200804function TBinaryProtocolImpl.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200805begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200806 result.ElementType := TType(ReadByte);
807 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100808 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200809end;
810
811procedure TBinaryProtocolImpl.ReadListEnd;
812begin
813
814end;
815
Jens Geyer17c3ad92017-09-05 20:31:27 +0200816function TBinaryProtocolImpl.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200817begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200818 result.KeyType := TType(ReadByte);
819 result.ValueType := 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.ReadMapEnd;
825begin
826
827end;
828
Jens Geyer17c3ad92017-09-05 20:31:27 +0200829function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200830var
831 size : Integer;
832 version : Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200833begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100834 Reset;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200835 Init( result);
Jens Geyer589ee5b2021-03-29 21:40:55 +0200836
Jens Geyerd5436f52014-10-03 19:50:38 +0200837 size := ReadI32;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200838 if (size < 0) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200839 version := size and Integer( VERSION_MASK);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200840 if ( version <> Integer( VERSION_1)) then begin
Jens Geyere0e32402016-04-20 21:50:48 +0200841 raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) );
Jens Geyerd5436f52014-10-03 19:50:38 +0200842 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200843 result.Type_ := TMessageType( size and $000000ff);
844 result.Name := ReadString;
845 result.SeqID := ReadI32;
Jens Geyer589ee5b2021-03-29 21:40:55 +0200846 Exit;
847 end;
848
849 try
850 if FStrictRead
851 then raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' );
852
Jens Geyer17c3ad92017-09-05 20:31:27 +0200853 result.Name := ReadStringBody( size );
854 result.Type_ := TMessageType( ReadByte );
855 result.SeqID := ReadI32;
Jens Geyer589ee5b2021-03-29 21:40:55 +0200856 except
857 if CharUtils.IsHtmlDoctype(size)
858 then raise TProtocolExceptionInvalidData.Create('Remote end sends HTML instead of data')
859 else raise; // something else
Jens Geyerd5436f52014-10-03 19:50:38 +0200860 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200861end;
862
863procedure TBinaryProtocolImpl.ReadMessageEnd;
864begin
865 inherited;
866
867end;
868
Jens Geyer17c3ad92017-09-05 20:31:27 +0200869function TBinaryProtocolImpl.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +0200870begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200871 result.ElementType := TType(ReadByte);
872 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100873 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200874end;
875
876procedure TBinaryProtocolImpl.ReadSetEnd;
877begin
878
879end;
880
881function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100882var buf : TBytes;
Jens Geyerd5436f52014-10-03 19:50:38 +0200883begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100884 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100885 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200886 FTrans.ReadAll( buf, 0, size );
887 Result := TEncoding.UTF8.GetString( buf);
888end;
889
Jens Geyer17c3ad92017-09-05 20:31:27 +0200890function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +0200891begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200892 Init( Result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200893end;
894
895procedure TBinaryProtocolImpl.ReadStructEnd;
896begin
897 inherited;
898
899end;
900
901procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
902var iLen : Integer;
903begin
904 iLen := Length(b);
905 WriteI32( iLen);
906 if iLen > 0 then FTrans.Write(b, 0, iLen);
907end;
908
909procedure TBinaryProtocolImpl.WriteBool(b: Boolean);
910begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200911 if b then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200912 WriteByte( 1 );
Jens Geyer17c3ad92017-09-05 20:31:27 +0200913 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200914 WriteByte( 0 );
915 end;
916end;
917
918procedure TBinaryProtocolImpl.WriteByte(b: ShortInt);
Jens Geyerd5436f52014-10-03 19:50:38 +0200919begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200920 FTrans.Write( @b, 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200921end;
922
923procedure TBinaryProtocolImpl.WriteDouble( const d: Double);
924begin
925 WriteI64(ConvertDoubleToInt64(d));
926end;
927
Jens Geyer17c3ad92017-09-05 20:31:27 +0200928procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +0200929begin
930 WriteByte(ShortInt(field.Type_));
931 WriteI16(field.ID);
932end;
933
934procedure TBinaryProtocolImpl.WriteFieldEnd;
935begin
936
937end;
938
939procedure TBinaryProtocolImpl.WriteFieldStop;
940begin
941 WriteByte(ShortInt(TType.Stop));
942end;
943
944procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200945var i16out : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200946begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200947 i16out[0] := Byte($FF and (i16 shr 8));
948 i16out[1] := Byte($FF and i16);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200949 FTrans.Write( @i16out, 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200950end;
951
952procedure TBinaryProtocolImpl.WriteI32(i32: Integer);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200953var i32out : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200954begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200955 i32out[0] := Byte($FF and (i32 shr 24));
956 i32out[1] := Byte($FF and (i32 shr 16));
957 i32out[2] := Byte($FF and (i32 shr 8));
958 i32out[3] := Byte($FF and i32);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200959 FTrans.Write( @i32out, 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200960end;
961
962procedure TBinaryProtocolImpl.WriteI64( const i64: Int64);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200963var i64out : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200964begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200965 i64out[0] := Byte($FF and (i64 shr 56));
966 i64out[1] := Byte($FF and (i64 shr 48));
967 i64out[2] := Byte($FF and (i64 shr 40));
968 i64out[3] := Byte($FF and (i64 shr 32));
969 i64out[4] := Byte($FF and (i64 shr 24));
970 i64out[5] := Byte($FF and (i64 shr 16));
971 i64out[6] := Byte($FF and (i64 shr 8));
972 i64out[7] := Byte($FF and i64);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200973 FTrans.Write( @i64out, 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200974end;
975
Jens Geyer17c3ad92017-09-05 20:31:27 +0200976procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +0200977begin
978 WriteByte(ShortInt(list.ElementType));
979 WriteI32(list.Count);
980end;
981
982procedure TBinaryProtocolImpl.WriteListEnd;
983begin
984
985end;
986
Jens Geyer17c3ad92017-09-05 20:31:27 +0200987procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +0200988begin
989 WriteByte(ShortInt(map.KeyType));
990 WriteByte(ShortInt(map.ValueType));
991 WriteI32(map.Count);
992end;
993
994procedure TBinaryProtocolImpl.WriteMapEnd;
995begin
996
997end;
998
Jens Geyer17c3ad92017-09-05 20:31:27 +0200999procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001000var version : Cardinal;
Jens Geyerd5436f52014-10-03 19:50:38 +02001001begin
Jens Geyer41f47af2019-11-09 23:24:52 +01001002 Reset;
Jens Geyerfad7fd32019-11-09 23:24:52 +01001003 if FStrictWrite then begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001004 version := VERSION_1 or Cardinal( msg.Type_);
1005 WriteI32( Integer( version) );
1006 WriteString( msg.Name);
1007 WriteI32( msg.SeqID);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001008 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001009 WriteString( msg.Name);
1010 WriteByte(ShortInt( msg.Type_));
1011 WriteI32( msg.SeqID);
1012 end;
1013end;
1014
1015procedure TBinaryProtocolImpl.WriteMessageEnd;
1016begin
1017
1018end;
1019
Jens Geyer17c3ad92017-09-05 20:31:27 +02001020procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet);
Jens Geyerd5436f52014-10-03 19:50:38 +02001021begin
1022 WriteByte(ShortInt(set_.ElementType));
1023 WriteI32(set_.Count);
1024end;
1025
1026procedure TBinaryProtocolImpl.WriteSetEnd;
1027begin
1028
1029end;
1030
Jens Geyer17c3ad92017-09-05 20:31:27 +02001031procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001032begin
1033
1034end;
1035
1036procedure TBinaryProtocolImpl.WriteStructEnd;
1037begin
1038
1039end;
1040
Jens Geyer41f47af2019-11-09 23:24:52 +01001041function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer;
1042// Return the minimum number of bytes a type will consume on the wire
1043begin
1044 case aType of
1045 TType.Stop: result := 0;
1046 TType.Void: result := 0;
1047 TType.Bool_: result := SizeOf(Byte);
1048 TType.Byte_: result := SizeOf(Byte);
1049 TType.Double_: result := SizeOf(Double);
1050 TType.I16: result := SizeOf(Int16);
1051 TType.I32: result := SizeOf(Int32);
1052 TType.I64: result := SizeOf(Int64);
1053 TType.String_: result := SizeOf(Int32); // string length
1054 TType.Struct: result := 0; // empty struct
1055 TType.Map: result := SizeOf(Int32); // element count
1056 TType.Set_: result := SizeOf(Int32); // element count
1057 TType.List: result := SizeOf(Int32); // element count
1058 else
1059 raise TTransportExceptionBadArgs.Create('Unhandled type code');
1060 end;
1061end;
1062
1063
Jens Geyerd5436f52014-10-03 19:50:38 +02001064{ TProtocolException }
1065
Jens Geyere0e32402016-04-20 21:50:48 +02001066constructor TProtocolException.HiddenCreate(const Msg: string);
Jens Geyerd5436f52014-10-03 19:50:38 +02001067begin
Jens Geyere0e32402016-04-20 21:50:48 +02001068 inherited Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001069end;
1070
Jens Geyere0e32402016-04-20 21:50:48 +02001071class function TProtocolException.Create(const Msg: string): TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001072begin
Jens Geyere0e32402016-04-20 21:50:48 +02001073 Result := TProtocolExceptionUnknown.Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001074end;
1075
Jens Geyere0e32402016-04-20 21:50:48 +02001076class function TProtocolException.Create: TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001077begin
Jens Geyere0e32402016-04-20 21:50:48 +02001078 Result := TProtocolExceptionUnknown.Create('');
1079end;
1080
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001081class function TProtocolException.Create(aType: TExceptionType): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001082begin
1083{$WARN SYMBOL_DEPRECATED OFF}
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001084 Result := Create(aType, '');
Jens Geyere0e32402016-04-20 21:50:48 +02001085{$WARN SYMBOL_DEPRECATED DEFAULT}
1086end;
1087
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001088class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001089begin
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001090 case aType of
1091 TExceptionType.INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg);
1092 TExceptionType.NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg);
1093 TExceptionType.SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg);
1094 TExceptionType.BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg);
1095 TExceptionType.NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg);
1096 TExceptionType.DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg);
Jens Geyere0e32402016-04-20 21:50:48 +02001097 else
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001098 ASSERT( TExceptionType.UNKNOWN = aType);
Jens Geyere0e32402016-04-20 21:50:48 +02001099 Result := TProtocolExceptionUnknown.Create(msg);
1100 end;
1101end;
1102
1103{ TProtocolExceptionSpecialized }
1104
1105constructor TProtocolExceptionSpecialized.Create(const Msg: string);
1106begin
1107 inherited HiddenCreate(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001108end;
1109
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001110{ specialized TProtocolExceptions }
1111
1112class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType;
1113begin
1114 result := TExceptionType.UNKNOWN;
1115end;
1116
1117class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType;
1118begin
1119 result := TExceptionType.INVALID_DATA;
1120end;
1121
1122class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType;
1123begin
1124 result := TExceptionType.NEGATIVE_SIZE;
1125end;
1126
1127class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType;
1128begin
1129 result := TExceptionType.SIZE_LIMIT;
1130end;
1131
1132class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType;
1133begin
1134 result := TExceptionType.BAD_VERSION;
1135end;
1136
1137class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType;
1138begin
1139 result := TExceptionType.NOT_IMPLEMENTED;
1140end;
1141
1142class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType;
1143begin
1144 result := TExceptionType.DEPTH_LIMIT;
1145end;
1146
Jens Geyerd5436f52014-10-03 19:50:38 +02001147{ TBinaryProtocolImpl.TFactory }
1148
Jens Geyerfad7fd32019-11-09 23:24:52 +01001149constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +02001150begin
1151 inherited Create;
1152 FStrictRead := AStrictRead;
1153 FStrictWrite := AStrictWrite;
1154end;
1155
Jens Geyerd5436f52014-10-03 19:50:38 +02001156function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol;
1157begin
1158 Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite);
1159end;
1160
1161
1162{ TProtocolDecorator }
1163
1164constructor TProtocolDecorator.Create( const aProtocol : IProtocol);
1165begin
1166 ASSERT( aProtocol <> nil);
1167 inherited Create( aProtocol.Transport);
1168 FWrappedProtocol := aProtocol;
1169end;
1170
1171
Jens Geyer17c3ad92017-09-05 20:31:27 +02001172procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerd5436f52014-10-03 19:50:38 +02001173begin
1174 FWrappedProtocol.WriteMessageBegin( msg);
1175end;
1176
1177
1178procedure TProtocolDecorator.WriteMessageEnd;
1179begin
1180 FWrappedProtocol.WriteMessageEnd;
1181end;
1182
1183
Jens Geyer17c3ad92017-09-05 20:31:27 +02001184procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001185begin
1186 FWrappedProtocol.WriteStructBegin( struc);
1187end;
1188
1189
1190procedure TProtocolDecorator.WriteStructEnd;
1191begin
1192 FWrappedProtocol.WriteStructEnd;
1193end;
1194
1195
Jens Geyer17c3ad92017-09-05 20:31:27 +02001196procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +02001197begin
1198 FWrappedProtocol.WriteFieldBegin( field);
1199end;
1200
1201
1202procedure TProtocolDecorator.WriteFieldEnd;
1203begin
1204 FWrappedProtocol.WriteFieldEnd;
1205end;
1206
1207
1208procedure TProtocolDecorator.WriteFieldStop;
1209begin
1210 FWrappedProtocol.WriteFieldStop;
1211end;
1212
1213
Jens Geyer17c3ad92017-09-05 20:31:27 +02001214procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +02001215begin
1216 FWrappedProtocol.WriteMapBegin( map);
1217end;
1218
1219
1220procedure TProtocolDecorator.WriteMapEnd;
1221begin
1222 FWrappedProtocol.WriteMapEnd;
1223end;
1224
1225
Jens Geyer17c3ad92017-09-05 20:31:27 +02001226procedure TProtocolDecorator.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +02001227begin
1228 FWrappedProtocol.WriteListBegin( list);
1229end;
1230
1231
1232procedure TProtocolDecorator.WriteListEnd();
1233begin
1234 FWrappedProtocol.WriteListEnd();
1235end;
1236
1237
Jens Geyer17c3ad92017-09-05 20:31:27 +02001238procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet );
Jens Geyerd5436f52014-10-03 19:50:38 +02001239begin
1240 FWrappedProtocol.WriteSetBegin( set_);
1241end;
1242
1243
1244procedure TProtocolDecorator.WriteSetEnd();
1245begin
1246 FWrappedProtocol.WriteSetEnd();
1247end;
1248
1249
1250procedure TProtocolDecorator.WriteBool( b: Boolean);
1251begin
1252 FWrappedProtocol.WriteBool( b);
1253end;
1254
1255
1256procedure TProtocolDecorator.WriteByte( b: ShortInt);
1257begin
1258 FWrappedProtocol.WriteByte( b);
1259end;
1260
1261
1262procedure TProtocolDecorator.WriteI16( i16: SmallInt);
1263begin
1264 FWrappedProtocol.WriteI16( i16);
1265end;
1266
1267
1268procedure TProtocolDecorator.WriteI32( i32: Integer);
1269begin
1270 FWrappedProtocol.WriteI32( i32);
1271end;
1272
1273
1274procedure TProtocolDecorator.WriteI64( const i64: Int64);
1275begin
1276 FWrappedProtocol.WriteI64( i64);
1277end;
1278
1279
1280procedure TProtocolDecorator.WriteDouble( const d: Double);
1281begin
1282 FWrappedProtocol.WriteDouble( d);
1283end;
1284
1285
1286procedure TProtocolDecorator.WriteString( const s: string );
1287begin
1288 FWrappedProtocol.WriteString( s);
1289end;
1290
1291
1292procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString);
1293begin
1294 FWrappedProtocol.WriteAnsiString( s);
1295end;
1296
1297
1298procedure TProtocolDecorator.WriteBinary( const b: TBytes);
1299begin
1300 FWrappedProtocol.WriteBinary( b);
1301end;
1302
1303
Jens Geyer17c3ad92017-09-05 20:31:27 +02001304function TProtocolDecorator.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +02001305begin
1306 result := FWrappedProtocol.ReadMessageBegin;
1307end;
1308
1309
1310procedure TProtocolDecorator.ReadMessageEnd();
1311begin
1312 FWrappedProtocol.ReadMessageEnd();
1313end;
1314
1315
Jens Geyer17c3ad92017-09-05 20:31:27 +02001316function TProtocolDecorator.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +02001317begin
1318 result := FWrappedProtocol.ReadStructBegin;
1319end;
1320
1321
1322procedure TProtocolDecorator.ReadStructEnd;
1323begin
1324 FWrappedProtocol.ReadStructEnd;
1325end;
1326
1327
Jens Geyer17c3ad92017-09-05 20:31:27 +02001328function TProtocolDecorator.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +02001329begin
1330 result := FWrappedProtocol.ReadFieldBegin;
1331end;
1332
1333
1334procedure TProtocolDecorator.ReadFieldEnd();
1335begin
1336 FWrappedProtocol.ReadFieldEnd();
1337end;
1338
1339
Jens Geyer17c3ad92017-09-05 20:31:27 +02001340function TProtocolDecorator.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +02001341begin
1342 result := FWrappedProtocol.ReadMapBegin;
1343end;
1344
1345
1346procedure TProtocolDecorator.ReadMapEnd();
1347begin
1348 FWrappedProtocol.ReadMapEnd();
1349end;
1350
1351
Jens Geyer17c3ad92017-09-05 20:31:27 +02001352function TProtocolDecorator.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +02001353begin
1354 result := FWrappedProtocol.ReadListBegin;
1355end;
1356
1357
1358procedure TProtocolDecorator.ReadListEnd();
1359begin
1360 FWrappedProtocol.ReadListEnd();
1361end;
1362
1363
Jens Geyer17c3ad92017-09-05 20:31:27 +02001364function TProtocolDecorator.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +02001365begin
1366 result := FWrappedProtocol.ReadSetBegin;
1367end;
1368
1369
1370procedure TProtocolDecorator.ReadSetEnd();
1371begin
1372 FWrappedProtocol.ReadSetEnd();
1373end;
1374
1375
1376function TProtocolDecorator.ReadBool: Boolean;
1377begin
1378 result := FWrappedProtocol.ReadBool;
1379end;
1380
1381
1382function TProtocolDecorator.ReadByte: ShortInt;
1383begin
1384 result := FWrappedProtocol.ReadByte;
1385end;
1386
1387
1388function TProtocolDecorator.ReadI16: SmallInt;
1389begin
1390 result := FWrappedProtocol.ReadI16;
1391end;
1392
1393
1394function TProtocolDecorator.ReadI32: Integer;
1395begin
1396 result := FWrappedProtocol.ReadI32;
1397end;
1398
1399
1400function TProtocolDecorator.ReadI64: Int64;
1401begin
1402 result := FWrappedProtocol.ReadI64;
1403end;
1404
1405
1406function TProtocolDecorator.ReadDouble:Double;
1407begin
1408 result := FWrappedProtocol.ReadDouble;
1409end;
1410
1411
1412function TProtocolDecorator.ReadBinary: TBytes;
1413begin
1414 result := FWrappedProtocol.ReadBinary;
1415end;
1416
1417
1418function TProtocolDecorator.ReadString: string;
1419begin
1420 result := FWrappedProtocol.ReadString;
1421end;
1422
1423
1424function TProtocolDecorator.ReadAnsiString: AnsiString;
1425begin
1426 result := FWrappedProtocol.ReadAnsiString;
1427end;
1428
1429
Jens Geyer41f47af2019-11-09 23:24:52 +01001430function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer;
1431begin
1432 result := FWrappedProtocol.GetMinSerializedSize(aType);
1433end;
1434
1435
Jens Geyer17c3ad92017-09-05 20:31:27 +02001436{ Init helper functions }
1437
1438procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer);
1439begin
1440 rec.Name := AName;
1441 rec.Type_ := AMessageType;
1442 rec.SeqID := ASeqID;
1443end;
1444
1445
1446procedure Init( var rec : TThriftStruct; const AName: string = '');
1447begin
1448 rec.Name := AName;
1449end;
1450
1451
1452procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt);
1453begin
1454 rec.Name := AName;
1455 rec.Type_ := AType;
1456 rec.Id := AId;
1457end;
1458
1459
1460procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer);
1461begin
1462 rec.ValueType := AValueType;
1463 rec.KeyType := AKeyType;
1464 rec.Count := ACount;
1465end;
1466
1467
1468procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer);
1469begin
1470 rec.Count := ACount;
1471 rec.ElementType := AElementType;
1472end;
1473
1474
1475procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer);
1476begin
1477 rec.Count := ACount;
1478 rec.ElementType := AElementType;
1479end;
1480
1481
1482
Jens Geyerd5436f52014-10-03 19:50:38 +02001483end.
1484