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}
34 function Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
35 procedure Alloc(N
: Integer);
38 procedure CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
39 function Allocated(): Boolean;
40 function AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
42 procedure BeginReading();
43 procedure Seek(Pos
: Integer);
44 procedure Skip(Size
: Integer);
45 function BytesLeft(): Integer;
46 function ReadData(V
: Pointer; N
: Integer): Integer;
47 function ReadChar(): Char;
48 function ReadByte(): Byte;
49 function ReadWord(): Word;
50 function ReadLongWord(): LongWord;
51 function ReadShortInt(): ShortInt;
52 function ReadSmallInt(): SmallInt;
53 function ReadLongInt(): LongInt;
54 function ReadInt64(): Int64;
55 function ReadString(): String;
56 function ReadMD5(): TMD5Digest
;
58 procedure WriteData(V
: Pointer; N
: Integer);
59 procedure Write(V
: Byte); overload
;
60 procedure Write(V
: Word); overload
;
61 procedure Write(V
: LongWord); overload
;
62 procedure Write(V
: ShortInt); overload
;
63 procedure Write(V
: SmallInt); overload
;
64 procedure Write(V
: LongInt); overload
;
65 procedure Write(V
: Int64); overload
;
66 procedure Write(V
: String); overload
;
67 procedure Write(V
: TMD5Digest
); overload
;
68 procedure Write(V
: TMsg
);
78 function TMsg
.Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
81 if Full
then CurSize
:= N
else CurSize
:= 0;
87 Result
:= (N
> 0) and (V
<> nil);
90 procedure TMsg
.Alloc(N
: Integer);
96 raise Exception
.Create('TMsg.Alloc: no mem');
102 procedure TMsg
.Free();
104 if not OwnMemory
then
105 raise Exception
.Create('TMsg.Free: called on borrowed memory');
113 procedure TMsg
.Clear();
121 function TMsg
.Allocated(): Boolean;
126 procedure TMsg
.CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
128 if N
< From
.CurSize
then
129 raise Exception
.Create('TMsg.Copy: can''t copy into a smaller TMsg');
130 Move(From
, Self
, SizeOf(TMsg
));
132 Move(From
.Data
^, Data
^, From
.CurSize
);
135 function TMsg
.AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
137 if OwnMemory
then Self
.Free();
141 if Full
then CurSize
:= N
;
142 Result
:= (N
> 0) and (P
<> nil);
145 procedure TMsg
.WriteData(V
: Pointer; N
: Integer);
149 if CurSize
+ N
> MaxSize
then
153 NewSize
:= MaxSize
+ ((N
+ AllocStep
- 1) div AllocStep
) * AllocStep
; // round up
154 if ReAllocMem(Data
, NewSize
) = nil then
155 raise Exception
.Create('TMsg.WriteData: out of memory on realloc');
161 raise Exception
.Create('TMsg.WriteData: buffer overrun on borrowed memory!');
165 Move(V
^, (Data
+ CurSize
)^, N
);
166 CurSize
:= CurSize
+ N
;
169 procedure TMsg
.Write(V
: TMsg
);
171 WriteData(V
.Data
, V
.CurSize
);
174 procedure TMsg
.Write(V
: Byte); overload
;
179 procedure TMsg
.Write(V
: Word); overload
;
184 procedure TMsg
.Write(V
: LongWord); overload
;
189 procedure TMsg
.Write(V
: ShortInt); overload
;
194 procedure TMsg
.Write(V
: SmallInt); overload
;
199 procedure TMsg
.Write(V
: LongInt); overload
;
204 procedure TMsg
.Write(V
: Int64); overload
;
209 procedure TMsg
.Write(V
: AnsiString); overload
;
213 // TODO: Write(Word(Length(V)));
214 Write(Byte(Length(V
)));
215 for I
:= 1 to High(V
) do
219 procedure TMsg
.Write(V
: TMD5Digest
); overload
;
227 procedure TMsg
.BeginReading();
233 procedure TMsg
.Seek(Pos
: Integer);
235 if Pos
> CurSize
then
236 raise Exception
.Create('TMsg.Seek: buffer overrun!');
240 procedure TMsg
.Skip(Size
: Integer);
242 if ReadCount
+ Size
> CurSize
then
243 raise Exception
.Create('TMsg.Skip: buffer overrun!');
244 ReadCount
:= ReadCount
+ Size
;
247 function TMsg
.BytesLeft(): Integer;
249 Result
:= CurSize
- ReadCount
;
252 function TMsg
.ReadData(V
: Pointer; N
: Integer): Integer;
255 if ReadCount
+ N
> CurSize
then
257 // TODO: maybe partial reads?
258 ReadCount
:= CurSize
+ 1;
259 raise Exception
.Create('TMsg.ReadData: buffer overrun!');
262 Move((Data
+ ReadCount
)^, V
^, N
);
263 ReadCount
:= ReadCount
+ N
;
267 function TMsg
.ReadChar(): Char;
270 ReadData(@Result
, 1);
273 function TMsg
.ReadByte(): Byte;
276 ReadData(@Result
, 1);
279 function TMsg
.ReadWord(): Word;
282 ReadData(@Result
, 2);
285 function TMsg
.ReadLongWord(): LongWord;
288 ReadData(@Result
, 4);
291 function TMsg
.ReadShortInt(): ShortInt;
294 ReadData(@Result
, 1);
297 function TMsg
.ReadSmallInt(): SmallInt;
300 ReadData(@Result
, 2);
303 function TMsg
.ReadLongInt(): LongInt;
306 ReadData(@Result
, 4);
309 function TMsg
.ReadInt64(): Int64;
312 ReadData(@Result
, 8);
315 function TMsg
.ReadString(): string;
321 // TODO: L := ReadWord();
323 if (L
> 0) and (L
<> Byte(-1)) then
325 SetLength(Result
, L
);
327 Result
[I
] := ReadChar();
331 function TMsg
.ReadMD5(): TMD5Digest
;
336 Result
[I
] := ReadByte();