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