1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE ../shared/a_modes.inc}
33 function Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
34 procedure Alloc(N
: Integer);
37 procedure CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
38 function Allocated(): Boolean;
39 function AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
41 procedure BeginReading();
42 function ReadData(V
: Pointer; N
: Integer): Integer;
43 function ReadChar(): Char;
44 function ReadByte(): Byte;
45 function ReadWord(): Word;
46 function ReadLongWord(): LongWord;
47 function ReadShortInt(): ShortInt;
48 function ReadSmallInt(): SmallInt;
49 function ReadLongInt(): LongInt;
50 function ReadInt64(): Int64;
51 function ReadString(): String;
52 function ReadMD5(): TMD5Digest
;
54 procedure WriteData(V
: Pointer; N
: Integer);
55 procedure Write(V
: Byte); overload
;
56 procedure Write(V
: Word); overload
;
57 procedure Write(V
: LongWord); overload
;
58 procedure Write(V
: ShortInt); overload
;
59 procedure Write(V
: SmallInt); overload
;
60 procedure Write(V
: LongInt); overload
;
61 procedure Write(V
: Int64); overload
;
62 procedure Write(V
: String); overload
;
63 procedure Write(V
: TMD5Digest
); overload
;
73 function TMsg
.Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
76 if Full
then CurSize
:= N
else CurSize
:= 0;
82 Result
:= (N
> 0) and (V
<> nil);
85 procedure TMsg
.Alloc(N
: Integer);
91 raise Exception
.Create('TMsg.Alloc: no mem');
96 procedure TMsg
.Free();
99 raise Exception
.Create('TMsg.Free: called on borrowed memory');
106 procedure TMsg
.Clear();
114 function TMsg
.Allocated(): Boolean;
119 procedure TMsg
.CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
121 if N
< From
.CurSize
then
122 raise Exception
.Create('TMsg.Copy: can''t copy into a smaller TMsg');
123 Move(From
, Self
, SizeOf(TMsg
));
125 Move(From
.Data
^, Data
^, From
.CurSize
);
128 function TMsg
.AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
130 if OwnMemory
then Self
.Free();
134 if Full
then CurSize
:= N
;
135 Result
:= (N
> 0) and (P
<> nil);
138 procedure TMsg
.WriteData(V
: Pointer; N
: Integer);
140 if CurSize
+ N
> MaxSize
then
143 raise Exception
.Create('TMsg.WriteData: buffer overrun!');
146 Move(V
^, (Data
+ CurSize
)^, N
);
147 CurSize
:= CurSize
+ N
;
150 procedure TMsg
.Write(V
: Byte); overload
;
155 procedure TMsg
.Write(V
: Word); overload
;
160 procedure TMsg
.Write(V
: LongWord); overload
;
165 procedure TMsg
.Write(V
: ShortInt); overload
;
170 procedure TMsg
.Write(V
: SmallInt); overload
;
175 procedure TMsg
.Write(V
: LongInt); overload
;
180 procedure TMsg
.Write(V
: Int64); overload
;
185 procedure TMsg
.Write(V
: AnsiString); overload
;
189 // TODO: Write(Word(Length(V)));
190 Write(Byte(Length(V
)));
191 for I
:= 1 to High(V
) do
195 procedure TMsg
.Write(V
: TMD5Digest
); overload
;
203 procedure TMsg
.BeginReading();
209 function TMsg
.ReadData(V
: Pointer; N
: Integer): Integer;
212 if ReadCount
+ N
> CurSize
then
214 // TODO: maybe partial reads?
215 ReadCount
:= CurSize
+ 1;
216 raise Exception
.Create('TMsg.ReadData: buffer overrun!');
219 Move((Data
+ ReadCount
)^, V
^, N
);
220 ReadCount
:= ReadCount
+ N
;
224 function TMsg
.ReadChar(): Char;
227 ReadData(@Result
, 1);
230 function TMsg
.ReadByte(): Byte;
233 ReadData(@Result
, 1);
236 function TMsg
.ReadWord(): Word;
239 ReadData(@Result
, 2);
242 function TMsg
.ReadLongWord(): LongWord;
245 ReadData(@Result
, 4);
248 function TMsg
.ReadShortInt(): ShortInt;
251 ReadData(@Result
, 2);
254 function TMsg
.ReadSmallInt(): SmallInt;
257 ReadData(@Result
, 1);
260 function TMsg
.ReadLongInt(): LongInt;
263 ReadData(@Result
, 4);
266 function TMsg
.ReadInt64(): Int64;
269 ReadData(@Result
, 8);
272 function TMsg
.ReadString(): string;
278 // TODO: L := ReadWord();
280 if (L
> 0) and (L
<> Byte(-1)) then
282 SetLength(Result
, L
);
284 Result
[I
] := ReadChar();
288 function TMsg
.ReadMD5(): TMD5Digest
;
293 Result
[I
] := ReadByte();