Changeset 7 for ProtocolBuffers


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í.
Location:
ProtocolBuffers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ProtocolBuffers/Demo.lpi

    r6 r7  
    3838        <IsPartOfProject Value="True"/>
    3939        <UnitName Value="Demo"/>
    40         <CursorPos X="66" Y="10"/>
     40        <CursorPos X="49" Y="10"/>
    4141        <TopLine Value="1"/>
    4242        <EditorIndex Value="3"/>
    43         <UsageCount Value="20"/>
     43        <UsageCount Value="21"/>
    4444        <Loaded Value="True"/>
    4545      </Unit0>
     
    5050        <ResourceBaseClass Value="Form"/>
    5151        <UnitName Value="UMainForm"/>
    52         <CursorPos X="29" Y="41"/>
    53         <TopLine Value="30"/>
     52        <CursorPos X="41" Y="43"/>
     53        <TopLine Value="24"/>
    5454        <EditorIndex Value="0"/>
    55         <UsageCount Value="20"/>
     55        <UsageCount Value="21"/>
    5656        <Loaded Value="True"/>
    5757      </Unit1>
     
    6060        <IsPartOfProject Value="True"/>
    6161        <UnitName Value="UProtocolBuffers"/>
    62         <CursorPos X="48" Y="102"/>
    63         <TopLine Value="85"/>
     62        <CursorPos X="14" Y="160"/>
     63        <TopLine Value="144"/>
    6464        <EditorIndex Value="1"/>
    65         <UsageCount Value="20"/>
     65        <UsageCount Value="21"/>
    6666        <Loaded Value="True"/>
    6767      </Unit2>
     
    7373        <TopLine Value="2"/>
    7474        <EditorIndex Value="2"/>
    75         <UsageCount Value="20"/>
     75        <UsageCount Value="21"/>
    7676        <Loaded Value="True"/>
    7777      </Unit3>
     
    7979    <JumpHistory Count="30" HistoryIndex="29">
    8080      <Position1>
    81         <Filename Value="UMemoryStreamEx.pas"/>
    82         <Caret Line="49" Column="13" TopLine="36"/>
     81        <Filename Value="UMainForm.pas"/>
     82        <Caret Line="34" Column="14" TopLine="28"/>
    8383      </Position1>
    8484      <Position2>
    85         <Filename Value="UMemoryStreamEx.pas"/>
    86         <Caret Line="74" Column="14" TopLine="61"/>
     85        <Filename Value="UProtocolBuffers.pas"/>
     86        <Caret Line="55" Column="18" TopLine="39"/>
    8787      </Position2>
    8888      <Position3>
    89         <Filename Value="UMemoryStreamEx.pas"/>
    90         <Caret Line="49" Column="13" TopLine="36"/>
     89        <Filename Value="UProtocolBuffers.pas"/>
     90        <Caret Line="41" Column="3" TopLine="19"/>
    9191      </Position3>
    9292      <Position4>
    93         <Filename Value="UMemoryStreamEx.pas"/>
    94         <Caret Line="74" Column="14" TopLine="61"/>
     93        <Filename Value="UProtocolBuffers.pas"/>
     94        <Caret Line="19" Column="3" TopLine="9"/>
    9595      </Position4>
    9696      <Position5>
    97         <Filename Value="UMemoryStreamEx.pas"/>
    98         <Caret Line="3" Column="19" TopLine="1"/>
     97        <Filename Value="UProtocolBuffers.pas"/>
     98        <Caret Line="96" Column="34" TopLine="67"/>
    9999      </Position5>
    100100      <Position6>
    101         <Filename Value="UMemoryStreamEx.pas"/>
    102         <Caret Line="76" Column="14" TopLine="63"/>
     101        <Filename Value="UMainForm.pas"/>
     102        <Caret Line="42" Column="5" TopLine="15"/>
    103103      </Position6>
    104104      <Position7>
    105         <Filename Value="UMemoryStreamEx.pas"/>
    106         <Caret Line="44" Column="14" TopLine="31"/>
     105        <Filename Value="UProtocolBuffers.pas"/>
     106        <Caret Line="49" Column="46" TopLine="23"/>
    107107      </Position7>
    108108      <Position8>
    109         <Filename Value="UMemoryStreamEx.pas"/>
    110         <Caret Line="39" Column="14" TopLine="26"/>
     109        <Filename Value="UMainForm.pas"/>
     110        <Caret Line="42" Column="5" TopLine="28"/>
    111111      </Position8>
    112112      <Position9>
    113         <Filename Value="UMemoryStreamEx.pas"/>
    114         <Caret Line="106" Column="58" TopLine="98"/>
     113        <Filename Value="UProtocolBuffers.pas"/>
     114        <Caret Line="97" Column="5" TopLine="65"/>
    115115      </Position9>
    116116      <Position10>
    117         <Filename Value="UMemoryStreamEx.pas"/>
    118         <Caret Line="76" Column="14" TopLine="63"/>
     117        <Filename Value="UMainForm.pas"/>
     118        <Caret Line="42" Column="9" TopLine="30"/>
    119119      </Position10>
    120120      <Position11>
    121         <Filename Value="UMemoryStreamEx.pas"/>
    122         <Caret Line="39" Column="14" TopLine="26"/>
     121        <Filename Value="UProtocolBuffers.pas"/>
     122        <Caret Line="110" Column="11" TopLine="84"/>
    123123      </Position11>
    124124      <Position12>
    125         <Filename Value="UMemoryStreamEx.pas"/>
    126         <Caret Line="19" Column="40" TopLine="1"/>
     125        <Filename Value="UProtocolBuffers.pas"/>
     126        <Caret Line="107" Column="1" TopLine="85"/>
    127127      </Position12>
    128128      <Position13>
    129         <Filename Value="UProtocolBuffers.pas"/>
    130         <Caret Line="45" Column="3" TopLine="32"/>
     129        <Filename Value="UMainForm.pas"/>
     130        <Caret Line="43" Column="41" TopLine="30"/>
    131131      </Position13>
    132132      <Position14>
    133         <Filename Value="UMainForm.pas"/>
    134         <Caret Line="33" Column="36" TopLine="15"/>
     133        <Filename Value="UProtocolBuffers.pas"/>
     134        <Caret Line="104" Column="1" TopLine="72"/>
    135135      </Position14>
    136136      <Position15>
    137         <Filename Value="UMainForm.pas"/>
    138         <Caret Line="41" Column="3" TopLine="16"/>
     137        <Filename Value="UProtocolBuffers.pas"/>
     138        <Caret Line="103" Column="5" TopLine="71"/>
    139139      </Position15>
    140140      <Position16>
    141         <Filename Value="UMainForm.pas"/>
    142         <Caret Line="45" Column="46" TopLine="22"/>
     141        <Filename Value="UProtocolBuffers.pas"/>
     142        <Caret Line="38" Column="3" TopLine="19"/>
    143143      </Position16>
    144144      <Position17>
    145         <Filename Value="UMainForm.pas"/>
    146         <Caret Line="3" Column="14" TopLine="1"/>
     145        <Filename Value="UProtocolBuffers.pas"/>
     146        <Caret Line="80" Column="5" TopLine="48"/>
    147147      </Position17>
    148148      <Position18>
    149         <Filename Value="UMainForm.pas"/>
    150         <Caret Line="34" Column="14" TopLine="28"/>
     149        <Filename Value="UProtocolBuffers.pas"/>
     150        <Caret Line="154" Column="1" TopLine="122"/>
    151151      </Position18>
    152152      <Position19>
    153153        <Filename Value="UProtocolBuffers.pas"/>
    154         <Caret Line="55" Column="18" TopLine="39"/>
     154        <Caret Line="96" Column="44" TopLine="90"/>
    155155      </Position19>
    156156      <Position20>
    157157        <Filename Value="UProtocolBuffers.pas"/>
    158         <Caret Line="41" Column="3" TopLine="19"/>
     158        <Caret Line="29" Column="17" TopLine="13"/>
    159159      </Position20>
    160160      <Position21>
    161161        <Filename Value="UProtocolBuffers.pas"/>
    162         <Caret Line="19" Column="3" TopLine="9"/>
     162        <Caret Line="53" Column="32" TopLine="37"/>
    163163      </Position21>
    164164      <Position22>
    165165        <Filename Value="UProtocolBuffers.pas"/>
    166         <Caret Line="96" Column="34" TopLine="67"/>
     166        <Caret Line="156" Column="18" TopLine="133"/>
    167167      </Position22>
    168168      <Position23>
    169         <Filename Value="UMainForm.pas"/>
    170         <Caret Line="42" Column="5" TopLine="15"/>
     169        <Filename Value="UProtocolBuffers.pas"/>
     170        <Caret Line="117" Column="54" TopLine="117"/>
    171171      </Position23>
    172172      <Position24>
    173         <Filename Value="UProtocolBuffers.pas"/>
    174         <Caret Line="49" Column="46" TopLine="23"/>
     173        <Filename Value="Demo.lpr"/>
     174        <Caret Line="10" Column="66" TopLine="1"/>
    175175      </Position24>
    176176      <Position25>
    177         <Filename Value="UMainForm.pas"/>
    178         <Caret Line="42" Column="5" TopLine="28"/>
     177        <Filename Value="Demo.lpr"/>
     178        <Caret Line="19" Column="51" TopLine="1"/>
    179179      </Position25>
    180180      <Position26>
    181         <Filename Value="UProtocolBuffers.pas"/>
    182         <Caret Line="97" Column="5" TopLine="65"/>
     181        <Filename Value="Demo.lpr"/>
     182        <Caret Line="10" Column="39" TopLine="1"/>
    183183      </Position26>
    184184      <Position27>
    185         <Filename Value="UMainForm.pas"/>
    186         <Caret Line="42" Column="9" TopLine="30"/>
     185        <Filename Value="UProtocolBuffers.pas"/>
     186        <Caret Line="156" Column="39" TopLine="140"/>
    187187      </Position27>
    188188      <Position28>
    189189        <Filename Value="UProtocolBuffers.pas"/>
    190         <Caret Line="110" Column="11" TopLine="84"/>
     190        <Caret Line="55" Column="47" TopLine="39"/>
    191191      </Position28>
    192192      <Position29>
    193193        <Filename Value="UProtocolBuffers.pas"/>
    194         <Caret Line="107" Column="1" TopLine="85"/>
     194        <Caret Line="104" Column="5" TopLine="72"/>
    195195      </Position29>
    196196      <Position30>
    197         <Filename Value="UMainForm.pas"/>
    198         <Caret Line="43" Column="41" TopLine="30"/>
     197        <Filename Value="UProtocolBuffers.pas"/>
     198        <Caret Line="103" Column="36" TopLine="101"/>
    199199      </Position30>
    200200    </JumpHistory>
  • ProtocolBuffers/UMainForm.pas

    r6 r7  
    3333  Text: string;
    3434  I: Integer;
     35  NewItem: TPBItem;
    3536begin
    3637  Stream := TMemoryStreamEx.Create;
    3738  PB := TProtocolBuffer.Create;
    3839  with PB do begin
    39     BaseMessage := TPBMessage.Create;
    40     BaseMessage.Name := 'SampleMessage';
    41     BaseMessage.Tag := $aaaa;
     40    with BaseMessage do begin
     41      Name := 'SampleMessage';
     42      NewItem := TPBIntegerItem.Create;
     43      TPBIntegerItem(NewItem).Value := $5555555;
     44      Items.Add(NewItem);
     45    end;
    4246    SaveToStream(Stream);
    4347  end;
  • 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.