Statistics
| Branch: | Tag: | Revision:

root / UPacket.pas

History | View | Annotate | Download (1.9 kB)

1
(*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License, Version 1.0 only
6
 * (the "License").  You may not use this file except in compliance
7
 * with the License.
8
 *
9
 * You can obtain a copy of the license at
10
 * http://www.opensource.org/licenses/cddl1.php.
11
 * See the License for the specific language governing permissions
12
 * and limitations under the License.
13
 *
14
 * When distributing Covered Code, include this CDDL HEADER in each
15
 * file and include the License file at
16
 * http://www.opensource.org/licenses/cddl1.php.  If applicable,
17
 * add the following below this CDDL HEADER, with the fields enclosed
18
 * by brackets "[]" replaced with your own identifying * information:
19
 *      Portions Copyright [yyyy] [name of copyright owner]
20
 *
21
 * CDDL HEADER END
22
 *
23
 *
24
 *      Portions Copyright 2007 Andreas Schneider
25
 *)
26
unit UPacket;
27
28
interface
29
30
uses
31
  Classes, UEnhancedMemoryStream;
32
33
type
34
  TPacket = class(TObject)
35
    constructor Create(APacketID: Byte; ALength: Cardinal);
36
    destructor Destroy; override;
37
  protected
38
    FStream: TEnhancedMemoryStream;
39
    FPacketID: Byte;
40
    FLength: Cardinal;
41
    function GetStream: TEnhancedMemoryStream;
42
  published
43
    property Stream: TEnhancedMemoryStream read GetStream;
44
    property PacketID: Byte read FPacketID;
45
    property PacketLength: Cardinal read FLength;
46
  end;
47
48
implementation
49
50
constructor TPacket.Create(APacketID: Byte; ALength: Cardinal);
51
begin
52
  FStream := TEnhancedMemoryStream.Create;
53
  FPacketID := APacketID;
54
  FLength := ALength;
55
  FStream.WriteByte(FPacketID);
56
  if FLength = 0 then
57
    FStream.WriteCardinal(0);
58
end;
59
60
destructor TPacket.Destroy;
61
begin
62
  FStream.Free;
63
  inherited;
64
end;
65
66
function TPacket.GetStream: TEnhancedMemoryStream;
67
begin
68
  if FLength = 0 then
69
  begin
70
    FStream.Position := 1;
71
    FStream.WriteCardinal(FStream.Size);
72
  end;
73
  FStream.Position := 0;
74
  Result := FStream;
75
end;
76
77
end.
78