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