DEADSOFTWARE

Net: Buffer outgoing messages
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
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/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_nethandler;
19 interface
21 uses e_msg, g_net, g_netmsg, ENet;
23 type
24 TNetClientMsgHandler = function (M: TMsg): Boolean;
25 TNetHostMsgHandler = function (S: pTNetClient; M: TMsg): Boolean;
27 procedure g_Net_Client_HandlePacket(P: pENetPacket; Handler: TNetClientMsgHandler);
28 procedure g_Net_Host_HandlePacket(S: pTNetClient; P: pENetPacket; Handler: TNetHostMsgHandler);
30 function g_Net_ClientMsgHandler(NetMsg: TMsg): Boolean;
31 function g_Net_ClientLightMsgHandler(NetMsg: TMsg): Boolean;
32 function g_Net_HostMsgHandler(S: pTNetClient; NetMsg: TMsg): Boolean;
34 implementation
36 uses sysutils, g_console;
38 procedure g_Net_Client_HandlePacket(P: pENetPacket; Handler: TNetClientMsgHandler);
39 var
40 MNext: Integer;
41 MSize: LongWord;
42 MHandled: Boolean;
43 NetMsg: TMsg;
44 begin
45 if not NetMsg.Init(P^.data, P^.dataLength, True) then
46 Exit;
48 MNext := 0;
50 while NetMsg.BytesLeft() > 0 do
51 begin
52 MSize := NetMsg.ReadLongWord();
53 MNext := NetMsg.ReadCount + MSize;
54 MHandled := Handler(NetMsg); // TODO: maybe do something with this bool
55 NetMsg.Seek(MNext);
56 end;
58 enet_packet_destroy(P);
59 end;
61 procedure g_Net_Host_HandlePacket(S: pTNetClient; P: pENetPacket; Handler: TNetHostMsgHandler);
62 var
63 MNext: Integer;
64 MSize: LongWord;
65 MHandled: Boolean;
66 NetMsg: TMsg;
67 begin
68 if not NetMsg.Init(P^.data, P^.dataLength, True) then
69 Exit;
71 MNext := 0;
73 while NetMsg.BytesLeft() > 0 do
74 begin
75 MSize := NetMsg.ReadLongWord();
76 MNext := NetMsg.ReadCount + MSize;
77 MHandled := Handler(S, NetMsg); // TODO: maybe do something with this bool
78 NetMsg.Seek(MNext);
79 end;
81 enet_packet_destroy(P);
82 end;
85 function g_Net_ClientMsgHandler(NetMsg: TMsg): Boolean;
86 var
87 MID: Byte;
88 begin
89 Result := True;
90 MID := NetMsg.ReadByte();
92 case MID of
93 NET_MSG_CHAT: MC_RECV_Chat(NetMsg);
94 NET_MSG_GFX: MC_RECV_Effect(NetMsg);
95 NET_MSG_SND: MC_RECV_Sound(NetMsg);
96 NET_MSG_SCORE: MC_RECV_GameStats(NetMsg);
97 NET_MSG_COOP: MC_RECV_CoopStats(NetMsg);
98 NET_MSG_GEVENT: MC_RECV_GameEvent(NetMsg);
99 NET_MSG_FLAG: MC_RECV_FlagEvent(NetMsg);
100 NET_MSG_GSET: MC_RECV_GameSettings(NetMsg);
102 NET_MSG_PLR: MC_RECV_PlayerCreate(NetMsg);
103 NET_MSG_PLRPOS: MC_RECV_PlayerPos(NetMsg);
104 NET_MSG_PLRSTA: MC_RECV_PlayerStats(NetMsg);
105 NET_MSG_PLRDEL: MC_RECV_PlayerDelete(NetMsg);
106 NET_MSG_PLRDMG: MC_RECV_PlayerDamage(NetMsg);
107 NET_MSG_PLRDIE: MC_RECV_PlayerDeath(NetMsg);
108 NET_MSG_PLRFIRE:MC_RECV_PlayerFire(NetMsg);
109 NET_MSG_PLRSET: MC_RECV_PlayerSettings(NetMsg);
111 NET_MSG_MSPAWN: MC_RECV_MonsterSpawn(NetMsg);
112 NET_MSG_MPOS: MC_RECV_MonsterPos(NetMsg);
113 NET_MSG_MSTATE: MC_RECV_MonsterState(NetMsg);
114 NET_MSG_MSHOT: MC_RECV_MonsterShot(NetMsg);
115 NET_MSG_MDEL: MC_RECV_MonsterDelete(NetMsg);
117 NET_MSG_SHADD: MC_RECV_CreateShot(NetMsg);
118 NET_MSG_SHPOS: MC_RECV_UpdateShot(NetMsg);
119 NET_MSG_SHDEL: MC_RECV_DeleteShot(NetMsg);
121 NET_MSG_ISPAWN: MC_RECV_ItemSpawn(NetMsg);
122 NET_MSG_IDEL: MC_RECV_ItemDestroy(NetMsg);
124 NET_MSG_PSTATE: MC_RECV_PanelState(NetMsg);
125 NET_MSG_PTEX: MC_RECV_PanelTexture(NetMsg);
127 NET_MSG_TSOUND: MC_RECV_TriggerSound(NetMsg);
128 NET_MSG_TMUSIC: MC_RECV_TriggerMusic(NetMsg);
130 NET_MSG_TIME_SYNC: MC_RECV_TimeSync(NetMsg);
131 NET_MSG_VOTE_EVENT: MC_RECV_VoteEvent(NetMsg);
133 else
134 begin
135 Result := False;
136 g_Console_Add('unknown message ID: ' + IntToStr(MID));
137 end;
138 end;
139 end;
141 function g_Net_ClientLightMsgHandler(NetMsg: TMsg): Boolean;
142 var
143 MID: Byte;
144 begin
145 Result := True;
146 MID := NetMsg.ReadByte();
148 case MID of
149 NET_MSG_GEVENT: MC_RECV_GameEvent(NetMsg);
150 NET_MSG_GSET: MC_RECV_GameSettings(NetMsg);
152 NET_MSG_PLR: if NetState <> NET_STATE_AUTH then MC_RECV_PlayerCreate(NetMsg);
153 NET_MSG_PLRDEL: if NetState <> NET_STATE_AUTH then MC_RECV_PlayerDelete(NetMsg);
155 else Result := False;
156 end;
157 end;
159 function g_Net_HostMsgHandler(S: pTNetClient; NetMsg: TMsg): Boolean;
160 var
161 MID: Byte;
162 begin
163 Result := True;
164 MID := NetMsg.ReadByte();
165 g_Console_Add('MID = ' + IntTOStr(MID));
167 case MID of
168 NET_MSG_INFO: MH_RECV_Info(S, NetMsg);
169 NET_MSG_CHAT: MH_RECV_Chat(S, NetMsg);
170 NET_MSG_REQFST: MH_RECV_FullStateRequest(S, NetMsg);
172 NET_MSG_PLRPOS: MH_RECV_PlayerPos(S, NetMsg);
173 NET_MSG_PLRSET: MH_RECV_PlayerSettings(S, NetMsg);
174 NET_MSG_CHEAT: MH_RECV_CheatRequest(S, NetMsg);
176 NET_MSG_RCON_AUTH: MH_RECV_RCONPassword(S, NetMsg);
177 NET_MSG_RCON_CMD: MH_RECV_RCONCommand(S, NetMsg);
179 NET_MSG_MAP_REQUEST: MH_RECV_MapRequest(S, NetMsg);
180 NET_MSG_RES_REQUEST: MH_RECV_ResRequest(S, NetMsg);
182 NET_MSG_VOTE_EVENT: MH_RECV_Vote(S, NetMsg);
183 end;
184 end;
186 end.