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