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