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