Ignore:
Timestamp:
Oct 9, 2009, 2:15:48 PM (15 years ago)
Author:
george
Message:
  • Přidáno: Některé funkce pro dekódování.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ProtocolBuffers/UProtocolBuffers.pas

    r6 r7  
    1818
    1919  { TPBItem }
    20 
    2120  TPBItem = class
    2221    Name: string;
     
    2423    ItemType: TPBItemType;
    2524    ItemMode: TPBItemMode;
     25    procedure SaveVariantToStream(Stream: TStream; Value: Integer);
    2626    procedure SaveToStream(Stream: TStream); virtual;
    27   end;
    28 
    29   TPBMessage = class;
    30 
     27    procedure LoadFromStream(Stream: TStream); virtual;
     28    function LoadVariantFromStream(Stream: TStream);
     29  end;
     30
     31  TPBMessageItem = class;
     32
     33  { TPBStringItem }
    3134  TPBStringItem = class(TPBItem)
    3235    Value: string;
    3336  end;
    3437
     38  { TPBIntegerItem }
    3539  TPBIntegerItem = class(TPBItem)
    3640    Value: Integer;
    37   end;
    38 
     41    procedure SaveToStream(Stream: TStream); override;
     42    constructor Create;
     43  end;
     44
     45  { TPBMessageItem }
    3946  TPBMessageItem = class(TPBItem)
    40     Tag: Integer;
    41     Name: string;
    42     ItemType: TPBItemType;
    43   end;
    44 
    45   { TPBMessage }
    46 
    47   TPBMessage = class(TPBItem)
    4847    Items: TList; // TList<TPBItem>;
    4948    procedure SaveToStream(Stream: TStream); override;
     49    procedure LoadFromStream(Stream: TStream); override;
    5050    constructor Create;
     51    destructor Destroy; override;
    5152  end;
    5253
    5354  { TProtocolBuffer }
    54 
    5555  TProtocolBuffer = class
    56     BaseMessage: TPBMessage;
     56    BaseMessage: TPBMessageItem;
    5757    procedure LoadFromStream(Stream: TStream);
    5858    procedure SaveToStream(Stream: TStream);
     59    constructor Create;
    5960    destructor Destroy; override;
    6061  end;
     
    6970procedure TProtocolBuffer.LoadFromStream(Stream: TStream);
    7071begin
    71 
     72  BaseMessage.LoadFromStream(Stream);
    7273end;
    7374
     
    7576begin
    7677  BaseMessage.SaveToStream(Stream);
     78end;
     79
     80constructor TProtocolBuffer.Create;
     81begin
     82  BaseMessage := TPBMessageItem.Create;
    7783end;
    7884
     
    8389end;
    8490
    85 { TPBMessage }
    86 
    87 procedure TPBMessage.SaveToStream(Stream: TStream);
     91{ TPBMessageItem }
     92
     93procedure TPBMessageItem.SaveToStream(Stream: TStream);
     94var
     95  I: Integer;
    8896begin
    8997  inherited SaveToStream(Stream);
    90 end;
    91 
    92 constructor TPBMessage.Create;
     98  for I := 0 to Items.Count - 1 do
     99    TPBItem(Items[I]).SaveToStream(Stream);
     100end;
     101
     102procedure TPBMessageItem.LoadFromStream(Stream: TStream);
     103begin
     104  inherited LoadFromStream(Stream);
     105end;
     106
     107constructor TPBMessageItem.Create;
    93108begin
    94109  ItemType := itLengthDelimited;
     110  Items := TList.Create;
     111end;
     112
     113destructor TPBMessageItem.Destroy;
     114var
     115  I: Integer;
     116begin
     117  for I := 0 to Items.Count - 1 do
     118    TPBItem(Items[I]).Free;
     119  Items.Free;
     120  inherited Destroy;
    95121end;
    96122
    97123{ TPBItem }
    98124
    99 procedure TPBItem.SaveToStream(Stream: TStream);
     125procedure TPBItem.SaveVariantToStream(Stream: TStream; Value: Integer);
    100126var
    101127  ByteIndex: Byte;
     
    103129begin
    104130  with TMemoryStreamEx(Stream) do begin
    105     Data := ((Tag and $f) shl 3) or (Integer(ItemType) and $7);
    106     ByteIndex := 0;
    107     while Tag > (1 shl (ByteIndex * 8 + 4)) do begin
     131    Data := Value and $7f;
     132    ByteIndex := 1;
     133    while Value > (1 shl (ByteIndex * 7)) do begin
    108134      WriteByte(Data or $80);
    109       Data := (Tag shr (ByteIndex * 8 + 4)) and $7f;
     135      Data := (Value shr (ByteIndex * 7)) and $7f;
    110136      Inc(ByteIndex);
    111137    end;
     
    114140end;
    115141
     142procedure TPBItem.SaveToStream(Stream: TStream);
     143var
     144  ByteIndex: Byte;
     145  Data: Byte;
     146begin
     147  with TMemoryStreamEx(Stream) do begin
     148    Data := ((Tag and $f) shl 3) or (Integer(ItemType) and $7);
     149    ByteIndex := 0;
     150    while Tag > (1 shl (ByteIndex * 7 + 4)) do begin
     151      WriteByte(Data or $80);
     152      Data := (Tag shr (ByteIndex * 7 + 4)) and $7f;
     153      Inc(ByteIndex);
     154    end;
     155    WriteByte(Data);
     156  end
     157end;
     158
     159procedure TPBItem.LoadFromStream(Stream: TStream);
     160var
     161  Data: Byte;
     162  ByteIndex: Byte;
     163begin
     164  Data := TMemoryStreamEx(Stream).ReadByte;
     165  ItemType := TPBItemType(Data and 3);
     166  Tag := (Data shr 3) and $f;
     167  ByteIndex := 0;
     168  while Data > $7f do begin
     169    Data := TMemoryStreamEx(Stream).ReadByte;
     170    Tag := Tag or ((Data and $7f) shl (ByteIndex * 7 + 4))
     171    Inc(ByteIndex);
     172  end;
     173end;
     174
     175function TPBItem.LoadVariantFromStream(Stream: TStream);
     176var
     177  Data: Byte;
     178  ByteIndex: Byte;
     179begin
     180  Data := TMemoryStreamEx(Stream).ReadByte;
     181  Tag := Data and $7f;
     182  ByteIndex := 1;
     183  while Data > $7f do begin
     184    Data := TMemoryStreamEx(Stream).ReadByte;
     185    Tag := Tag or ((Data and $7f) shl (ByteIndex * 7))
     186    Inc(ByteIndex);
     187  end;
     188end;
     189
     190{ TPBIntegerItem }
     191
     192procedure TPBIntegerItem.SaveToStream(Stream: TStream);
     193begin
     194  inherited SaveToStream(Stream);
     195  SaveVariantToStream(Stream, Value);
     196end;
     197
     198constructor TPBIntegerItem.Create;
     199begin
     200  ItemType := itVariant;
     201end;
     202
    116203end.
    117204
Note: See TracChangeset for help on using the changeset viewer.