Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1 | (* |
| 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 | |
| 22 | unit Thrift.Protocol; |
| 23 | |
| 24 | interface |
| 25 | |
| 26 | uses |
| 27 | Classes, |
| 28 | SysUtils, |
| 29 | Contnrs, |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 30 | Thrift.Exception, |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 31 | Thrift.Stream, |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 32 | Thrift.Utils, |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 33 | Thrift.Collections, |
| 34 | Thrift.Transport; |
| 35 | |
| 36 | type |
| 37 | |
| 38 | TType = ( |
| 39 | Stop = 0, |
| 40 | Void = 1, |
| 41 | Bool_ = 2, |
| 42 | Byte_ = 3, |
| 43 | Double_ = 4, |
| 44 | I16 = 6, |
| 45 | I32 = 8, |
| 46 | I64 = 10, |
| 47 | String_ = 11, |
| 48 | Struct = 12, |
| 49 | Map = 13, |
| 50 | Set_ = 14, |
| 51 | List = 15 |
| 52 | ); |
| 53 | |
| 54 | TMessageType = ( |
| 55 | Call = 1, |
| 56 | Reply = 2, |
| 57 | Exception = 3, |
| 58 | Oneway = 4 |
| 59 | ); |
| 60 | |
Jens Geyer | f0e6331 | 2015-03-01 18:47:49 +0100 | [diff] [blame] | 61 | const |
| 62 | VALID_TTYPES = [ |
| 63 | TType.Stop, TType.Void, |
| 64 | TType.Bool_, TType.Byte_, TType.Double_, TType.I16, TType.I32, TType.I64, TType.String_, |
| 65 | TType.Struct, TType.Map, TType.Set_, TType.List |
| 66 | ]; |
| 67 | |
| 68 | VALID_MESSAGETYPES = [Low(TMessageType)..High(TMessageType)]; |
| 69 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 70 | const |
| 71 | DEFAULT_RECURSION_LIMIT = 64; |
| 72 | |
Jens Geyer | f0e6331 | 2015-03-01 18:47:49 +0100 | [diff] [blame] | 73 | type |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 74 | IProtocol = interface; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 75 | |
| 76 | TThriftMessage = record |
| 77 | Name: string; |
| 78 | Type_: TMessageType; |
| 79 | SeqID: Integer; |
| 80 | end; |
| 81 | |
| 82 | TThriftStruct = record |
| 83 | Name: string; |
| 84 | end; |
| 85 | |
| 86 | TThriftField = record |
| 87 | Name: string; |
| 88 | Type_: TType; |
| 89 | Id: SmallInt; |
| 90 | end; |
| 91 | |
| 92 | TThriftList = record |
| 93 | ElementType: TType; |
| 94 | Count: Integer; |
| 95 | end; |
| 96 | |
| 97 | TThriftMap = record |
| 98 | KeyType: TType; |
| 99 | ValueType: TType; |
| 100 | Count: Integer; |
| 101 | end; |
| 102 | |
| 103 | TThriftSet = record |
| 104 | ElementType: TType; |
| 105 | Count: Integer; |
| 106 | end; |
| 107 | |
| 108 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 109 | IProtocolFactory = interface |
| 110 | ['{7CD64A10-4E9F-4E99-93BF-708A31F4A67B}'] |
| 111 | function GetProtocol( const trans: ITransport): IProtocol; |
| 112 | end; |
| 113 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 114 | TProtocolException = class abstract( TException) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 115 | public |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 116 | type TExceptionType = ( |
| 117 | UNKNOWN = 0, |
| 118 | INVALID_DATA = 1, |
| 119 | NEGATIVE_SIZE = 2, |
| 120 | SIZE_LIMIT = 3, |
| 121 | BAD_VERSION = 4, |
| 122 | NOT_IMPLEMENTED = 5, |
| 123 | DEPTH_LIMIT = 6 |
| 124 | ); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 125 | strict protected |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 126 | constructor HiddenCreate(const Msg: string); |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 127 | class function GetType: TExceptionType; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 128 | public |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 129 | // purposefully hide inherited constructor |
| 130 | class function Create(const Msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)'; |
| 131 | class function Create: TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)'; |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 132 | class function Create( aType: TExceptionType): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)'; |
| 133 | class function Create( aType: TExceptionType; const msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)'; |
| 134 | property Type_: TExceptionType read GetType; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 135 | end; |
| 136 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 137 | // Needed to remove deprecation warning |
| 138 | TProtocolExceptionSpecialized = class abstract (TProtocolException) |
| 139 | public |
| 140 | constructor Create(const Msg: string); |
| 141 | end; |
| 142 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 143 | TProtocolExceptionUnknown = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 144 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 145 | class function GetType: TProtocolException.TExceptionType; override; |
| 146 | end; |
| 147 | |
| 148 | TProtocolExceptionInvalidData = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 149 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 150 | class function GetType: TProtocolException.TExceptionType; override; |
| 151 | end; |
| 152 | |
| 153 | TProtocolExceptionNegativeSize = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 154 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 155 | class function GetType: TProtocolException.TExceptionType; override; |
| 156 | end; |
| 157 | |
| 158 | TProtocolExceptionSizeLimit = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 159 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 160 | class function GetType: TProtocolException.TExceptionType; override; |
| 161 | end; |
| 162 | |
| 163 | TProtocolExceptionBadVersion = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 164 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 165 | class function GetType: TProtocolException.TExceptionType; override; |
| 166 | end; |
| 167 | |
| 168 | TProtocolExceptionNotImplemented = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 169 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 170 | class function GetType: TProtocolException.TExceptionType; override; |
| 171 | end; |
| 172 | |
| 173 | TProtocolExceptionDepthLimit = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 174 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 175 | class function GetType: TProtocolException.TExceptionType; override; |
| 176 | end; |
| 177 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 178 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 179 | |
| 180 | TProtocolUtil = class |
| 181 | public |
| 182 | class procedure Skip( prot: IProtocol; type_: TType); |
| 183 | end; |
| 184 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 185 | IProtocolRecursionTracker = interface |
| 186 | ['{29CA033F-BB56-49B1-9EE3-31B1E82FC7A5}'] |
| 187 | // no members yet |
| 188 | end; |
| 189 | |
| 190 | TProtocolRecursionTrackerImpl = class abstract( TInterfacedObject, IProtocolRecursionTracker) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 191 | strict protected |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 192 | FProtocol : IProtocol; |
| 193 | public |
| 194 | constructor Create( prot : IProtocol); |
| 195 | destructor Destroy; override; |
| 196 | end; |
| 197 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 198 | IProtocol = interface |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 199 | ['{7F3640D7-5082-49E7-B562-84202F323C3A}'] |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 200 | function GetTransport: ITransport; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 201 | procedure WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 202 | procedure WriteMessageEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 203 | procedure WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 204 | procedure WriteStructEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 205 | procedure WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 206 | procedure WriteFieldEnd; |
| 207 | procedure WriteFieldStop; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 208 | procedure WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 209 | procedure WriteMapEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 210 | procedure WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 211 | procedure WriteListEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 212 | procedure WriteSetBegin( const set_: TThriftSet ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 213 | procedure WriteSetEnd(); |
| 214 | procedure WriteBool( b: Boolean); |
| 215 | procedure WriteByte( b: ShortInt); |
| 216 | procedure WriteI16( i16: SmallInt); |
| 217 | procedure WriteI32( i32: Integer); |
| 218 | procedure WriteI64( const i64: Int64); |
| 219 | procedure WriteDouble( const d: Double); |
| 220 | procedure WriteString( const s: string ); |
| 221 | procedure WriteAnsiString( const s: AnsiString); |
| 222 | procedure WriteBinary( const b: TBytes); |
| 223 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 224 | function ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 225 | procedure ReadMessageEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 226 | function ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 227 | procedure ReadStructEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 228 | function ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 229 | procedure ReadFieldEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 230 | function ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 231 | procedure ReadMapEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 232 | function ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 233 | procedure ReadListEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 234 | function ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 235 | procedure ReadSetEnd(); |
| 236 | function ReadBool: Boolean; |
| 237 | function ReadByte: ShortInt; |
| 238 | function ReadI16: SmallInt; |
| 239 | function ReadI32: Integer; |
| 240 | function ReadI64: Int64; |
| 241 | function ReadDouble:Double; |
| 242 | function ReadBinary: TBytes; |
| 243 | function ReadString: string; |
| 244 | function ReadAnsiString: AnsiString; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 245 | |
| 246 | procedure SetRecursionLimit( value : Integer); |
| 247 | function GetRecursionLimit : Integer; |
| 248 | function NextRecursionLevel : IProtocolRecursionTracker; |
| 249 | procedure IncrementRecursionDepth; |
| 250 | procedure DecrementRecursionDepth; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 251 | function GetMinSerializedSize( const aType : TType) : Integer; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 252 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 253 | property Transport: ITransport read GetTransport; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 254 | property RecursionLimit : Integer read GetRecursionLimit write SetRecursionLimit; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 255 | end; |
| 256 | |
| 257 | TProtocolImpl = class abstract( TInterfacedObject, IProtocol) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 258 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 259 | FTrans : ITransport; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 260 | FRecursionLimit : Integer; |
| 261 | FRecursionDepth : Integer; |
| 262 | |
| 263 | procedure SetRecursionLimit( value : Integer); |
| 264 | function GetRecursionLimit : Integer; |
| 265 | function NextRecursionLevel : IProtocolRecursionTracker; |
| 266 | procedure IncrementRecursionDepth; |
| 267 | procedure DecrementRecursionDepth; |
| 268 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 269 | function GetMinSerializedSize( const aType : TType) : Integer; virtual; abstract; |
| 270 | procedure CheckReadBytesAvailable( const value : TThriftList); overload; inline; |
| 271 | procedure CheckReadBytesAvailable( const value : TThriftSet); overload; inline; |
| 272 | procedure CheckReadBytesAvailable( const value : TThriftMap); overload; inline; |
| 273 | |
| 274 | procedure Reset; virtual; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 275 | function GetTransport: ITransport; |
| 276 | public |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 277 | procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 278 | procedure WriteMessageEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 279 | procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 280 | procedure WriteStructEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 281 | procedure WriteFieldBegin( const field: TThriftField); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 282 | procedure WriteFieldEnd; virtual; abstract; |
| 283 | procedure WriteFieldStop; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 284 | procedure WriteMapBegin( const map: TThriftMap); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 285 | procedure WriteMapEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 286 | procedure WriteListBegin( const list: TThriftList); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 287 | procedure WriteListEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 288 | procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 289 | procedure WriteSetEnd(); virtual; abstract; |
| 290 | procedure WriteBool( b: Boolean); virtual; abstract; |
| 291 | procedure WriteByte( b: ShortInt); virtual; abstract; |
| 292 | procedure WriteI16( i16: SmallInt); virtual; abstract; |
| 293 | procedure WriteI32( i32: Integer); virtual; abstract; |
| 294 | procedure WriteI64( const i64: Int64); virtual; abstract; |
| 295 | procedure WriteDouble( const d: Double); virtual; abstract; |
| 296 | procedure WriteString( const s: string ); virtual; |
| 297 | procedure WriteAnsiString( const s: AnsiString); virtual; |
| 298 | procedure WriteBinary( const b: TBytes); virtual; abstract; |
| 299 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 300 | function ReadMessageBegin: TThriftMessage; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 301 | procedure ReadMessageEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 302 | function ReadStructBegin: TThriftStruct; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 303 | procedure ReadStructEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 304 | function ReadFieldBegin: TThriftField; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 305 | procedure ReadFieldEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 306 | function ReadMapBegin: TThriftMap; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 307 | procedure ReadMapEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 308 | function ReadListBegin: TThriftList; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 309 | procedure ReadListEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 310 | function ReadSetBegin: TThriftSet; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 311 | procedure ReadSetEnd(); virtual; abstract; |
| 312 | function ReadBool: Boolean; virtual; abstract; |
| 313 | function ReadByte: ShortInt; virtual; abstract; |
| 314 | function ReadI16: SmallInt; virtual; abstract; |
| 315 | function ReadI32: Integer; virtual; abstract; |
| 316 | function ReadI64: Int64; virtual; abstract; |
| 317 | function ReadDouble:Double; virtual; abstract; |
| 318 | function ReadBinary: TBytes; virtual; abstract; |
| 319 | function ReadString: string; virtual; |
| 320 | function ReadAnsiString: AnsiString; virtual; |
| 321 | |
| 322 | property Transport: ITransport read GetTransport; |
| 323 | |
| 324 | constructor Create( trans: ITransport ); |
| 325 | end; |
| 326 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 327 | IBase = interface( ISupportsToString) |
| 328 | ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}'] |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 329 | procedure Read( const iprot: IProtocol); |
| 330 | procedure Write( const iprot: IProtocol); |
| 331 | end; |
| 332 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 333 | |
| 334 | TBinaryProtocolImpl = class( TProtocolImpl ) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 335 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 336 | const |
| 337 | VERSION_MASK : Cardinal = $ffff0000; |
| 338 | VERSION_1 : Cardinal = $80010000; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 339 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 340 | FStrictRead : Boolean; |
| 341 | FStrictWrite : Boolean; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 342 | function GetMinSerializedSize( const aType : TType) : Integer; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 343 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 344 | strict private |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 345 | function ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 346 | function ReadStringBody( size: Integer): string; |
| 347 | |
| 348 | public |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 349 | type |
| 350 | TFactory = class( TInterfacedObject, IProtocolFactory) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 351 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 352 | FStrictRead : Boolean; |
| 353 | FStrictWrite : Boolean; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 354 | function GetProtocol( const trans: ITransport): IProtocol; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 355 | public |
| 356 | constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 357 | end; |
| 358 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 359 | constructor Create( const trans: ITransport; strictRead: Boolean = FALSE; strictWrite: Boolean = TRUE); reintroduce; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 360 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 361 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 362 | procedure WriteMessageEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 363 | procedure WriteStructBegin( const struc: TThriftStruct); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 364 | procedure WriteStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 365 | procedure WriteFieldBegin( const field: TThriftField); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 366 | procedure WriteFieldEnd; override; |
| 367 | procedure WriteFieldStop; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 368 | procedure WriteMapBegin( const map: TThriftMap); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 369 | procedure WriteMapEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 370 | procedure WriteListBegin( const list: TThriftList); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 371 | procedure WriteListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 372 | procedure WriteSetBegin( const set_: TThriftSet ); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 373 | procedure WriteSetEnd(); override; |
| 374 | procedure WriteBool( b: Boolean); override; |
| 375 | procedure WriteByte( b: ShortInt); override; |
| 376 | procedure WriteI16( i16: SmallInt); override; |
| 377 | procedure WriteI32( i32: Integer); override; |
| 378 | procedure WriteI64( const i64: Int64); override; |
| 379 | procedure WriteDouble( const d: Double); override; |
| 380 | procedure WriteBinary( const b: TBytes); override; |
| 381 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 382 | function ReadMessageBegin: TThriftMessage; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 383 | procedure ReadMessageEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 384 | function ReadStructBegin: TThriftStruct; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 385 | procedure ReadStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 386 | function ReadFieldBegin: TThriftField; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 387 | procedure ReadFieldEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 388 | function ReadMapBegin: TThriftMap; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 389 | procedure ReadMapEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 390 | function ReadListBegin: TThriftList; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 391 | procedure ReadListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 392 | function ReadSetBegin: TThriftSet; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 393 | procedure ReadSetEnd(); override; |
| 394 | function ReadBool: Boolean; override; |
| 395 | function ReadByte: ShortInt; override; |
| 396 | function ReadI16: SmallInt; override; |
| 397 | function ReadI32: Integer; override; |
| 398 | function ReadI64: Int64; override; |
| 399 | function ReadDouble:Double; override; |
| 400 | function ReadBinary: TBytes; override; |
| 401 | |
| 402 | end; |
| 403 | |
| 404 | |
| 405 | { TProtocolDecorator forwards all requests to an enclosed TProtocol instance, |
| 406 | providing a way to author concise concrete decorator subclasses. The decorator |
| 407 | does not (and should not) modify the behaviour of the enclosed TProtocol |
| 408 | |
| 409 | See p.175 of Design Patterns (by Gamma et al.) |
| 410 | } |
| 411 | TProtocolDecorator = class( TProtocolImpl) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 412 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 413 | FWrappedProtocol : IProtocol; |
| 414 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 415 | strict protected |
| 416 | function GetMinSerializedSize( const aType : TType) : Integer; override; |
| 417 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 418 | public |
| 419 | // Encloses the specified protocol. |
| 420 | // All operations will be forward to the given protocol. Must be non-null. |
| 421 | constructor Create( const aProtocol : IProtocol); |
| 422 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 423 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 424 | procedure WriteMessageEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 425 | procedure WriteStructBegin( const struc: TThriftStruct); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 426 | procedure WriteStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 427 | procedure WriteFieldBegin( const field: TThriftField); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 428 | procedure WriteFieldEnd; override; |
| 429 | procedure WriteFieldStop; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 430 | procedure WriteMapBegin( const map: TThriftMap); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 431 | procedure WriteMapEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 432 | procedure WriteListBegin( const list: TThriftList); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 433 | procedure WriteListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 434 | procedure WriteSetBegin( const set_: TThriftSet ); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 435 | procedure WriteSetEnd(); override; |
| 436 | procedure WriteBool( b: Boolean); override; |
| 437 | procedure WriteByte( b: ShortInt); override; |
| 438 | procedure WriteI16( i16: SmallInt); override; |
| 439 | procedure WriteI32( i32: Integer); override; |
| 440 | procedure WriteI64( const i64: Int64); override; |
| 441 | procedure WriteDouble( const d: Double); override; |
| 442 | procedure WriteString( const s: string ); override; |
| 443 | procedure WriteAnsiString( const s: AnsiString); override; |
| 444 | procedure WriteBinary( const b: TBytes); override; |
| 445 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 446 | function ReadMessageBegin: TThriftMessage; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 447 | procedure ReadMessageEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 448 | function ReadStructBegin: TThriftStruct; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 449 | procedure ReadStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 450 | function ReadFieldBegin: TThriftField; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 451 | procedure ReadFieldEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 452 | function ReadMapBegin: TThriftMap; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 453 | procedure ReadMapEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 454 | function ReadListBegin: TThriftList; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 455 | procedure ReadListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 456 | function ReadSetBegin: TThriftSet; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 457 | procedure ReadSetEnd(); override; |
| 458 | function ReadBool: Boolean; override; |
| 459 | function ReadByte: ShortInt; override; |
| 460 | function ReadI16: SmallInt; override; |
| 461 | function ReadI32: Integer; override; |
| 462 | function ReadI64: Int64; override; |
| 463 | function ReadDouble:Double; override; |
| 464 | function ReadBinary: TBytes; override; |
| 465 | function ReadString: string; override; |
| 466 | function ReadAnsiString: AnsiString; override; |
| 467 | end; |
| 468 | |
| 469 | |
| 470 | type |
| 471 | IRequestEvents = interface |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 472 | ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}'] |
| 473 | // Called before reading arguments. |
| 474 | procedure PreRead; |
| 475 | // Called between reading arguments and calling the handler. |
| 476 | procedure PostRead; |
| 477 | // Called between calling the handler and writing the response. |
| 478 | procedure PreWrite; |
| 479 | // Called after writing the response. |
| 480 | procedure PostWrite; |
| 481 | // Called when an oneway (async) function call completes successfully. |
| 482 | procedure OnewayComplete; |
| 483 | // Called if the handler throws an undeclared exception. |
| 484 | procedure UnhandledError( const e : Exception); |
| 485 | // Called when a client has finished request-handling to clean up |
| 486 | procedure CleanupContext; |
| 487 | end; |
| 488 | |
| 489 | |
| 490 | IProcessorEvents = interface |
| 491 | ['{A8661119-657C-447D-93C5-512E36162A45}'] |
| 492 | // Called when a client is about to call the processor. |
| 493 | procedure Processing( const transport : ITransport); |
| 494 | // Called on any service function invocation |
| 495 | function CreateRequestContext( const aFunctionName : string) : IRequestEvents; |
| 496 | // Called when a client has finished request-handling to clean up |
| 497 | procedure CleanupContext; |
| 498 | end; |
| 499 | |
| 500 | |
| 501 | IProcessor = interface |
| 502 | ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}'] |
Jens Geyer | d430bbd | 2013-09-26 23:37:54 +0200 | [diff] [blame] | 503 | function Process( const iprot :IProtocol; const oprot: IProtocol; const events : IProcessorEvents = nil): Boolean; |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 504 | end; |
| 505 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 506 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 507 | procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline; |
| 508 | procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline; |
| 509 | procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline; |
| 510 | procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 511 | procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 512 | procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 513 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 514 | |
| 515 | implementation |
| 516 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 517 | function ConvertInt64ToDouble( const n: Int64): Double; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 518 | begin |
| 519 | ASSERT( SizeOf(n) = SizeOf(Result)); |
| 520 | System.Move( n, Result, SizeOf(Result)); |
| 521 | end; |
| 522 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 523 | function ConvertDoubleToInt64( const d: Double): Int64; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 524 | begin |
| 525 | ASSERT( SizeOf(d) = SizeOf(Result)); |
| 526 | System.Move( d, Result, SizeOf(Result)); |
| 527 | end; |
| 528 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 529 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 530 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 531 | { TProtocolRecursionTrackerImpl } |
| 532 | |
| 533 | constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol); |
| 534 | begin |
| 535 | inherited Create; |
| 536 | |
| 537 | // storing the pointer *after* the (successful) increment is important here |
| 538 | prot.IncrementRecursionDepth; |
| 539 | FProtocol := prot; |
| 540 | end; |
| 541 | |
| 542 | destructor TProtocolRecursionTrackerImpl.Destroy; |
| 543 | begin |
| 544 | try |
| 545 | // we have to release the reference iff the pointer has been stored |
| 546 | if FProtocol <> nil then begin |
| 547 | FProtocol.DecrementRecursionDepth; |
| 548 | FProtocol := nil; |
| 549 | end; |
| 550 | finally |
| 551 | inherited Destroy; |
| 552 | end; |
| 553 | end; |
| 554 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 555 | { TProtocolImpl } |
| 556 | |
| 557 | constructor TProtocolImpl.Create(trans: ITransport); |
| 558 | begin |
| 559 | inherited Create; |
| 560 | FTrans := trans; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 561 | FRecursionLimit := DEFAULT_RECURSION_LIMIT; |
| 562 | FRecursionDepth := 0; |
| 563 | end; |
| 564 | |
| 565 | procedure TProtocolImpl.SetRecursionLimit( value : Integer); |
| 566 | begin |
| 567 | FRecursionLimit := value; |
| 568 | end; |
| 569 | |
| 570 | function TProtocolImpl.GetRecursionLimit : Integer; |
| 571 | begin |
| 572 | result := FRecursionLimit; |
| 573 | end; |
| 574 | |
| 575 | function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker; |
| 576 | begin |
| 577 | result := TProtocolRecursionTrackerImpl.Create(Self); |
| 578 | end; |
| 579 | |
| 580 | procedure TProtocolImpl.IncrementRecursionDepth; |
| 581 | begin |
| 582 | if FRecursionDepth < FRecursionLimit |
| 583 | then Inc(FRecursionDepth) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 584 | else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded'); |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 585 | end; |
| 586 | |
| 587 | procedure TProtocolImpl.DecrementRecursionDepth; |
| 588 | begin |
| 589 | Dec(FRecursionDepth) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 590 | end; |
| 591 | |
| 592 | function TProtocolImpl.GetTransport: ITransport; |
| 593 | begin |
| 594 | Result := FTrans; |
| 595 | end; |
| 596 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 597 | procedure TProtocolImpl.Reset; |
| 598 | begin |
| 599 | if FTrans.TransportControl <> nil |
| 600 | then FTrans.TransportControl.ResetConsumedMessageSize; |
| 601 | end; |
| 602 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 603 | function TProtocolImpl.ReadAnsiString: AnsiString; |
| 604 | var |
| 605 | b : TBytes; |
| 606 | len : Integer; |
| 607 | begin |
| 608 | Result := ''; |
| 609 | b := ReadBinary; |
| 610 | len := Length( b ); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 611 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 612 | SetLength( Result, len); |
| 613 | System.Move( b[0], Pointer(Result)^, len ); |
| 614 | end; |
| 615 | end; |
| 616 | |
| 617 | function TProtocolImpl.ReadString: string; |
| 618 | begin |
| 619 | Result := TEncoding.UTF8.GetString( ReadBinary ); |
| 620 | end; |
| 621 | |
| 622 | procedure TProtocolImpl.WriteAnsiString(const s: AnsiString); |
| 623 | var |
| 624 | b : TBytes; |
| 625 | len : Integer; |
| 626 | begin |
| 627 | len := Length(s); |
| 628 | SetLength( b, len); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 629 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 630 | System.Move( Pointer(s)^, b[0], len ); |
| 631 | end; |
| 632 | WriteBinary( b ); |
| 633 | end; |
| 634 | |
| 635 | procedure TProtocolImpl.WriteString(const s: string); |
| 636 | var |
| 637 | b : TBytes; |
| 638 | begin |
| 639 | b := TEncoding.UTF8.GetBytes(s); |
| 640 | WriteBinary( b ); |
| 641 | end; |
| 642 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 643 | |
| 644 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList); |
| 645 | begin |
| 646 | FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType)); |
| 647 | end; |
| 648 | |
| 649 | |
| 650 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet); |
| 651 | begin |
| 652 | FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType)); |
| 653 | end; |
| 654 | |
| 655 | |
| 656 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap); |
| 657 | var nPairSize : Integer |
| 658 | ; |
| 659 | begin |
| 660 | nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType); |
| 661 | FTrans.CheckReadBytesAvailable( value.Count * nPairSize); |
| 662 | end; |
| 663 | |
| 664 | |
| 665 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 666 | { TProtocolUtil } |
| 667 | |
| 668 | class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 669 | var field : TThriftField; |
| 670 | map : TThriftMap; |
| 671 | set_ : TThriftSet; |
| 672 | list : TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 673 | i : Integer; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 674 | tracker : IProtocolRecursionTracker; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 675 | begin |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 676 | tracker := prot.NextRecursionLevel; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 677 | case type_ of |
| 678 | // simple types |
| 679 | TType.Bool_ : prot.ReadBool(); |
| 680 | TType.Byte_ : prot.ReadByte(); |
| 681 | TType.I16 : prot.ReadI16(); |
| 682 | TType.I32 : prot.ReadI32(); |
| 683 | TType.I64 : prot.ReadI64(); |
| 684 | TType.Double_ : prot.ReadDouble(); |
| 685 | TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it. |
| 686 | |
| 687 | // structured types |
| 688 | TType.Struct : begin |
| 689 | prot.ReadStructBegin(); |
| 690 | while TRUE do begin |
| 691 | field := prot.ReadFieldBegin(); |
| 692 | if (field.Type_ = TType.Stop) then Break; |
| 693 | Skip(prot, field.Type_); |
| 694 | prot.ReadFieldEnd(); |
| 695 | end; |
| 696 | prot.ReadStructEnd(); |
| 697 | end; |
| 698 | |
| 699 | TType.Map : begin |
| 700 | map := prot.ReadMapBegin(); |
| 701 | for i := 0 to map.Count-1 do begin |
| 702 | Skip(prot, map.KeyType); |
| 703 | Skip(prot, map.ValueType); |
| 704 | end; |
| 705 | prot.ReadMapEnd(); |
| 706 | end; |
| 707 | |
| 708 | TType.Set_ : begin |
| 709 | set_ := prot.ReadSetBegin(); |
| 710 | for i := 0 to set_.Count-1 |
| 711 | do Skip( prot, set_.ElementType); |
| 712 | prot.ReadSetEnd(); |
| 713 | end; |
| 714 | |
| 715 | TType.List : begin |
| 716 | list := prot.ReadListBegin(); |
| 717 | for i := 0 to list.Count-1 |
| 718 | do Skip( prot, list.ElementType); |
| 719 | prot.ReadListEnd(); |
| 720 | end; |
| 721 | |
| 722 | else |
Jens Geyer | 5f723cd | 2017-01-10 21:57:48 +0100 | [diff] [blame] | 723 | raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_))); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 724 | end; |
| 725 | end; |
| 726 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 727 | |
| 728 | { TBinaryProtocolImpl } |
| 729 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 730 | constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 731 | begin |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 732 | inherited Create( trans); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 733 | FStrictRead := strictRead; |
| 734 | FStrictWrite := strictWrite; |
| 735 | end; |
| 736 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 737 | function TBinaryProtocolImpl.ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 738 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 739 | Result := FTrans.ReadAll( pBuf, buflen, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 740 | end; |
| 741 | |
| 742 | function TBinaryProtocolImpl.ReadBinary: TBytes; |
| 743 | var |
| 744 | size : Integer; |
| 745 | buf : TBytes; |
| 746 | begin |
| 747 | size := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 748 | FTrans.CheckReadBytesAvailable( size); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 749 | SetLength( buf, size); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 750 | FTrans.ReadAll( buf, 0, size); |
| 751 | Result := buf; |
| 752 | end; |
| 753 | |
| 754 | function TBinaryProtocolImpl.ReadBool: Boolean; |
| 755 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 756 | Result := (ReadByte = 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 757 | end; |
| 758 | |
| 759 | function TBinaryProtocolImpl.ReadByte: ShortInt; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 760 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 761 | ReadAll( @result, SizeOf(result), 0, 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 762 | end; |
| 763 | |
| 764 | function TBinaryProtocolImpl.ReadDouble: Double; |
| 765 | begin |
| 766 | Result := ConvertInt64ToDouble( ReadI64 ) |
| 767 | end; |
| 768 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 769 | function TBinaryProtocolImpl.ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 770 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 771 | Init( result, '', TType( ReadByte), 0); |
| 772 | if ( result.Type_ <> TType.Stop ) then begin |
| 773 | result.Id := ReadI16; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 774 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 775 | end; |
| 776 | |
| 777 | procedure TBinaryProtocolImpl.ReadFieldEnd; |
| 778 | begin |
| 779 | |
| 780 | end; |
| 781 | |
| 782 | function TBinaryProtocolImpl.ReadI16: SmallInt; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 783 | var i16in : packed array[0..1] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 784 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 785 | ReadAll( @i16in, Sizeof(i16in), 0, 2); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 786 | Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF)); |
| 787 | end; |
| 788 | |
| 789 | function TBinaryProtocolImpl.ReadI32: Integer; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 790 | var i32in : packed array[0..3] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 791 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 792 | ReadAll( @i32in, SizeOf(i32in), 0, 4); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 793 | |
| 794 | Result := Integer( |
| 795 | ((i32in[0] and $FF) shl 24) or |
| 796 | ((i32in[1] and $FF) shl 16) or |
| 797 | ((i32in[2] and $FF) shl 8) or |
| 798 | (i32in[3] and $FF)); |
| 799 | |
| 800 | end; |
| 801 | |
| 802 | function TBinaryProtocolImpl.ReadI64: Int64; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 803 | var i64in : packed array[0..7] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 804 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 805 | ReadAll( @i64in, SizeOf(i64in), 0, 8); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 806 | Result := |
| 807 | (Int64( i64in[0] and $FF) shl 56) or |
| 808 | (Int64( i64in[1] and $FF) shl 48) or |
| 809 | (Int64( i64in[2] and $FF) shl 40) or |
| 810 | (Int64( i64in[3] and $FF) shl 32) or |
| 811 | (Int64( i64in[4] and $FF) shl 24) or |
| 812 | (Int64( i64in[5] and $FF) shl 16) or |
| 813 | (Int64( i64in[6] and $FF) shl 8) or |
| 814 | (Int64( i64in[7] and $FF)); |
| 815 | end; |
| 816 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 817 | function TBinaryProtocolImpl.ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 818 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 819 | result.ElementType := TType(ReadByte); |
| 820 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 821 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 822 | end; |
| 823 | |
| 824 | procedure TBinaryProtocolImpl.ReadListEnd; |
| 825 | begin |
| 826 | |
| 827 | end; |
| 828 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 829 | function TBinaryProtocolImpl.ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 830 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 831 | result.KeyType := TType(ReadByte); |
| 832 | result.ValueType := TType(ReadByte); |
| 833 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 834 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 835 | end; |
| 836 | |
| 837 | procedure TBinaryProtocolImpl.ReadMapEnd; |
| 838 | begin |
| 839 | |
| 840 | end; |
| 841 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 842 | function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 843 | var |
| 844 | size : Integer; |
| 845 | version : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 846 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 847 | Reset; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 848 | Init( result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 849 | size := ReadI32; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 850 | if (size < 0) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 851 | version := size and Integer( VERSION_MASK); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 852 | if ( version <> Integer( VERSION_1)) then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 853 | raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 854 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 855 | result.Type_ := TMessageType( size and $000000ff); |
| 856 | result.Name := ReadString; |
| 857 | result.SeqID := ReadI32; |
| 858 | end |
| 859 | else begin |
| 860 | if FStrictRead then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 861 | raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 862 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 863 | result.Name := ReadStringBody( size ); |
| 864 | result.Type_ := TMessageType( ReadByte ); |
| 865 | result.SeqID := ReadI32; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 866 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 867 | end; |
| 868 | |
| 869 | procedure TBinaryProtocolImpl.ReadMessageEnd; |
| 870 | begin |
| 871 | inherited; |
| 872 | |
| 873 | end; |
| 874 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 875 | function TBinaryProtocolImpl.ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 876 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 877 | result.ElementType := TType(ReadByte); |
| 878 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 879 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 880 | end; |
| 881 | |
| 882 | procedure TBinaryProtocolImpl.ReadSetEnd; |
| 883 | begin |
| 884 | |
| 885 | end; |
| 886 | |
| 887 | function TBinaryProtocolImpl.ReadStringBody( size: Integer): string; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 888 | var buf : TBytes; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 889 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 890 | FTrans.CheckReadBytesAvailable( size); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 891 | SetLength( buf, size); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 892 | FTrans.ReadAll( buf, 0, size ); |
| 893 | Result := TEncoding.UTF8.GetString( buf); |
| 894 | end; |
| 895 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 896 | function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 897 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 898 | Init( Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 899 | end; |
| 900 | |
| 901 | procedure TBinaryProtocolImpl.ReadStructEnd; |
| 902 | begin |
| 903 | inherited; |
| 904 | |
| 905 | end; |
| 906 | |
| 907 | procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes); |
| 908 | var iLen : Integer; |
| 909 | begin |
| 910 | iLen := Length(b); |
| 911 | WriteI32( iLen); |
| 912 | if iLen > 0 then FTrans.Write(b, 0, iLen); |
| 913 | end; |
| 914 | |
| 915 | procedure TBinaryProtocolImpl.WriteBool(b: Boolean); |
| 916 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 917 | if b then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 918 | WriteByte( 1 ); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 919 | end else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 920 | WriteByte( 0 ); |
| 921 | end; |
| 922 | end; |
| 923 | |
| 924 | procedure TBinaryProtocolImpl.WriteByte(b: ShortInt); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 925 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 926 | FTrans.Write( @b, 0, 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 927 | end; |
| 928 | |
| 929 | procedure TBinaryProtocolImpl.WriteDouble( const d: Double); |
| 930 | begin |
| 931 | WriteI64(ConvertDoubleToInt64(d)); |
| 932 | end; |
| 933 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 934 | procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 935 | begin |
| 936 | WriteByte(ShortInt(field.Type_)); |
| 937 | WriteI16(field.ID); |
| 938 | end; |
| 939 | |
| 940 | procedure TBinaryProtocolImpl.WriteFieldEnd; |
| 941 | begin |
| 942 | |
| 943 | end; |
| 944 | |
| 945 | procedure TBinaryProtocolImpl.WriteFieldStop; |
| 946 | begin |
| 947 | WriteByte(ShortInt(TType.Stop)); |
| 948 | end; |
| 949 | |
| 950 | procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 951 | var i16out : packed array[0..1] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 952 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 953 | i16out[0] := Byte($FF and (i16 shr 8)); |
| 954 | i16out[1] := Byte($FF and i16); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 955 | FTrans.Write( @i16out, 0, 2); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 956 | end; |
| 957 | |
| 958 | procedure TBinaryProtocolImpl.WriteI32(i32: Integer); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 959 | var i32out : packed array[0..3] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 960 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 961 | i32out[0] := Byte($FF and (i32 shr 24)); |
| 962 | i32out[1] := Byte($FF and (i32 shr 16)); |
| 963 | i32out[2] := Byte($FF and (i32 shr 8)); |
| 964 | i32out[3] := Byte($FF and i32); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 965 | FTrans.Write( @i32out, 0, 4); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 966 | end; |
| 967 | |
| 968 | procedure TBinaryProtocolImpl.WriteI64( const i64: Int64); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 969 | var i64out : packed array[0..7] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 970 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 971 | i64out[0] := Byte($FF and (i64 shr 56)); |
| 972 | i64out[1] := Byte($FF and (i64 shr 48)); |
| 973 | i64out[2] := Byte($FF and (i64 shr 40)); |
| 974 | i64out[3] := Byte($FF and (i64 shr 32)); |
| 975 | i64out[4] := Byte($FF and (i64 shr 24)); |
| 976 | i64out[5] := Byte($FF and (i64 shr 16)); |
| 977 | i64out[6] := Byte($FF and (i64 shr 8)); |
| 978 | i64out[7] := Byte($FF and i64); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 979 | FTrans.Write( @i64out, 0, 8); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 980 | end; |
| 981 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 982 | procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 983 | begin |
| 984 | WriteByte(ShortInt(list.ElementType)); |
| 985 | WriteI32(list.Count); |
| 986 | end; |
| 987 | |
| 988 | procedure TBinaryProtocolImpl.WriteListEnd; |
| 989 | begin |
| 990 | |
| 991 | end; |
| 992 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 993 | procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 994 | begin |
| 995 | WriteByte(ShortInt(map.KeyType)); |
| 996 | WriteByte(ShortInt(map.ValueType)); |
| 997 | WriteI32(map.Count); |
| 998 | end; |
| 999 | |
| 1000 | procedure TBinaryProtocolImpl.WriteMapEnd; |
| 1001 | begin |
| 1002 | |
| 1003 | end; |
| 1004 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1005 | procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1006 | var version : Cardinal; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1007 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 1008 | Reset; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1009 | if FStrictWrite then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1010 | version := VERSION_1 or Cardinal( msg.Type_); |
| 1011 | WriteI32( Integer( version) ); |
| 1012 | WriteString( msg.Name); |
| 1013 | WriteI32( msg.SeqID); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1014 | end else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1015 | WriteString( msg.Name); |
| 1016 | WriteByte(ShortInt( msg.Type_)); |
| 1017 | WriteI32( msg.SeqID); |
| 1018 | end; |
| 1019 | end; |
| 1020 | |
| 1021 | procedure TBinaryProtocolImpl.WriteMessageEnd; |
| 1022 | begin |
| 1023 | |
| 1024 | end; |
| 1025 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1026 | procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1027 | begin |
| 1028 | WriteByte(ShortInt(set_.ElementType)); |
| 1029 | WriteI32(set_.Count); |
| 1030 | end; |
| 1031 | |
| 1032 | procedure TBinaryProtocolImpl.WriteSetEnd; |
| 1033 | begin |
| 1034 | |
| 1035 | end; |
| 1036 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1037 | procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1038 | begin |
| 1039 | |
| 1040 | end; |
| 1041 | |
| 1042 | procedure TBinaryProtocolImpl.WriteStructEnd; |
| 1043 | begin |
| 1044 | |
| 1045 | end; |
| 1046 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 1047 | function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer; |
| 1048 | // Return the minimum number of bytes a type will consume on the wire |
| 1049 | begin |
| 1050 | case aType of |
| 1051 | TType.Stop: result := 0; |
| 1052 | TType.Void: result := 0; |
| 1053 | TType.Bool_: result := SizeOf(Byte); |
| 1054 | TType.Byte_: result := SizeOf(Byte); |
| 1055 | TType.Double_: result := SizeOf(Double); |
| 1056 | TType.I16: result := SizeOf(Int16); |
| 1057 | TType.I32: result := SizeOf(Int32); |
| 1058 | TType.I64: result := SizeOf(Int64); |
| 1059 | TType.String_: result := SizeOf(Int32); // string length |
| 1060 | TType.Struct: result := 0; // empty struct |
| 1061 | TType.Map: result := SizeOf(Int32); // element count |
| 1062 | TType.Set_: result := SizeOf(Int32); // element count |
| 1063 | TType.List: result := SizeOf(Int32); // element count |
| 1064 | else |
| 1065 | raise TTransportExceptionBadArgs.Create('Unhandled type code'); |
| 1066 | end; |
| 1067 | end; |
| 1068 | |
| 1069 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1070 | { TProtocolException } |
| 1071 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1072 | constructor TProtocolException.HiddenCreate(const Msg: string); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1073 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1074 | inherited Create(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1075 | end; |
| 1076 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1077 | class function TProtocolException.Create(const Msg: string): TProtocolException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1078 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1079 | Result := TProtocolExceptionUnknown.Create(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1080 | end; |
| 1081 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1082 | class function TProtocolException.Create: TProtocolException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1083 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1084 | Result := TProtocolExceptionUnknown.Create(''); |
| 1085 | end; |
| 1086 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1087 | class function TProtocolException.Create(aType: TExceptionType): TProtocolException; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1088 | begin |
| 1089 | {$WARN SYMBOL_DEPRECATED OFF} |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1090 | Result := Create(aType, ''); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1091 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
| 1092 | end; |
| 1093 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1094 | class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1095 | begin |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1096 | case aType of |
| 1097 | TExceptionType.INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg); |
| 1098 | TExceptionType.NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg); |
| 1099 | TExceptionType.SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg); |
| 1100 | TExceptionType.BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg); |
| 1101 | TExceptionType.NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg); |
| 1102 | TExceptionType.DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1103 | else |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1104 | ASSERT( TExceptionType.UNKNOWN = aType); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1105 | Result := TProtocolExceptionUnknown.Create(msg); |
| 1106 | end; |
| 1107 | end; |
| 1108 | |
| 1109 | { TProtocolExceptionSpecialized } |
| 1110 | |
| 1111 | constructor TProtocolExceptionSpecialized.Create(const Msg: string); |
| 1112 | begin |
| 1113 | inherited HiddenCreate(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1114 | end; |
| 1115 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1116 | { specialized TProtocolExceptions } |
| 1117 | |
| 1118 | class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType; |
| 1119 | begin |
| 1120 | result := TExceptionType.UNKNOWN; |
| 1121 | end; |
| 1122 | |
| 1123 | class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType; |
| 1124 | begin |
| 1125 | result := TExceptionType.INVALID_DATA; |
| 1126 | end; |
| 1127 | |
| 1128 | class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType; |
| 1129 | begin |
| 1130 | result := TExceptionType.NEGATIVE_SIZE; |
| 1131 | end; |
| 1132 | |
| 1133 | class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType; |
| 1134 | begin |
| 1135 | result := TExceptionType.SIZE_LIMIT; |
| 1136 | end; |
| 1137 | |
| 1138 | class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType; |
| 1139 | begin |
| 1140 | result := TExceptionType.BAD_VERSION; |
| 1141 | end; |
| 1142 | |
| 1143 | class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType; |
| 1144 | begin |
| 1145 | result := TExceptionType.NOT_IMPLEMENTED; |
| 1146 | end; |
| 1147 | |
| 1148 | class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType; |
| 1149 | begin |
| 1150 | result := TExceptionType.DEPTH_LIMIT; |
| 1151 | end; |
| 1152 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1153 | { TBinaryProtocolImpl.TFactory } |
| 1154 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1155 | constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1156 | begin |
| 1157 | inherited Create; |
| 1158 | FStrictRead := AStrictRead; |
| 1159 | FStrictWrite := AStrictWrite; |
| 1160 | end; |
| 1161 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1162 | function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol; |
| 1163 | begin |
| 1164 | Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite); |
| 1165 | end; |
| 1166 | |
| 1167 | |
| 1168 | { TProtocolDecorator } |
| 1169 | |
| 1170 | constructor TProtocolDecorator.Create( const aProtocol : IProtocol); |
| 1171 | begin |
| 1172 | ASSERT( aProtocol <> nil); |
| 1173 | inherited Create( aProtocol.Transport); |
| 1174 | FWrappedProtocol := aProtocol; |
| 1175 | end; |
| 1176 | |
| 1177 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1178 | procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1179 | begin |
| 1180 | FWrappedProtocol.WriteMessageBegin( msg); |
| 1181 | end; |
| 1182 | |
| 1183 | |
| 1184 | procedure TProtocolDecorator.WriteMessageEnd; |
| 1185 | begin |
| 1186 | FWrappedProtocol.WriteMessageEnd; |
| 1187 | end; |
| 1188 | |
| 1189 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1190 | procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1191 | begin |
| 1192 | FWrappedProtocol.WriteStructBegin( struc); |
| 1193 | end; |
| 1194 | |
| 1195 | |
| 1196 | procedure TProtocolDecorator.WriteStructEnd; |
| 1197 | begin |
| 1198 | FWrappedProtocol.WriteStructEnd; |
| 1199 | end; |
| 1200 | |
| 1201 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1202 | procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1203 | begin |
| 1204 | FWrappedProtocol.WriteFieldBegin( field); |
| 1205 | end; |
| 1206 | |
| 1207 | |
| 1208 | procedure TProtocolDecorator.WriteFieldEnd; |
| 1209 | begin |
| 1210 | FWrappedProtocol.WriteFieldEnd; |
| 1211 | end; |
| 1212 | |
| 1213 | |
| 1214 | procedure TProtocolDecorator.WriteFieldStop; |
| 1215 | begin |
| 1216 | FWrappedProtocol.WriteFieldStop; |
| 1217 | end; |
| 1218 | |
| 1219 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1220 | procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1221 | begin |
| 1222 | FWrappedProtocol.WriteMapBegin( map); |
| 1223 | end; |
| 1224 | |
| 1225 | |
| 1226 | procedure TProtocolDecorator.WriteMapEnd; |
| 1227 | begin |
| 1228 | FWrappedProtocol.WriteMapEnd; |
| 1229 | end; |
| 1230 | |
| 1231 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1232 | procedure TProtocolDecorator.WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1233 | begin |
| 1234 | FWrappedProtocol.WriteListBegin( list); |
| 1235 | end; |
| 1236 | |
| 1237 | |
| 1238 | procedure TProtocolDecorator.WriteListEnd(); |
| 1239 | begin |
| 1240 | FWrappedProtocol.WriteListEnd(); |
| 1241 | end; |
| 1242 | |
| 1243 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1244 | procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1245 | begin |
| 1246 | FWrappedProtocol.WriteSetBegin( set_); |
| 1247 | end; |
| 1248 | |
| 1249 | |
| 1250 | procedure TProtocolDecorator.WriteSetEnd(); |
| 1251 | begin |
| 1252 | FWrappedProtocol.WriteSetEnd(); |
| 1253 | end; |
| 1254 | |
| 1255 | |
| 1256 | procedure TProtocolDecorator.WriteBool( b: Boolean); |
| 1257 | begin |
| 1258 | FWrappedProtocol.WriteBool( b); |
| 1259 | end; |
| 1260 | |
| 1261 | |
| 1262 | procedure TProtocolDecorator.WriteByte( b: ShortInt); |
| 1263 | begin |
| 1264 | FWrappedProtocol.WriteByte( b); |
| 1265 | end; |
| 1266 | |
| 1267 | |
| 1268 | procedure TProtocolDecorator.WriteI16( i16: SmallInt); |
| 1269 | begin |
| 1270 | FWrappedProtocol.WriteI16( i16); |
| 1271 | end; |
| 1272 | |
| 1273 | |
| 1274 | procedure TProtocolDecorator.WriteI32( i32: Integer); |
| 1275 | begin |
| 1276 | FWrappedProtocol.WriteI32( i32); |
| 1277 | end; |
| 1278 | |
| 1279 | |
| 1280 | procedure TProtocolDecorator.WriteI64( const i64: Int64); |
| 1281 | begin |
| 1282 | FWrappedProtocol.WriteI64( i64); |
| 1283 | end; |
| 1284 | |
| 1285 | |
| 1286 | procedure TProtocolDecorator.WriteDouble( const d: Double); |
| 1287 | begin |
| 1288 | FWrappedProtocol.WriteDouble( d); |
| 1289 | end; |
| 1290 | |
| 1291 | |
| 1292 | procedure TProtocolDecorator.WriteString( const s: string ); |
| 1293 | begin |
| 1294 | FWrappedProtocol.WriteString( s); |
| 1295 | end; |
| 1296 | |
| 1297 | |
| 1298 | procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString); |
| 1299 | begin |
| 1300 | FWrappedProtocol.WriteAnsiString( s); |
| 1301 | end; |
| 1302 | |
| 1303 | |
| 1304 | procedure TProtocolDecorator.WriteBinary( const b: TBytes); |
| 1305 | begin |
| 1306 | FWrappedProtocol.WriteBinary( b); |
| 1307 | end; |
| 1308 | |
| 1309 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1310 | function TProtocolDecorator.ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1311 | begin |
| 1312 | result := FWrappedProtocol.ReadMessageBegin; |
| 1313 | end; |
| 1314 | |
| 1315 | |
| 1316 | procedure TProtocolDecorator.ReadMessageEnd(); |
| 1317 | begin |
| 1318 | FWrappedProtocol.ReadMessageEnd(); |
| 1319 | end; |
| 1320 | |
| 1321 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1322 | function TProtocolDecorator.ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1323 | begin |
| 1324 | result := FWrappedProtocol.ReadStructBegin; |
| 1325 | end; |
| 1326 | |
| 1327 | |
| 1328 | procedure TProtocolDecorator.ReadStructEnd; |
| 1329 | begin |
| 1330 | FWrappedProtocol.ReadStructEnd; |
| 1331 | end; |
| 1332 | |
| 1333 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1334 | function TProtocolDecorator.ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1335 | begin |
| 1336 | result := FWrappedProtocol.ReadFieldBegin; |
| 1337 | end; |
| 1338 | |
| 1339 | |
| 1340 | procedure TProtocolDecorator.ReadFieldEnd(); |
| 1341 | begin |
| 1342 | FWrappedProtocol.ReadFieldEnd(); |
| 1343 | end; |
| 1344 | |
| 1345 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1346 | function TProtocolDecorator.ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1347 | begin |
| 1348 | result := FWrappedProtocol.ReadMapBegin; |
| 1349 | end; |
| 1350 | |
| 1351 | |
| 1352 | procedure TProtocolDecorator.ReadMapEnd(); |
| 1353 | begin |
| 1354 | FWrappedProtocol.ReadMapEnd(); |
| 1355 | end; |
| 1356 | |
| 1357 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1358 | function TProtocolDecorator.ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1359 | begin |
| 1360 | result := FWrappedProtocol.ReadListBegin; |
| 1361 | end; |
| 1362 | |
| 1363 | |
| 1364 | procedure TProtocolDecorator.ReadListEnd(); |
| 1365 | begin |
| 1366 | FWrappedProtocol.ReadListEnd(); |
| 1367 | end; |
| 1368 | |
| 1369 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1370 | function TProtocolDecorator.ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1371 | begin |
| 1372 | result := FWrappedProtocol.ReadSetBegin; |
| 1373 | end; |
| 1374 | |
| 1375 | |
| 1376 | procedure TProtocolDecorator.ReadSetEnd(); |
| 1377 | begin |
| 1378 | FWrappedProtocol.ReadSetEnd(); |
| 1379 | end; |
| 1380 | |
| 1381 | |
| 1382 | function TProtocolDecorator.ReadBool: Boolean; |
| 1383 | begin |
| 1384 | result := FWrappedProtocol.ReadBool; |
| 1385 | end; |
| 1386 | |
| 1387 | |
| 1388 | function TProtocolDecorator.ReadByte: ShortInt; |
| 1389 | begin |
| 1390 | result := FWrappedProtocol.ReadByte; |
| 1391 | end; |
| 1392 | |
| 1393 | |
| 1394 | function TProtocolDecorator.ReadI16: SmallInt; |
| 1395 | begin |
| 1396 | result := FWrappedProtocol.ReadI16; |
| 1397 | end; |
| 1398 | |
| 1399 | |
| 1400 | function TProtocolDecorator.ReadI32: Integer; |
| 1401 | begin |
| 1402 | result := FWrappedProtocol.ReadI32; |
| 1403 | end; |
| 1404 | |
| 1405 | |
| 1406 | function TProtocolDecorator.ReadI64: Int64; |
| 1407 | begin |
| 1408 | result := FWrappedProtocol.ReadI64; |
| 1409 | end; |
| 1410 | |
| 1411 | |
| 1412 | function TProtocolDecorator.ReadDouble:Double; |
| 1413 | begin |
| 1414 | result := FWrappedProtocol.ReadDouble; |
| 1415 | end; |
| 1416 | |
| 1417 | |
| 1418 | function TProtocolDecorator.ReadBinary: TBytes; |
| 1419 | begin |
| 1420 | result := FWrappedProtocol.ReadBinary; |
| 1421 | end; |
| 1422 | |
| 1423 | |
| 1424 | function TProtocolDecorator.ReadString: string; |
| 1425 | begin |
| 1426 | result := FWrappedProtocol.ReadString; |
| 1427 | end; |
| 1428 | |
| 1429 | |
| 1430 | function TProtocolDecorator.ReadAnsiString: AnsiString; |
| 1431 | begin |
| 1432 | result := FWrappedProtocol.ReadAnsiString; |
| 1433 | end; |
| 1434 | |
| 1435 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame^] | 1436 | function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer; |
| 1437 | begin |
| 1438 | result := FWrappedProtocol.GetMinSerializedSize(aType); |
| 1439 | end; |
| 1440 | |
| 1441 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1442 | { Init helper functions } |
| 1443 | |
| 1444 | procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer); |
| 1445 | begin |
| 1446 | rec.Name := AName; |
| 1447 | rec.Type_ := AMessageType; |
| 1448 | rec.SeqID := ASeqID; |
| 1449 | end; |
| 1450 | |
| 1451 | |
| 1452 | procedure Init( var rec : TThriftStruct; const AName: string = ''); |
| 1453 | begin |
| 1454 | rec.Name := AName; |
| 1455 | end; |
| 1456 | |
| 1457 | |
| 1458 | procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt); |
| 1459 | begin |
| 1460 | rec.Name := AName; |
| 1461 | rec.Type_ := AType; |
| 1462 | rec.Id := AId; |
| 1463 | end; |
| 1464 | |
| 1465 | |
| 1466 | procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer); |
| 1467 | begin |
| 1468 | rec.ValueType := AValueType; |
| 1469 | rec.KeyType := AKeyType; |
| 1470 | rec.Count := ACount; |
| 1471 | end; |
| 1472 | |
| 1473 | |
| 1474 | procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer); |
| 1475 | begin |
| 1476 | rec.Count := ACount; |
| 1477 | rec.ElementType := AElementType; |
| 1478 | end; |
| 1479 | |
| 1480 | |
| 1481 | procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer); |
| 1482 | begin |
| 1483 | rec.Count := ACount; |
| 1484 | rec.ElementType := AElementType; |
| 1485 | end; |
| 1486 | |
| 1487 | |
| 1488 | |
| 1489 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1490 | |
| 1491 | end. |
| 1492 | |