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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
63 TType.Stop, TType.Void,
64 TType.Bool_, TType.Byte_, TType.Double_, TType.I16, TType.I32, TType.I64, TType.String_,
65 TType.Struct, TType.Map, TType.Set_, TType.List
68 VALID_MESSAGETYPES = [Low(TMessageType)..High(TMessageType)];
71 DEFAULT_RECURSION_LIMIT = 64;
74 IProtocol = interface;
76 TThriftMessage = record
82 TThriftStruct = record
110 IProtocolFactory = interface
111 ['{7CD64A10-4E9F-4E99-93BF-708A31F4A67B}']
112 function GetProtocol( const trans: ITransport): IProtocol;
115 TProtocolException = class( TException)
117 const // TODO(jensg): change into enum
126 constructor HiddenCreate(const Msg: string);
128 // purposefully hide inherited constructor
129 class function Create(const Msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
130 class function Create: TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
131 class function Create( type_: Integer): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
132 class function Create( type_: Integer; const msg: string): TProtocolException; overload; deprecated 'Use specialized TProtocolException types (or regenerate from IDL)';
135 // Needed to remove deprecation warning
136 TProtocolExceptionSpecialized = class abstract (TProtocolException)
138 constructor Create(const Msg: string);
141 TProtocolExceptionUnknown = class (TProtocolExceptionSpecialized);
142 TProtocolExceptionInvalidData = class (TProtocolExceptionSpecialized);
143 TProtocolExceptionNegativeSize = class (TProtocolExceptionSpecialized);
144 TProtocolExceptionSizeLimit = class (TProtocolExceptionSpecialized);
145 TProtocolExceptionBadVersion = class (TProtocolExceptionSpecialized);
146 TProtocolExceptionNotImplemented = class (TProtocolExceptionSpecialized);
147 TProtocolExceptionDepthLimit = class (TProtocolExceptionSpecialized);
150 TProtocolUtil = class
152 class procedure Skip( prot: IProtocol; type_: TType);
155 IProtocolRecursionTracker = interface
156 ['{29CA033F-BB56-49B1-9EE3-31B1E82FC7A5}']
160 TProtocolRecursionTrackerImpl = class abstract( TInterfacedObject, IProtocolRecursionTracker)
162 FProtocol : IProtocol;
164 constructor Create( prot : IProtocol);
165 destructor Destroy; override;
168 IProtocol = interface
169 ['{602A7FFB-0D9E-4CD8-8D7F-E5076660588A}']
170 function GetTransport: ITransport;
171 procedure WriteMessageBegin( const msg: TThriftMessage);
172 procedure WriteMessageEnd;
173 procedure WriteStructBegin( const struc: TThriftStruct);
174 procedure WriteStructEnd;
175 procedure WriteFieldBegin( const field: TThriftField);
176 procedure WriteFieldEnd;
177 procedure WriteFieldStop;
178 procedure WriteMapBegin( const map: TThriftMap);
179 procedure WriteMapEnd;
180 procedure WriteListBegin( const list: TThriftList);
181 procedure WriteListEnd();
182 procedure WriteSetBegin( const set_: TThriftSet );
183 procedure WriteSetEnd();
184 procedure WriteBool( b: Boolean);
185 procedure WriteByte( b: ShortInt);
186 procedure WriteI16( i16: SmallInt);
187 procedure WriteI32( i32: Integer);
188 procedure WriteI64( const i64: Int64);
189 procedure WriteDouble( const d: Double);
190 procedure WriteString( const s: string );
191 procedure WriteAnsiString( const s: AnsiString);
192 procedure WriteBinary( const b: TBytes);
194 function ReadMessageBegin: TThriftMessage;
195 procedure ReadMessageEnd();
196 function ReadStructBegin: TThriftStruct;
197 procedure ReadStructEnd;
198 function ReadFieldBegin: TThriftField;
199 procedure ReadFieldEnd();
200 function ReadMapBegin: TThriftMap;
201 procedure ReadMapEnd();
202 function ReadListBegin: TThriftList;
203 procedure ReadListEnd();
204 function ReadSetBegin: TThriftSet;
205 procedure ReadSetEnd();
206 function ReadBool: Boolean;
207 function ReadByte: ShortInt;
208 function ReadI16: SmallInt;
209 function ReadI32: Integer;
210 function ReadI64: Int64;
211 function ReadDouble:Double;
212 function ReadBinary: TBytes;
213 function ReadString: string;
214 function ReadAnsiString: AnsiString;
216 procedure SetRecursionLimit( value : Integer);
217 function GetRecursionLimit : Integer;
218 function NextRecursionLevel : IProtocolRecursionTracker;
219 procedure IncrementRecursionDepth;
220 procedure DecrementRecursionDepth;
222 property Transport: ITransport read GetTransport;
223 property RecursionLimit : Integer read GetRecursionLimit write SetRecursionLimit;
226 TProtocolImpl = class abstract( TInterfacedObject, IProtocol)
229 FRecursionLimit : Integer;
230 FRecursionDepth : Integer;
232 procedure SetRecursionLimit( value : Integer);
233 function GetRecursionLimit : Integer;
234 function NextRecursionLevel : IProtocolRecursionTracker;
235 procedure IncrementRecursionDepth;
236 procedure DecrementRecursionDepth;
238 function GetTransport: ITransport;
240 procedure WriteMessageBegin( const msg: TThriftMessage); virtual; abstract;
241 procedure WriteMessageEnd; virtual; abstract;
242 procedure WriteStructBegin( const struc: TThriftStruct); virtual; abstract;
243 procedure WriteStructEnd; virtual; abstract;
244 procedure WriteFieldBegin( const field: TThriftField); virtual; abstract;
245 procedure WriteFieldEnd; virtual; abstract;
246 procedure WriteFieldStop; virtual; abstract;
247 procedure WriteMapBegin( const map: TThriftMap); virtual; abstract;
248 procedure WriteMapEnd; virtual; abstract;
249 procedure WriteListBegin( const list: TThriftList); virtual; abstract;
250 procedure WriteListEnd(); virtual; abstract;
251 procedure WriteSetBegin( const set_: TThriftSet ); virtual; abstract;
252 procedure WriteSetEnd(); virtual; abstract;
253 procedure WriteBool( b: Boolean); virtual; abstract;
254 procedure WriteByte( b: ShortInt); virtual; abstract;
255 procedure WriteI16( i16: SmallInt); virtual; abstract;
256 procedure WriteI32( i32: Integer); virtual; abstract;
257 procedure WriteI64( const i64: Int64); virtual; abstract;
258 procedure WriteDouble( const d: Double); virtual; abstract;
259 procedure WriteString( const s: string ); virtual;
260 procedure WriteAnsiString( const s: AnsiString); virtual;
261 procedure WriteBinary( const b: TBytes); virtual; abstract;
263 function ReadMessageBegin: TThriftMessage; virtual; abstract;
264 procedure ReadMessageEnd(); virtual; abstract;
265 function ReadStructBegin: TThriftStruct; virtual; abstract;
266 procedure ReadStructEnd; virtual; abstract;
267 function ReadFieldBegin: TThriftField; virtual; abstract;
268 procedure ReadFieldEnd(); virtual; abstract;
269 function ReadMapBegin: TThriftMap; virtual; abstract;
270 procedure ReadMapEnd(); virtual; abstract;
271 function ReadListBegin: TThriftList; virtual; abstract;
272 procedure ReadListEnd(); virtual; abstract;
273 function ReadSetBegin: TThriftSet; virtual; abstract;
274 procedure ReadSetEnd(); virtual; abstract;
275 function ReadBool: Boolean; virtual; abstract;
276 function ReadByte: ShortInt; virtual; abstract;
277 function ReadI16: SmallInt; virtual; abstract;
278 function ReadI32: Integer; virtual; abstract;
279 function ReadI64: Int64; virtual; abstract;
280 function ReadDouble:Double; virtual; abstract;
281 function ReadBinary: TBytes; virtual; abstract;
282 function ReadString: string; virtual;
283 function ReadAnsiString: AnsiString; virtual;
285 property Transport: ITransport read GetTransport;
287 constructor Create( trans: ITransport );
290 IBase = interface( ISupportsToString)
291 ['{AFF6CECA-5200-4540-950E-9B89E0C1C00C}']
292 procedure Read( const iprot: IProtocol);
293 procedure Write( const iprot: IProtocol);
297 TBinaryProtocolImpl = class( TProtocolImpl )
300 VERSION_MASK : Cardinal = $ffff0000;
301 VERSION_1 : Cardinal = $80010000;
303 FStrictRead : Boolean;
304 FStrictWrite : Boolean;
307 function ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer; inline;
308 function ReadStringBody( size: Integer): string;
313 TFactory = class( TInterfacedObject, IProtocolFactory)
315 FStrictRead : Boolean;
316 FStrictWrite : Boolean;
318 function GetProtocol( const trans: ITransport): IProtocol;
319 constructor Create( AStrictRead, AStrictWrite: Boolean ); overload;
320 constructor Create; overload;
323 constructor Create( const trans: ITransport); overload;
324 constructor Create( const trans: ITransport; strictRead: Boolean; strictWrite: Boolean); overload;
326 procedure WriteMessageBegin( const msg: TThriftMessage); override;
327 procedure WriteMessageEnd; override;
328 procedure WriteStructBegin( const struc: TThriftStruct); override;
329 procedure WriteStructEnd; override;
330 procedure WriteFieldBegin( const field: TThriftField); override;
331 procedure WriteFieldEnd; override;
332 procedure WriteFieldStop; override;
333 procedure WriteMapBegin( const map: TThriftMap); override;
334 procedure WriteMapEnd; override;
335 procedure WriteListBegin( const list: TThriftList); override;
336 procedure WriteListEnd(); override;
337 procedure WriteSetBegin( const set_: TThriftSet ); override;
338 procedure WriteSetEnd(); override;
339 procedure WriteBool( b: Boolean); override;
340 procedure WriteByte( b: ShortInt); override;
341 procedure WriteI16( i16: SmallInt); override;
342 procedure WriteI32( i32: Integer); override;
343 procedure WriteI64( const i64: Int64); override;
344 procedure WriteDouble( const d: Double); override;
345 procedure WriteBinary( const b: TBytes); override;
347 function ReadMessageBegin: TThriftMessage; override;
348 procedure ReadMessageEnd(); override;
349 function ReadStructBegin: TThriftStruct; override;
350 procedure ReadStructEnd; override;
351 function ReadFieldBegin: TThriftField; override;
352 procedure ReadFieldEnd(); override;
353 function ReadMapBegin: TThriftMap; override;
354 procedure ReadMapEnd(); override;
355 function ReadListBegin: TThriftList; override;
356 procedure ReadListEnd(); override;
357 function ReadSetBegin: TThriftSet; override;
358 procedure ReadSetEnd(); override;
359 function ReadBool: Boolean; override;
360 function ReadByte: ShortInt; override;
361 function ReadI16: SmallInt; override;
362 function ReadI32: Integer; override;
363 function ReadI64: Int64; override;
364 function ReadDouble:Double; override;
365 function ReadBinary: TBytes; override;
370 { TProtocolDecorator forwards all requests to an enclosed TProtocol instance,
371 providing a way to author concise concrete decorator subclasses. The decorator
372 does not (and should not) modify the behaviour of the enclosed TProtocol
374 See p.175 of Design Patterns (by Gamma et al.)
376 TProtocolDecorator = class( TProtocolImpl)
378 FWrappedProtocol : IProtocol;
381 // Encloses the specified protocol.
382 // All operations will be forward to the given protocol. Must be non-null.
383 constructor Create( const aProtocol : IProtocol);
385 procedure WriteMessageBegin( const msg: TThriftMessage); override;
386 procedure WriteMessageEnd; override;
387 procedure WriteStructBegin( const struc: TThriftStruct); override;
388 procedure WriteStructEnd; override;
389 procedure WriteFieldBegin( const field: TThriftField); override;
390 procedure WriteFieldEnd; override;
391 procedure WriteFieldStop; override;
392 procedure WriteMapBegin( const map: TThriftMap); override;
393 procedure WriteMapEnd; override;
394 procedure WriteListBegin( const list: TThriftList); override;
395 procedure WriteListEnd(); override;
396 procedure WriteSetBegin( const set_: TThriftSet ); override;
397 procedure WriteSetEnd(); override;
398 procedure WriteBool( b: Boolean); override;
399 procedure WriteByte( b: ShortInt); override;
400 procedure WriteI16( i16: SmallInt); override;
401 procedure WriteI32( i32: Integer); override;
402 procedure WriteI64( const i64: Int64); override;
403 procedure WriteDouble( const d: Double); override;
404 procedure WriteString( const s: string ); override;
405 procedure WriteAnsiString( const s: AnsiString); override;
406 procedure WriteBinary( const b: TBytes); override;
408 function ReadMessageBegin: TThriftMessage; override;
409 procedure ReadMessageEnd(); override;
410 function ReadStructBegin: TThriftStruct; override;
411 procedure ReadStructEnd; override;
412 function ReadFieldBegin: TThriftField; override;
413 procedure ReadFieldEnd(); override;
414 function ReadMapBegin: TThriftMap; override;
415 procedure ReadMapEnd(); override;
416 function ReadListBegin: TThriftList; override;
417 procedure ReadListEnd(); override;
418 function ReadSetBegin: TThriftSet; override;
419 procedure ReadSetEnd(); override;
420 function ReadBool: Boolean; override;
421 function ReadByte: ShortInt; override;
422 function ReadI16: SmallInt; override;
423 function ReadI32: Integer; override;
424 function ReadI64: Int64; override;
425 function ReadDouble:Double; override;
426 function ReadBinary: TBytes; override;
427 function ReadString: string; override;
428 function ReadAnsiString: AnsiString; override;
433 IRequestEvents = interface
434 ['{F926A26A-5B00-4560-86FA-2CAE3BA73DAF}']
435 // Called before reading arguments.
437 // Called between reading arguments and calling the handler.
439 // Called between calling the handler and writing the response.
441 // Called after writing the response.
443 // Called when an oneway (async) function call completes successfully.
444 procedure OnewayComplete;
445 // Called if the handler throws an undeclared exception.
446 procedure UnhandledError( const e : Exception);
447 // Called when a client has finished request-handling to clean up
448 procedure CleanupContext;
452 IProcessorEvents = interface
453 ['{A8661119-657C-447D-93C5-512E36162A45}']
454 // Called when a client is about to call the processor.
455 procedure Processing( const transport : ITransport);
456 // Called on any service function invocation
457 function CreateRequestContext( const aFunctionName : string) : IRequestEvents;
458 // Called when a client has finished request-handling to clean up
459 procedure CleanupContext;
463 IProcessor = interface
464 ['{7BAE92A5-46DA-4F13-B6EA-0EABE233EE5F}']
465 function Process( const iprot :IProtocol; const oprot: IProtocol; const events : IProcessorEvents = nil): Boolean;
469 procedure Init( var rec : TThriftMessage; const AName: string = ''; const AMessageType: TMessageType = Low(TMessageType); const ASeqID: Integer = 0); overload; inline;
470 procedure Init( var rec : TThriftStruct; const AName: string = ''); overload; inline;
471 procedure Init( var rec : TThriftField; const AName: string = ''; const AType: TType = Low(TType); const AID: SmallInt = 0); overload; inline;
472 procedure Init( var rec : TThriftMap; const AKeyType: TType = Low(TType); const AValueType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
473 procedure Init( var rec : TThriftSet; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
474 procedure Init( var rec : TThriftList; const AElementType: TType = Low(TType); const ACount: Integer = 0); overload; inline;
479 function ConvertInt64ToDouble( const n: Int64): Double;
481 ASSERT( SizeOf(n) = SizeOf(Result));
482 System.Move( n, Result, SizeOf(Result));
485 function ConvertDoubleToInt64( const d: Double): Int64;
487 ASSERT( SizeOf(d) = SizeOf(Result));
488 System.Move( d, Result, SizeOf(Result));
493 { TProtocolRecursionTrackerImpl }
495 constructor TProtocolRecursionTrackerImpl.Create( prot : IProtocol);
499 // storing the pointer *after* the (successful) increment is important here
500 prot.IncrementRecursionDepth;
504 destructor TProtocolRecursionTrackerImpl.Destroy;
507 // we have to release the reference iff the pointer has been stored
508 if FProtocol <> nil then begin
509 FProtocol.DecrementRecursionDepth;
519 constructor TProtocolImpl.Create(trans: ITransport);
523 FRecursionLimit := DEFAULT_RECURSION_LIMIT;
524 FRecursionDepth := 0;
527 procedure TProtocolImpl.SetRecursionLimit( value : Integer);
529 FRecursionLimit := value;
532 function TProtocolImpl.GetRecursionLimit : Integer;
534 result := FRecursionLimit;
537 function TProtocolImpl.NextRecursionLevel : IProtocolRecursionTracker;
539 result := TProtocolRecursionTrackerImpl.Create(Self);
542 procedure TProtocolImpl.IncrementRecursionDepth;
544 if FRecursionDepth < FRecursionLimit
545 then Inc(FRecursionDepth)
546 else raise TProtocolExceptionDepthLimit.Create('Depth limit exceeded');
549 procedure TProtocolImpl.DecrementRecursionDepth;
554 function TProtocolImpl.GetTransport: ITransport;
559 function TProtocolImpl.ReadAnsiString: AnsiString;
569 SetLength( Result, len);
570 System.Move( b[0], Pointer(Result)^, len );
574 function TProtocolImpl.ReadString: string;
576 Result := TEncoding.UTF8.GetString( ReadBinary );
579 procedure TProtocolImpl.WriteAnsiString(const s: AnsiString);
588 System.Move( Pointer(s)^, b[0], len );
593 procedure TProtocolImpl.WriteString(const s: string);
597 b := TEncoding.UTF8.GetBytes(s);
603 class procedure TProtocolUtil.Skip( prot: IProtocol; type_: TType);
604 var field : TThriftField;
609 tracker : IProtocolRecursionTracker;
611 tracker := prot.NextRecursionLevel;
614 TType.Bool_ : prot.ReadBool();
615 TType.Byte_ : prot.ReadByte();
616 TType.I16 : prot.ReadI16();
617 TType.I32 : prot.ReadI32();
618 TType.I64 : prot.ReadI64();
619 TType.Double_ : prot.ReadDouble();
620 TType.String_ : prot.ReadBinary();// Don't try to decode the string, just skip it.
624 prot.ReadStructBegin();
626 field := prot.ReadFieldBegin();
627 if (field.Type_ = TType.Stop) then Break;
628 Skip(prot, field.Type_);
631 prot.ReadStructEnd();
635 map := prot.ReadMapBegin();
636 for i := 0 to map.Count-1 do begin
637 Skip(prot, map.KeyType);
638 Skip(prot, map.ValueType);
644 set_ := prot.ReadSetBegin();
645 for i := 0 to set_.Count-1
646 do Skip( prot, set_.ElementType);
651 list := prot.ReadListBegin();
652 for i := 0 to list.Count-1
653 do Skip( prot, list.ElementType);
658 raise TProtocolExceptionInvalidData.Create('Unexpected type '+IntToStr(Ord(type_)));
663 { TBinaryProtocolImpl }
665 constructor TBinaryProtocolImpl.Create( const trans: ITransport);
668 Create( trans, False, True);
671 constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead,
672 strictWrite: Boolean);
674 inherited Create( trans );
675 FStrictRead := strictRead;
676 FStrictWrite := strictWrite;
679 function TBinaryProtocolImpl.ReadAll( const pBuf : Pointer; const buflen : Integer; off: Integer; len: Integer ): Integer;
681 Result := FTrans.ReadAll( pBuf, buflen, off, len );
684 function TBinaryProtocolImpl.ReadBinary: TBytes;
690 SetLength( buf, size );
691 FTrans.ReadAll( buf, 0, size);
695 function TBinaryProtocolImpl.ReadBool: Boolean;
697 Result := (ReadByte = 1);
700 function TBinaryProtocolImpl.ReadByte: ShortInt;
702 ReadAll( @result, SizeOf(result), 0, 1);
705 function TBinaryProtocolImpl.ReadDouble: Double;
707 Result := ConvertInt64ToDouble( ReadI64 )
710 function TBinaryProtocolImpl.ReadFieldBegin: TThriftField;
712 Init( result, '', TType( ReadByte), 0);
713 if ( result.Type_ <> TType.Stop ) then begin
714 result.Id := ReadI16;
718 procedure TBinaryProtocolImpl.ReadFieldEnd;
723 function TBinaryProtocolImpl.ReadI16: SmallInt;
724 var i16in : packed array[0..1] of Byte;
726 ReadAll( @i16in, Sizeof(i16in), 0, 2);
727 Result := SmallInt(((i16in[0] and $FF) shl 8) or (i16in[1] and $FF));
730 function TBinaryProtocolImpl.ReadI32: Integer;
731 var i32in : packed array[0..3] of Byte;
733 ReadAll( @i32in, SizeOf(i32in), 0, 4);
736 ((i32in[0] and $FF) shl 24) or
737 ((i32in[1] and $FF) shl 16) or
738 ((i32in[2] and $FF) shl 8) or
743 function TBinaryProtocolImpl.ReadI64: Int64;
744 var i64in : packed array[0..7] of Byte;
746 ReadAll( @i64in, SizeOf(i64in), 0, 8);
748 (Int64( i64in[0] and $FF) shl 56) or
749 (Int64( i64in[1] and $FF) shl 48) or
750 (Int64( i64in[2] and $FF) shl 40) or
751 (Int64( i64in[3] and $FF) shl 32) or
752 (Int64( i64in[4] and $FF) shl 24) or
753 (Int64( i64in[5] and $FF) shl 16) or
754 (Int64( i64in[6] and $FF) shl 8) or
755 (Int64( i64in[7] and $FF));
758 function TBinaryProtocolImpl.ReadListBegin: TThriftList;
760 result.ElementType := TType(ReadByte);
761 result.Count := ReadI32;
764 procedure TBinaryProtocolImpl.ReadListEnd;
769 function TBinaryProtocolImpl.ReadMapBegin: TThriftMap;
771 result.KeyType := TType(ReadByte);
772 result.ValueType := TType(ReadByte);
773 result.Count := ReadI32;
776 procedure TBinaryProtocolImpl.ReadMapEnd;
781 function TBinaryProtocolImpl.ReadMessageBegin: TThriftMessage;
788 if (size < 0) then begin
789 version := size and Integer( VERSION_MASK);
790 if ( version <> Integer( VERSION_1)) then begin
791 raise TProtocolExceptionBadVersion.Create('Bad version in ReadMessageBegin: ' + IntToStr(version) );
793 result.Type_ := TMessageType( size and $000000ff);
794 result.Name := ReadString;
795 result.SeqID := ReadI32;
798 if FStrictRead then begin
799 raise TProtocolExceptionBadVersion.Create('Missing version in readMessageBegin, old client?' );
801 result.Name := ReadStringBody( size );
802 result.Type_ := TMessageType( ReadByte );
803 result.SeqID := ReadI32;
807 procedure TBinaryProtocolImpl.ReadMessageEnd;
813 function TBinaryProtocolImpl.ReadSetBegin: TThriftSet;
815 result.ElementType := TType(ReadByte);
816 result.Count := ReadI32;
819 procedure TBinaryProtocolImpl.ReadSetEnd;
824 function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
828 SetLength( buf, size );
829 FTrans.ReadAll( buf, 0, size );
830 Result := TEncoding.UTF8.GetString( buf);
833 function TBinaryProtocolImpl.ReadStructBegin: TThriftStruct;
838 procedure TBinaryProtocolImpl.ReadStructEnd;
844 procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
849 if iLen > 0 then FTrans.Write(b, 0, iLen);
852 procedure TBinaryProtocolImpl.WriteBool(b: Boolean);
861 procedure TBinaryProtocolImpl.WriteByte(b: ShortInt);
863 FTrans.Write( @b, 0, 1);
866 procedure TBinaryProtocolImpl.WriteDouble( const d: Double);
868 WriteI64(ConvertDoubleToInt64(d));
871 procedure TBinaryProtocolImpl.WriteFieldBegin( const field: TThriftField);
873 WriteByte(ShortInt(field.Type_));
877 procedure TBinaryProtocolImpl.WriteFieldEnd;
882 procedure TBinaryProtocolImpl.WriteFieldStop;
884 WriteByte(ShortInt(TType.Stop));
887 procedure TBinaryProtocolImpl.WriteI16(i16: SmallInt);
888 var i16out : packed array[0..1] of Byte;
890 i16out[0] := Byte($FF and (i16 shr 8));
891 i16out[1] := Byte($FF and i16);
892 FTrans.Write( @i16out, 0, 2);
895 procedure TBinaryProtocolImpl.WriteI32(i32: Integer);
896 var i32out : packed array[0..3] of Byte;
898 i32out[0] := Byte($FF and (i32 shr 24));
899 i32out[1] := Byte($FF and (i32 shr 16));
900 i32out[2] := Byte($FF and (i32 shr 8));
901 i32out[3] := Byte($FF and i32);
902 FTrans.Write( @i32out, 0, 4);
905 procedure TBinaryProtocolImpl.WriteI64( const i64: Int64);
906 var i64out : packed array[0..7] of Byte;
908 i64out[0] := Byte($FF and (i64 shr 56));
909 i64out[1] := Byte($FF and (i64 shr 48));
910 i64out[2] := Byte($FF and (i64 shr 40));
911 i64out[3] := Byte($FF and (i64 shr 32));
912 i64out[4] := Byte($FF and (i64 shr 24));
913 i64out[5] := Byte($FF and (i64 shr 16));
914 i64out[6] := Byte($FF and (i64 shr 8));
915 i64out[7] := Byte($FF and i64);
916 FTrans.Write( @i64out, 0, 8);
919 procedure TBinaryProtocolImpl.WriteListBegin( const list: TThriftList);
921 WriteByte(ShortInt(list.ElementType));
922 WriteI32(list.Count);
925 procedure TBinaryProtocolImpl.WriteListEnd;
930 procedure TBinaryProtocolImpl.WriteMapBegin( const map: TThriftMap);
932 WriteByte(ShortInt(map.KeyType));
933 WriteByte(ShortInt(map.ValueType));
937 procedure TBinaryProtocolImpl.WriteMapEnd;
942 procedure TBinaryProtocolImpl.WriteMessageBegin( const msg: TThriftMessage);
948 version := VERSION_1 or Cardinal( msg.Type_);
949 WriteI32( Integer( version) );
950 WriteString( msg.Name);
951 WriteI32( msg.SeqID);
954 WriteString( msg.Name);
955 WriteByte(ShortInt( msg.Type_));
956 WriteI32( msg.SeqID);
960 procedure TBinaryProtocolImpl.WriteMessageEnd;
965 procedure TBinaryProtocolImpl.WriteSetBegin( const set_: TThriftSet);
967 WriteByte(ShortInt(set_.ElementType));
968 WriteI32(set_.Count);
971 procedure TBinaryProtocolImpl.WriteSetEnd;
976 procedure TBinaryProtocolImpl.WriteStructBegin( const struc: TThriftStruct);
981 procedure TBinaryProtocolImpl.WriteStructEnd;
986 { TProtocolException }
988 constructor TProtocolException.HiddenCreate(const Msg: string);
990 inherited Create(Msg);
993 class function TProtocolException.Create(const Msg: string): TProtocolException;
995 Result := TProtocolExceptionUnknown.Create(Msg);
998 class function TProtocolException.Create: TProtocolException;
1000 Result := TProtocolExceptionUnknown.Create('');
1003 class function TProtocolException.Create(type_: Integer): TProtocolException;
1005 {$WARN SYMBOL_DEPRECATED OFF}
1006 Result := Create(type_, '');
1007 {$WARN SYMBOL_DEPRECATED DEFAULT}
1010 class function TProtocolException.Create(type_: Integer; const msg: string): TProtocolException;
1013 INVALID_DATA: Result := TProtocolExceptionInvalidData.Create(msg);
1014 NEGATIVE_SIZE: Result := TProtocolExceptionNegativeSize.Create(msg);
1015 SIZE_LIMIT: Result := TProtocolExceptionSizeLimit.Create(msg);
1016 BAD_VERSION: Result := TProtocolExceptionBadVersion.Create(msg);
1017 NOT_IMPLEMENTED: Result := TProtocolExceptionNotImplemented.Create(msg);
1018 DEPTH_LIMIT: Result := TProtocolExceptionDepthLimit.Create(msg);
1020 Result := TProtocolExceptionUnknown.Create(msg);
1024 { TProtocolExceptionSpecialized }
1026 constructor TProtocolExceptionSpecialized.Create(const Msg: string);
1028 inherited HiddenCreate(Msg);
1031 { TBinaryProtocolImpl.TFactory }
1033 constructor TBinaryProtocolImpl.TFactory.Create(AStrictRead, AStrictWrite: Boolean);
1036 FStrictRead := AStrictRead;
1037 FStrictWrite := AStrictWrite;
1040 constructor TBinaryProtocolImpl.TFactory.Create;
1043 Create( False, True )
1046 function TBinaryProtocolImpl.TFactory.GetProtocol( const trans: ITransport): IProtocol;
1048 Result := TBinaryProtocolImpl.Create( trans, FStrictRead, FStrictWrite);
1052 { TProtocolDecorator }
1054 constructor TProtocolDecorator.Create( const aProtocol : IProtocol);
1056 ASSERT( aProtocol <> nil);
1057 inherited Create( aProtocol.Transport);
1058 FWrappedProtocol := aProtocol;
1062 procedure TProtocolDecorator.WriteMessageBegin( const msg: TThriftMessage);
1064 FWrappedProtocol.WriteMessageBegin( msg);
1068 procedure TProtocolDecorator.WriteMessageEnd;
1070 FWrappedProtocol.WriteMessageEnd;
1074 procedure TProtocolDecorator.WriteStructBegin( const struc: TThriftStruct);
1076 FWrappedProtocol.WriteStructBegin( struc);
1080 procedure TProtocolDecorator.WriteStructEnd;
1082 FWrappedProtocol.WriteStructEnd;
1086 procedure TProtocolDecorator.WriteFieldBegin( const field: TThriftField);
1088 FWrappedProtocol.WriteFieldBegin( field);
1092 procedure TProtocolDecorator.WriteFieldEnd;
1094 FWrappedProtocol.WriteFieldEnd;
1098 procedure TProtocolDecorator.WriteFieldStop;
1100 FWrappedProtocol.WriteFieldStop;
1104 procedure TProtocolDecorator.WriteMapBegin( const map: TThriftMap);
1106 FWrappedProtocol.WriteMapBegin( map);
1110 procedure TProtocolDecorator.WriteMapEnd;
1112 FWrappedProtocol.WriteMapEnd;
1116 procedure TProtocolDecorator.WriteListBegin( const list: TThriftList);
1118 FWrappedProtocol.WriteListBegin( list);
1122 procedure TProtocolDecorator.WriteListEnd();
1124 FWrappedProtocol.WriteListEnd();
1128 procedure TProtocolDecorator.WriteSetBegin( const set_: TThriftSet );
1130 FWrappedProtocol.WriteSetBegin( set_);
1134 procedure TProtocolDecorator.WriteSetEnd();
1136 FWrappedProtocol.WriteSetEnd();
1140 procedure TProtocolDecorator.WriteBool( b: Boolean);
1142 FWrappedProtocol.WriteBool( b);
1146 procedure TProtocolDecorator.WriteByte( b: ShortInt);
1148 FWrappedProtocol.WriteByte( b);
1152 procedure TProtocolDecorator.WriteI16( i16: SmallInt);
1154 FWrappedProtocol.WriteI16( i16);
1158 procedure TProtocolDecorator.WriteI32( i32: Integer);
1160 FWrappedProtocol.WriteI32( i32);
1164 procedure TProtocolDecorator.WriteI64( const i64: Int64);
1166 FWrappedProtocol.WriteI64( i64);
1170 procedure TProtocolDecorator.WriteDouble( const d: Double);
1172 FWrappedProtocol.WriteDouble( d);
1176 procedure TProtocolDecorator.WriteString( const s: string );
1178 FWrappedProtocol.WriteString( s);
1182 procedure TProtocolDecorator.WriteAnsiString( const s: AnsiString);
1184 FWrappedProtocol.WriteAnsiString( s);
1188 procedure TProtocolDecorator.WriteBinary( const b: TBytes);
1190 FWrappedProtocol.WriteBinary( b);
1194 function TProtocolDecorator.ReadMessageBegin: TThriftMessage;
1196 result := FWrappedProtocol.ReadMessageBegin;
1200 procedure TProtocolDecorator.ReadMessageEnd();
1202 FWrappedProtocol.ReadMessageEnd();
1206 function TProtocolDecorator.ReadStructBegin: TThriftStruct;
1208 result := FWrappedProtocol.ReadStructBegin;
1212 procedure TProtocolDecorator.ReadStructEnd;
1214 FWrappedProtocol.ReadStructEnd;
1218 function TProtocolDecorator.ReadFieldBegin: TThriftField;
1220 result := FWrappedProtocol.ReadFieldBegin;
1224 procedure TProtocolDecorator.ReadFieldEnd();
1226 FWrappedProtocol.ReadFieldEnd();
1230 function TProtocolDecorator.ReadMapBegin: TThriftMap;
1232 result := FWrappedProtocol.ReadMapBegin;
1236 procedure TProtocolDecorator.ReadMapEnd();
1238 FWrappedProtocol.ReadMapEnd();
1242 function TProtocolDecorator.ReadListBegin: TThriftList;
1244 result := FWrappedProtocol.ReadListBegin;
1248 procedure TProtocolDecorator.ReadListEnd();
1250 FWrappedProtocol.ReadListEnd();
1254 function TProtocolDecorator.ReadSetBegin: TThriftSet;
1256 result := FWrappedProtocol.ReadSetBegin;
1260 procedure TProtocolDecorator.ReadSetEnd();
1262 FWrappedProtocol.ReadSetEnd();
1266 function TProtocolDecorator.ReadBool: Boolean;
1268 result := FWrappedProtocol.ReadBool;
1272 function TProtocolDecorator.ReadByte: ShortInt;
1274 result := FWrappedProtocol.ReadByte;
1278 function TProtocolDecorator.ReadI16: SmallInt;
1280 result := FWrappedProtocol.ReadI16;
1284 function TProtocolDecorator.ReadI32: Integer;
1286 result := FWrappedProtocol.ReadI32;
1290 function TProtocolDecorator.ReadI64: Int64;
1292 result := FWrappedProtocol.ReadI64;
1296 function TProtocolDecorator.ReadDouble:Double;
1298 result := FWrappedProtocol.ReadDouble;
1302 function TProtocolDecorator.ReadBinary: TBytes;
1304 result := FWrappedProtocol.ReadBinary;
1308 function TProtocolDecorator.ReadString: string;
1310 result := FWrappedProtocol.ReadString;
1314 function TProtocolDecorator.ReadAnsiString: AnsiString;
1316 result := FWrappedProtocol.ReadAnsiString;
1320 { Init helper functions }
1322 procedure Init( var rec : TThriftMessage; const AName: string; const AMessageType: TMessageType; const ASeqID: Integer);
1325 rec.Type_ := AMessageType;
1326 rec.SeqID := ASeqID;
1330 procedure Init( var rec : TThriftStruct; const AName: string = '');
1336 procedure Init( var rec : TThriftField; const AName: string; const AType: TType; const AID: SmallInt);
1344 procedure Init( var rec : TThriftMap; const AKeyType, AValueType: TType; const ACount: Integer);
1346 rec.ValueType := AValueType;
1347 rec.KeyType := AKeyType;
1348 rec.Count := ACount;
1352 procedure Init( var rec : TThriftSet; const AElementType: TType; const ACount: Integer);
1354 rec.Count := ACount;
1355 rec.ElementType := AElementType;
1359 procedure Init( var rec : TThriftList; const AElementType: TType; const ACount: Integer);
1361 rec.Count := ACount;
1362 rec.ElementType := AElementType;