DEADSOFTWARE

headless: use dfserver.cfg instead dfconfig.cfg by default
[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 procedure g_Net_Client_HandlePacket(P: pENetPacket; Handler: TNetClientMsgHandler);
38 var
39 MNext: Integer;
40 MSize: LongWord;
41 MHandled: Boolean = false;
42 NetMsg: TMsg;
43 begin
44 if not NetMsg.Init(P^.data, P^.dataLength, True) then
45 Exit;
47 MNext := 0;
49 while NetMsg.BytesLeft() > 0 do
50 begin
51 MSize := NetMsg.ReadLongWord();
52 MNext := NetMsg.ReadCount + MSize;
53 MHandled := Handler(NetMsg); // TODO: maybe do something with this bool
54 NetMsg.Seek(MNext);
55 end;
57 MHandled := not MHandled; //k8: stfu, fpc!
59 enet_packet_destroy(P);
60 end;
62 procedure g_Net_Host_HandlePacket(S: pTNetClient; P: pENetPacket; Handler: TNetHostMsgHandler);
63 var
64 MNext: Integer;
65 MSize: LongWord;
66 MHandled: Boolean = false;
67 NetMsg: TMsg;
68 begin
69 if not NetMsg.Init(P^.data, P^.dataLength, True) then
70 Exit;
72 MNext := 0;
74 while NetMsg.BytesLeft() > 0 do
75 begin
76 MSize := NetMsg.ReadLongWord();
77 MNext := NetMsg.ReadCount + MSize;
78 MHandled := Handler(S, NetMsg); // TODO: maybe do something with this bool
79 NetMsg.Seek(MNext);
80 end;
82 MHandled := not MHandled; //k8: stfu, fpc!
84 enet_packet_destroy(P);
85 end;
88 function g_Net_ClientMsgHandler(NetMsg: TMsg): Boolean;
89 var
90 MID: Byte;
91 begin
92 Result := True;
93 MID := NetMsg.ReadByte();
95 case MID of
96 NET_MSG_CHAT: MC_RECV_Chat(NetMsg);
97 NET_MSG_GFX: MC_RECV_Effect(NetMsg);
98 NET_MSG_SND: MC_RECV_Sound(NetMsg);
99 NET_MSG_SCORE: MC_RECV_GameStats(NetMsg);
100 NET_MSG_COOP: MC_RECV_CoopStats(NetMsg);
101 NET_MSG_GEVENT: MC_RECV_GameEvent(NetMsg);
102 NET_MSG_FLAG: MC_RECV_FlagEvent(NetMsg);
103 NET_MSG_GSET: MC_RECV_GameSettings(NetMsg);
104 NET_MSG_FLAGPOS:MC_RECV_FlagPos(NetMsg);
106 NET_MSG_PLR: MC_RECV_PlayerCreate(NetMsg);
107 NET_MSG_PLRPOS: MC_RECV_PlayerPos(NetMsg);
108 NET_MSG_PLRSTA: MC_RECV_PlayerStats(NetMsg);
109 NET_MSG_PLRDEL: MC_RECV_PlayerDelete(NetMsg);
110 NET_MSG_PLRDMG: MC_RECV_PlayerDamage(NetMsg);
111 NET_MSG_PLRDIE: MC_RECV_PlayerDeath(NetMsg);
112 NET_MSG_PLRFIRE:MC_RECV_PlayerFire(NetMsg);
113 NET_MSG_PLRSET: MC_RECV_PlayerSettings(NetMsg);
115 NET_MSG_MSPAWN: MC_RECV_MonsterSpawn(NetMsg);
116 NET_MSG_MPOS: MC_RECV_MonsterPos(NetMsg);
117 NET_MSG_MSTATE: MC_RECV_MonsterState(NetMsg);
118 NET_MSG_MSHOT: MC_RECV_MonsterShot(NetMsg);
119 NET_MSG_MDEL: MC_RECV_MonsterDelete(NetMsg);
121 NET_MSG_SHADD: MC_RECV_CreateShot(NetMsg);
122 NET_MSG_SHPOS: MC_RECV_UpdateShot(NetMsg);
123 NET_MSG_SHDEL: MC_RECV_DeleteShot(NetMsg);
125 NET_MSG_ISPAWN: MC_RECV_ItemSpawn(NetMsg);
126 NET_MSG_IDEL: MC_RECV_ItemDestroy(NetMsg);
127 NET_MSG_IPOS: MC_RECV_ItemPos(NetMsg);
129 NET_MSG_PSTATE: MC_RECV_PanelState(NetMsg);
130 NET_MSG_PTEX: MC_RECV_PanelTexture(NetMsg);
132 NET_MSG_TSOUND: MC_RECV_TriggerSound(NetMsg);
133 NET_MSG_TMUSIC: MC_RECV_TriggerMusic(NetMsg);
135 NET_MSG_TIME_SYNC: MC_RECV_TimeSync(NetMsg);
136 NET_MSG_VOTE_EVENT: MC_RECV_VoteEvent(NetMsg);
138 else
139 begin
140 Result := False;
141 g_Console_Add('unknown message ID: ' + IntToStr(MID));
142 end;
143 end;
144 end;
146 function g_Net_ClientLightMsgHandler(NetMsg: TMsg): Boolean;
147 var
148 MID: Byte;
149 begin
150 Result := True;
151 MID := NetMsg.ReadByte();
153 case MID of
154 NET_MSG_GEVENT: MC_RECV_GameEvent(NetMsg);
155 NET_MSG_GSET: MC_RECV_GameSettings(NetMsg);
157 NET_MSG_PLR: if NetState <> NET_STATE_AUTH then MC_RECV_PlayerCreate(NetMsg);
158 NET_MSG_PLRDEL: if NetState <> NET_STATE_AUTH then MC_RECV_PlayerDelete(NetMsg);
160 else Result := False;
161 end;
162 end;
164 function g_Net_HostMsgHandler(S: pTNetClient; NetMsg: TMsg): Boolean;
165 var
166 MID: Byte;
167 begin
168 Result := True;
169 MID := NetMsg.ReadByte();
171 case MID of
172 NET_MSG_INFO: MH_RECV_Info(S, NetMsg);
173 NET_MSG_CHAT: MH_RECV_Chat(S, NetMsg);
174 NET_MSG_REQFST: MH_RECV_FullStateRequest(S, NetMsg);
176 NET_MSG_PLRPOS: MH_RECV_PlayerPos(S, NetMsg);
177 NET_MSG_PLRSET: MH_RECV_PlayerSettings(S, NetMsg);
178 NET_MSG_CHEAT: MH_RECV_CheatRequest(S, NetMsg);
180 NET_MSG_RCON_AUTH: MH_RECV_RCONPassword(S, NetMsg);
181 NET_MSG_RCON_CMD: MH_RECV_RCONCommand(S, NetMsg);
183 //NET_MSG_MAP_REQUEST: MH_RECV_MapRequest(S, NetMsg);
184 //NET_MSG_RES_REQUEST: MH_RECV_ResRequest(S, NetMsg);
186 NET_MSG_VOTE_EVENT: MH_RECV_Vote(S, NetMsg);
187 end;
188 end;
190 end.