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 procedure Seek(Pos
: Integer);
43 procedure Skip(Size
: Integer);
44 function BytesLeft(): Integer;
45 function ReadData(V
: Pointer; N
: Integer): Integer;
46 function ReadChar(): Char;
47 function ReadByte(): Byte;
48 function ReadWord(): Word;
49 function ReadLongWord(): LongWord;
50 function ReadShortInt(): ShortInt;
51 function ReadSmallInt(): SmallInt;
52 function ReadLongInt(): LongInt;
53 function ReadInt64(): Int64;
54 function ReadString(): String;
55 function ReadMD5(): TMD5Digest
;
57 procedure WriteData(V
: Pointer; N
: Integer);
58 procedure Write(V
: Byte); overload
;
59 procedure Write(V
: Word); overload
;
60 procedure Write(V
: LongWord); overload
;
61 procedure Write(V
: ShortInt); overload
;
62 procedure Write(V
: SmallInt); overload
;
63 procedure Write(V
: LongInt); overload
;
64 procedure Write(V
: Int64); overload
;
65 procedure Write(V
: String); overload
;
66 procedure Write(V
: TMD5Digest
); overload
;
67 procedure Write(V
: TMsg
);
77 function TMsg
.Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
80 if Full
then CurSize
:= N
else CurSize
:= 0;
86 Result
:= (N
> 0) and (V
<> nil);
89 procedure TMsg
.Alloc(N
: Integer);
95 raise Exception
.Create('TMsg.Alloc: no mem');
100 procedure TMsg
.Free();
102 if not OwnMemory
then
103 raise Exception
.Create('TMsg.Free: called on borrowed memory');
111 procedure TMsg
.Clear();
119 function TMsg
.Allocated(): Boolean;
124 procedure TMsg
.CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
126 if N
< From
.CurSize
then
127 raise Exception
.Create('TMsg.Copy: can''t copy into a smaller TMsg');
128 Move(From
, Self
, SizeOf(TMsg
));
130 Move(From
.Data
^, Data
^, From
.CurSize
);
133 function TMsg
.AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
135 if OwnMemory
then Self
.Free();
139 if Full
then CurSize
:= N
;
140 Result
:= (N
> 0) and (P
<> nil);
143 procedure TMsg
.WriteData(V
: Pointer; N
: Integer);
145 if CurSize
+ N
> MaxSize
then
148 raise Exception
.Create('TMsg.WriteData: buffer overrun!');
151 Move(V
^, (Data
+ CurSize
)^, N
);
152 CurSize
:= CurSize
+ N
;
155 procedure TMsg
.Write(V
: TMsg
);
157 if CurSize
+ V
.CurSize
> MaxSize
then
160 raise Exception
.Create('TMsg.WriteData: buffer overrun!');
163 Move(V
.Data
^, (Data
+ CurSize
)^, V
.CurSize
);
164 CurSize
:= CurSize
+ V
.CurSize
;
167 procedure TMsg
.Write(V
: Byte); overload
;
172 procedure TMsg
.Write(V
: Word); overload
;
177 procedure TMsg
.Write(V
: LongWord); overload
;
182 procedure TMsg
.Write(V
: ShortInt); overload
;
187 procedure TMsg
.Write(V
: SmallInt); overload
;
192 procedure TMsg
.Write(V
: LongInt); overload
;
197 procedure TMsg
.Write(V
: Int64); overload
;
202 procedure TMsg
.Write(V
: AnsiString); overload
;
206 // TODO: Write(Word(Length(V)));
207 Write(Byte(Length(V
)));
208 for I
:= 1 to High(V
) do
212 procedure TMsg
.Write(V
: TMD5Digest
); overload
;
220 procedure TMsg
.BeginReading();
226 procedure TMsg
.Seek(Pos
: Integer);
228 if Pos
> CurSize
then
229 raise Exception
.Create('TMsg.Seek: buffer overrun!');
233 procedure TMsg
.Skip(Size
: Integer);
235 if ReadCount
+ Size
> CurSize
then
236 raise Exception
.Create('TMsg.Skip: buffer overrun!');
237 ReadCount
:= ReadCount
+ Size
;
240 function TMsg
.BytesLeft(): Integer;
242 Result
:= CurSize
- ReadCount
;
245 function TMsg
.ReadData(V
: Pointer; N
: Integer): Integer;
248 if ReadCount
+ N
> CurSize
then
250 // TODO: maybe partial reads?
251 ReadCount
:= CurSize
+ 1;
252 raise Exception
.Create('TMsg.ReadData: buffer overrun!');
255 Move((Data
+ ReadCount
)^, V
^, N
);
256 ReadCount
:= ReadCount
+ N
;
260 function TMsg
.ReadChar(): Char;
263 ReadData(@Result
, 1);
266 function TMsg
.ReadByte(): Byte;
269 ReadData(@Result
, 1);
272 function TMsg
.ReadWord(): Word;
275 ReadData(@Result
, 2);
278 function TMsg
.ReadLongWord(): LongWord;
281 ReadData(@Result
, 4);
284 function TMsg
.ReadShortInt(): ShortInt;
287 ReadData(@Result
, 1);
290 function TMsg
.ReadSmallInt(): SmallInt;
293 ReadData(@Result
, 2);
296 function TMsg
.ReadLongInt(): LongInt;
299 ReadData(@Result
, 4);
302 function TMsg
.ReadInt64(): Int64;
305 ReadData(@Result
, 8);
308 function TMsg
.ReadString(): string;
314 // TODO: L := ReadWord();
316 if (L
> 0) and (L
<> Byte(-1)) then
318 SetLength(Result
, L
);
320 Result
[I
] := ReadChar();
324 function TMsg
.ReadMD5(): TMD5Digest
;
329 Result
[I
] := ReadByte();