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