From 87e0299484302b1cd82417553fc7337d3a6c740f Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 3 Mar 2024 09:20:55 +0800 Subject: [PATCH] Reduce code nesting in Recv() method Also replace the variable name `Result` with `Error` Since `UnpackPacket()` returns zero on success. This red a bit funny: if(!Result) { // do something with the result } It now is: if(Error) continue; // do something with the result --- src/engine/shared/network_client.cpp | 62 +++++---- src/engine/shared/network_server.cpp | 183 +++++++++++++-------------- 2 files changed, 121 insertions(+), 124 deletions(-) diff --git a/src/engine/shared/network_client.cpp b/src/engine/shared/network_client.cpp index ef7c340309..7d043f5e7c 100644 --- a/src/engine/shared/network_client.cpp +++ b/src/engine/shared/network_client.cpp @@ -74,44 +74,43 @@ int CNetClient::Recv(CNetChunk *pChunk, TOKEN *pResponseToken) // TODO: empty the recvinfo NETADDR Addr; - int Result = UnpackPacket(&Addr, m_RecvUnpacker.m_aBuffer, &m_RecvUnpacker.m_Data); + int Error = UnpackPacket(&Addr, m_RecvUnpacker.m_aBuffer, &m_RecvUnpacker.m_Data); // no more packets for now - if(Result > 0) + if(Error > 0) break; + if(Error) + continue; - if(!Result) + if(m_Connection.State() != NET_CONNSTATE_OFFLINE && m_Connection.State() != NET_CONNSTATE_ERROR && net_addr_comp(m_Connection.PeerAddress(), &Addr, true) == 0) { - if(m_Connection.State() != NET_CONNSTATE_OFFLINE && m_Connection.State() != NET_CONNSTATE_ERROR && net_addr_comp(m_Connection.PeerAddress(), &Addr, true) == 0) + if(m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr)) { - if(m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr)) - { - if(!(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)) - m_RecvUnpacker.Start(&Addr, &m_Connection, 0); - } + if(!(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)) + m_RecvUnpacker.Start(&Addr, &m_Connection, 0); } - else + } + else + { + int Accept = m_TokenManager.ProcessMessage(&Addr, &m_RecvUnpacker.m_Data); + if(!Accept) + continue; + + if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL) + { + if(m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_TOKEN) + m_TokenCache.AddToken(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, NET_TOKENFLAG_ALLOWBROADCAST|NET_TOKENFLAG_RESPONSEONLY); + } + else if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS && Accept != -1) { - int Accept = m_TokenManager.ProcessMessage(&Addr, &m_RecvUnpacker.m_Data); - if(!Accept) - continue; - - if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL) - { - if(m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_TOKEN) - m_TokenCache.AddToken(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, NET_TOKENFLAG_ALLOWBROADCAST|NET_TOKENFLAG_RESPONSEONLY); - } - else if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS && Accept != -1) - { - pChunk->m_Flags = NETSENDFLAG_CONNLESS; - pChunk->m_ClientID = -1; - pChunk->m_Address = Addr; - pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; - pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; - - if(pResponseToken) - *pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken; - return 1; - } + pChunk->m_Flags = NETSENDFLAG_CONNLESS; + pChunk->m_ClientID = -1; + pChunk->m_Address = Addr; + pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; + pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; + + if(pResponseToken) + *pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken; + return 1; } } } @@ -202,4 +201,3 @@ const char *CNetClient::ErrorString() const { return m_Connection.ErrorString(); } - diff --git a/src/engine/shared/network_server.cpp b/src/engine/shared/network_server.cpp index 011a2bc982..9a423b3cab 100644 --- a/src/engine/shared/network_server.cpp +++ b/src/engine/shared/network_server.cpp @@ -101,130 +101,129 @@ int CNetServer::Recv(CNetChunk *pChunk, TOKEN *pResponseToken) // TODO: empty the recvinfo NETADDR Addr; - int Result = UnpackPacket(&Addr, m_RecvUnpacker.m_aBuffer, &m_RecvUnpacker.m_Data); + int Error = UnpackPacket(&Addr, m_RecvUnpacker.m_aBuffer, &m_RecvUnpacker.m_Data); // no more packets for now - if(Result > 0) + if(Error > 0) break; + if(Error) + continue; - if(!Result) + // check for bans + char aBuf[128]; + int LastInfoQuery; + if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf), &LastInfoQuery)) { - // check for bans - char aBuf[128]; - int LastInfoQuery; - if(NetBan() && NetBan()->IsBanned(&Addr, aBuf, sizeof(aBuf), &LastInfoQuery)) + // banned, reply with a message (5 second cooldown) + int Time = time_timestamp(); + if(LastInfoQuery + 5 < Time) { - // banned, reply with a message (5 second cooldown) - int Time = time_timestamp(); - if(LastInfoQuery + 5 < Time) - { - SendControlMsg(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf) + 1); - } - continue; + SendControlMsg(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf) + 1); } + continue; + } - bool Found = false; - // try to find matching slot - for(int i = 0; i < NET_MAX_CLIENTS; i++) - { - if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) - continue; + bool Found = false; + // try to find matching slot + for(int i = 0; i < NET_MAX_CLIENTS; i++) + { + if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) + continue; - if(net_addr_comp(m_aSlots[i].m_Connection.PeerAddress(), &Addr, true) == 0) + if(net_addr_comp(m_aSlots[i].m_Connection.PeerAddress(), &Addr, true) == 0) + { + if(m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr)) { - if(m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr)) + if(m_RecvUnpacker.m_Data.m_DataSize) { - if(m_RecvUnpacker.m_Data.m_DataSize) + if(!(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)) + m_RecvUnpacker.Start(&Addr, &m_aSlots[i].m_Connection, i); + else { - if(!(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS)) - m_RecvUnpacker.Start(&Addr, &m_aSlots[i].m_Connection, i); - else - { - pChunk->m_Flags = NETSENDFLAG_CONNLESS; - pChunk->m_Address = *m_aSlots[i].m_Connection.PeerAddress(); - pChunk->m_ClientID = i; - pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; - pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; - if(pResponseToken) - *pResponseToken = NET_TOKEN_NONE; - return 1; - } + pChunk->m_Flags = NETSENDFLAG_CONNLESS; + pChunk->m_Address = *m_aSlots[i].m_Connection.PeerAddress(); + pChunk->m_ClientID = i; + pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; + pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; + if(pResponseToken) + *pResponseToken = NET_TOKEN_NONE; + return 1; } } - Found = true; } + Found = true; } + } - if(Found) - continue; + if(Found) + continue; - int Accept = m_TokenManager.ProcessMessage(&Addr, &m_RecvUnpacker.m_Data); - if(Accept <= 0) - continue; + int Accept = m_TokenManager.ProcessMessage(&Addr, &m_RecvUnpacker.m_Data); + if(Accept <= 0) + continue; - if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL) + if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONTROL) + { + if(m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_CONNECT) { - if(m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_CONNECT) + // check if there are free slots + if(m_NumClients >= m_MaxClients) { - // check if there are free slots - if(m_NumClients >= m_MaxClients) - { - const char FullMsg[] = "This server is full"; - SendControlMsg(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg)); + const char FullMsg[] = "This server is full"; + SendControlMsg(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg)); + continue; + } + + // only allow a specific number of players with the same ip + int FoundAddr = 1; + + bool Continue = false; + for(int i = 0; i < NET_MAX_CLIENTS; i++) + { + if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) continue; - } - // only allow a specific number of players with the same ip - int FoundAddr = 1; - - bool Continue = false; - for(int i = 0; i < NET_MAX_CLIENTS; i++) + if(!net_addr_comp(&Addr, m_aSlots[i].m_Connection.PeerAddress(), false)) { - if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) - continue; - - if(!net_addr_comp(&Addr, m_aSlots[i].m_Connection.PeerAddress(), false)) + if(FoundAddr++ >= m_MaxClientsPerIP) { - if(FoundAddr++ >= m_MaxClientsPerIP) - { - char aBuf[128]; - str_format(aBuf, sizeof(aBuf), "Only %d players with the same IP are allowed", m_MaxClientsPerIP); - SendControlMsg(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf) + 1); - Continue = true; - break; - } + char aBuf[128]; + str_format(aBuf, sizeof(aBuf), "Only %d players with the same IP are allowed", m_MaxClientsPerIP); + SendControlMsg(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, 0, NET_CTRLMSG_CLOSE, aBuf, str_length(aBuf) + 1); + Continue = true; + break; } } + } - if(Continue) - continue; + if(Continue) + continue; - for(int i = 0; i < NET_MAX_CLIENTS; i++) + for(int i = 0; i < NET_MAX_CLIENTS; i++) + { + if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) { - if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE) - { - m_NumClients++; - m_aSlots[i].m_Connection.SetToken(m_RecvUnpacker.m_Data.m_Token); - m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr); - if(m_pfnNewClient) - m_pfnNewClient(i, m_UserPtr); - break; - } + m_NumClients++; + m_aSlots[i].m_Connection.SetToken(m_RecvUnpacker.m_Data.m_Token); + m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr); + if(m_pfnNewClient) + m_pfnNewClient(i, m_UserPtr); + break; } } - else if(m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_TOKEN) - m_TokenCache.AddToken(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, NET_TOKENFLAG_RESPONSEONLY); - } - else if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS) - { - pChunk->m_Flags = NETSENDFLAG_CONNLESS; - pChunk->m_ClientID = -1; - pChunk->m_Address = Addr; - pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; - pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; - if(pResponseToken) - *pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken; - return 1; } + else if(m_RecvUnpacker.m_Data.m_aChunkData[0] == NET_CTRLMSG_TOKEN) + m_TokenCache.AddToken(&Addr, m_RecvUnpacker.m_Data.m_ResponseToken, NET_TOKENFLAG_RESPONSEONLY); + } + else if(m_RecvUnpacker.m_Data.m_Flags&NET_PACKETFLAG_CONNLESS) + { + pChunk->m_Flags = NETSENDFLAG_CONNLESS; + pChunk->m_ClientID = -1; + pChunk->m_Address = Addr; + pChunk->m_DataSize = m_RecvUnpacker.m_Data.m_DataSize; + pChunk->m_pData = m_RecvUnpacker.m_Data.m_aChunkData; + if(pResponseToken) + *pResponseToken = m_RecvUnpacker.m_Data.m_ResponseToken; + return 1; } } return 0;