Skip to content

Commit 77de9e8

Browse files
StephenCWillsAJenbo
authored andcommitted
Avoid sending PT_DISCONNECT packet to dropped player
1 parent 6673d31 commit 77de9e8

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

Source/dvlnet/base.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,14 @@ bool base::SNetLeaveGame(int type)
409409

410410
bool base::SNetDropPlayer(int playerid, uint32_t flags)
411411
{
412+
const plr_t plr = static_cast<plr_t>(playerid);
412413
auto pkt = pktfty->make_packet<PT_DISCONNECT>(plr_self,
413414
PLR_BROADCAST,
414-
static_cast<plr_t>(playerid),
415+
plr,
415416
static_cast<leaveinfo_t>(flags));
417+
// Disconnect at the network layer first so we
418+
// don't send players their own disconnect packet
419+
DisconnectNet(plr);
416420
send(*pkt);
417421
RecvLocal(*pkt);
418422
return true;

Source/dvlnet/tcp_client.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ void tcp_client::send(packet &pkt)
123123
});
124124
}
125125

126+
void tcp_client::DisconnectNet(plr_t plr)
127+
{
128+
if (local_server != nullptr)
129+
local_server->DisconnectNet(plr);
130+
}
131+
126132
bool tcp_client::SNetLeaveGame(int type)
127133
{
128134
auto ret = base::SNetLeaveGame(type);

Source/dvlnet/tcp_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class tcp_client : public base {
2323

2424
void poll() override;
2525
void send(packet &pkt) override;
26+
void DisconnectNet(plr_t plr) override;
2627

2728
bool SNetLeaveGame(int type) override;
2829

Source/dvlnet/tcp_server.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,27 @@ void tcp_server::HandleTimeout(const scc &con, const asio::error_code &ec)
212212

213213
void tcp_server::DropConnection(const scc &con)
214214
{
215-
if (con->plr != PLR_BROADCAST) {
216-
auto pkt = pktfty.make_packet<PT_DISCONNECT>(PLR_MASTER, PLR_BROADCAST,
217-
con->plr, static_cast<leaveinfo_t>(LEAVE_DROP));
218-
connections[con->plr] = nullptr;
219-
SendPacket(*pkt);
220-
// TODO: investigate if it is really ok for the server to
221-
// drop a client directly.
215+
plr_t plr = con->plr;
216+
con->timer.cancel();
217+
con->socket.close();
218+
if (plr == PLR_BROADCAST) {
219+
return;
222220
}
221+
connections[plr] = nullptr;
222+
223+
auto pkt = pktfty.make_packet<PT_DISCONNECT>(PLR_MASTER, PLR_BROADCAST,
224+
con->plr, static_cast<leaveinfo_t>(LEAVE_DROP));
225+
SendPacket(*pkt);
226+
}
227+
228+
void tcp_server::DisconnectNet(plr_t plr)
229+
{
230+
scc &con = connections[plr];
231+
if (con == nullptr)
232+
return;
223233
con->timer.cancel();
224234
con->socket.close();
235+
con = nullptr;
225236
}
226237

227238
void tcp_server::Close()

Source/dvlnet/tcp_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class tcp_server {
3030
tcp_server(asio::io_context &ioc, const std::string &bindaddr,
3131
unsigned short port, packet_factory &pktfty);
3232
std::string LocalhostSelf();
33+
void DisconnectNet(plr_t plr);
3334
void Close();
3435
virtual ~tcp_server();
3536

0 commit comments

Comments
 (0)