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, |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 34 | Thrift.Configuration, |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 35 | Thrift.Transport; |
| 36 | |
| 37 | type |
| 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 Geyer | f0e6331 | 2015-03-01 18:47:49 +0100 | [diff] [blame] | 62 | const |
| 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 | |
| 71 | type |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 72 | IProtocol = interface; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 73 | |
| 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 107 | IProtocolFactory = interface |
| 108 | ['{7CD64A10-4E9F-4E99-93BF-708A31F4A67B}'] |
| 109 | function GetProtocol( const trans: ITransport): IProtocol; |
| 110 | end; |
| 111 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 112 | TProtocolException = class abstract( TException) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 113 | public |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 114 | 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 Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 123 | strict protected |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 124 | constructor HiddenCreate(const Msg: string); |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 125 | class function GetType: TExceptionType; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 126 | public |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 127 | // 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 Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 130 | 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 Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 133 | end; |
| 134 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 135 | // Needed to remove deprecation warning |
| 136 | TProtocolExceptionSpecialized = class abstract (TProtocolException) |
| 137 | public |
| 138 | constructor Create(const Msg: string); |
| 139 | end; |
| 140 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 141 | TProtocolExceptionUnknown = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 142 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 143 | class function GetType: TProtocolException.TExceptionType; override; |
| 144 | end; |
| 145 | |
| 146 | TProtocolExceptionInvalidData = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 147 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 148 | class function GetType: TProtocolException.TExceptionType; override; |
| 149 | end; |
| 150 | |
| 151 | TProtocolExceptionNegativeSize = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 152 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 153 | class function GetType: TProtocolException.TExceptionType; override; |
| 154 | end; |
| 155 | |
| 156 | TProtocolExceptionSizeLimit = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 157 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 158 | class function GetType: TProtocolException.TExceptionType; override; |
| 159 | end; |
| 160 | |
| 161 | TProtocolExceptionBadVersion = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 162 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 163 | class function GetType: TProtocolException.TExceptionType; override; |
| 164 | end; |
| 165 | |
| 166 | TProtocolExceptionNotImplemented = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 167 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 168 | class function GetType: TProtocolException.TExceptionType; override; |
| 169 | end; |
| 170 | |
| 171 | TProtocolExceptionDepthLimit = class (TProtocolExceptionSpecialized) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 172 | strict protected |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 173 | class function GetType: TProtocolException.TExceptionType; override; |
| 174 | end; |
| 175 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 176 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 177 | |
| 178 | TProtocolUtil = class |
| 179 | public |
| 180 | class procedure Skip( prot: IProtocol; type_: TType); |
| 181 | end; |
| 182 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 183 | IProtocolRecursionTracker = interface |
| 184 | ['{29CA033F-BB56-49B1-9EE3-31B1E82FC7A5}'] |
| 185 | // no members yet |
| 186 | end; |
| 187 | |
| 188 | TProtocolRecursionTrackerImpl = class abstract( TInterfacedObject, IProtocolRecursionTracker) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 189 | strict protected |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 190 | FProtocol : IProtocol; |
| 191 | public |
| 192 | constructor Create( prot : IProtocol); |
| 193 | destructor Destroy; override; |
| 194 | end; |
| 195 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 196 | IProtocol = interface |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 197 | ['{F0040D99-937F-400D-9932-AF04F665899F}'] |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 198 | function GetTransport: ITransport; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 199 | procedure WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 200 | procedure WriteMessageEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 201 | procedure WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 202 | procedure WriteStructEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 203 | procedure WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 204 | procedure WriteFieldEnd; |
| 205 | procedure WriteFieldStop; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 206 | procedure WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 207 | procedure WriteMapEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 208 | procedure WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 209 | procedure WriteListEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 210 | procedure WriteSetBegin( const set_: TThriftSet ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 211 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 222 | function ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 223 | procedure ReadMessageEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 224 | function ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 225 | procedure ReadStructEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 226 | function ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 227 | procedure ReadFieldEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 228 | function ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 229 | procedure ReadMapEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 230 | function ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 231 | procedure ReadListEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 232 | function ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 233 | 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 Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 243 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 244 | function NextRecursionLevel : IProtocolRecursionTracker; |
| 245 | procedure IncrementRecursionDepth; |
| 246 | procedure DecrementRecursionDepth; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 247 | function GetMinSerializedSize( const aType : TType) : Integer; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 248 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 249 | property Transport: ITransport read GetTransport; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 250 | function Configuration : IThriftConfiguration; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 251 | end; |
| 252 | |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame^] | 253 | TProtocolImplClass = class of TProtocolImpl; |
| 254 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 255 | TProtocolImpl = class abstract( TInterfacedObject, IProtocol) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 256 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 257 | FTrans : ITransport; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 258 | FRecursionLimit : Integer; |
| 259 | FRecursionDepth : Integer; |
| 260 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 261 | function NextRecursionLevel : IProtocolRecursionTracker; |
| 262 | procedure IncrementRecursionDepth; |
| 263 | procedure DecrementRecursionDepth; |
| 264 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 265 | 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 Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 271 | function GetTransport: ITransport; |
| 272 | function Configuration : IThriftConfiguration; |
| 273 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 274 | procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 275 | procedure WriteMessageEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 276 | procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 277 | procedure WriteStructEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 278 | procedure WriteFieldBegin( const field: TThriftField); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 279 | procedure WriteFieldEnd; virtual; abstract; |
| 280 | procedure WriteFieldStop; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 281 | procedure WriteMapBegin( const map: TThriftMap); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 282 | procedure WriteMapEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 283 | procedure WriteListBegin( const list: TThriftList); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 284 | procedure WriteListEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 285 | procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 286 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 297 | function ReadMessageBegin: TThriftMessage; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 298 | procedure ReadMessageEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 299 | function ReadStructBegin: TThriftStruct; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 300 | procedure ReadStructEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 301 | function ReadFieldBegin: TThriftField; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 302 | procedure ReadFieldEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 303 | function ReadMapBegin: TThriftMap; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 304 | procedure ReadMapEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 305 | function ReadListBegin: TThriftList; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 306 | procedure ReadListEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 307 | function ReadSetBegin: TThriftSet; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 308 | 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 Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 319 | property Transport: ITransport read GetTransport; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 320 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 321 | public |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame^] | 322 | constructor Create( const aTransport : ITransport); virtual; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 323 | end; |
| 324 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 325 | IBase = interface( ISupportsToString) |
| 326 | ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}'] |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 327 | procedure Read( const iprot: IProtocol); |
| 328 | procedure Write( const iprot: IProtocol); |
| 329 | end; |
| 330 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 331 | |
| 332 | TBinaryProtocolImpl = class( TProtocolImpl ) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 333 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 334 | const |
| 335 | VERSION_MASK : Cardinal = $ffff0000; |
| 336 | VERSION_1 : Cardinal = $80010000; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 337 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 338 | FStrictRead : Boolean; |
| 339 | FStrictWrite : Boolean; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 340 | function GetMinSerializedSize( const aType : TType) : Integer; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 341 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 342 | strict private |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 343 | 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] | 344 | function ReadStringBody( size: Integer): string; |
| 345 | |
| 346 | public |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 347 | type |
| 348 | TFactory = class( TInterfacedObject, IProtocolFactory) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 349 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 350 | FStrictRead : Boolean; |
| 351 | FStrictWrite : Boolean; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 352 | function GetProtocol( const trans: ITransport): IProtocol; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 353 | public |
| 354 | constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 355 | end; |
| 356 | |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame^] | 357 | constructor Create( const trans: ITransport); overload; override; |
| 358 | constructor Create( const trans: ITransport; strictRead, strictWrite: Boolean); reintroduce; overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 359 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 360 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 361 | procedure WriteMessageEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 362 | procedure WriteStructBegin( const struc: TThriftStruct); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 363 | procedure WriteStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 364 | procedure WriteFieldBegin( const field: TThriftField); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 365 | procedure WriteFieldEnd; override; |
| 366 | procedure WriteFieldStop; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 367 | procedure WriteMapBegin( const map: TThriftMap); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 368 | procedure WriteMapEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 369 | procedure WriteListBegin( const list: TThriftList); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 370 | procedure WriteListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 371 | procedure WriteSetBegin( const set_: TThriftSet ); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 372 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 381 | function ReadMessageBegin: TThriftMessage; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 382 | procedure ReadMessageEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 383 | function ReadStructBegin: TThriftStruct; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 384 | procedure ReadStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 385 | function ReadFieldBegin: TThriftField; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 386 | procedure ReadFieldEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 387 | function ReadMapBegin: TThriftMap; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 388 | procedure ReadMapEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 389 | function ReadListBegin: TThriftList; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 390 | procedure ReadListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 391 | function ReadSetBegin: TThriftSet; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 392 | 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 Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 411 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 412 | FWrappedProtocol : IProtocol; |
| 413 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 414 | strict protected |
| 415 | function GetMinSerializedSize( const aType : TType) : Integer; override; |
| 416 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 417 | public |
| 418 | // Encloses the specified protocol. |
| 419 | // All operations will be forward to the given protocol. Must be non-null. |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame^] | 420 | constructor Create( const aProtocol : IProtocol); reintroduce; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 421 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 422 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 423 | procedure WriteMessageEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 424 | procedure WriteStructBegin( const struc: TThriftStruct); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 425 | procedure WriteStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 426 | procedure WriteFieldBegin( const field: TThriftField); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 427 | procedure WriteFieldEnd; override; |
| 428 | procedure WriteFieldStop; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 429 | procedure WriteMapBegin( const map: TThriftMap); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 430 | procedure WriteMapEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 431 | procedure WriteListBegin( const list: TThriftList); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 432 | procedure WriteListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 433 | procedure WriteSetBegin( const set_: TThriftSet ); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 434 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 445 | function ReadMessageBegin: TThriftMessage; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 446 | procedure ReadMessageEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 447 | function ReadStructBegin: TThriftStruct; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 448 | procedure ReadStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 449 | function ReadFieldBegin: TThriftField; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 450 | procedure ReadFieldEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 451 | function ReadMapBegin: TThriftMap; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 452 | procedure ReadMapEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 453 | function ReadListBegin: TThriftList; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 454 | procedure ReadListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 455 | function ReadSetBegin: TThriftSet; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 456 | 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 | |
| 469 | type |
| 470 | IRequestEvents = interface |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 471 | ['{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 Geyer | d430bbd | 2013-09-26 23:37:54 +0200 | [diff] [blame] | 502 | 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] | 503 | end; |
| 504 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 505 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 506 | procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline; |
| 507 | procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline; |
| 508 | procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline; |
| 509 | procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 510 | procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 511 | procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 512 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 513 | |
| 514 | implementation |
| 515 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 516 | function ConvertInt64ToDouble( const n: Int64): Double; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 517 | begin |
| 518 | ASSERT( SizeOf(n) = SizeOf(Result)); |
| 519 | System.Move( n, Result, SizeOf(Result)); |
| 520 | end; |
| 521 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 522 | function ConvertDoubleToInt64( const d: Double): Int64; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 523 | begin |
| 524 | ASSERT( SizeOf(d) = SizeOf(Result)); |
| 525 | System.Move( d, Result, SizeOf(Result)); |
| 526 | end; |
| 527 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 528 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 529 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 530 | { TProtocolRecursionTrackerImpl } |
| 531 | |
| 532 | constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol); |
| 533 | begin |
| 534 | inherited Create; |
| 535 | |
| 536 | // storing the pointer *after* the (successful) increment is important here |
| 537 | prot.IncrementRecursionDepth; |
| 538 | FProtocol := prot; |
| 539 | end; |
| 540 | |
| 541 | destructor TProtocolRecursionTrackerImpl.Destroy; |
| 542 | begin |
| 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; |
| 552 | end; |
| 553 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 554 | { TProtocolImpl } |
| 555 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 556 | constructor TProtocolImpl.Create( const aTransport : ITransport); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 557 | begin |
| 558 | inherited Create; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 559 | FTrans := aTransport; |
| 560 | FRecursionLimit := aTransport.Configuration.RecursionLimit; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 561 | FRecursionDepth := 0; |
| 562 | end; |
| 563 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 564 | function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker; |
| 565 | begin |
| 566 | result := TProtocolRecursionTrackerImpl.Create(Self); |
| 567 | end; |
| 568 | |
| 569 | procedure TProtocolImpl.IncrementRecursionDepth; |
| 570 | begin |
| 571 | if FRecursionDepth < FRecursionLimit |
| 572 | then Inc(FRecursionDepth) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 573 | else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded'); |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 574 | end; |
| 575 | |
| 576 | procedure TProtocolImpl.DecrementRecursionDepth; |
| 577 | begin |
| 578 | Dec(FRecursionDepth) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 579 | end; |
| 580 | |
| 581 | function TProtocolImpl.GetTransport: ITransport; |
| 582 | begin |
| 583 | Result := FTrans; |
| 584 | end; |
| 585 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 586 | function TProtocolImpl.Configuration : IThriftConfiguration; |
| 587 | begin |
| 588 | Result := FTrans.Configuration; |
| 589 | end; |
| 590 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 591 | procedure TProtocolImpl.Reset; |
| 592 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 593 | FTrans.ResetConsumedMessageSize; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 594 | end; |
| 595 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 596 | function TProtocolImpl.ReadAnsiString: AnsiString; |
| 597 | var |
| 598 | b : TBytes; |
| 599 | len : Integer; |
| 600 | begin |
| 601 | Result := ''; |
| 602 | b := ReadBinary; |
| 603 | len := Length( b ); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 604 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 605 | SetLength( Result, len); |
| 606 | System.Move( b[0], Pointer(Result)^, len ); |
| 607 | end; |
| 608 | end; |
| 609 | |
| 610 | function TProtocolImpl.ReadString: string; |
| 611 | begin |
| 612 | Result := TEncoding.UTF8.GetString( ReadBinary ); |
| 613 | end; |
| 614 | |
| 615 | procedure TProtocolImpl.WriteAnsiString(const s: AnsiString); |
| 616 | var |
| 617 | b : TBytes; |
| 618 | len : Integer; |
| 619 | begin |
| 620 | len := Length(s); |
| 621 | SetLength( b, len); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 622 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 623 | System.Move( Pointer(s)^, b[0], len ); |
| 624 | end; |
| 625 | WriteBinary( b ); |
| 626 | end; |
| 627 | |
| 628 | procedure TProtocolImpl.WriteString(const s: string); |
| 629 | var |
| 630 | b : TBytes; |
| 631 | begin |
| 632 | b := TEncoding.UTF8.GetBytes(s); |
| 633 | WriteBinary( b ); |
| 634 | end; |
| 635 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 636 | |
| 637 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList); |
| 638 | begin |
| 639 | FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType)); |
| 640 | end; |
| 641 | |
| 642 | |
| 643 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet); |
| 644 | begin |
| 645 | FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType)); |
| 646 | end; |
| 647 | |
| 648 | |
| 649 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 650 | var nPairSize : Integer; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 651 | begin |
| 652 | nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType); |
| 653 | FTrans.CheckReadBytesAvailable( value.Count * nPairSize); |
| 654 | end; |
| 655 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 656 | { TProtocolUtil } |
| 657 | |
| 658 | class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 659 | var field : TThriftField; |
| 660 | map : TThriftMap; |
| 661 | set_ : TThriftSet; |
| 662 | list : TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 663 | i : Integer; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 664 | tracker : IProtocolRecursionTracker; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 665 | begin |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 666 | tracker := prot.NextRecursionLevel; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 667 | 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 Geyer | 5f723cd | 2017-01-10 21:57:48 +0100 | [diff] [blame] | 713 | raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_))); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 714 | end; |
| 715 | end; |
| 716 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 717 | |
| 718 | { TBinaryProtocolImpl } |
| 719 | |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame^] | 720 | constructor TBinaryProtocolImpl.Create( const trans: ITransport); |
| 721 | begin |
| 722 | // call the real CTOR |
| 723 | Self.Create( trans, FALSE, TRUE); |
| 724 | end; |
| 725 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 726 | constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 727 | begin |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 728 | inherited Create( trans); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 729 | FStrictRead := strictRead; |
| 730 | FStrictWrite := strictWrite; |
| 731 | end; |
| 732 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 733 | 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] | 734 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 735 | Result := FTrans.ReadAll( pBuf, buflen, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 736 | end; |
| 737 | |
| 738 | function TBinaryProtocolImpl.ReadBinary: TBytes; |
| 739 | var |
| 740 | size : Integer; |
| 741 | buf : TBytes; |
| 742 | begin |
| 743 | size := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 744 | FTrans.CheckReadBytesAvailable( size); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 745 | SetLength( buf, size); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 746 | FTrans.ReadAll( buf, 0, size); |
| 747 | Result := buf; |
| 748 | end; |
| 749 | |
| 750 | function TBinaryProtocolImpl.ReadBool: Boolean; |
| 751 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 752 | Result := (ReadByte = 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 753 | end; |
| 754 | |
| 755 | function TBinaryProtocolImpl.ReadByte: ShortInt; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 756 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 757 | ReadAll( @result, SizeOf(result), 0, 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 758 | end; |
| 759 | |
| 760 | function TBinaryProtocolImpl.ReadDouble: Double; |
| 761 | begin |
| 762 | Result := ConvertInt64ToDouble( ReadI64 ) |
| 763 | end; |
| 764 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 765 | function TBinaryProtocolImpl.ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 766 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 767 | Init( result, '', TType( ReadByte), 0); |
| 768 | if ( result.Type_ <> TType.Stop ) then begin |
| 769 | result.Id := ReadI16; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 770 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 771 | end; |
| 772 | |
| 773 | procedure TBinaryProtocolImpl.ReadFieldEnd; |
| 774 | begin |
| 775 | |
| 776 | end; |
| 777 | |
| 778 | function TBinaryProtocolImpl.ReadI16: SmallInt; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 779 | var i16in : packed array[0..1] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 780 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 781 | ReadAll( @i16in, Sizeof(i16in), 0, 2); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 782 | Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF)); |
| 783 | end; |
| 784 | |
| 785 | function TBinaryProtocolImpl.ReadI32: Integer; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 786 | var i32in : packed array[0..3] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 787 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 788 | ReadAll( @i32in, SizeOf(i32in), 0, 4); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 789 | |
| 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 | |
| 796 | end; |
| 797 | |
| 798 | function TBinaryProtocolImpl.ReadI64: Int64; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 799 | var i64in : packed array[0..7] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 800 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 801 | ReadAll( @i64in, SizeOf(i64in), 0, 8); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 802 | 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)); |
| 811 | end; |
| 812 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 813 | function TBinaryProtocolImpl.ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 814 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 815 | result.ElementType := TType(ReadByte); |
| 816 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 817 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 818 | end; |
| 819 | |
| 820 | procedure TBinaryProtocolImpl.ReadListEnd; |
| 821 | begin |
| 822 | |
| 823 | end; |
| 824 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 825 | function TBinaryProtocolImpl.ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 826 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 827 | result.KeyType := TType(ReadByte); |
| 828 | result.ValueType := TType(ReadByte); |
| 829 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 830 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 831 | end; |
| 832 | |
| 833 | procedure TBinaryProtocolImpl.ReadMapEnd; |
| 834 | begin |
| 835 | |
| 836 | end; |
| 837 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 838 | function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 839 | var |
| 840 | size : Integer; |
| 841 | version : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 842 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 843 | Reset; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 844 | Init( result); |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 845 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 846 | size := ReadI32; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 847 | if (size < 0) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 848 | version := size and Integer( VERSION_MASK); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 849 | if ( version <> Integer( VERSION_1)) then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 850 | raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 851 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 852 | result.Type_ := TMessageType( size and $000000ff); |
| 853 | result.Name := ReadString; |
| 854 | result.SeqID := ReadI32; |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 855 | Exit; |
| 856 | end; |
| 857 | |
| 858 | try |
| 859 | if FStrictRead |
| 860 | then raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' ); |
| 861 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 862 | result.Name := ReadStringBody( size ); |
| 863 | result.Type_ := TMessageType( ReadByte ); |
| 864 | result.SeqID := ReadI32; |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 865 | except |
| 866 | if CharUtils.IsHtmlDoctype(size) |
| 867 | then raise TProtocolExceptionInvalidData.Create('Remote end sends HTML instead of data') |
| 868 | else raise; // something else |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 869 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 870 | end; |
| 871 | |
| 872 | procedure TBinaryProtocolImpl.ReadMessageEnd; |
| 873 | begin |
| 874 | inherited; |
| 875 | |
| 876 | end; |
| 877 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 878 | function TBinaryProtocolImpl.ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 879 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 880 | result.ElementType := TType(ReadByte); |
| 881 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 882 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 883 | end; |
| 884 | |
| 885 | procedure TBinaryProtocolImpl.ReadSetEnd; |
| 886 | begin |
| 887 | |
| 888 | end; |
| 889 | |
| 890 | function TBinaryProtocolImpl.ReadStringBody( size: Integer): string; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 891 | var buf : TBytes; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 892 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 893 | FTrans.CheckReadBytesAvailable( size); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 894 | SetLength( buf, size); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 895 | FTrans.ReadAll( buf, 0, size ); |
| 896 | Result := TEncoding.UTF8.GetString( buf); |
| 897 | end; |
| 898 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 899 | function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 900 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 901 | Init( Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 902 | end; |
| 903 | |
| 904 | procedure TBinaryProtocolImpl.ReadStructEnd; |
| 905 | begin |
| 906 | inherited; |
| 907 | |
| 908 | end; |
| 909 | |
| 910 | procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes); |
| 911 | var iLen : Integer; |
| 912 | begin |
| 913 | iLen := Length(b); |
| 914 | WriteI32( iLen); |
| 915 | if iLen > 0 then FTrans.Write(b, 0, iLen); |
| 916 | end; |
| 917 | |
| 918 | procedure TBinaryProtocolImpl.WriteBool(b: Boolean); |
| 919 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 920 | if b then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 921 | WriteByte( 1 ); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 922 | end else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 923 | WriteByte( 0 ); |
| 924 | end; |
| 925 | end; |
| 926 | |
| 927 | procedure TBinaryProtocolImpl.WriteByte(b: ShortInt); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 928 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 929 | FTrans.Write( @b, 0, 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 930 | end; |
| 931 | |
| 932 | procedure TBinaryProtocolImpl.WriteDouble( const d: Double); |
| 933 | begin |
| 934 | WriteI64(ConvertDoubleToInt64(d)); |
| 935 | end; |
| 936 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 937 | procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 938 | begin |
| 939 | WriteByte(ShortInt(field.Type_)); |
| 940 | WriteI16(field.ID); |
| 941 | end; |
| 942 | |
| 943 | procedure TBinaryProtocolImpl.WriteFieldEnd; |
| 944 | begin |
| 945 | |
| 946 | end; |
| 947 | |
| 948 | procedure TBinaryProtocolImpl.WriteFieldStop; |
| 949 | begin |
| 950 | WriteByte(ShortInt(TType.Stop)); |
| 951 | end; |
| 952 | |
| 953 | procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 954 | var i16out : packed array[0..1] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 955 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 956 | i16out[0] := Byte($FF and (i16 shr 8)); |
| 957 | i16out[1] := Byte($FF and i16); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 958 | FTrans.Write( @i16out, 0, 2); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 959 | end; |
| 960 | |
| 961 | procedure TBinaryProtocolImpl.WriteI32(i32: Integer); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 962 | var i32out : packed array[0..3] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 963 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 964 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 968 | FTrans.Write( @i32out, 0, 4); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 969 | end; |
| 970 | |
| 971 | procedure TBinaryProtocolImpl.WriteI64( const i64: Int64); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 972 | var i64out : packed array[0..7] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 973 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 974 | 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 Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 982 | FTrans.Write( @i64out, 0, 8); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 983 | end; |
| 984 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 985 | procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 986 | begin |
| 987 | WriteByte(ShortInt(list.ElementType)); |
| 988 | WriteI32(list.Count); |
| 989 | end; |
| 990 | |
| 991 | procedure TBinaryProtocolImpl.WriteListEnd; |
| 992 | begin |
| 993 | |
| 994 | end; |
| 995 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 996 | procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 997 | begin |
| 998 | WriteByte(ShortInt(map.KeyType)); |
| 999 | WriteByte(ShortInt(map.ValueType)); |
| 1000 | WriteI32(map.Count); |
| 1001 | end; |
| 1002 | |
| 1003 | procedure TBinaryProtocolImpl.WriteMapEnd; |
| 1004 | begin |
| 1005 | |
| 1006 | end; |
| 1007 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1008 | procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1009 | var version : Cardinal; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1010 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1011 | Reset; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1012 | if FStrictWrite then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1013 | version := VERSION_1 or Cardinal( msg.Type_); |
| 1014 | WriteI32( Integer( version) ); |
| 1015 | WriteString( msg.Name); |
| 1016 | WriteI32( msg.SeqID); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1017 | end else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1018 | WriteString( msg.Name); |
| 1019 | WriteByte(ShortInt( msg.Type_)); |
| 1020 | WriteI32( msg.SeqID); |
| 1021 | end; |
| 1022 | end; |
| 1023 | |
| 1024 | procedure TBinaryProtocolImpl.WriteMessageEnd; |
| 1025 | begin |
| 1026 | |
| 1027 | end; |
| 1028 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1029 | procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1030 | begin |
| 1031 | WriteByte(ShortInt(set_.ElementType)); |
| 1032 | WriteI32(set_.Count); |
| 1033 | end; |
| 1034 | |
| 1035 | procedure TBinaryProtocolImpl.WriteSetEnd; |
| 1036 | begin |
| 1037 | |
| 1038 | end; |
| 1039 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1040 | procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1041 | begin |
| 1042 | |
| 1043 | end; |
| 1044 | |
| 1045 | procedure TBinaryProtocolImpl.WriteStructEnd; |
| 1046 | begin |
| 1047 | |
| 1048 | end; |
| 1049 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1050 | function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer; |
| 1051 | // Return the minimum number of bytes a type will consume on the wire |
| 1052 | begin |
| 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; |
| 1070 | end; |
| 1071 | |
| 1072 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1073 | { TProtocolException } |
| 1074 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1075 | constructor TProtocolException.HiddenCreate(const Msg: string); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1076 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1077 | inherited Create(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1078 | end; |
| 1079 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1080 | class function TProtocolException.Create(const Msg: string): TProtocolException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1081 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1082 | Result := TProtocolExceptionUnknown.Create(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1083 | end; |
| 1084 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1085 | class function TProtocolException.Create: TProtocolException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1086 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1087 | Result := TProtocolExceptionUnknown.Create(''); |
| 1088 | end; |
| 1089 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1090 | class function TProtocolException.Create(aType: TExceptionType): TProtocolException; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1091 | begin |
| 1092 | {$WARN SYMBOL_DEPRECATED OFF} |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1093 | Result := Create(aType, ''); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1094 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
| 1095 | end; |
| 1096 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1097 | class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1098 | begin |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1099 | 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 Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1106 | else |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1107 | ASSERT( TExceptionType.UNKNOWN = aType); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1108 | Result := TProtocolExceptionUnknown.Create(msg); |
| 1109 | end; |
| 1110 | end; |
| 1111 | |
| 1112 | { TProtocolExceptionSpecialized } |
| 1113 | |
| 1114 | constructor TProtocolExceptionSpecialized.Create(const Msg: string); |
| 1115 | begin |
| 1116 | inherited HiddenCreate(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1117 | end; |
| 1118 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1119 | { specialized TProtocolExceptions } |
| 1120 | |
| 1121 | class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType; |
| 1122 | begin |
| 1123 | result := TExceptionType.UNKNOWN; |
| 1124 | end; |
| 1125 | |
| 1126 | class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType; |
| 1127 | begin |
| 1128 | result := TExceptionType.INVALID_DATA; |
| 1129 | end; |
| 1130 | |
| 1131 | class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType; |
| 1132 | begin |
| 1133 | result := TExceptionType.NEGATIVE_SIZE; |
| 1134 | end; |
| 1135 | |
| 1136 | class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType; |
| 1137 | begin |
| 1138 | result := TExceptionType.SIZE_LIMIT; |
| 1139 | end; |
| 1140 | |
| 1141 | class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType; |
| 1142 | begin |
| 1143 | result := TExceptionType.BAD_VERSION; |
| 1144 | end; |
| 1145 | |
| 1146 | class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType; |
| 1147 | begin |
| 1148 | result := TExceptionType.NOT_IMPLEMENTED; |
| 1149 | end; |
| 1150 | |
| 1151 | class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType; |
| 1152 | begin |
| 1153 | result := TExceptionType.DEPTH_LIMIT; |
| 1154 | end; |
| 1155 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1156 | { TBinaryProtocolImpl.TFactory } |
| 1157 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1158 | constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1159 | begin |
| 1160 | inherited Create; |
| 1161 | FStrictRead := AStrictRead; |
| 1162 | FStrictWrite := AStrictWrite; |
| 1163 | end; |
| 1164 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1165 | function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol; |
| 1166 | begin |
| 1167 | Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite); |
| 1168 | end; |
| 1169 | |
| 1170 | |
| 1171 | { TProtocolDecorator } |
| 1172 | |
| 1173 | constructor TProtocolDecorator.Create( const aProtocol : IProtocol); |
| 1174 | begin |
| 1175 | ASSERT( aProtocol <> nil); |
| 1176 | inherited Create( aProtocol.Transport); |
| 1177 | FWrappedProtocol := aProtocol; |
| 1178 | end; |
| 1179 | |
| 1180 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1181 | procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1182 | begin |
| 1183 | FWrappedProtocol.WriteMessageBegin( msg); |
| 1184 | end; |
| 1185 | |
| 1186 | |
| 1187 | procedure TProtocolDecorator.WriteMessageEnd; |
| 1188 | begin |
| 1189 | FWrappedProtocol.WriteMessageEnd; |
| 1190 | end; |
| 1191 | |
| 1192 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1193 | procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1194 | begin |
| 1195 | FWrappedProtocol.WriteStructBegin( struc); |
| 1196 | end; |
| 1197 | |
| 1198 | |
| 1199 | procedure TProtocolDecorator.WriteStructEnd; |
| 1200 | begin |
| 1201 | FWrappedProtocol.WriteStructEnd; |
| 1202 | end; |
| 1203 | |
| 1204 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1205 | procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1206 | begin |
| 1207 | FWrappedProtocol.WriteFieldBegin( field); |
| 1208 | end; |
| 1209 | |
| 1210 | |
| 1211 | procedure TProtocolDecorator.WriteFieldEnd; |
| 1212 | begin |
| 1213 | FWrappedProtocol.WriteFieldEnd; |
| 1214 | end; |
| 1215 | |
| 1216 | |
| 1217 | procedure TProtocolDecorator.WriteFieldStop; |
| 1218 | begin |
| 1219 | FWrappedProtocol.WriteFieldStop; |
| 1220 | end; |
| 1221 | |
| 1222 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1223 | procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1224 | begin |
| 1225 | FWrappedProtocol.WriteMapBegin( map); |
| 1226 | end; |
| 1227 | |
| 1228 | |
| 1229 | procedure TProtocolDecorator.WriteMapEnd; |
| 1230 | begin |
| 1231 | FWrappedProtocol.WriteMapEnd; |
| 1232 | end; |
| 1233 | |
| 1234 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1235 | procedure TProtocolDecorator.WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1236 | begin |
| 1237 | FWrappedProtocol.WriteListBegin( list); |
| 1238 | end; |
| 1239 | |
| 1240 | |
| 1241 | procedure TProtocolDecorator.WriteListEnd(); |
| 1242 | begin |
| 1243 | FWrappedProtocol.WriteListEnd(); |
| 1244 | end; |
| 1245 | |
| 1246 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1247 | procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1248 | begin |
| 1249 | FWrappedProtocol.WriteSetBegin( set_); |
| 1250 | end; |
| 1251 | |
| 1252 | |
| 1253 | procedure TProtocolDecorator.WriteSetEnd(); |
| 1254 | begin |
| 1255 | FWrappedProtocol.WriteSetEnd(); |
| 1256 | end; |
| 1257 | |
| 1258 | |
| 1259 | procedure TProtocolDecorator.WriteBool( b: Boolean); |
| 1260 | begin |
| 1261 | FWrappedProtocol.WriteBool( b); |
| 1262 | end; |
| 1263 | |
| 1264 | |
| 1265 | procedure TProtocolDecorator.WriteByte( b: ShortInt); |
| 1266 | begin |
| 1267 | FWrappedProtocol.WriteByte( b); |
| 1268 | end; |
| 1269 | |
| 1270 | |
| 1271 | procedure TProtocolDecorator.WriteI16( i16: SmallInt); |
| 1272 | begin |
| 1273 | FWrappedProtocol.WriteI16( i16); |
| 1274 | end; |
| 1275 | |
| 1276 | |
| 1277 | procedure TProtocolDecorator.WriteI32( i32: Integer); |
| 1278 | begin |
| 1279 | FWrappedProtocol.WriteI32( i32); |
| 1280 | end; |
| 1281 | |
| 1282 | |
| 1283 | procedure TProtocolDecorator.WriteI64( const i64: Int64); |
| 1284 | begin |
| 1285 | FWrappedProtocol.WriteI64( i64); |
| 1286 | end; |
| 1287 | |
| 1288 | |
| 1289 | procedure TProtocolDecorator.WriteDouble( const d: Double); |
| 1290 | begin |
| 1291 | FWrappedProtocol.WriteDouble( d); |
| 1292 | end; |
| 1293 | |
| 1294 | |
| 1295 | procedure TProtocolDecorator.WriteString( const s: string ); |
| 1296 | begin |
| 1297 | FWrappedProtocol.WriteString( s); |
| 1298 | end; |
| 1299 | |
| 1300 | |
| 1301 | procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString); |
| 1302 | begin |
| 1303 | FWrappedProtocol.WriteAnsiString( s); |
| 1304 | end; |
| 1305 | |
| 1306 | |
| 1307 | procedure TProtocolDecorator.WriteBinary( const b: TBytes); |
| 1308 | begin |
| 1309 | FWrappedProtocol.WriteBinary( b); |
| 1310 | end; |
| 1311 | |
| 1312 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1313 | function TProtocolDecorator.ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1314 | begin |
| 1315 | result := FWrappedProtocol.ReadMessageBegin; |
| 1316 | end; |
| 1317 | |
| 1318 | |
| 1319 | procedure TProtocolDecorator.ReadMessageEnd(); |
| 1320 | begin |
| 1321 | FWrappedProtocol.ReadMessageEnd(); |
| 1322 | end; |
| 1323 | |
| 1324 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1325 | function TProtocolDecorator.ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1326 | begin |
| 1327 | result := FWrappedProtocol.ReadStructBegin; |
| 1328 | end; |
| 1329 | |
| 1330 | |
| 1331 | procedure TProtocolDecorator.ReadStructEnd; |
| 1332 | begin |
| 1333 | FWrappedProtocol.ReadStructEnd; |
| 1334 | end; |
| 1335 | |
| 1336 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1337 | function TProtocolDecorator.ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1338 | begin |
| 1339 | result := FWrappedProtocol.ReadFieldBegin; |
| 1340 | end; |
| 1341 | |
| 1342 | |
| 1343 | procedure TProtocolDecorator.ReadFieldEnd(); |
| 1344 | begin |
| 1345 | FWrappedProtocol.ReadFieldEnd(); |
| 1346 | end; |
| 1347 | |
| 1348 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1349 | function TProtocolDecorator.ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1350 | begin |
| 1351 | result := FWrappedProtocol.ReadMapBegin; |
| 1352 | end; |
| 1353 | |
| 1354 | |
| 1355 | procedure TProtocolDecorator.ReadMapEnd(); |
| 1356 | begin |
| 1357 | FWrappedProtocol.ReadMapEnd(); |
| 1358 | end; |
| 1359 | |
| 1360 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1361 | function TProtocolDecorator.ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1362 | begin |
| 1363 | result := FWrappedProtocol.ReadListBegin; |
| 1364 | end; |
| 1365 | |
| 1366 | |
| 1367 | procedure TProtocolDecorator.ReadListEnd(); |
| 1368 | begin |
| 1369 | FWrappedProtocol.ReadListEnd(); |
| 1370 | end; |
| 1371 | |
| 1372 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1373 | function TProtocolDecorator.ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1374 | begin |
| 1375 | result := FWrappedProtocol.ReadSetBegin; |
| 1376 | end; |
| 1377 | |
| 1378 | |
| 1379 | procedure TProtocolDecorator.ReadSetEnd(); |
| 1380 | begin |
| 1381 | FWrappedProtocol.ReadSetEnd(); |
| 1382 | end; |
| 1383 | |
| 1384 | |
| 1385 | function TProtocolDecorator.ReadBool: Boolean; |
| 1386 | begin |
| 1387 | result := FWrappedProtocol.ReadBool; |
| 1388 | end; |
| 1389 | |
| 1390 | |
| 1391 | function TProtocolDecorator.ReadByte: ShortInt; |
| 1392 | begin |
| 1393 | result := FWrappedProtocol.ReadByte; |
| 1394 | end; |
| 1395 | |
| 1396 | |
| 1397 | function TProtocolDecorator.ReadI16: SmallInt; |
| 1398 | begin |
| 1399 | result := FWrappedProtocol.ReadI16; |
| 1400 | end; |
| 1401 | |
| 1402 | |
| 1403 | function TProtocolDecorator.ReadI32: Integer; |
| 1404 | begin |
| 1405 | result := FWrappedProtocol.ReadI32; |
| 1406 | end; |
| 1407 | |
| 1408 | |
| 1409 | function TProtocolDecorator.ReadI64: Int64; |
| 1410 | begin |
| 1411 | result := FWrappedProtocol.ReadI64; |
| 1412 | end; |
| 1413 | |
| 1414 | |
| 1415 | function TProtocolDecorator.ReadDouble:Double; |
| 1416 | begin |
| 1417 | result := FWrappedProtocol.ReadDouble; |
| 1418 | end; |
| 1419 | |
| 1420 | |
| 1421 | function TProtocolDecorator.ReadBinary: TBytes; |
| 1422 | begin |
| 1423 | result := FWrappedProtocol.ReadBinary; |
| 1424 | end; |
| 1425 | |
| 1426 | |
| 1427 | function TProtocolDecorator.ReadString: string; |
| 1428 | begin |
| 1429 | result := FWrappedProtocol.ReadString; |
| 1430 | end; |
| 1431 | |
| 1432 | |
| 1433 | function TProtocolDecorator.ReadAnsiString: AnsiString; |
| 1434 | begin |
| 1435 | result := FWrappedProtocol.ReadAnsiString; |
| 1436 | end; |
| 1437 | |
| 1438 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1439 | function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer; |
| 1440 | begin |
| 1441 | result := FWrappedProtocol.GetMinSerializedSize(aType); |
| 1442 | end; |
| 1443 | |
| 1444 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1445 | { Init helper functions } |
| 1446 | |
| 1447 | procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer); |
| 1448 | begin |
| 1449 | rec.Name := AName; |
| 1450 | rec.Type_ := AMessageType; |
| 1451 | rec.SeqID := ASeqID; |
| 1452 | end; |
| 1453 | |
| 1454 | |
| 1455 | procedure Init( var rec : TThriftStruct; const AName: string = ''); |
| 1456 | begin |
| 1457 | rec.Name := AName; |
| 1458 | end; |
| 1459 | |
| 1460 | |
| 1461 | procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt); |
| 1462 | begin |
| 1463 | rec.Name := AName; |
| 1464 | rec.Type_ := AType; |
| 1465 | rec.Id := AId; |
| 1466 | end; |
| 1467 | |
| 1468 | |
| 1469 | procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer); |
| 1470 | begin |
| 1471 | rec.ValueType := AValueType; |
| 1472 | rec.KeyType := AKeyType; |
| 1473 | rec.Count := ACount; |
| 1474 | end; |
| 1475 | |
| 1476 | |
| 1477 | procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer); |
| 1478 | begin |
| 1479 | rec.Count := ACount; |
| 1480 | rec.ElementType := AElementType; |
| 1481 | end; |
| 1482 | |
| 1483 | |
| 1484 | procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer); |
| 1485 | begin |
| 1486 | rec.Count := ACount; |
| 1487 | rec.ElementType := AElementType; |
| 1488 | end; |
| 1489 | |
| 1490 | |
| 1491 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1492 | end. |
| 1493 | |