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 | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 196 | IThriftBytes = interface; // forward |
| 197 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 198 | IProtocol = interface |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 199 | ['{6067A28E-15BF-4C9D-9A6F-D991BB3DCB85}'] |
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); |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 222 | procedure WriteBinary( const b: TBytes); overload; |
| 223 | procedure WriteBinary( const b: IThriftBytes); overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 224 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 225 | function ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 226 | procedure ReadMessageEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 227 | function ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 228 | procedure ReadStructEnd; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 229 | function ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 230 | procedure ReadFieldEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 231 | function ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 232 | procedure ReadMapEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 233 | function ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 234 | procedure ReadListEnd(); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 235 | function ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 236 | procedure ReadSetEnd(); |
| 237 | function ReadBool: Boolean; |
| 238 | function ReadByte: ShortInt; |
| 239 | function ReadI16: SmallInt; |
| 240 | function ReadI32: Integer; |
| 241 | function ReadI64: Int64; |
| 242 | function ReadDouble:Double; |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 243 | function ReadBinary: TBytes; // IMPORTANT: this is NOT safe across module boundaries |
| 244 | function ReadBinaryCOM : IThriftBytes; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 245 | function ReadString: string; |
| 246 | function ReadAnsiString: AnsiString; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 247 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 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 | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 254 | function Configuration : IThriftConfiguration; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 255 | end; |
| 256 | |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame] | 257 | TProtocolImplClass = class of TProtocolImpl; |
| 258 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 259 | TProtocolImpl = class abstract( TInterfacedObject, IProtocol) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 260 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 261 | FTrans : ITransport; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 262 | FRecursionLimit : Integer; |
| 263 | FRecursionDepth : Integer; |
| 264 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 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 | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 275 | function GetTransport: ITransport; |
| 276 | function Configuration : IThriftConfiguration; |
| 277 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 278 | procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 279 | procedure WriteMessageEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 280 | procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 281 | procedure WriteStructEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 282 | procedure WriteFieldBegin( const field: TThriftField); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 283 | procedure WriteFieldEnd; virtual; abstract; |
| 284 | procedure WriteFieldStop; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 285 | procedure WriteMapBegin( const map: TThriftMap); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 286 | procedure WriteMapEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 287 | procedure WriteListBegin( const list: TThriftList); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 288 | procedure WriteListEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 289 | procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 290 | procedure WriteSetEnd(); virtual; abstract; |
| 291 | procedure WriteBool( b: Boolean); virtual; abstract; |
| 292 | procedure WriteByte( b: ShortInt); virtual; abstract; |
| 293 | procedure WriteI16( i16: SmallInt); virtual; abstract; |
| 294 | procedure WriteI32( i32: Integer); virtual; abstract; |
| 295 | procedure WriteI64( const i64: Int64); virtual; abstract; |
| 296 | procedure WriteDouble( const d: Double); virtual; abstract; |
| 297 | procedure WriteString( const s: string ); virtual; |
| 298 | procedure WriteAnsiString( const s: AnsiString); virtual; |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 299 | procedure WriteBinary( const b: TBytes); overload; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 300 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 301 | function ReadMessageBegin: TThriftMessage; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 302 | procedure ReadMessageEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 303 | function ReadStructBegin: TThriftStruct; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 304 | procedure ReadStructEnd; virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 305 | function ReadFieldBegin: TThriftField; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 306 | procedure ReadFieldEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 307 | function ReadMapBegin: TThriftMap; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 308 | procedure ReadMapEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 309 | function ReadListBegin: TThriftList; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 310 | procedure ReadListEnd(); virtual; abstract; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 311 | function ReadSetBegin: TThriftSet; virtual; abstract; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 312 | procedure ReadSetEnd(); virtual; abstract; |
| 313 | function ReadBool: Boolean; virtual; abstract; |
| 314 | function ReadByte: ShortInt; virtual; abstract; |
| 315 | function ReadI16: SmallInt; virtual; abstract; |
| 316 | function ReadI32: Integer; virtual; abstract; |
| 317 | function ReadI64: Int64; virtual; abstract; |
| 318 | function ReadDouble:Double; virtual; abstract; |
| 319 | function ReadBinary: TBytes; virtual; abstract; |
| 320 | function ReadString: string; virtual; |
| 321 | function ReadAnsiString: AnsiString; virtual; |
| 322 | |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 323 | // provide generic implementation for all derived classes |
| 324 | procedure WriteBinary( const bytes : IThriftBytes); overload; virtual; |
| 325 | function ReadBinaryCOM : IThriftBytes; virtual; |
| 326 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 327 | property Transport: ITransport read GetTransport; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 328 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 329 | public |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame] | 330 | constructor Create( const aTransport : ITransport); virtual; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 331 | end; |
| 332 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 333 | IBase = interface( ISupportsToString) |
| 334 | ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}'] |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 335 | procedure Read( const prot: IProtocol); |
| 336 | procedure Write( const prot: IProtocol); |
| 337 | end; |
| 338 | |
| 339 | |
| 340 | IThriftBytes = interface( ISupportsToString) |
| 341 | ['{CDBEF7E8-BEF2-4A0A-983A-F334E3FF0016}'] |
| 342 | function GetCount : Integer; |
| 343 | procedure SetCount(const value : Integer); |
| 344 | |
| 345 | // WARNING: This returns a direct pointer to the underlying data structure |
| 346 | function QueryRawDataPtr : Pointer; |
| 347 | |
| 348 | property Count : Integer read GetCount write SetCount; |
| 349 | end; |
| 350 | |
| 351 | |
| 352 | TThriftBytesImpl = class( TInterfacedObject, IThriftBytes, ISupportsToString) |
| 353 | strict private |
| 354 | FData : TBytes; |
| 355 | |
| 356 | strict protected |
| 357 | function GetCount : Integer; |
| 358 | procedure SetCount(const value : Integer); |
| 359 | function QueryRawDataPtr : Pointer; |
| 360 | |
| 361 | public |
| 362 | constructor Create; overload; |
| 363 | constructor Create( const bytes : TBytes); overload; |
| 364 | constructor Create( var bytes : TBytes; const aTakeOwnership : Boolean = FALSE); overload; |
| 365 | |
| 366 | function ToString : string; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 367 | end; |
| 368 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 369 | |
| 370 | TBinaryProtocolImpl = class( TProtocolImpl ) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 371 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 372 | const |
| 373 | VERSION_MASK : Cardinal = $ffff0000; |
| 374 | VERSION_1 : Cardinal = $80010000; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 375 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 376 | FStrictRead : Boolean; |
| 377 | FStrictWrite : Boolean; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 378 | function GetMinSerializedSize( const aType : TType) : Integer; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 379 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 380 | strict private |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 381 | 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] | 382 | function ReadStringBody( size: Integer): string; |
| 383 | |
| 384 | public |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 385 | type |
| 386 | TFactory = class( TInterfacedObject, IProtocolFactory) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 387 | strict protected |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 388 | FStrictRead : Boolean; |
| 389 | FStrictWrite : Boolean; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 390 | function GetProtocol( const trans: ITransport): IProtocol; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 391 | public |
| 392 | constructor Create( const aStrictRead : Boolean = FALSE; const aStrictWrite: Boolean = TRUE); reintroduce; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 393 | end; |
| 394 | |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame] | 395 | constructor Create( const trans: ITransport); overload; override; |
| 396 | constructor Create( const trans: ITransport; strictRead, strictWrite: Boolean); reintroduce; overload; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 397 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 398 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 399 | procedure WriteMessageEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 400 | procedure WriteStructBegin( const struc: TThriftStruct); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 401 | procedure WriteStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 402 | procedure WriteFieldBegin( const field: TThriftField); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 403 | procedure WriteFieldEnd; override; |
| 404 | procedure WriteFieldStop; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 405 | procedure WriteMapBegin( const map: TThriftMap); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 406 | procedure WriteMapEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 407 | procedure WriteListBegin( const list: TThriftList); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 408 | procedure WriteListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 409 | procedure WriteSetBegin( const set_: TThriftSet ); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 410 | procedure WriteSetEnd(); override; |
| 411 | procedure WriteBool( b: Boolean); override; |
| 412 | procedure WriteByte( b: ShortInt); override; |
| 413 | procedure WriteI16( i16: SmallInt); override; |
| 414 | procedure WriteI32( i32: Integer); override; |
| 415 | procedure WriteI64( const i64: Int64); override; |
| 416 | procedure WriteDouble( const d: Double); override; |
| 417 | procedure WriteBinary( const b: TBytes); override; |
| 418 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 419 | function ReadMessageBegin: TThriftMessage; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 420 | procedure ReadMessageEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 421 | function ReadStructBegin: TThriftStruct; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 422 | procedure ReadStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 423 | function ReadFieldBegin: TThriftField; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 424 | procedure ReadFieldEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 425 | function ReadMapBegin: TThriftMap; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 426 | procedure ReadMapEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 427 | function ReadListBegin: TThriftList; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 428 | procedure ReadListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 429 | function ReadSetBegin: TThriftSet; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 430 | procedure ReadSetEnd(); override; |
| 431 | function ReadBool: Boolean; override; |
| 432 | function ReadByte: ShortInt; override; |
| 433 | function ReadI16: SmallInt; override; |
| 434 | function ReadI32: Integer; override; |
| 435 | function ReadI64: Int64; override; |
| 436 | function ReadDouble:Double; override; |
| 437 | function ReadBinary: TBytes; override; |
| 438 | |
| 439 | end; |
| 440 | |
| 441 | |
| 442 | { TProtocolDecorator forwards all requests to an enclosed TProtocol instance, |
| 443 | providing a way to author concise concrete decorator subclasses. The decorator |
| 444 | does not (and should not) modify the behaviour of the enclosed TProtocol |
| 445 | |
| 446 | See p.175 of Design Patterns (by Gamma et al.) |
| 447 | } |
| 448 | TProtocolDecorator = class( TProtocolImpl) |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 449 | strict private |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 450 | FWrappedProtocol : IProtocol; |
| 451 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 452 | strict protected |
| 453 | function GetMinSerializedSize( const aType : TType) : Integer; override; |
| 454 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 455 | public |
| 456 | // Encloses the specified protocol. |
| 457 | // 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] | 458 | constructor Create( const aProtocol : IProtocol); reintroduce; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 459 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 460 | procedure WriteMessageBegin( const msg: TThriftMessage); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 461 | procedure WriteMessageEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 462 | procedure WriteStructBegin( const struc: TThriftStruct); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 463 | procedure WriteStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 464 | procedure WriteFieldBegin( const field: TThriftField); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 465 | procedure WriteFieldEnd; override; |
| 466 | procedure WriteFieldStop; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 467 | procedure WriteMapBegin( const map: TThriftMap); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 468 | procedure WriteMapEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 469 | procedure WriteListBegin( const list: TThriftList); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 470 | procedure WriteListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 471 | procedure WriteSetBegin( const set_: TThriftSet ); override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 472 | procedure WriteSetEnd(); override; |
| 473 | procedure WriteBool( b: Boolean); override; |
| 474 | procedure WriteByte( b: ShortInt); override; |
| 475 | procedure WriteI16( i16: SmallInt); override; |
| 476 | procedure WriteI32( i32: Integer); override; |
| 477 | procedure WriteI64( const i64: Int64); override; |
| 478 | procedure WriteDouble( const d: Double); override; |
| 479 | procedure WriteString( const s: string ); override; |
| 480 | procedure WriteAnsiString( const s: AnsiString); override; |
| 481 | procedure WriteBinary( const b: TBytes); override; |
| 482 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 483 | function ReadMessageBegin: TThriftMessage; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 484 | procedure ReadMessageEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 485 | function ReadStructBegin: TThriftStruct; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 486 | procedure ReadStructEnd; override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 487 | function ReadFieldBegin: TThriftField; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 488 | procedure ReadFieldEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 489 | function ReadMapBegin: TThriftMap; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 490 | procedure ReadMapEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 491 | function ReadListBegin: TThriftList; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 492 | procedure ReadListEnd(); override; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 493 | function ReadSetBegin: TThriftSet; override; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 494 | procedure ReadSetEnd(); override; |
| 495 | function ReadBool: Boolean; override; |
| 496 | function ReadByte: ShortInt; override; |
| 497 | function ReadI16: SmallInt; override; |
| 498 | function ReadI32: Integer; override; |
| 499 | function ReadI64: Int64; override; |
| 500 | function ReadDouble:Double; override; |
| 501 | function ReadBinary: TBytes; override; |
| 502 | function ReadString: string; override; |
| 503 | function ReadAnsiString: AnsiString; override; |
| 504 | end; |
| 505 | |
| 506 | |
| 507 | type |
| 508 | IRequestEvents = interface |
Jens Geyer | 0164040 | 2013-09-25 21:12:21 +0200 | [diff] [blame] | 509 | ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}'] |
| 510 | // Called before reading arguments. |
| 511 | procedure PreRead; |
| 512 | // Called between reading arguments and calling the handler. |
| 513 | procedure PostRead; |
| 514 | // Called between calling the handler and writing the response. |
| 515 | procedure PreWrite; |
| 516 | // Called after writing the response. |
| 517 | procedure PostWrite; |
| 518 | // Called when an oneway (async) function call completes successfully. |
| 519 | procedure OnewayComplete; |
| 520 | // Called if the handler throws an undeclared exception. |
| 521 | procedure UnhandledError( const e : Exception); |
| 522 | // Called when a client has finished request-handling to clean up |
| 523 | procedure CleanupContext; |
| 524 | end; |
| 525 | |
| 526 | |
| 527 | IProcessorEvents = interface |
| 528 | ['{A8661119-657C-447D-93C5-512E36162A45}'] |
| 529 | // Called when a client is about to call the processor. |
| 530 | procedure Processing( const transport : ITransport); |
| 531 | // Called on any service function invocation |
| 532 | function CreateRequestContext( const aFunctionName : string) : IRequestEvents; |
| 533 | // Called when a client has finished request-handling to clean up |
| 534 | procedure CleanupContext; |
| 535 | end; |
| 536 | |
| 537 | |
| 538 | IProcessor = interface |
| 539 | ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}'] |
Jens Geyer | d430bbd | 2013-09-26 23:37:54 +0200 | [diff] [blame] | 540 | 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] | 541 | end; |
| 542 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 543 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 544 | procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline; |
| 545 | procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline; |
| 546 | procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline; |
| 547 | procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 548 | procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 549 | procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline; |
| 550 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 551 | |
| 552 | implementation |
| 553 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 554 | function ConvertInt64ToDouble( const n: Int64): Double; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 555 | begin |
| 556 | ASSERT( SizeOf(n) = SizeOf(Result)); |
| 557 | System.Move( n, Result, SizeOf(Result)); |
| 558 | end; |
| 559 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 560 | function ConvertDoubleToInt64( const d: Double): Int64; inline; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 561 | begin |
| 562 | ASSERT( SizeOf(d) = SizeOf(Result)); |
| 563 | System.Move( d, Result, SizeOf(Result)); |
| 564 | end; |
| 565 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 566 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 567 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 568 | { TProtocolRecursionTrackerImpl } |
| 569 | |
| 570 | constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol); |
| 571 | begin |
| 572 | inherited Create; |
| 573 | |
| 574 | // storing the pointer *after* the (successful) increment is important here |
| 575 | prot.IncrementRecursionDepth; |
| 576 | FProtocol := prot; |
| 577 | end; |
| 578 | |
| 579 | destructor TProtocolRecursionTrackerImpl.Destroy; |
| 580 | begin |
| 581 | try |
| 582 | // we have to release the reference iff the pointer has been stored |
| 583 | if FProtocol <> nil then begin |
| 584 | FProtocol.DecrementRecursionDepth; |
| 585 | FProtocol := nil; |
| 586 | end; |
| 587 | finally |
| 588 | inherited Destroy; |
| 589 | end; |
| 590 | end; |
| 591 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 592 | { TProtocolImpl } |
| 593 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 594 | constructor TProtocolImpl.Create( const aTransport : ITransport); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 595 | begin |
| 596 | inherited Create; |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 597 | FTrans := aTransport; |
| 598 | FRecursionLimit := aTransport.Configuration.RecursionLimit; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 599 | FRecursionDepth := 0; |
| 600 | end; |
| 601 | |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 602 | function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker; |
| 603 | begin |
| 604 | result := TProtocolRecursionTrackerImpl.Create(Self); |
| 605 | end; |
| 606 | |
| 607 | procedure TProtocolImpl.IncrementRecursionDepth; |
| 608 | begin |
| 609 | if FRecursionDepth < FRecursionLimit |
| 610 | then Inc(FRecursionDepth) |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 611 | else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded'); |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 612 | end; |
| 613 | |
| 614 | procedure TProtocolImpl.DecrementRecursionDepth; |
| 615 | begin |
| 616 | Dec(FRecursionDepth) |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 617 | end; |
| 618 | |
| 619 | function TProtocolImpl.GetTransport: ITransport; |
| 620 | begin |
| 621 | Result := FTrans; |
| 622 | end; |
| 623 | |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 624 | function TProtocolImpl.Configuration : IThriftConfiguration; |
| 625 | begin |
| 626 | Result := FTrans.Configuration; |
| 627 | end; |
| 628 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 629 | procedure TProtocolImpl.Reset; |
| 630 | begin |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 631 | FTrans.ResetConsumedMessageSize; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 632 | end; |
| 633 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 634 | function TProtocolImpl.ReadAnsiString: AnsiString; |
| 635 | var |
| 636 | b : TBytes; |
| 637 | len : Integer; |
| 638 | begin |
| 639 | Result := ''; |
| 640 | b := ReadBinary; |
| 641 | len := Length( b ); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 642 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 643 | SetLength( Result, len); |
| 644 | System.Move( b[0], Pointer(Result)^, len ); |
| 645 | end; |
| 646 | end; |
| 647 | |
| 648 | function TProtocolImpl.ReadString: string; |
| 649 | begin |
| 650 | Result := TEncoding.UTF8.GetString( ReadBinary ); |
| 651 | end; |
| 652 | |
| 653 | procedure TProtocolImpl.WriteAnsiString(const s: AnsiString); |
| 654 | var |
| 655 | b : TBytes; |
| 656 | len : Integer; |
| 657 | begin |
| 658 | len := Length(s); |
| 659 | SetLength( b, len); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 660 | if len > 0 then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 661 | System.Move( Pointer(s)^, b[0], len ); |
| 662 | end; |
| 663 | WriteBinary( b ); |
| 664 | end; |
| 665 | |
| 666 | procedure TProtocolImpl.WriteString(const s: string); |
| 667 | var |
| 668 | b : TBytes; |
| 669 | begin |
| 670 | b := TEncoding.UTF8.GetBytes(s); |
| 671 | WriteBinary( b ); |
| 672 | end; |
| 673 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 674 | |
| 675 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftList); |
| 676 | begin |
| 677 | FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType)); |
| 678 | end; |
| 679 | |
| 680 | |
| 681 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftSet); |
| 682 | begin |
| 683 | FTrans.CheckReadBytesAvailable( value.Count * GetMinSerializedSize(value.ElementType)); |
| 684 | end; |
| 685 | |
| 686 | |
| 687 | procedure TProtocolImpl.CheckReadBytesAvailable( const value : TThriftMap); |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 688 | var nPairSize : Integer; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 689 | begin |
| 690 | nPairSize := GetMinSerializedSize(value.KeyType) + GetMinSerializedSize(value.ValueType); |
| 691 | FTrans.CheckReadBytesAvailable( value.Count * nPairSize); |
| 692 | end; |
| 693 | |
Jens Geyer | 07f4bb5 | 2022-09-03 14:50:06 +0200 | [diff] [blame^] | 694 | |
| 695 | procedure TProtocolImpl.WriteBinary( const bytes : IThriftBytes); |
| 696 | var tmp : TBytes; |
| 697 | begin |
| 698 | SetLength( tmp, bytes.Count); |
| 699 | if Length(tmp) > 0 |
| 700 | then Move( bytes.QueryRawDataPtr^, tmp[0], Length(tmp)); |
| 701 | WriteBinary( tmp); |
| 702 | end; |
| 703 | |
| 704 | |
| 705 | function TProtocolImpl.ReadBinaryCOM : IThriftBytes; |
| 706 | var bytes : TBytes; |
| 707 | begin |
| 708 | bytes := ReadBinary; |
| 709 | result := TThriftBytesImpl.Create(bytes,TRUE); |
| 710 | end; |
| 711 | |
| 712 | |
| 713 | { TThriftBytesImpl } |
| 714 | |
| 715 | constructor TThriftBytesImpl.Create; |
| 716 | begin |
| 717 | inherited Create; |
| 718 | ASSERT( Length(FData) = 0); |
| 719 | end; |
| 720 | |
| 721 | |
| 722 | constructor TThriftBytesImpl.Create( const bytes : TBytes); |
| 723 | begin |
| 724 | FData := bytes; // copies the data |
| 725 | end; |
| 726 | |
| 727 | |
| 728 | constructor TThriftBytesImpl.Create( var bytes : TBytes; const aTakeOwnership : Boolean); |
| 729 | |
| 730 | procedure SwapPointer( var one, two); |
| 731 | var |
| 732 | pOne : Pointer absolute one; |
| 733 | pTwo : Pointer absolute two; |
| 734 | pTmp : Pointer; |
| 735 | begin |
| 736 | pTmp := pOne; |
| 737 | pOne := pTwo; |
| 738 | pTwo := pTmp; |
| 739 | end; |
| 740 | |
| 741 | begin |
| 742 | inherited Create; |
| 743 | ASSERT( Length(FData) = 0); |
| 744 | |
| 745 | if aTakeOwnership |
| 746 | then SwapPointer( FData, bytes) |
| 747 | else FData := bytes; // copies the data |
| 748 | end; |
| 749 | |
| 750 | |
| 751 | function TThriftBytesImpl.ToString : string; |
| 752 | var sb : TThriftStringBuilder; |
| 753 | begin |
| 754 | sb := TThriftStringBuilder.Create(); |
| 755 | try |
| 756 | sb.Append('Bin: '); |
| 757 | sb.Append( FData); |
| 758 | |
| 759 | result := sb.ToString; |
| 760 | finally |
| 761 | sb.Free; |
| 762 | end; |
| 763 | end; |
| 764 | |
| 765 | |
| 766 | function TThriftBytesImpl.GetCount : Integer; |
| 767 | begin |
| 768 | result := Length(FData); |
| 769 | end; |
| 770 | |
| 771 | |
| 772 | procedure TThriftBytesImpl.SetCount(const value : Integer); |
| 773 | begin |
| 774 | SetLength( FData, value); |
| 775 | end; |
| 776 | |
| 777 | |
| 778 | function TThriftBytesImpl.QueryRawDataPtr : Pointer; |
| 779 | begin |
| 780 | result := FData; |
| 781 | end; |
| 782 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 783 | { TProtocolUtil } |
| 784 | |
| 785 | class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 786 | var field : TThriftField; |
| 787 | map : TThriftMap; |
| 788 | set_ : TThriftSet; |
| 789 | list : TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 790 | i : Integer; |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 791 | tracker : IProtocolRecursionTracker; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 792 | begin |
Jens Geyer | d47fcdd | 2015-07-09 22:05:18 +0200 | [diff] [blame] | 793 | tracker := prot.NextRecursionLevel; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 794 | case type_ of |
| 795 | // simple types |
| 796 | TType.Bool_ : prot.ReadBool(); |
| 797 | TType.Byte_ : prot.ReadByte(); |
| 798 | TType.I16 : prot.ReadI16(); |
| 799 | TType.I32 : prot.ReadI32(); |
| 800 | TType.I64 : prot.ReadI64(); |
| 801 | TType.Double_ : prot.ReadDouble(); |
| 802 | TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it. |
| 803 | |
| 804 | // structured types |
| 805 | TType.Struct : begin |
| 806 | prot.ReadStructBegin(); |
| 807 | while TRUE do begin |
| 808 | field := prot.ReadFieldBegin(); |
| 809 | if (field.Type_ = TType.Stop) then Break; |
| 810 | Skip(prot, field.Type_); |
| 811 | prot.ReadFieldEnd(); |
| 812 | end; |
| 813 | prot.ReadStructEnd(); |
| 814 | end; |
| 815 | |
| 816 | TType.Map : begin |
| 817 | map := prot.ReadMapBegin(); |
| 818 | for i := 0 to map.Count-1 do begin |
| 819 | Skip(prot, map.KeyType); |
| 820 | Skip(prot, map.ValueType); |
| 821 | end; |
| 822 | prot.ReadMapEnd(); |
| 823 | end; |
| 824 | |
| 825 | TType.Set_ : begin |
| 826 | set_ := prot.ReadSetBegin(); |
| 827 | for i := 0 to set_.Count-1 |
| 828 | do Skip( prot, set_.ElementType); |
| 829 | prot.ReadSetEnd(); |
| 830 | end; |
| 831 | |
| 832 | TType.List : begin |
| 833 | list := prot.ReadListBegin(); |
| 834 | for i := 0 to list.Count-1 |
| 835 | do Skip( prot, list.ElementType); |
| 836 | prot.ReadListEnd(); |
| 837 | end; |
| 838 | |
| 839 | else |
Jens Geyer | 5f723cd | 2017-01-10 21:57:48 +0100 | [diff] [blame] | 840 | raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_))); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 841 | end; |
| 842 | end; |
| 843 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 844 | |
| 845 | { TBinaryProtocolImpl } |
| 846 | |
Jens Geyer | 3b68653 | 2021-07-01 23:04:08 +0200 | [diff] [blame] | 847 | constructor TBinaryProtocolImpl.Create( const trans: ITransport); |
| 848 | begin |
| 849 | // call the real CTOR |
| 850 | Self.Create( trans, FALSE, TRUE); |
| 851 | end; |
| 852 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 853 | constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead, strictWrite: Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 854 | begin |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 855 | inherited Create( trans); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 856 | FStrictRead := strictRead; |
| 857 | FStrictWrite := strictWrite; |
| 858 | end; |
| 859 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 860 | 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] | 861 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 862 | Result := FTrans.ReadAll( pBuf, buflen, off, len ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 863 | end; |
| 864 | |
| 865 | function TBinaryProtocolImpl.ReadBinary: TBytes; |
| 866 | var |
| 867 | size : Integer; |
| 868 | buf : TBytes; |
| 869 | begin |
| 870 | size := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 871 | FTrans.CheckReadBytesAvailable( size); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 872 | SetLength( buf, size); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 873 | FTrans.ReadAll( buf, 0, size); |
| 874 | Result := buf; |
| 875 | end; |
| 876 | |
| 877 | function TBinaryProtocolImpl.ReadBool: Boolean; |
| 878 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 879 | Result := (ReadByte = 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 880 | end; |
| 881 | |
| 882 | function TBinaryProtocolImpl.ReadByte: ShortInt; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 883 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 884 | ReadAll( @result, SizeOf(result), 0, 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 885 | end; |
| 886 | |
| 887 | function TBinaryProtocolImpl.ReadDouble: Double; |
| 888 | begin |
| 889 | Result := ConvertInt64ToDouble( ReadI64 ) |
| 890 | end; |
| 891 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 892 | function TBinaryProtocolImpl.ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 893 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 894 | Init( result, '', TType( ReadByte), 0); |
| 895 | if ( result.Type_ <> TType.Stop ) then begin |
| 896 | result.Id := ReadI16; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 897 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 898 | end; |
| 899 | |
| 900 | procedure TBinaryProtocolImpl.ReadFieldEnd; |
| 901 | begin |
| 902 | |
| 903 | end; |
| 904 | |
| 905 | function TBinaryProtocolImpl.ReadI16: SmallInt; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 906 | var i16in : packed array[0..1] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 907 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 908 | ReadAll( @i16in, Sizeof(i16in), 0, 2); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 909 | Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF)); |
| 910 | end; |
| 911 | |
| 912 | function TBinaryProtocolImpl.ReadI32: Integer; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 913 | var i32in : packed array[0..3] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 914 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 915 | ReadAll( @i32in, SizeOf(i32in), 0, 4); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 916 | |
| 917 | Result := Integer( |
| 918 | ((i32in[0] and $FF) shl 24) or |
| 919 | ((i32in[1] and $FF) shl 16) or |
| 920 | ((i32in[2] and $FF) shl 8) or |
| 921 | (i32in[3] and $FF)); |
| 922 | |
| 923 | end; |
| 924 | |
| 925 | function TBinaryProtocolImpl.ReadI64: Int64; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 926 | var i64in : packed array[0..7] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 927 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 928 | ReadAll( @i64in, SizeOf(i64in), 0, 8); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 929 | Result := |
| 930 | (Int64( i64in[0] and $FF) shl 56) or |
| 931 | (Int64( i64in[1] and $FF) shl 48) or |
| 932 | (Int64( i64in[2] and $FF) shl 40) or |
| 933 | (Int64( i64in[3] and $FF) shl 32) or |
| 934 | (Int64( i64in[4] and $FF) shl 24) or |
| 935 | (Int64( i64in[5] and $FF) shl 16) or |
| 936 | (Int64( i64in[6] and $FF) shl 8) or |
| 937 | (Int64( i64in[7] and $FF)); |
| 938 | end; |
| 939 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 940 | function TBinaryProtocolImpl.ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 941 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 942 | result.ElementType := TType(ReadByte); |
| 943 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 944 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 945 | end; |
| 946 | |
| 947 | procedure TBinaryProtocolImpl.ReadListEnd; |
| 948 | begin |
| 949 | |
| 950 | end; |
| 951 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 952 | function TBinaryProtocolImpl.ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 953 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 954 | result.KeyType := TType(ReadByte); |
| 955 | result.ValueType := TType(ReadByte); |
| 956 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 957 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 958 | end; |
| 959 | |
| 960 | procedure TBinaryProtocolImpl.ReadMapEnd; |
| 961 | begin |
| 962 | |
| 963 | end; |
| 964 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 965 | function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 966 | var |
| 967 | size : Integer; |
| 968 | version : Integer; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 969 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 970 | Reset; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 971 | Init( result); |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 972 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 973 | size := ReadI32; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 974 | if (size < 0) then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 975 | version := size and Integer( VERSION_MASK); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 976 | if ( version <> Integer( VERSION_1)) then begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 977 | raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 978 | end; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 979 | result.Type_ := TMessageType( size and $000000ff); |
| 980 | result.Name := ReadString; |
| 981 | result.SeqID := ReadI32; |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 982 | Exit; |
| 983 | end; |
| 984 | |
| 985 | try |
| 986 | if FStrictRead |
| 987 | then raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' ); |
| 988 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 989 | result.Name := ReadStringBody( size ); |
| 990 | result.Type_ := TMessageType( ReadByte ); |
| 991 | result.SeqID := ReadI32; |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 992 | except |
| 993 | if CharUtils.IsHtmlDoctype(size) |
| 994 | then raise TProtocolExceptionInvalidData.Create('Remote end sends HTML instead of data') |
| 995 | else raise; // something else |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 996 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 997 | end; |
| 998 | |
| 999 | procedure TBinaryProtocolImpl.ReadMessageEnd; |
| 1000 | begin |
| 1001 | inherited; |
| 1002 | |
| 1003 | end; |
| 1004 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1005 | function TBinaryProtocolImpl.ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1006 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1007 | result.ElementType := TType(ReadByte); |
| 1008 | result.Count := ReadI32; |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1009 | CheckReadBytesAvailable(result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1010 | end; |
| 1011 | |
| 1012 | procedure TBinaryProtocolImpl.ReadSetEnd; |
| 1013 | begin |
| 1014 | |
| 1015 | end; |
| 1016 | |
| 1017 | function TBinaryProtocolImpl.ReadStringBody( size: Integer): string; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1018 | var buf : TBytes; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1019 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1020 | FTrans.CheckReadBytesAvailable( size); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1021 | SetLength( buf, size); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1022 | FTrans.ReadAll( buf, 0, size ); |
| 1023 | Result := TEncoding.UTF8.GetString( buf); |
| 1024 | end; |
| 1025 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1026 | function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1027 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1028 | Init( Result); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1029 | end; |
| 1030 | |
| 1031 | procedure TBinaryProtocolImpl.ReadStructEnd; |
| 1032 | begin |
| 1033 | inherited; |
| 1034 | |
| 1035 | end; |
| 1036 | |
| 1037 | procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes); |
| 1038 | var iLen : Integer; |
| 1039 | begin |
| 1040 | iLen := Length(b); |
| 1041 | WriteI32( iLen); |
| 1042 | if iLen > 0 then FTrans.Write(b, 0, iLen); |
| 1043 | end; |
| 1044 | |
| 1045 | procedure TBinaryProtocolImpl.WriteBool(b: Boolean); |
| 1046 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1047 | if b then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1048 | WriteByte( 1 ); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1049 | end else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1050 | WriteByte( 0 ); |
| 1051 | end; |
| 1052 | end; |
| 1053 | |
| 1054 | procedure TBinaryProtocolImpl.WriteByte(b: ShortInt); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1055 | begin |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1056 | FTrans.Write( @b, 0, 1); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1057 | end; |
| 1058 | |
| 1059 | procedure TBinaryProtocolImpl.WriteDouble( const d: Double); |
| 1060 | begin |
| 1061 | WriteI64(ConvertDoubleToInt64(d)); |
| 1062 | end; |
| 1063 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1064 | procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1065 | begin |
| 1066 | WriteByte(ShortInt(field.Type_)); |
| 1067 | WriteI16(field.ID); |
| 1068 | end; |
| 1069 | |
| 1070 | procedure TBinaryProtocolImpl.WriteFieldEnd; |
| 1071 | begin |
| 1072 | |
| 1073 | end; |
| 1074 | |
| 1075 | procedure TBinaryProtocolImpl.WriteFieldStop; |
| 1076 | begin |
| 1077 | WriteByte(ShortInt(TType.Stop)); |
| 1078 | end; |
| 1079 | |
| 1080 | procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1081 | var i16out : packed array[0..1] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1082 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1083 | i16out[0] := Byte($FF and (i16 shr 8)); |
| 1084 | i16out[1] := Byte($FF and i16); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1085 | FTrans.Write( @i16out, 0, 2); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1086 | end; |
| 1087 | |
| 1088 | procedure TBinaryProtocolImpl.WriteI32(i32: Integer); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1089 | var i32out : packed array[0..3] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1090 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1091 | i32out[0] := Byte($FF and (i32 shr 24)); |
| 1092 | i32out[1] := Byte($FF and (i32 shr 16)); |
| 1093 | i32out[2] := Byte($FF and (i32 shr 8)); |
| 1094 | i32out[3] := Byte($FF and i32); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1095 | FTrans.Write( @i32out, 0, 4); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1096 | end; |
| 1097 | |
| 1098 | procedure TBinaryProtocolImpl.WriteI64( const i64: Int64); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1099 | var i64out : packed array[0..7] of Byte; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1100 | begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1101 | i64out[0] := Byte($FF and (i64 shr 56)); |
| 1102 | i64out[1] := Byte($FF and (i64 shr 48)); |
| 1103 | i64out[2] := Byte($FF and (i64 shr 40)); |
| 1104 | i64out[3] := Byte($FF and (i64 shr 32)); |
| 1105 | i64out[4] := Byte($FF and (i64 shr 24)); |
| 1106 | i64out[5] := Byte($FF and (i64 shr 16)); |
| 1107 | i64out[6] := Byte($FF and (i64 shr 8)); |
| 1108 | i64out[7] := Byte($FF and i64); |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1109 | FTrans.Write( @i64out, 0, 8); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1110 | end; |
| 1111 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1112 | procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1113 | begin |
| 1114 | WriteByte(ShortInt(list.ElementType)); |
| 1115 | WriteI32(list.Count); |
| 1116 | end; |
| 1117 | |
| 1118 | procedure TBinaryProtocolImpl.WriteListEnd; |
| 1119 | begin |
| 1120 | |
| 1121 | end; |
| 1122 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1123 | procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1124 | begin |
| 1125 | WriteByte(ShortInt(map.KeyType)); |
| 1126 | WriteByte(ShortInt(map.ValueType)); |
| 1127 | WriteI32(map.Count); |
| 1128 | end; |
| 1129 | |
| 1130 | procedure TBinaryProtocolImpl.WriteMapEnd; |
| 1131 | begin |
| 1132 | |
| 1133 | end; |
| 1134 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1135 | procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1136 | var version : Cardinal; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1137 | begin |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1138 | Reset; |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1139 | if FStrictWrite then begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1140 | version := VERSION_1 or Cardinal( msg.Type_); |
| 1141 | WriteI32( Integer( version) ); |
| 1142 | WriteString( msg.Name); |
| 1143 | WriteI32( msg.SeqID); |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1144 | end else begin |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1145 | WriteString( msg.Name); |
| 1146 | WriteByte(ShortInt( msg.Type_)); |
| 1147 | WriteI32( msg.SeqID); |
| 1148 | end; |
| 1149 | end; |
| 1150 | |
| 1151 | procedure TBinaryProtocolImpl.WriteMessageEnd; |
| 1152 | begin |
| 1153 | |
| 1154 | end; |
| 1155 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1156 | procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1157 | begin |
| 1158 | WriteByte(ShortInt(set_.ElementType)); |
| 1159 | WriteI32(set_.Count); |
| 1160 | end; |
| 1161 | |
| 1162 | procedure TBinaryProtocolImpl.WriteSetEnd; |
| 1163 | begin |
| 1164 | |
| 1165 | end; |
| 1166 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1167 | procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1168 | begin |
| 1169 | |
| 1170 | end; |
| 1171 | |
| 1172 | procedure TBinaryProtocolImpl.WriteStructEnd; |
| 1173 | begin |
| 1174 | |
| 1175 | end; |
| 1176 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1177 | function TBinaryProtocolImpl.GetMinSerializedSize( const aType : TType) : Integer; |
| 1178 | // Return the minimum number of bytes a type will consume on the wire |
| 1179 | begin |
| 1180 | case aType of |
| 1181 | TType.Stop: result := 0; |
| 1182 | TType.Void: result := 0; |
| 1183 | TType.Bool_: result := SizeOf(Byte); |
| 1184 | TType.Byte_: result := SizeOf(Byte); |
| 1185 | TType.Double_: result := SizeOf(Double); |
| 1186 | TType.I16: result := SizeOf(Int16); |
| 1187 | TType.I32: result := SizeOf(Int32); |
| 1188 | TType.I64: result := SizeOf(Int64); |
| 1189 | TType.String_: result := SizeOf(Int32); // string length |
| 1190 | TType.Struct: result := 0; // empty struct |
| 1191 | TType.Map: result := SizeOf(Int32); // element count |
| 1192 | TType.Set_: result := SizeOf(Int32); // element count |
| 1193 | TType.List: result := SizeOf(Int32); // element count |
| 1194 | else |
| 1195 | raise TTransportExceptionBadArgs.Create('Unhandled type code'); |
| 1196 | end; |
| 1197 | end; |
| 1198 | |
| 1199 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1200 | { TProtocolException } |
| 1201 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1202 | constructor TProtocolException.HiddenCreate(const Msg: string); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1203 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1204 | inherited Create(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1205 | end; |
| 1206 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1207 | class function TProtocolException.Create(const Msg: string): TProtocolException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1208 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1209 | Result := TProtocolExceptionUnknown.Create(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1210 | end; |
| 1211 | |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1212 | class function TProtocolException.Create: TProtocolException; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1213 | begin |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1214 | Result := TProtocolExceptionUnknown.Create(''); |
| 1215 | end; |
| 1216 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1217 | class function TProtocolException.Create(aType: TExceptionType): TProtocolException; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1218 | begin |
| 1219 | {$WARN SYMBOL_DEPRECATED OFF} |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1220 | Result := Create(aType, ''); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1221 | {$WARN SYMBOL_DEPRECATED DEFAULT} |
| 1222 | end; |
| 1223 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1224 | class function TProtocolException.Create(aType: TExceptionType; const msg: string): TProtocolException; |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1225 | begin |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1226 | case aType of |
| 1227 | TExceptionType.INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg); |
| 1228 | TExceptionType.NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg); |
| 1229 | TExceptionType.SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg); |
| 1230 | TExceptionType.BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg); |
| 1231 | TExceptionType.NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg); |
| 1232 | TExceptionType.DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1233 | else |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1234 | ASSERT( TExceptionType.UNKNOWN = aType); |
Jens Geyer | e0e3240 | 2016-04-20 21:50:48 +0200 | [diff] [blame] | 1235 | Result := TProtocolExceptionUnknown.Create(msg); |
| 1236 | end; |
| 1237 | end; |
| 1238 | |
| 1239 | { TProtocolExceptionSpecialized } |
| 1240 | |
| 1241 | constructor TProtocolExceptionSpecialized.Create(const Msg: string); |
| 1242 | begin |
| 1243 | inherited HiddenCreate(Msg); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1244 | end; |
| 1245 | |
Jens Geyer | 9f11c1e | 2019-11-09 19:39:20 +0100 | [diff] [blame] | 1246 | { specialized TProtocolExceptions } |
| 1247 | |
| 1248 | class function TProtocolExceptionUnknown.GetType: TProtocolException.TExceptionType; |
| 1249 | begin |
| 1250 | result := TExceptionType.UNKNOWN; |
| 1251 | end; |
| 1252 | |
| 1253 | class function TProtocolExceptionInvalidData.GetType: TProtocolException.TExceptionType; |
| 1254 | begin |
| 1255 | result := TExceptionType.INVALID_DATA; |
| 1256 | end; |
| 1257 | |
| 1258 | class function TProtocolExceptionNegativeSize.GetType: TProtocolException.TExceptionType; |
| 1259 | begin |
| 1260 | result := TExceptionType.NEGATIVE_SIZE; |
| 1261 | end; |
| 1262 | |
| 1263 | class function TProtocolExceptionSizeLimit.GetType: TProtocolException.TExceptionType; |
| 1264 | begin |
| 1265 | result := TExceptionType.SIZE_LIMIT; |
| 1266 | end; |
| 1267 | |
| 1268 | class function TProtocolExceptionBadVersion.GetType: TProtocolException.TExceptionType; |
| 1269 | begin |
| 1270 | result := TExceptionType.BAD_VERSION; |
| 1271 | end; |
| 1272 | |
| 1273 | class function TProtocolExceptionNotImplemented.GetType: TProtocolException.TExceptionType; |
| 1274 | begin |
| 1275 | result := TExceptionType.NOT_IMPLEMENTED; |
| 1276 | end; |
| 1277 | |
| 1278 | class function TProtocolExceptionDepthLimit.GetType: TProtocolException.TExceptionType; |
| 1279 | begin |
| 1280 | result := TExceptionType.DEPTH_LIMIT; |
| 1281 | end; |
| 1282 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1283 | { TBinaryProtocolImpl.TFactory } |
| 1284 | |
Jens Geyer | fad7fd3 | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1285 | constructor TBinaryProtocolImpl.TFactory.Create( const aStrictRead, aStrictWrite: Boolean); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1286 | begin |
| 1287 | inherited Create; |
| 1288 | FStrictRead := AStrictRead; |
| 1289 | FStrictWrite := AStrictWrite; |
| 1290 | end; |
| 1291 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1292 | function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol; |
| 1293 | begin |
| 1294 | Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite); |
| 1295 | end; |
| 1296 | |
| 1297 | |
| 1298 | { TProtocolDecorator } |
| 1299 | |
| 1300 | constructor TProtocolDecorator.Create( const aProtocol : IProtocol); |
| 1301 | begin |
| 1302 | ASSERT( aProtocol <> nil); |
| 1303 | inherited Create( aProtocol.Transport); |
| 1304 | FWrappedProtocol := aProtocol; |
| 1305 | end; |
| 1306 | |
| 1307 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1308 | procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1309 | begin |
| 1310 | FWrappedProtocol.WriteMessageBegin( msg); |
| 1311 | end; |
| 1312 | |
| 1313 | |
| 1314 | procedure TProtocolDecorator.WriteMessageEnd; |
| 1315 | begin |
| 1316 | FWrappedProtocol.WriteMessageEnd; |
| 1317 | end; |
| 1318 | |
| 1319 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1320 | procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1321 | begin |
| 1322 | FWrappedProtocol.WriteStructBegin( struc); |
| 1323 | end; |
| 1324 | |
| 1325 | |
| 1326 | procedure TProtocolDecorator.WriteStructEnd; |
| 1327 | begin |
| 1328 | FWrappedProtocol.WriteStructEnd; |
| 1329 | end; |
| 1330 | |
| 1331 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1332 | procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1333 | begin |
| 1334 | FWrappedProtocol.WriteFieldBegin( field); |
| 1335 | end; |
| 1336 | |
| 1337 | |
| 1338 | procedure TProtocolDecorator.WriteFieldEnd; |
| 1339 | begin |
| 1340 | FWrappedProtocol.WriteFieldEnd; |
| 1341 | end; |
| 1342 | |
| 1343 | |
| 1344 | procedure TProtocolDecorator.WriteFieldStop; |
| 1345 | begin |
| 1346 | FWrappedProtocol.WriteFieldStop; |
| 1347 | end; |
| 1348 | |
| 1349 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1350 | procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1351 | begin |
| 1352 | FWrappedProtocol.WriteMapBegin( map); |
| 1353 | end; |
| 1354 | |
| 1355 | |
| 1356 | procedure TProtocolDecorator.WriteMapEnd; |
| 1357 | begin |
| 1358 | FWrappedProtocol.WriteMapEnd; |
| 1359 | end; |
| 1360 | |
| 1361 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1362 | procedure TProtocolDecorator.WriteListBegin( const list: TThriftList); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1363 | begin |
| 1364 | FWrappedProtocol.WriteListBegin( list); |
| 1365 | end; |
| 1366 | |
| 1367 | |
| 1368 | procedure TProtocolDecorator.WriteListEnd(); |
| 1369 | begin |
| 1370 | FWrappedProtocol.WriteListEnd(); |
| 1371 | end; |
| 1372 | |
| 1373 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1374 | procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet ); |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1375 | begin |
| 1376 | FWrappedProtocol.WriteSetBegin( set_); |
| 1377 | end; |
| 1378 | |
| 1379 | |
| 1380 | procedure TProtocolDecorator.WriteSetEnd(); |
| 1381 | begin |
| 1382 | FWrappedProtocol.WriteSetEnd(); |
| 1383 | end; |
| 1384 | |
| 1385 | |
| 1386 | procedure TProtocolDecorator.WriteBool( b: Boolean); |
| 1387 | begin |
| 1388 | FWrappedProtocol.WriteBool( b); |
| 1389 | end; |
| 1390 | |
| 1391 | |
| 1392 | procedure TProtocolDecorator.WriteByte( b: ShortInt); |
| 1393 | begin |
| 1394 | FWrappedProtocol.WriteByte( b); |
| 1395 | end; |
| 1396 | |
| 1397 | |
| 1398 | procedure TProtocolDecorator.WriteI16( i16: SmallInt); |
| 1399 | begin |
| 1400 | FWrappedProtocol.WriteI16( i16); |
| 1401 | end; |
| 1402 | |
| 1403 | |
| 1404 | procedure TProtocolDecorator.WriteI32( i32: Integer); |
| 1405 | begin |
| 1406 | FWrappedProtocol.WriteI32( i32); |
| 1407 | end; |
| 1408 | |
| 1409 | |
| 1410 | procedure TProtocolDecorator.WriteI64( const i64: Int64); |
| 1411 | begin |
| 1412 | FWrappedProtocol.WriteI64( i64); |
| 1413 | end; |
| 1414 | |
| 1415 | |
| 1416 | procedure TProtocolDecorator.WriteDouble( const d: Double); |
| 1417 | begin |
| 1418 | FWrappedProtocol.WriteDouble( d); |
| 1419 | end; |
| 1420 | |
| 1421 | |
| 1422 | procedure TProtocolDecorator.WriteString( const s: string ); |
| 1423 | begin |
| 1424 | FWrappedProtocol.WriteString( s); |
| 1425 | end; |
| 1426 | |
| 1427 | |
| 1428 | procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString); |
| 1429 | begin |
| 1430 | FWrappedProtocol.WriteAnsiString( s); |
| 1431 | end; |
| 1432 | |
| 1433 | |
| 1434 | procedure TProtocolDecorator.WriteBinary( const b: TBytes); |
| 1435 | begin |
| 1436 | FWrappedProtocol.WriteBinary( b); |
| 1437 | end; |
| 1438 | |
| 1439 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1440 | function TProtocolDecorator.ReadMessageBegin: TThriftMessage; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1441 | begin |
| 1442 | result := FWrappedProtocol.ReadMessageBegin; |
| 1443 | end; |
| 1444 | |
| 1445 | |
| 1446 | procedure TProtocolDecorator.ReadMessageEnd(); |
| 1447 | begin |
| 1448 | FWrappedProtocol.ReadMessageEnd(); |
| 1449 | end; |
| 1450 | |
| 1451 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1452 | function TProtocolDecorator.ReadStructBegin: TThriftStruct; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1453 | begin |
| 1454 | result := FWrappedProtocol.ReadStructBegin; |
| 1455 | end; |
| 1456 | |
| 1457 | |
| 1458 | procedure TProtocolDecorator.ReadStructEnd; |
| 1459 | begin |
| 1460 | FWrappedProtocol.ReadStructEnd; |
| 1461 | end; |
| 1462 | |
| 1463 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1464 | function TProtocolDecorator.ReadFieldBegin: TThriftField; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1465 | begin |
| 1466 | result := FWrappedProtocol.ReadFieldBegin; |
| 1467 | end; |
| 1468 | |
| 1469 | |
| 1470 | procedure TProtocolDecorator.ReadFieldEnd(); |
| 1471 | begin |
| 1472 | FWrappedProtocol.ReadFieldEnd(); |
| 1473 | end; |
| 1474 | |
| 1475 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1476 | function TProtocolDecorator.ReadMapBegin: TThriftMap; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1477 | begin |
| 1478 | result := FWrappedProtocol.ReadMapBegin; |
| 1479 | end; |
| 1480 | |
| 1481 | |
| 1482 | procedure TProtocolDecorator.ReadMapEnd(); |
| 1483 | begin |
| 1484 | FWrappedProtocol.ReadMapEnd(); |
| 1485 | end; |
| 1486 | |
| 1487 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1488 | function TProtocolDecorator.ReadListBegin: TThriftList; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1489 | begin |
| 1490 | result := FWrappedProtocol.ReadListBegin; |
| 1491 | end; |
| 1492 | |
| 1493 | |
| 1494 | procedure TProtocolDecorator.ReadListEnd(); |
| 1495 | begin |
| 1496 | FWrappedProtocol.ReadListEnd(); |
| 1497 | end; |
| 1498 | |
| 1499 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1500 | function TProtocolDecorator.ReadSetBegin: TThriftSet; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1501 | begin |
| 1502 | result := FWrappedProtocol.ReadSetBegin; |
| 1503 | end; |
| 1504 | |
| 1505 | |
| 1506 | procedure TProtocolDecorator.ReadSetEnd(); |
| 1507 | begin |
| 1508 | FWrappedProtocol.ReadSetEnd(); |
| 1509 | end; |
| 1510 | |
| 1511 | |
| 1512 | function TProtocolDecorator.ReadBool: Boolean; |
| 1513 | begin |
| 1514 | result := FWrappedProtocol.ReadBool; |
| 1515 | end; |
| 1516 | |
| 1517 | |
| 1518 | function TProtocolDecorator.ReadByte: ShortInt; |
| 1519 | begin |
| 1520 | result := FWrappedProtocol.ReadByte; |
| 1521 | end; |
| 1522 | |
| 1523 | |
| 1524 | function TProtocolDecorator.ReadI16: SmallInt; |
| 1525 | begin |
| 1526 | result := FWrappedProtocol.ReadI16; |
| 1527 | end; |
| 1528 | |
| 1529 | |
| 1530 | function TProtocolDecorator.ReadI32: Integer; |
| 1531 | begin |
| 1532 | result := FWrappedProtocol.ReadI32; |
| 1533 | end; |
| 1534 | |
| 1535 | |
| 1536 | function TProtocolDecorator.ReadI64: Int64; |
| 1537 | begin |
| 1538 | result := FWrappedProtocol.ReadI64; |
| 1539 | end; |
| 1540 | |
| 1541 | |
| 1542 | function TProtocolDecorator.ReadDouble:Double; |
| 1543 | begin |
| 1544 | result := FWrappedProtocol.ReadDouble; |
| 1545 | end; |
| 1546 | |
| 1547 | |
| 1548 | function TProtocolDecorator.ReadBinary: TBytes; |
| 1549 | begin |
| 1550 | result := FWrappedProtocol.ReadBinary; |
| 1551 | end; |
| 1552 | |
| 1553 | |
| 1554 | function TProtocolDecorator.ReadString: string; |
| 1555 | begin |
| 1556 | result := FWrappedProtocol.ReadString; |
| 1557 | end; |
| 1558 | |
| 1559 | |
| 1560 | function TProtocolDecorator.ReadAnsiString: AnsiString; |
| 1561 | begin |
| 1562 | result := FWrappedProtocol.ReadAnsiString; |
| 1563 | end; |
| 1564 | |
| 1565 | |
Jens Geyer | 41f47af | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 1566 | function TProtocolDecorator.GetMinSerializedSize( const aType : TType) : Integer; |
| 1567 | begin |
| 1568 | result := FWrappedProtocol.GetMinSerializedSize(aType); |
| 1569 | end; |
| 1570 | |
| 1571 | |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 1572 | { Init helper functions } |
| 1573 | |
| 1574 | procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer); |
| 1575 | begin |
| 1576 | rec.Name := AName; |
| 1577 | rec.Type_ := AMessageType; |
| 1578 | rec.SeqID := ASeqID; |
| 1579 | end; |
| 1580 | |
| 1581 | |
| 1582 | procedure Init( var rec : TThriftStruct; const AName: string = ''); |
| 1583 | begin |
| 1584 | rec.Name := AName; |
| 1585 | end; |
| 1586 | |
| 1587 | |
| 1588 | procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt); |
| 1589 | begin |
| 1590 | rec.Name := AName; |
| 1591 | rec.Type_ := AType; |
| 1592 | rec.Id := AId; |
| 1593 | end; |
| 1594 | |
| 1595 | |
| 1596 | procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer); |
| 1597 | begin |
| 1598 | rec.ValueType := AValueType; |
| 1599 | rec.KeyType := AKeyType; |
| 1600 | rec.Count := ACount; |
| 1601 | end; |
| 1602 | |
| 1603 | |
| 1604 | procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer); |
| 1605 | begin |
| 1606 | rec.Count := ACount; |
| 1607 | rec.ElementType := AElementType; |
| 1608 | end; |
| 1609 | |
| 1610 | |
| 1611 | procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer); |
| 1612 | begin |
| 1613 | rec.Count := ACount; |
| 1614 | rec.ElementType := AElementType; |
| 1615 | end; |
| 1616 | |
| 1617 | |
| 1618 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 1619 | end. |
| 1620 | |