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