-
Notifications
You must be signed in to change notification settings - Fork 3
/
i_net.pas
253 lines (219 loc) · 6.59 KB
/
i_net.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//------------------------------------------------------------------------------
//
// DoomXS - A basic Windows source port of Doom
// based on original Linux Doom as published by "id Software"
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2021-2022 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// Site: https://sourceforge.net/projects/doomxs/
//------------------------------------------------------------------------------
unit i_net;
interface
procedure I_InitNetwork;
procedure I_NetCmd;
implementation
uses
WinSock,
d_delphi,
d_net,
i_system,
g_game,
m_argv;
var
netget: PProcedure;
netsend: PProcedure;
const
IPPORT_USERRESERVED = 5000;
var
DOOMPORT: integer = (IPPORT_USERRESERVED + $1d);
sendsocket: TSocket;
insocket: TSocket;
sendaddress: array[0..MAXNETNODES - 1] of TSockAddrIn;
// UDPsocket
function UDPsocket: TSocket;
begin
// allocate a socket
Result := socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if Result = INVALID_SOCKET then
I_Error('UDPsocket(): Can''t create socket: Result = %d'#13#10, [Result]);
end;
// BindToLocalPort
procedure BindToLocalPort(s: integer; port: integer);
var
v: integer;
address: TSockAddrIn;
begin
ZeroMemory(@address, SizeOf(address));
address.sin_family := AF_INET;
address.sin_addr.s_addr := INADDR_ANY;
address.sin_port := port;
v := bind(s, address, SizeOf(address));
if v = -1 then
I_Error('BindToLocalPort(): Failed.');
end;
// PacketSend
procedure PacketSend;
var
c: integer;
begin
c := sendto(sendsocket, netbuffer^, doomcom.datalength, 0,
sendaddress[doomcom.remotenode], SizeOf(sendaddress[doomcom.remotenode]));
if c = SOCKET_ERROR then
I_Error('PacketSend(): sendto() failed.');
end;
// PacketGet
procedure PacketGet;
var
i: integer;
c: integer;
fromaddress: TSockAddrIn;
fromlen: integer;
begin
fromlen := SizeOf(fromaddress);
c := recvfrom(insocket, netbuffer^, SizeOf(doomdata_t), 0, fromaddress, fromlen);
if c = SOCKET_ERROR then
begin
c := WSAGetLastError;
if c <> WSAEWOULDBLOCK then
I_Error('PacketGet(): Network error.');
doomcom.remotenode := -1; // no packet
Exit;
end;
// find remote node number
i := 0;
while i < doomcom.numnodes do
begin
if fromaddress.sin_addr.s_addr = sendaddress[i].sin_addr.s_addr then
Break;
inc(i);
end;
if i = doomcom.numnodes then
begin
// packet is not from one of the players (new game broadcast)
doomcom.remotenode := -1; // no packet
Exit;
end;
doomcom.remotenode := i; // good packet from a game player
doomcom.datalength := c;
end;
// I_InitNetwork
procedure I_InitNetwork;
var
trueval: integer;
i, err: integer;
p: integer;
hostentry: PHostEnt; // host information entry
wsadata: TWSAData;
begin
trueval := 1;
ZeroMemory(@doomcom, SizeOf(doomcom_t));
// set up for network
i := M_CheckParm('-dup');
if (i > 0) and (i < myargc - 1) then
begin
doomcom.ticdup := Ord(myargv[i + 1][1]) - Ord('0');
if doomcom.ticdup < 1 then
doomcom.ticdup := 1
else if doomcom.ticdup > 9 then
doomcom.ticdup := 9;
end
else
doomcom.ticdup := 1;
if M_CheckParm('-extratic') > 0 then
doomcom.extratics := 1
else
doomcom.extratics := 0;
p := M_CheckParm('-port');
if (p > 0) and (p < myargc - 1) then
begin
DOOMPORT := atoi(myargv[p + 1]);
printf('Using Port %d'#13#10, [DOOMPORT]);
end;
// parse network game options,
// -net <consoleplayer> <host> <host> ...
i := M_CheckParm('-net');
if i = 0 then
begin
// single player game
netgame := False;
doomcom.id := DOOMCOM_ID;
doomcom.numplayers := 1;
doomcom.numnodes := 1;
doomcom.deathmatch := 0;
doomcom.consoleplayer := 0;
Exit;
end;
err := WSAStartup($0101, wsadata);
if err <> 0 then
I_Error('I_InitNetwork(): Could not initialize Windows Sockets, WSAStartup Result = %d', [err]);
netsend := PacketSend;
netget := PacketGet;
netgame := True;
// parse player number and host list
doomcom.consoleplayer := Ord(myargv[i + 1][1]) - Ord('1');
printf('console player number: %d'#13#10, [doomcom.consoleplayer]);
doomcom.numnodes := 1; // this node for sure
inc(i);
inc(i);
while (i < myargc) and (myargv[i][1] <> '-') do
begin
sendaddress[doomcom.numnodes].sin_family := AF_INET;
if myargv[i][1] = '.' then
begin
printf('Node number %d address %s'#13#10, [doomcom.numnodes, Copy(myargv[i], 2, Length(myargv[i]) - 1)]);
sendaddress[doomcom.numnodes].sin_addr.s_addr := inet_addr(PChar(Copy(myargv[i], 2, Length(myargv[i]) - 1)));
end
else
begin
printf('Node number %d hostname %s'#13#10, [doomcom.numnodes, myargv[i]]);
hostentry := gethostbyname(PChar(myargv[i]));
if hostentry = nil then
I_Error('gethostbyname(): couldn''t find %s', [myargv[i]]);
sendaddress[doomcom.numnodes].sin_addr.s_addr := PInteger(hostentry.h_addr_list^)^;
end;
inc(i);
if (i < myargc) and (myargv[i][1] = ':') then
begin
printf('Node number %d port %s'#13#10, [doomcom.numnodes, Copy(myargv[i], 2, Length(myargv[i]) - 1)]);
sendaddress[doomcom.numnodes].sin_port := htons(atoi(Copy(myargv[i], 2, Length(myargv[i]) - 1)));
inc(i);
end
else
sendaddress[doomcom.numnodes].sin_port := htons(DOOMPORT);
inc(doomcom.numnodes);
end;
printf('Total number of players : %d'#13#10, [doomcom.numnodes]);
doomcom.id := DOOMCOM_ID;
doomcom.numplayers := doomcom.numnodes;
// build message to receive
insocket := UDPsocket;
BindToLocalPort(insocket,htons(DOOMPORT));
ioctlsocket(insocket, FIONBIO, trueval);
sendsocket := UDPsocket;
end;
procedure I_NetCmd;
begin
if doomcom.command = CMD_SEND then
netsend
else if doomcom.command = CMD_GET then
netget
else
I_Error('I_NetCmd(): Bad net cmd: %d', [doomcom.command]);
end;
end.