DEADSOFTWARE

Net: Don't process network in ProcessLoading() for client's code
[d2df-sdl.git] / src / game / g_nethandler.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_nethandler;
18 interface
20 uses e_msg, g_net, g_netmsg, ENet;
22 type
23 TNetClientMsgHandler = function (M: TMsg): Boolean;
24 TNetHostMsgHandler = function (S: pTNetClient; M: TMsg): Boolean;
26 procedure g_Net_Client_HandlePacket(P: pENetPacket; Handler: TNetClientMsgHandler);
27 procedure g_Net_Host_HandlePacket(S: pTNetClient; P: pENetPacket; Handler: TNetHostMsgHandler);
29 function g_Net_ClientMsgHandler(NetMsg: TMsg): Boolean;
30 function g_Net_ClientLightMsgHandler(NetMsg: TMsg): Boolean;
31 function g_Net_HostMsgHandler(S: pTNetClient; NetMsg: TMsg): Boolean;
33 implementation
35 uses sysutils, g_console;
37 // TODO: Unify this with g_Net_Host_HandlePacket() somehow? They're almost the same.
38 procedure g_Net_Client_HandlePacket(P: pENetPacket; Handler: TNetClientMsgHandler);
39 var
40 MNext: Integer;
41 MSize: LongWord;
42 MHandled: Boolean = false;
43 NetMsg: TMsg;
44 Err: Boolean;
45 begin
46 if not NetMsg.Init(P^.data, P^.dataLength, True)
47 then exit;
49 Err := False;
50 MNext := 0;
52 while (NetMsg.BytesLeft() > 0) and (not Err) do
53 begin
54 try
55 MSize := NetMsg.ReadLongWord();
56 MNext := NetMsg.ReadCount + MSize;
57 MHandled := Handler(NetMsg); // TODO: maybe do something with this bool
58 NetMsg.Seek(MNext);
59 except
60 Err := True;
61 end;
62 end;
64 enet_packet_destroy(P);
65 //if Err
66 // then MC_MalformedPacket(S);
67 end;
69 procedure g_Net_Host_HandlePacket(S: pTNetClient; P: pENetPacket; Handler: TNetHostMsgHandler);
70 var
71 MNext: Integer;
72 MSize: LongWord;
73 MHandled: Boolean = false;
74 NetMsg: TMsg;
75 Err: Boolean;
76 begin
77 if not NetMsg.Init(P^.data, P^.dataLength, True)
78 then exit;
80 Err := False;
81 MNext := 0;
83 while (NetMsg.BytesLeft() > 0) and (not Err) do
84 begin
85 try
86 MSize := NetMsg.ReadLongWord();
87 MNext := NetMsg.ReadCount + MSize;
88 MHandled := Handler(S, NetMsg); // TODO: maybe do something with this bool
89 NetMsg.Seek(MNext);
90 except
91 Err := True;
92 end;
93 end;
95 enet_packet_destroy(P);
96 if Err
97 then MH_MalformedPacket(S);
98 end;
101 function g_Net_ClientMsgHandler(NetMsg: TMsg): Boolean;
102 var
103 MID: Byte;
104 Err: Boolean;
105 begin
106 Result := True;
107 Err := False;
108 try
109 MID := NetMsg.ReadByte();
110 except
111 MID := 0;
112 Err := True;
113 end;
115 //if Err then begin MC_MalformedPacket(S); Exit; end;
117 case MID of
118 NET_MSG_CHAT: MC_RECV_Chat(NetMsg);
119 NET_MSG_GFX: MC_RECV_Effect(NetMsg);
120 NET_MSG_SND: MC_RECV_Sound(NetMsg);
121 NET_MSG_SCORE: MC_RECV_GameStats(NetMsg);
122 NET_MSG_COOP: MC_RECV_CoopStats(NetMsg);
123 NET_MSG_GEVENT: MC_RECV_GameEvent(NetMsg);
124 NET_MSG_FLAG: MC_RECV_FlagEvent(NetMsg);
125 NET_MSG_GSET: MC_RECV_GameSettings(NetMsg);
126 NET_MSG_FLAGPOS:MC_RECV_FlagPos(NetMsg);
128 NET_MSG_PLR: MC_RECV_PlayerCreate(NetMsg);
129 NET_MSG_PLRPOS: MC_RECV_PlayerPos(NetMsg);
130 NET_MSG_PLRSTA: MC_RECV_PlayerStats(NetMsg);
131 NET_MSG_PLRDEL: MC_RECV_PlayerDelete(NetMsg);
132 NET_MSG_PLRDMG: MC_RECV_PlayerDamage(NetMsg);
133 NET_MSG_PLRDIE: MC_RECV_PlayerDeath(NetMsg);
134 NET_MSG_PLRFIRE:MC_RECV_PlayerFire(NetMsg);
135 NET_MSG_PLRSET: MC_RECV_PlayerSettings(NetMsg);
137 NET_MSG_MSPAWN: MC_RECV_MonsterSpawn(NetMsg);
138 NET_MSG_MPOS: MC_RECV_MonsterPos(NetMsg);
139 NET_MSG_MSTATE: MC_RECV_MonsterState(NetMsg);
140 NET_MSG_MSHOT: MC_RECV_MonsterShot(NetMsg);
141 NET_MSG_MDEL: MC_RECV_MonsterDelete(NetMsg);
143 NET_MSG_SHADD: MC_RECV_CreateShot(NetMsg);
144 NET_MSG_SHPOS: MC_RECV_UpdateShot(NetMsg);
145 NET_MSG_SHDEL: MC_RECV_DeleteShot(NetMsg);
147 NET_MSG_ISPAWN: MC_RECV_ItemSpawn(NetMsg);
148 NET_MSG_IDEL: MC_RECV_ItemDestroy(NetMsg);
149 NET_MSG_IPOS: MC_RECV_ItemPos(NetMsg);
151 NET_MSG_PSTATE: MC_RECV_PanelState(NetMsg);
152 NET_MSG_PTEX: MC_RECV_PanelTexture(NetMsg);
154 NET_MSG_TSOUND: MC_RECV_TriggerSound(NetMsg);
155 NET_MSG_TMUSIC: MC_RECV_TriggerMusic(NetMsg);
157 NET_MSG_TIME_SYNC: MC_RECV_TimeSync(NetMsg);
158 NET_MSG_VOTE_EVENT: MC_RECV_VoteEvent(NetMsg);
160 else
161 begin
162 Result := False;
163 g_Console_Add('unknown message ID: ' + IntToStr(MID));
164 end;
165 end;
166 end;
168 function g_Net_ClientLightMsgHandler(NetMsg: TMsg): Boolean;
169 var
170 MID: Byte;
171 Err: Boolean;
172 begin
173 Result := True;
174 Err := False;
175 try
176 MID := NetMsg.ReadByte();
177 except
178 MID := 0;
179 Err := True;
180 end;
182 //if Err then begin MC_MalformedPacket(S); Exit; end;
184 case MID of
185 NET_MSG_GEVENT: MC_RECV_GameEvent(NetMsg);
186 NET_MSG_GSET: MC_RECV_GameSettings(NetMsg);
188 NET_MSG_PLR: if NetState <> NET_STATE_AUTH then MC_RECV_PlayerCreate(NetMsg);
189 NET_MSG_PLRDEL: if NetState <> NET_STATE_AUTH then MC_RECV_PlayerDelete(NetMsg);
191 else Result := False;
192 end;
193 end;
195 function g_Net_HostMsgHandler(S: pTNetClient; NetMsg: TMsg): Boolean;
196 var
197 MID: Byte;
198 Err: Boolean;
199 begin
200 Result := True;
201 Err := False;
202 try
203 MID := NetMsg.ReadByte();
204 except
205 MID := 0;
206 Err := True;
207 end;
209 if Err then begin MH_MalformedPacket(S); Exit; end;
211 case MID of
212 NET_MSG_INFO: MH_RECV_Info(S, NetMsg);
213 NET_MSG_CHAT: MH_RECV_Chat(S, NetMsg);
214 NET_MSG_REQFST: MH_RECV_FullStateRequest(S, NetMsg);
216 NET_MSG_PLRPOS: MH_RECV_PlayerPos(S, NetMsg);
217 NET_MSG_PLRSET: MH_RECV_PlayerSettings(S, NetMsg);
218 NET_MSG_CHEAT: MH_RECV_CheatRequest(S, NetMsg);
220 NET_MSG_RCON_AUTH: MH_RECV_RCONPassword(S, NetMsg);
221 NET_MSG_RCON_CMD: MH_RECV_RCONCommand(S, NetMsg);
223 //NET_MSG_MAP_REQUEST: MH_RECV_MapRequest(S, NetMsg);
224 //NET_MSG_RES_REQUEST: MH_RECV_ResRequest(S, NetMsg);
226 NET_MSG_VOTE_EVENT: MH_RECV_Vote(S, NetMsg);
227 end;
228 end;
230 end.