Changeset 172 for CoolStreaming
- Timestamp:
- Feb 22, 2011, 1:53:47 PM (14 years ago)
- Location:
- CoolStreaming
- Files:
-
- 7 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
CoolStreaming/CoolStreaming.lpk
r125 r172 6 6 <Author Value="Chronos"/> 7 7 <CompilerOptions> 8 <Version Value=" 9"/>8 <Version Value="10"/> 9 9 <PathDelim Value="\"/> 10 10 <SearchPaths> … … 49 49 </Files> 50 50 <Type Value="RunAndDesignTime"/> 51 <RequiredPkgs Count=" 2">51 <RequiredPkgs Count="3"> 52 52 <Item1> 53 <PackageName Value=" TemplateGenerics"/>53 <PackageName Value="MicroThreading"/> 54 54 </Item1> 55 55 <Item2> 56 <PackageName Value="TemplateGenerics"/> 57 </Item2> 58 <Item3> 56 59 <PackageName Value="FCL"/> 57 60 <MinVersion Major="1" Valid="True"/> 58 </Item 2>61 </Item3> 59 62 </RequiredPkgs> 60 63 <UsageOptions> -
CoolStreaming/UStreamHelper.pas
r117 r172 6 6 7 7 uses 8 Classes, DateUtils, syncobjs ;8 Classes, DateUtils, syncobjs, UMicroThreading; 9 9 10 10 type … … 63 63 64 64 TThreadStreamHelper = class(TStreamHelper) 65 Lock: T CriticalSection;65 Lock: TMicroThreadCriticalSection; 66 66 procedure Clear; 67 67 constructor Create; … … 363 363 begin 364 364 inherited Create; 365 Lock := T CriticalSection.Create;365 Lock := TMicroThreadCriticalSection.Create; 366 366 end; 367 367 -
CoolStreaming/UVarBlockSerializer.pas
r125 r172 1 // 201 0-03-301 // 2011-02-22 2 2 3 3 unit UVarBlockSerializer; … … 5 5 {$mode Delphi}{$H+} 6 6 7 // One recursive VarInt size levelsupported7 // One level of recursive VarInt size supported 8 8 // Biggest UInt type is QWord (64-bit) 9 9 … … 12 12 uses 13 13 Classes, DateUtils, UStreamHelper, Math, SysUtils, USubStream, 14 Contnrs, SpecializedList ;14 Contnrs, SpecializedList, LCLProc; 15 15 16 16 const 17 17 BitAlignment = 8; 18 RealBase = 2;19 18 20 19 type … … 44 43 procedure WriteVarSInt(Value: Int64); 45 44 function ReadVarSInt: Int64; 46 procedure WriteVarFloat(Value: Double );47 function ReadVarFloat : Double;45 procedure WriteVarFloat(Value: Double; Base: Integer = 2); 46 function ReadVarFloat(Base: Integer = 2): Double; 48 47 procedure WriteVarString(Value: string); 49 48 function ReadVarString: string; … … 87 86 procedure WriteVarString(Index: Integer; Value: string); 88 87 function ReadVarString(Index: Integer): string; 89 procedure WriteVarIntegerArray(Index: Integer; List: TListInteger); 90 procedure ReadVarIntegerArray(Index: Integer; List: TListInteger); 88 procedure WriteVarUIntArray(Index: Integer; List: TListInteger); 89 procedure ReadVarUIntArray(Index: Integer; List: TListInteger); 90 procedure WriteVarStringArray(Index: Integer; List: TListString); 91 procedure ReadVarStringArray(Index: Integer; List: TListString); 91 92 92 93 procedure Clear; … … 198 199 Data := Stream.ReadByte; 199 200 if I = 0 then begin 200 Length := DecodeUnaryLength(Data); 201 if Length > (BitAlignment) then 202 raise Exception.Create(SUInt64Overflow); 203 LengthMask := GetUnaryLengthMask(Length); 204 Data := Data and (LengthMask xor $ff); 201 if Data = $ff then begin 202 // Read recursive length 203 Length := ReadVarUInt; 204 if Length > BitAlignment then 205 raise Exception.Create(SUInt64Overflow); 206 if Length > 0 then Data := Stream.ReadByte else 207 Data := 0; 208 end else begin 209 Length := DecodeUnaryLength(Data); 210 LengthMask := GetUnaryLengthMask(Length); 211 Data := Data and (LengthMask xor $ff); 212 end; 205 213 end; 206 214 Result := Result or (QWord(Data) shl ((Length - I - 1) * BitAlignment)); … … 219 227 end; 220 228 221 procedure TVarBlockSerializer.WriteVarFloat(Value: Double );229 procedure TVarBlockSerializer.WriteVarFloat(Value: Double; Base: Integer = 2); 222 230 var 223 231 Exponent: Integer; … … 229 237 // Normalize to integer number with base 10 exponent 230 238 Exponent := 0; 231 while Frac(Value) > 0 do begin 232 Value := Value * RealBase; 233 Dec(Exponent); 234 end; 235 while Frac(Value / RealBase) = 0 do begin 236 Value := Value / RealBase; 237 Inc(Exponent); 239 if Value <> 0 then begin 240 if Frac(Value) > 0 then begin 241 while Frac(Value) > 0 do begin 242 Value := Value * Base; 243 Dec(Exponent); 244 end; 245 end else 246 while Frac(Value / Base) = 0 do begin 247 Value := Value / Base; 248 Inc(Exponent); 249 end; 238 250 end; 239 251 Block.WriteVarSInt(Trunc(Value)); … … 245 257 end; 246 258 247 function TVarBlockSerializer.ReadVarFloat : Double;259 function TVarBlockSerializer.ReadVarFloat(Base: Integer = 2): Double; 248 260 var 249 261 Significant: Int64; … … 256 268 Significant := Block.ReadVarSInt; 257 269 Exponent := Block.ReadVarSInt; 258 Result := Significant * IntPower( RealBase, Exponent);270 Result := Significant * IntPower(Base, Exponent); 259 271 finally 260 272 Block.Free; … … 266 278 Stream: TVarBlockSerializer; 267 279 I: Integer; 280 P: PChar; 281 Unicode: Cardinal; 282 CharLen: Integer; 268 283 begin 269 284 try 270 285 Stream := TVarBlockSerializer.Create; 271 for I := 1 to Length(Value) do 272 Stream.WriteVarUInt(Integer(Value[I])); 286 P := PChar(Value); 287 for I := 0 to UTF8Length(Value) - 1 do begin 288 Unicode := UTF8CharacterToUnicode(P, CharLen); 289 Stream.WriteVarUInt(Unicode); 290 Inc(P, CharLen); 291 end; 273 292 WriteVarBlock(Stream); 274 293 finally … … 288 307 while Block.Stream.Position < Block.Stream.Size do begin 289 308 Character := Block.ReadVarUInt; 290 Result := Result + Char(Character);309 Result := Result + UnicodeToUTF8(Character); 291 310 end; 292 311 finally … … 352 371 I := 0; 353 372 Length := 1; 354 while I < Length do begin 373 355 374 Data := Stream.ReadByte; 356 if I = 0 then begin357 375 if Data = $ff then begin 358 376 // Read recursive length 359 377 Length := ReadVarUInt; 360 378 AStream.Size := Length; 361 Data := Stream.ReadByte; 362 AStream.WriteByte(Data); 379 if Length > 0 then begin 380 Data := Stream.ReadByte; 381 AStream.WriteByte(Data); 382 end; 363 383 end else begin 364 384 // Read unary length … … 367 387 LengthMask := GetUnaryLengthMask(Length); 368 388 Data := Data and (LengthMask xor $ff); 369 // Drop first byte if first data zero389 // Drop first byte if first data is zero 370 390 if Data <> 0 then AStream.WriteByte(Data) 371 391 else begin … … 378 398 end; 379 399 end; 380 end else AStream.WriteByte(Data); 381 Inc(I); 382 end; 400 401 // If CopyFrom parameter count is zero then whole source is copied 402 if Length > 1 then 403 AStream.CopyFrom(Stream, Length - 1); 383 404 AStream.Position := 0; 384 405 end; … … 572 593 procedure TVarBlockIndexed.ReadVarBlock(Index: Integer; Block: TVarBlockSerializer); 573 594 begin 574 TVarBlockSerializer(Items[Index]).Stream.Position := 0; 575 TVarBlockSerializer(Items[Index]).ReadVarBlock(Block); 595 with TVarBlockSerializer(Items[Index]) do begin 596 Stream.Position := 0; 597 ReadVarBlock(Block); 598 end; 576 599 end; 577 600 … … 650 673 function TVarBlockIndexed.ReadVarString(Index: Integer):string; 651 674 begin 652 TVarBlockSerializer(Items[Index]).Stream.Position := 0; 653 Result := TVarBlockSerializer(Items[Index]).ReadVarString; 654 end; 655 656 procedure TVarBlockIndexed.WriteVarIntegerArray(Index: Integer; 675 with TVarBlockSerializer(Items[Index]) do begin 676 Stream.Position := 0; 677 Result := ReadVarString; 678 end; 679 end; 680 681 procedure TVarBlockIndexed.WriteVarUIntArray(Index: Integer; 657 682 List: TListInteger); 658 683 var … … 670 695 end; 671 696 672 procedure TVarBlockIndexed.ReadVar IntegerArray(Index: Integer;697 procedure TVarBlockIndexed.ReadVarUIntArray(Index: Integer; 673 698 List: TListInteger); 674 699 var … … 681 706 while Temp.Stream.Position < Temp.Stream.Size do begin 682 707 List.Add(Temp.ReadVarUInt); 708 end; 709 finally 710 Temp.Free; 711 end; 712 end; 713 714 procedure TVarBlockIndexed.WriteVarStringArray(Index: Integer; 715 List: TListString); 716 var 717 I: Integer; 718 Temp: TVarBlockSerializer; 719 begin 720 try 721 Temp := TVarBlockSerializer.Create; 722 for I := 0 to List.Count - 1 do 723 Temp.WriteVarString(List[I]); 724 WriteVarBlock(Index, Temp); 725 finally 726 Temp.Free; 727 end; 728 end; 729 730 procedure TVarBlockIndexed.ReadVarStringArray(Index: Integer; List: TListString 731 ); 732 var 733 Temp: TVarBlockSerializer; 734 begin 735 try 736 Temp := TVarBlockSerializer.Create; 737 List.Clear; 738 ReadVarBlock(Index, Temp); 739 while Temp.Stream.Position < Temp.Stream.Size do begin 740 List.Add(Temp.ReadVarString); 683 741 end; 684 742 finally
Note:
See TracChangeset
for help on using the changeset viewer.