Skip to content

Commit

Permalink
net: Add reason in the exit message
Browse files Browse the repository at this point in the history
This avoid "you been kicked" after leaving a game
  • Loading branch information
IonAgorria committed Jun 10, 2024
1 parent 305ec2f commit 0fbe0e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
14 changes: 13 additions & 1 deletion Source/Network/CommonEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,28 @@ struct terEventPing : netCommandGeneral

struct netCommand4G_Exit : netCommandGeneral
{
enum ExitReason {
EXITREASON_NORMAL = 0,
EXITREASON_DROPPED,
EXITREASON_KICKED,
};

NETID netid;
explicit netCommand4G_Exit(NETID netid_) : netCommandGeneral(NETCOM_4G_ID_EXIT), netid(netid_) {
ExitReason reason = EXITREASON_NORMAL;

explicit netCommand4G_Exit(NETID netid_, ExitReason reason_) : netCommandGeneral(NETCOM_4G_ID_EXIT), netid(netid_), reason(reason_) {
}

explicit netCommand4G_Exit(XBuffer& in) : netCommandGeneral(NETCOM_4G_ID_EXIT) {
in.read(&netid, sizeof(netid));
uint32_t tmp = 0;
in.read(&tmp, sizeof(tmp));
reason = static_cast<ExitReason>(tmp);
}

void Write(XBuffer& out) const override {
out.write(&netid, sizeof(netid));
out.write(&reason, sizeof(uint32_t));
}
};

Expand Down
5 changes: 3 additions & 2 deletions Source/Network/P2P_interface1Th.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,12 @@ void PNetCenter::HandlerInputNetCommand()
netCommand4G_Exit nc4g_exit(in_ClientBuf);
//Host already does this before relaying to itself
if (!isHost()) {
if (nc4g_exit.netid == m_localNETID) {
if (nc4g_exit.netid == m_localNETID && nc4g_exit.reason == netCommand4G_Exit::EXITREASON_KICKED) {
ExecuteInternalCommand(PNC_COMMAND__RESET, false);
ExecuteInterfaceCommand(PNC_INTERFACE_COMMAND_KICKED);
} else {
ExitClient(nc4g_exit.netid);
bool normal = nc4g_exit.reason == netCommand4G_Exit::EXITREASON_NORMAL;
DeleteClient(nc4g_exit.netid, normal);
}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion Source/Network/P2P_interface2Th.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void PNetCenter::SecondThreadQuant()
}

//Send exit event to everyone connected (host or clients)
netCommand4G_Exit ex(m_localNETID);
netCommand4G_Exit ex(m_localNETID, netCommand4G_Exit::EXITREASON_NORMAL);
SendEvent(ex, NETID_ALL);
LogMsg("Sent Exit packets to NETID_ALL\n");
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Network/P2P_interface2Th_Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,8 @@ void PNetCenter::hostProcessPlayerClientPackets(PClientData* client) {
xassert(event.netid == netid);
if (event.netid == netid) {
SendEvent(event, NETID_ALL);
ExitClient(netid);
bool normal = event.reason == netCommand4G_Exit::EXITREASON_NORMAL;
DeleteClient(netid, normal);
}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion Source/Network/P2P_interface2Th_NetConn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void PNetCenter::SetConnectionTimeout(int _ms) {
void PNetCenter::KickPlayer(NETID netid) {
fprintf(stdout, "KickPlayer: 0x%" PRIX64 "\n", netid);

netCommand4G_Exit ex(netid);
netCommand4G_Exit ex(netid, netCommand4G_Exit::EXITREASON_KICKED);
SendEvent(ex, netid);

RemovePlayer(netid);
Expand Down

0 comments on commit 0fbe0e9

Please sign in to comment.