blob: 03cc37182ee7e001975aa9cb706801964c99ac2c [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
Jens Geyer3b686532021-07-01 23:04:08 +0200253 TProtocolImplClass = class of TProtocolImpl;
254
Jens Geyerd5436f52014-10-03 19:50:38 +0200255 TProtocolImpl = class abstract( TInterfacedObject, IProtocol)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100256 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200257 FTrans : ITransport;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200258 FRecursionLimit : Integer;
259 FRecursionDepth : Integer;
260
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200261 function NextRecursionLevel : IProtocolRecursionTracker;
262 procedure IncrementRecursionDepth;
263 procedure DecrementRecursionDepth;
264
Jens Geyer41f47af2019-11-09 23:24:52 +0100265 function GetMinSerializedSize( const aType : TType) : Integer; virtual; abstract;
266 procedure CheckReadBytesAvailable( const value : TThriftList); overload; inline;
267 procedure CheckReadBytesAvailable( const value : TThriftSet); overload; inline;
268 procedure CheckReadBytesAvailable( const value : TThriftMap); overload; inline;
269
270 procedure Reset; virtual;
Jens Geyera019cda2019-11-09 23:24:52 +0100271 function GetTransport: ITransport;
272 function Configuration : IThriftConfiguration;
273
Jens Geyer17c3ad92017-09-05 20:31:27 +0200274 procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200275 procedure WriteMessageEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200276 procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200277 procedure WriteStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200278 procedure WriteFieldBegin( const field: TThriftField); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200279 procedure WriteFieldEnd; virtual; abstract;
280 procedure WriteFieldStop; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200281 procedure WriteMapBegin( const map: TThriftMap); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200282 procedure WriteMapEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200283 procedure WriteListBegin( const list: TThriftList); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200284 procedure WriteListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200285 procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200286 procedure WriteSetEnd(); virtual; abstract;
287 procedure WriteBool( b: Boolean); virtual; abstract;
288 procedure WriteByte( b: ShortInt); virtual; abstract;
289 procedure WriteI16( i16: SmallInt); virtual; abstract;
290 procedure WriteI32( i32: Integer); virtual; abstract;
291 procedure WriteI64( const i64: Int64); virtual; abstract;
292 procedure WriteDouble( const d: Double); virtual; abstract;
293 procedure WriteString( const s: string ); virtual;
294 procedure WriteAnsiString( const s: AnsiString); virtual;
295 procedure WriteBinary( const b: TBytes); virtual; abstract;
296
Jens Geyer17c3ad92017-09-05 20:31:27 +0200297 function ReadMessageBegin: TThriftMessage; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200298 procedure ReadMessageEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200299 function ReadStructBegin: TThriftStruct; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200300 procedure ReadStructEnd; virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200301 function ReadFieldBegin: TThriftField; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200302 procedure ReadFieldEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200303 function ReadMapBegin: TThriftMap; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200304 procedure ReadMapEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200305 function ReadListBegin: TThriftList; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200306 procedure ReadListEnd(); virtual; abstract;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200307 function ReadSetBegin: TThriftSet; virtual; abstract;
Jens Geyerd5436f52014-10-03 19:50:38 +0200308 procedure ReadSetEnd(); virtual; abstract;
309 function ReadBool: Boolean; virtual; abstract;
310 function ReadByte: ShortInt; virtual; abstract;
311 function ReadI16: SmallInt; virtual; abstract;
312 function ReadI32: Integer; virtual; abstract;
313 function ReadI64: Int64; virtual; abstract;
314 function ReadDouble:Double; virtual; abstract;
315 function ReadBinary: TBytes; virtual; abstract;
316 function ReadString: string; virtual;
317 function ReadAnsiString: AnsiString; virtual;
318
Jens Geyera019cda2019-11-09 23:24:52 +0100319 property Transport: ITransport read GetTransport;
Jens Geyerd5436f52014-10-03 19:50:38 +0200320
Jens Geyera019cda2019-11-09 23:24:52 +0100321 public
Jens Geyer3b686532021-07-01 23:04:08 +0200322 constructor Create( const aTransport : ITransport); virtual;
Jens Geyerd5436f52014-10-03 19:50:38 +0200323 end;
324
Jens Geyer8f7487e2019-05-09 22:21:32 +0200325 IBase = interface( ISupportsToString)
326 ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}']
Jens Geyerd5436f52014-10-03 19:50:38 +0200327 procedure Read( const iprot: IProtocol);
328 procedure Write( const iprot: IProtocol);
329 end;
330
Jens Geyerd5436f52014-10-03 19:50:38 +0200331
332 TBinaryProtocolImpl = class( TProtocolImpl )
Jens Geyerfad7fd32019-11-09 23:24:52 +0100333 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200334 const
335 VERSION_MASK : Cardinal = $ffff0000;
336 VERSION_1 : Cardinal = $80010000;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100337 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200338 FStrictRead : Boolean;
339 FStrictWrite : Boolean;
Jens Geyer41f47af2019-11-09 23:24:52 +0100340 function GetMinSerializedSize( const aType : TType) : Integer; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200341
Jens Geyerfad7fd32019-11-09 23:24:52 +0100342 strict private
Jens Geyer17c3ad92017-09-05 20:31:27 +0200343 function ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200344 function ReadStringBody( size: Integer): string;
345
346 public
Jens Geyerd5436f52014-10-03 19:50:38 +0200347 type
348 TFactory = class( TInterfacedObject, IProtocolFactory)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100349 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 FStrictRead : Boolean;
351 FStrictWrite : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200352 function GetProtocol( const trans: ITransport): IProtocol;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100353 public
354 constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200355 end;
356
Jens Geyer3b686532021-07-01 23:04:08 +0200357 constructor Create( const trans: ITransport); overload; override;
358 constructor Create( const trans: ITransport; strictRead, strictWrite: Boolean); reintroduce; overload;
Jens Geyerd5436f52014-10-03 19:50:38 +0200359
Jens Geyer17c3ad92017-09-05 20:31:27 +0200360 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200361 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200362 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200363 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200364 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200365 procedure WriteFieldEnd; override;
366 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200367 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200368 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200369 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200370 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200371 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200372 procedure WriteSetEnd(); override;
373 procedure WriteBool( b: Boolean); override;
374 procedure WriteByte( b: ShortInt); override;
375 procedure WriteI16( i16: SmallInt); override;
376 procedure WriteI32( i32: Integer); override;
377 procedure WriteI64( const i64: Int64); override;
378 procedure WriteDouble( const d: Double); override;
379 procedure WriteBinary( const b: TBytes); override;
380
Jens Geyer17c3ad92017-09-05 20:31:27 +0200381 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200382 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200383 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200384 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200385 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200386 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200387 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200388 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200389 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200390 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200391 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200392 procedure ReadSetEnd(); override;
393 function ReadBool: Boolean; override;
394 function ReadByte: ShortInt; override;
395 function ReadI16: SmallInt; override;
396 function ReadI32: Integer; override;
397 function ReadI64: Int64; override;
398 function ReadDouble:Double; override;
399 function ReadBinary: TBytes; override;
400
401 end;
402
403
404 { TProtocolDecorator forwards all requests to an enclosed TProtocol instance,
405 providing a way to author concise concrete decorator subclasses. The decorator
406 does not (and should not) modify the behaviour of the enclosed TProtocol
407
408 See p.175 of Design Patterns (by Gamma et al.)
409 }
410 TProtocolDecorator = class( TProtocolImpl)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100411 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +0200412 FWrappedProtocol : IProtocol;
413
Jens Geyer41f47af2019-11-09 23:24:52 +0100414 strict protected
415 function GetMinSerializedSize( const aType : TType) : Integer; override;
416
Jens Geyerd5436f52014-10-03 19:50:38 +0200417 public
418 // Encloses the specified protocol.
419 // All operations will be forward to the given protocol. Must be non-null.
Jens Geyer3b686532021-07-01 23:04:08 +0200420 constructor Create( const aProtocol : IProtocol); reintroduce;
Jens Geyerd5436f52014-10-03 19:50:38 +0200421
Jens Geyer17c3ad92017-09-05 20:31:27 +0200422 procedure WriteMessageBegin( const msg: TThriftMessage); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200423 procedure WriteMessageEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200424 procedure WriteStructBegin( const struc: TThriftStruct); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200425 procedure WriteStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200426 procedure WriteFieldBegin( const field: TThriftField); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200427 procedure WriteFieldEnd; override;
428 procedure WriteFieldStop; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200429 procedure WriteMapBegin( const map: TThriftMap); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200430 procedure WriteMapEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200431 procedure WriteListBegin( const list: TThriftList); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200432 procedure WriteListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200433 procedure WriteSetBegin( const set_: TThriftSet ); override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200434 procedure WriteSetEnd(); override;
435 procedure WriteBool( b: Boolean); override;
436 procedure WriteByte( b: ShortInt); override;
437 procedure WriteI16( i16: SmallInt); override;
438 procedure WriteI32( i32: Integer); override;
439 procedure WriteI64( const i64: Int64); override;
440 procedure WriteDouble( const d: Double); override;
441 procedure WriteString( const s: string ); override;
442 procedure WriteAnsiString( const s: AnsiString); override;
443 procedure WriteBinary( const b: TBytes); override;
444
Jens Geyer17c3ad92017-09-05 20:31:27 +0200445 function ReadMessageBegin: TThriftMessage; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200446 procedure ReadMessageEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200447 function ReadStructBegin: TThriftStruct; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200448 procedure ReadStructEnd; override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200449 function ReadFieldBegin: TThriftField; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200450 procedure ReadFieldEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200451 function ReadMapBegin: TThriftMap; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200452 procedure ReadMapEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200453 function ReadListBegin: TThriftList; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200454 procedure ReadListEnd(); override;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200455 function ReadSetBegin: TThriftSet; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200456 procedure ReadSetEnd(); override;
457 function ReadBool: Boolean; override;
458 function ReadByte: ShortInt; override;
459 function ReadI16: SmallInt; override;
460 function ReadI32: Integer; override;
461 function ReadI64: Int64; override;
462 function ReadDouble:Double; override;
463 function ReadBinary: TBytes; override;
464 function ReadString: string; override;
465 function ReadAnsiString: AnsiString; override;
466 end;
467
468
469type
470 IRequestEvents = interface
Jens Geyer01640402013-09-25 21:12:21 +0200471 ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}']
472 // Called before reading arguments.
473 procedure PreRead;
474 // Called between reading arguments and calling the handler.
475 procedure PostRead;
476 // Called between calling the handler and writing the response.
477 procedure PreWrite;
478 // Called after writing the response.
479 procedure PostWrite;
480 // Called when an oneway (async) function call completes successfully.
481 procedure OnewayComplete;
482 // Called if the handler throws an undeclared exception.
483 procedure UnhandledError( const e : Exception);
484 // Called when a client has finished request-handling to clean up
485 procedure CleanupContext;
486 end;
487
488
489 IProcessorEvents = interface
490 ['{A8661119-657C-447D-93C5-512E36162A45}']
491 // Called when a client is about to call the processor.
492 procedure Processing( const transport : ITransport);
493 // Called on any service function invocation
494 function CreateRequestContext( const aFunctionName : string) : IRequestEvents;
495 // Called when a client has finished request-handling to clean up
496 procedure CleanupContext;
497 end;
498
499
500 IProcessor = interface
501 ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}']
Jens Geyerd430bbd2013-09-26 23:37:54 +0200502 function Process( const iprot :IProtocol; const oprot: IProtocol; const events : IProcessorEvents = nil): Boolean;
Jens Geyer01640402013-09-25 21:12:21 +0200503 end;
504
Jens Geyerd5436f52014-10-03 19:50:38 +0200505
Jens Geyer17c3ad92017-09-05 20:31:27 +0200506procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline;
507procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline;
508procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline;
509procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
510procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
511procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
512
Jens Geyerd5436f52014-10-03 19:50:38 +0200513
514implementation
515
Jens Geyerfad7fd32019-11-09 23:24:52 +0100516function ConvertInt64ToDouble( const n: Int64): Double; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200517begin
518 ASSERT( SizeOf(n) = SizeOf(Result));
519 System.Move( n, Result, SizeOf(Result));
520end;
521
Jens Geyerfad7fd32019-11-09 23:24:52 +0100522function ConvertDoubleToInt64( const d: Double): Int64; inline;
Jens Geyerd5436f52014-10-03 19:50:38 +0200523begin
524 ASSERT( SizeOf(d) = SizeOf(Result));
525 System.Move( d, Result, SizeOf(Result));
526end;
527
Jens Geyerd5436f52014-10-03 19:50:38 +0200528
Jens Geyerd5436f52014-10-03 19:50:38 +0200529
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200530{ TProtocolRecursionTrackerImpl }
531
532constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol);
533begin
534 inherited Create;
535
536 // storing the pointer *after* the (successful) increment is important here
537 prot.IncrementRecursionDepth;
538 FProtocol := prot;
539end;
540
541destructor TProtocolRecursionTrackerImpl.Destroy;
542begin
543 try
544 // we have to release the reference iff the pointer has been stored
545 if FProtocol <> nil then begin
546 FProtocol.DecrementRecursionDepth;
547 FProtocol := nil;
548 end;
549 finally
550 inherited Destroy;
551 end;
552end;
553
Jens Geyerd5436f52014-10-03 19:50:38 +0200554{ TProtocolImpl }
555
Jens Geyera019cda2019-11-09 23:24:52 +0100556constructor TProtocolImpl.Create( const aTransport : ITransport);
Jens Geyerd5436f52014-10-03 19:50:38 +0200557begin
558 inherited Create;
Jens Geyera019cda2019-11-09 23:24:52 +0100559 FTrans := aTransport;
560 FRecursionLimit := aTransport.Configuration.RecursionLimit;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200561 FRecursionDepth := 0;
562end;
563
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200564function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker;
565begin
566 result := TProtocolRecursionTrackerImpl.Create(Self);
567end;
568
569procedure TProtocolImpl.IncrementRecursionDepth;
570begin
571 if FRecursionDepth < FRecursionLimit
572 then Inc(FRecursionDepth)
Jens Geyere0e32402016-04-20 21:50:48 +0200573 else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded');
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200574end;
575
576procedure TProtocolImpl.DecrementRecursionDepth;
577begin
578 Dec(FRecursionDepth)
Jens Geyerd5436f52014-10-03 19:50:38 +0200579end;
580
581function TProtocolImpl.GetTransport: ITransport;
582begin
583 Result := FTrans;
584end;
585
Jens Geyera019cda2019-11-09 23:24:52 +0100586function TProtocolImpl.Configuration : IThriftConfiguration;
587begin
588 Result := FTrans.Configuration;
589end;
590
Jens Geyer41f47af2019-11-09 23:24:52 +0100591procedure TProtocolImpl.Reset;
592begin
Jens Geyera019cda2019-11-09 23:24:52 +0100593 FTrans.ResetConsumedMessageSize;
Jens Geyer41f47af2019-11-09 23:24:52 +0100594end;
595
Jens Geyerd5436f52014-10-03 19:50:38 +0200596function TProtocolImpl.ReadAnsiString: AnsiString;
597var
598 b : TBytes;
599 len : Integer;
600begin
601 Result := '';
602 b := ReadBinary;
603 len := Length( b );
Jens Geyerfad7fd32019-11-09 23:24:52 +0100604 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200605 SetLength( Result, len);
606 System.Move( b[0], Pointer(Result)^, len );
607 end;
608end;
609
610function TProtocolImpl.ReadString: string;
611begin
612 Result := TEncoding.UTF8.GetString( ReadBinary );
613end;
614
615procedure TProtocolImpl.WriteAnsiString(const s: AnsiString);
616var
617 b : TBytes;
618 len : Integer;
619begin
620 len := Length(s);
621 SetLength( b, len);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100622 if len > 0 then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200623 System.Move( Pointer(s)^, b[0], len );
624 end;
625 WriteBinary( b );
626end;
627
628procedure TProtocolImpl.WriteString(const s: string);
629var
630 b : TBytes;
631begin
632 b := TEncoding.UTF8.GetBytes(s);
633 WriteBinary( b );
634end;
635
Jens Geyer41f47af2019-11-09 23:24:52 +0100636
637procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList);
638begin
639 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
640end;
641
642
643procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet);
644begin
645 FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType));
646end;
647
648
649procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap);
Jens Geyera019cda2019-11-09 23:24:52 +0100650var nPairSize : Integer;
Jens Geyer41f47af2019-11-09 23:24:52 +0100651begin
652 nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType);
653 FTrans.CheckReadBytesAvailable( value.Count * nPairSize);
654end;
655
Jens Geyerd5436f52014-10-03 19:50:38 +0200656{ TProtocolUtil }
657
658class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200659var field : TThriftField;
660 map : TThriftMap;
661 set_ : TThriftSet;
662 list : TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200663 i : Integer;
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200664 tracker : IProtocolRecursionTracker;
Jens Geyerd5436f52014-10-03 19:50:38 +0200665begin
Jens Geyerd47fcdd2015-07-09 22:05:18 +0200666 tracker := prot.NextRecursionLevel;
Jens Geyerd5436f52014-10-03 19:50:38 +0200667 case type_ of
668 // simple types
669 TType.Bool_ : prot.ReadBool();
670 TType.Byte_ : prot.ReadByte();
671 TType.I16 : prot.ReadI16();
672 TType.I32 : prot.ReadI32();
673 TType.I64 : prot.ReadI64();
674 TType.Double_ : prot.ReadDouble();
675 TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it.
676
677 // structured types
678 TType.Struct : begin
679 prot.ReadStructBegin();
680 while TRUE do begin
681 field := prot.ReadFieldBegin();
682 if (field.Type_ = TType.Stop) then Break;
683 Skip(prot, field.Type_);
684 prot.ReadFieldEnd();
685 end;
686 prot.ReadStructEnd();
687 end;
688
689 TType.Map : begin
690 map := prot.ReadMapBegin();
691 for i := 0 to map.Count-1 do begin
692 Skip(prot, map.KeyType);
693 Skip(prot, map.ValueType);
694 end;
695 prot.ReadMapEnd();
696 end;
697
698 TType.Set_ : begin
699 set_ := prot.ReadSetBegin();
700 for i := 0 to set_.Count-1
701 do Skip( prot, set_.ElementType);
702 prot.ReadSetEnd();
703 end;
704
705 TType.List : begin
706 list := prot.ReadListBegin();
707 for i := 0 to list.Count-1
708 do Skip( prot, list.ElementType);
709 prot.ReadListEnd();
710 end;
711
712 else
Jens Geyer5f723cd2017-01-10 21:57:48 +0100713 raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_)));
Jens Geyerd5436f52014-10-03 19:50:38 +0200714 end;
715end;
716
Jens Geyerd5436f52014-10-03 19:50:38 +0200717
718{ TBinaryProtocolImpl }
719
Jens Geyer3b686532021-07-01 23:04:08 +0200720constructor TBinaryProtocolImpl.Create( const trans: ITransport);
721begin
722 // call the real CTOR
723 Self.Create( trans, FALSE, TRUE);
724end;
725
Jens Geyerfad7fd32019-11-09 23:24:52 +0100726constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +0200727begin
Jens Geyerfad7fd32019-11-09 23:24:52 +0100728 inherited Create( trans);
Jens Geyerd5436f52014-10-03 19:50:38 +0200729 FStrictRead := strictRead;
730 FStrictWrite := strictWrite;
731end;
732
Jens Geyer17c3ad92017-09-05 20:31:27 +0200733function TBinaryProtocolImpl.ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200734begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200735 Result := FTrans.ReadAll( pBuf, buflen, off, len );
Jens Geyerd5436f52014-10-03 19:50:38 +0200736end;
737
738function TBinaryProtocolImpl.ReadBinary: TBytes;
739var
740 size : Integer;
741 buf : TBytes;
742begin
743 size := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100744 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100745 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200746 FTrans.ReadAll( buf, 0, size);
747 Result := buf;
748end;
749
750function TBinaryProtocolImpl.ReadBool: Boolean;
751begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200752 Result := (ReadByte = 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200753end;
754
755function TBinaryProtocolImpl.ReadByte: ShortInt;
Jens Geyerd5436f52014-10-03 19:50:38 +0200756begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200757 ReadAll( @result, SizeOf(result), 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200758end;
759
760function TBinaryProtocolImpl.ReadDouble: Double;
761begin
762 Result := ConvertInt64ToDouble( ReadI64 )
763end;
764
Jens Geyer17c3ad92017-09-05 20:31:27 +0200765function TBinaryProtocolImpl.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +0200766begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200767 Init( result, '', TType( ReadByte), 0);
768 if ( result.Type_ <> TType.Stop ) then begin
769 result.Id := ReadI16;
Jens Geyerd5436f52014-10-03 19:50:38 +0200770 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200771end;
772
773procedure TBinaryProtocolImpl.ReadFieldEnd;
774begin
775
776end;
777
778function TBinaryProtocolImpl.ReadI16: SmallInt;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200779var i16in : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200780begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200781 ReadAll( @i16in, Sizeof(i16in), 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200782 Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF));
783end;
784
785function TBinaryProtocolImpl.ReadI32: Integer;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200786var i32in : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200787begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200788 ReadAll( @i32in, SizeOf(i32in), 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200789
790 Result := Integer(
791 ((i32in[0] and $FF) shl 24) or
792 ((i32in[1] and $FF) shl 16) or
793 ((i32in[2] and $FF) shl 8) or
794 (i32in[3] and $FF));
795
796end;
797
798function TBinaryProtocolImpl.ReadI64: Int64;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200799var i64in : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200800begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200801 ReadAll( @i64in, SizeOf(i64in), 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200802 Result :=
803 (Int64( i64in[0] and $FF) shl 56) or
804 (Int64( i64in[1] and $FF) shl 48) or
805 (Int64( i64in[2] and $FF) shl 40) or
806 (Int64( i64in[3] and $FF) shl 32) or
807 (Int64( i64in[4] and $FF) shl 24) or
808 (Int64( i64in[5] and $FF) shl 16) or
809 (Int64( i64in[6] and $FF) shl 8) or
810 (Int64( i64in[7] and $FF));
811end;
812
Jens Geyer17c3ad92017-09-05 20:31:27 +0200813function TBinaryProtocolImpl.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +0200814begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200815 result.ElementType := TType(ReadByte);
816 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100817 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200818end;
819
820procedure TBinaryProtocolImpl.ReadListEnd;
821begin
822
823end;
824
Jens Geyer17c3ad92017-09-05 20:31:27 +0200825function TBinaryProtocolImpl.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +0200826begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200827 result.KeyType := TType(ReadByte);
828 result.ValueType := TType(ReadByte);
829 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100830 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200831end;
832
833procedure TBinaryProtocolImpl.ReadMapEnd;
834begin
835
836end;
837
Jens Geyer17c3ad92017-09-05 20:31:27 +0200838function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +0200839var
840 size : Integer;
841 version : Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200842begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100843 Reset;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200844 Init( result);
Jens Geyer589ee5b2021-03-29 21:40:55 +0200845
Jens Geyerd5436f52014-10-03 19:50:38 +0200846 size := ReadI32;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200847 if (size < 0) then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200848 version := size and Integer( VERSION_MASK);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200849 if ( version <> Integer( VERSION_1)) then begin
Jens Geyere0e32402016-04-20 21:50:48 +0200850 raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) );
Jens Geyerd5436f52014-10-03 19:50:38 +0200851 end;
Jens Geyer17c3ad92017-09-05 20:31:27 +0200852 result.Type_ := TMessageType( size and $000000ff);
853 result.Name := ReadString;
854 result.SeqID := ReadI32;
Jens Geyer589ee5b2021-03-29 21:40:55 +0200855 Exit;
856 end;
857
858 try
859 if FStrictRead
860 then raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' );
861
Jens Geyer17c3ad92017-09-05 20:31:27 +0200862 result.Name := ReadStringBody( size );
863 result.Type_ := TMessageType( ReadByte );
864 result.SeqID := ReadI32;
Jens Geyer589ee5b2021-03-29 21:40:55 +0200865 except
866 if CharUtils.IsHtmlDoctype(size)
867 then raise TProtocolExceptionInvalidData.Create('Remote end sends HTML instead of data')
868 else raise; // something else
Jens Geyerd5436f52014-10-03 19:50:38 +0200869 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200870end;
871
872procedure TBinaryProtocolImpl.ReadMessageEnd;
873begin
874 inherited;
875
876end;
877
Jens Geyer17c3ad92017-09-05 20:31:27 +0200878function TBinaryProtocolImpl.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +0200879begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200880 result.ElementType := TType(ReadByte);
881 result.Count := ReadI32;
Jens Geyer41f47af2019-11-09 23:24:52 +0100882 CheckReadBytesAvailable(result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200883end;
884
885procedure TBinaryProtocolImpl.ReadSetEnd;
886begin
887
888end;
889
890function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100891var buf : TBytes;
Jens Geyerd5436f52014-10-03 19:50:38 +0200892begin
Jens Geyer41f47af2019-11-09 23:24:52 +0100893 FTrans.CheckReadBytesAvailable( size);
Jens Geyerfad7fd32019-11-09 23:24:52 +0100894 SetLength( buf, size);
Jens Geyerd5436f52014-10-03 19:50:38 +0200895 FTrans.ReadAll( buf, 0, size );
896 Result := TEncoding.UTF8.GetString( buf);
897end;
898
Jens Geyer17c3ad92017-09-05 20:31:27 +0200899function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +0200900begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200901 Init( Result);
Jens Geyerd5436f52014-10-03 19:50:38 +0200902end;
903
904procedure TBinaryProtocolImpl.ReadStructEnd;
905begin
906 inherited;
907
908end;
909
910procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
911var iLen : Integer;
912begin
913 iLen := Length(b);
914 WriteI32( iLen);
915 if iLen > 0 then FTrans.Write(b, 0, iLen);
916end;
917
918procedure TBinaryProtocolImpl.WriteBool(b: Boolean);
919begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200920 if b then begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200921 WriteByte( 1 );
Jens Geyer17c3ad92017-09-05 20:31:27 +0200922 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200923 WriteByte( 0 );
924 end;
925end;
926
927procedure TBinaryProtocolImpl.WriteByte(b: ShortInt);
Jens Geyerd5436f52014-10-03 19:50:38 +0200928begin
Jens Geyer17c3ad92017-09-05 20:31:27 +0200929 FTrans.Write( @b, 0, 1);
Jens Geyerd5436f52014-10-03 19:50:38 +0200930end;
931
932procedure TBinaryProtocolImpl.WriteDouble( const d: Double);
933begin
934 WriteI64(ConvertDoubleToInt64(d));
935end;
936
Jens Geyer17c3ad92017-09-05 20:31:27 +0200937procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +0200938begin
939 WriteByte(ShortInt(field.Type_));
940 WriteI16(field.ID);
941end;
942
943procedure TBinaryProtocolImpl.WriteFieldEnd;
944begin
945
946end;
947
948procedure TBinaryProtocolImpl.WriteFieldStop;
949begin
950 WriteByte(ShortInt(TType.Stop));
951end;
952
953procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200954var i16out : packed array[0..1] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200955begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200956 i16out[0] := Byte($FF and (i16 shr 8));
957 i16out[1] := Byte($FF and i16);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200958 FTrans.Write( @i16out, 0, 2);
Jens Geyerd5436f52014-10-03 19:50:38 +0200959end;
960
961procedure TBinaryProtocolImpl.WriteI32(i32: Integer);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200962var i32out : packed array[0..3] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200963begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200964 i32out[0] := Byte($FF and (i32 shr 24));
965 i32out[1] := Byte($FF and (i32 shr 16));
966 i32out[2] := Byte($FF and (i32 shr 8));
967 i32out[3] := Byte($FF and i32);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200968 FTrans.Write( @i32out, 0, 4);
Jens Geyerd5436f52014-10-03 19:50:38 +0200969end;
970
971procedure TBinaryProtocolImpl.WriteI64( const i64: Int64);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200972var i64out : packed array[0..7] of Byte;
Jens Geyerd5436f52014-10-03 19:50:38 +0200973begin
Jens Geyerd5436f52014-10-03 19:50:38 +0200974 i64out[0] := Byte($FF and (i64 shr 56));
975 i64out[1] := Byte($FF and (i64 shr 48));
976 i64out[2] := Byte($FF and (i64 shr 40));
977 i64out[3] := Byte($FF and (i64 shr 32));
978 i64out[4] := Byte($FF and (i64 shr 24));
979 i64out[5] := Byte($FF and (i64 shr 16));
980 i64out[6] := Byte($FF and (i64 shr 8));
981 i64out[7] := Byte($FF and i64);
Jens Geyer17c3ad92017-09-05 20:31:27 +0200982 FTrans.Write( @i64out, 0, 8);
Jens Geyerd5436f52014-10-03 19:50:38 +0200983end;
984
Jens Geyer17c3ad92017-09-05 20:31:27 +0200985procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +0200986begin
987 WriteByte(ShortInt(list.ElementType));
988 WriteI32(list.Count);
989end;
990
991procedure TBinaryProtocolImpl.WriteListEnd;
992begin
993
994end;
995
Jens Geyer17c3ad92017-09-05 20:31:27 +0200996procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +0200997begin
998 WriteByte(ShortInt(map.KeyType));
999 WriteByte(ShortInt(map.ValueType));
1000 WriteI32(map.Count);
1001end;
1002
1003procedure TBinaryProtocolImpl.WriteMapEnd;
1004begin
1005
1006end;
1007
Jens Geyer17c3ad92017-09-05 20:31:27 +02001008procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001009var version : Cardinal;
Jens Geyerd5436f52014-10-03 19:50:38 +02001010begin
Jens Geyer41f47af2019-11-09 23:24:52 +01001011 Reset;
Jens Geyerfad7fd32019-11-09 23:24:52 +01001012 if FStrictWrite then begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001013 version := VERSION_1 or Cardinal( msg.Type_);
1014 WriteI32( Integer( version) );
1015 WriteString( msg.Name);
1016 WriteI32( msg.SeqID);
Jens Geyerfad7fd32019-11-09 23:24:52 +01001017 end else begin
Jens Geyerd5436f52014-10-03 19:50:38 +02001018 WriteString( msg.Name);
1019 WriteByte(ShortInt( msg.Type_));
1020 WriteI32( msg.SeqID);
1021 end;
1022end;
1023
1024procedure TBinaryProtocolImpl.WriteMessageEnd;
1025begin
1026
1027end;
1028
Jens Geyer17c3ad92017-09-05 20:31:27 +02001029procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet);
Jens Geyerd5436f52014-10-03 19:50:38 +02001030begin
1031 WriteByte(ShortInt(set_.ElementType));
1032 WriteI32(set_.Count);
1033end;
1034
1035procedure TBinaryProtocolImpl.WriteSetEnd;
1036begin
1037
1038end;
1039
Jens Geyer17c3ad92017-09-05 20:31:27 +02001040procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001041begin
1042
1043end;
1044
1045procedure TBinaryProtocolImpl.WriteStructEnd;
1046begin
1047
1048end;
1049
Jens Geyer41f47af2019-11-09 23:24:52 +01001050function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer;
1051// Return the minimum number of bytes a type will consume on the wire
1052begin
1053 case aType of
1054 TType.Stop: result := 0;
1055 TType.Void: result := 0;
1056 TType.Bool_: result := SizeOf(Byte);
1057 TType.Byte_: result := SizeOf(Byte);
1058 TType.Double_: result := SizeOf(Double);
1059 TType.I16: result := SizeOf(Int16);
1060 TType.I32: result := SizeOf(Int32);
1061 TType.I64: result := SizeOf(Int64);
1062 TType.String_: result := SizeOf(Int32); // string length
1063 TType.Struct: result := 0; // empty struct
1064 TType.Map: result := SizeOf(Int32); // element count
1065 TType.Set_: result := SizeOf(Int32); // element count
1066 TType.List: result := SizeOf(Int32); // element count
1067 else
1068 raise TTransportExceptionBadArgs.Create('Unhandled type code');
1069 end;
1070end;
1071
1072
Jens Geyerd5436f52014-10-03 19:50:38 +02001073{ TProtocolException }
1074
Jens Geyere0e32402016-04-20 21:50:48 +02001075constructor TProtocolException.HiddenCreate(const Msg: string);
Jens Geyerd5436f52014-10-03 19:50:38 +02001076begin
Jens Geyere0e32402016-04-20 21:50:48 +02001077 inherited Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001078end;
1079
Jens Geyere0e32402016-04-20 21:50:48 +02001080class function TProtocolException.Create(const Msg: string): TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001081begin
Jens Geyere0e32402016-04-20 21:50:48 +02001082 Result := TProtocolExceptionUnknown.Create(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001083end;
1084
Jens Geyere0e32402016-04-20 21:50:48 +02001085class function TProtocolException.Create: TProtocolException;
Jens Geyerd5436f52014-10-03 19:50:38 +02001086begin
Jens Geyere0e32402016-04-20 21:50:48 +02001087 Result := TProtocolExceptionUnknown.Create('');
1088end;
1089
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001090class function TProtocolException.Create(aType: TExceptionType): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001091begin
1092{$WARN SYMBOL_DEPRECATED OFF}
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001093 Result := Create(aType, '');
Jens Geyere0e32402016-04-20 21:50:48 +02001094{$WARN SYMBOL_DEPRECATED DEFAULT}
1095end;
1096
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001097class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException;
Jens Geyere0e32402016-04-20 21:50:48 +02001098begin
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001099 case aType of
1100 TExceptionType.INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg);
1101 TExceptionType.NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg);
1102 TExceptionType.SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg);
1103 TExceptionType.BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg);
1104 TExceptionType.NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg);
1105 TExceptionType.DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg);
Jens Geyere0e32402016-04-20 21:50:48 +02001106 else
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001107 ASSERT( TExceptionType.UNKNOWN = aType);
Jens Geyere0e32402016-04-20 21:50:48 +02001108 Result := TProtocolExceptionUnknown.Create(msg);
1109 end;
1110end;
1111
1112{ TProtocolExceptionSpecialized }
1113
1114constructor TProtocolExceptionSpecialized.Create(const Msg: string);
1115begin
1116 inherited HiddenCreate(Msg);
Jens Geyerd5436f52014-10-03 19:50:38 +02001117end;
1118
Jens Geyer9f11c1e2019-11-09 19:39:20 +01001119{ specialized TProtocolExceptions }
1120
1121class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType;
1122begin
1123 result := TExceptionType.UNKNOWN;
1124end;
1125
1126class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType;
1127begin
1128 result := TExceptionType.INVALID_DATA;
1129end;
1130
1131class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType;
1132begin
1133 result := TExceptionType.NEGATIVE_SIZE;
1134end;
1135
1136class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType;
1137begin
1138 result := TExceptionType.SIZE_LIMIT;
1139end;
1140
1141class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType;
1142begin
1143 result := TExceptionType.BAD_VERSION;
1144end;
1145
1146class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType;
1147begin
1148 result := TExceptionType.NOT_IMPLEMENTED;
1149end;
1150
1151class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType;
1152begin
1153 result := TExceptionType.DEPTH_LIMIT;
1154end;
1155
Jens Geyerd5436f52014-10-03 19:50:38 +02001156{ TBinaryProtocolImpl.TFactory }
1157
Jens Geyerfad7fd32019-11-09 23:24:52 +01001158constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean);
Jens Geyerd5436f52014-10-03 19:50:38 +02001159begin
1160 inherited Create;
1161 FStrictRead := AStrictRead;
1162 FStrictWrite := AStrictWrite;
1163end;
1164
Jens Geyerd5436f52014-10-03 19:50:38 +02001165function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol;
1166begin
1167 Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite);
1168end;
1169
1170
1171{ TProtocolDecorator }
1172
1173constructor TProtocolDecorator.Create( const aProtocol : IProtocol);
1174begin
1175 ASSERT( aProtocol <> nil);
1176 inherited Create( aProtocol.Transport);
1177 FWrappedProtocol := aProtocol;
1178end;
1179
1180
Jens Geyer17c3ad92017-09-05 20:31:27 +02001181procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage);
Jens Geyerd5436f52014-10-03 19:50:38 +02001182begin
1183 FWrappedProtocol.WriteMessageBegin( msg);
1184end;
1185
1186
1187procedure TProtocolDecorator.WriteMessageEnd;
1188begin
1189 FWrappedProtocol.WriteMessageEnd;
1190end;
1191
1192
Jens Geyer17c3ad92017-09-05 20:31:27 +02001193procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct);
Jens Geyerd5436f52014-10-03 19:50:38 +02001194begin
1195 FWrappedProtocol.WriteStructBegin( struc);
1196end;
1197
1198
1199procedure TProtocolDecorator.WriteStructEnd;
1200begin
1201 FWrappedProtocol.WriteStructEnd;
1202end;
1203
1204
Jens Geyer17c3ad92017-09-05 20:31:27 +02001205procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField);
Jens Geyerd5436f52014-10-03 19:50:38 +02001206begin
1207 FWrappedProtocol.WriteFieldBegin( field);
1208end;
1209
1210
1211procedure TProtocolDecorator.WriteFieldEnd;
1212begin
1213 FWrappedProtocol.WriteFieldEnd;
1214end;
1215
1216
1217procedure TProtocolDecorator.WriteFieldStop;
1218begin
1219 FWrappedProtocol.WriteFieldStop;
1220end;
1221
1222
Jens Geyer17c3ad92017-09-05 20:31:27 +02001223procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap);
Jens Geyerd5436f52014-10-03 19:50:38 +02001224begin
1225 FWrappedProtocol.WriteMapBegin( map);
1226end;
1227
1228
1229procedure TProtocolDecorator.WriteMapEnd;
1230begin
1231 FWrappedProtocol.WriteMapEnd;
1232end;
1233
1234
Jens Geyer17c3ad92017-09-05 20:31:27 +02001235procedure TProtocolDecorator.WriteListBegin( const list: TThriftList);
Jens Geyerd5436f52014-10-03 19:50:38 +02001236begin
1237 FWrappedProtocol.WriteListBegin( list);
1238end;
1239
1240
1241procedure TProtocolDecorator.WriteListEnd();
1242begin
1243 FWrappedProtocol.WriteListEnd();
1244end;
1245
1246
Jens Geyer17c3ad92017-09-05 20:31:27 +02001247procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet );
Jens Geyerd5436f52014-10-03 19:50:38 +02001248begin
1249 FWrappedProtocol.WriteSetBegin( set_);
1250end;
1251
1252
1253procedure TProtocolDecorator.WriteSetEnd();
1254begin
1255 FWrappedProtocol.WriteSetEnd();
1256end;
1257
1258
1259procedure TProtocolDecorator.WriteBool( b: Boolean);
1260begin
1261 FWrappedProtocol.WriteBool( b);
1262end;
1263
1264
1265procedure TProtocolDecorator.WriteByte( b: ShortInt);
1266begin
1267 FWrappedProtocol.WriteByte( b);
1268end;
1269
1270
1271procedure TProtocolDecorator.WriteI16( i16: SmallInt);
1272begin
1273 FWrappedProtocol.WriteI16( i16);
1274end;
1275
1276
1277procedure TProtocolDecorator.WriteI32( i32: Integer);
1278begin
1279 FWrappedProtocol.WriteI32( i32);
1280end;
1281
1282
1283procedure TProtocolDecorator.WriteI64( const i64: Int64);
1284begin
1285 FWrappedProtocol.WriteI64( i64);
1286end;
1287
1288
1289procedure TProtocolDecorator.WriteDouble( const d: Double);
1290begin
1291 FWrappedProtocol.WriteDouble( d);
1292end;
1293
1294
1295procedure TProtocolDecorator.WriteString( const s: string );
1296begin
1297 FWrappedProtocol.WriteString( s);
1298end;
1299
1300
1301procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString);
1302begin
1303 FWrappedProtocol.WriteAnsiString( s);
1304end;
1305
1306
1307procedure TProtocolDecorator.WriteBinary( const b: TBytes);
1308begin
1309 FWrappedProtocol.WriteBinary( b);
1310end;
1311
1312
Jens Geyer17c3ad92017-09-05 20:31:27 +02001313function TProtocolDecorator.ReadMessageBegin: TThriftMessage;
Jens Geyerd5436f52014-10-03 19:50:38 +02001314begin
1315 result := FWrappedProtocol.ReadMessageBegin;
1316end;
1317
1318
1319procedure TProtocolDecorator.ReadMessageEnd();
1320begin
1321 FWrappedProtocol.ReadMessageEnd();
1322end;
1323
1324
Jens Geyer17c3ad92017-09-05 20:31:27 +02001325function TProtocolDecorator.ReadStructBegin: TThriftStruct;
Jens Geyerd5436f52014-10-03 19:50:38 +02001326begin
1327 result := FWrappedProtocol.ReadStructBegin;
1328end;
1329
1330
1331procedure TProtocolDecorator.ReadStructEnd;
1332begin
1333 FWrappedProtocol.ReadStructEnd;
1334end;
1335
1336
Jens Geyer17c3ad92017-09-05 20:31:27 +02001337function TProtocolDecorator.ReadFieldBegin: TThriftField;
Jens Geyerd5436f52014-10-03 19:50:38 +02001338begin
1339 result := FWrappedProtocol.ReadFieldBegin;
1340end;
1341
1342
1343procedure TProtocolDecorator.ReadFieldEnd();
1344begin
1345 FWrappedProtocol.ReadFieldEnd();
1346end;
1347
1348
Jens Geyer17c3ad92017-09-05 20:31:27 +02001349function TProtocolDecorator.ReadMapBegin: TThriftMap;
Jens Geyerd5436f52014-10-03 19:50:38 +02001350begin
1351 result := FWrappedProtocol.ReadMapBegin;
1352end;
1353
1354
1355procedure TProtocolDecorator.ReadMapEnd();
1356begin
1357 FWrappedProtocol.ReadMapEnd();
1358end;
1359
1360
Jens Geyer17c3ad92017-09-05 20:31:27 +02001361function TProtocolDecorator.ReadListBegin: TThriftList;
Jens Geyerd5436f52014-10-03 19:50:38 +02001362begin
1363 result := FWrappedProtocol.ReadListBegin;
1364end;
1365
1366
1367procedure TProtocolDecorator.ReadListEnd();
1368begin
1369 FWrappedProtocol.ReadListEnd();
1370end;
1371
1372
Jens Geyer17c3ad92017-09-05 20:31:27 +02001373function TProtocolDecorator.ReadSetBegin: TThriftSet;
Jens Geyerd5436f52014-10-03 19:50:38 +02001374begin
1375 result := FWrappedProtocol.ReadSetBegin;
1376end;
1377
1378
1379procedure TProtocolDecorator.ReadSetEnd();
1380begin
1381 FWrappedProtocol.ReadSetEnd();
1382end;
1383
1384
1385function TProtocolDecorator.ReadBool: Boolean;
1386begin
1387 result := FWrappedProtocol.ReadBool;
1388end;
1389
1390
1391function TProtocolDecorator.ReadByte: ShortInt;
1392begin
1393 result := FWrappedProtocol.ReadByte;
1394end;
1395
1396
1397function TProtocolDecorator.ReadI16: SmallInt;
1398begin
1399 result := FWrappedProtocol.ReadI16;
1400end;
1401
1402
1403function TProtocolDecorator.ReadI32: Integer;
1404begin
1405 result := FWrappedProtocol.ReadI32;
1406end;
1407
1408
1409function TProtocolDecorator.ReadI64: Int64;
1410begin
1411 result := FWrappedProtocol.ReadI64;
1412end;
1413
1414
1415function TProtocolDecorator.ReadDouble:Double;
1416begin
1417 result := FWrappedProtocol.ReadDouble;
1418end;
1419
1420
1421function TProtocolDecorator.ReadBinary: TBytes;
1422begin
1423 result := FWrappedProtocol.ReadBinary;
1424end;
1425
1426
1427function TProtocolDecorator.ReadString: string;
1428begin
1429 result := FWrappedProtocol.ReadString;
1430end;
1431
1432
1433function TProtocolDecorator.ReadAnsiString: AnsiString;
1434begin
1435 result := FWrappedProtocol.ReadAnsiString;
1436end;
1437
1438
Jens Geyer41f47af2019-11-09 23:24:52 +01001439function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer;
1440begin
1441 result := FWrappedProtocol.GetMinSerializedSize(aType);
1442end;
1443
1444
Jens Geyer17c3ad92017-09-05 20:31:27 +02001445{ Init helper functions }
1446
1447procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer);
1448begin
1449 rec.Name := AName;
1450 rec.Type_ := AMessageType;
1451 rec.SeqID := ASeqID;
1452end;
1453
1454
1455procedure Init( var rec : TThriftStruct; const AName: string = '');
1456begin
1457 rec.Name := AName;
1458end;
1459
1460
1461procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt);
1462begin
1463 rec.Name := AName;
1464 rec.Type_ := AType;
1465 rec.Id := AId;
1466end;
1467
1468
1469procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer);
1470begin
1471 rec.ValueType := AValueType;
1472 rec.KeyType := AKeyType;
1473 rec.Count := ACount;
1474end;
1475
1476
1477procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer);
1478begin
1479 rec.Count := ACount;
1480 rec.ElementType := AElementType;
1481end;
1482
1483
1484procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer);
1485begin
1486 rec.Count := ACount;
1487 rec.ElementType := AElementType;
1488end;
1489
1490
1491
Jens Geyerd5436f52014-10-03 19:50:38 +02001492end.
1493