Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git src/engine/server/server.cpp src/engine/server/server.cpp
- index 6c459257..3dc03cb0 100644
- --- src/engine/server/server.cpp
- +++ src/engine/server/server.cpp
- @@ -1086,7 +1086,7 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token)
- {
- if(m_aClients[i].m_State != CClient::STATE_EMPTY)
- {
- - if(GameServer()->IsClientPlayer(i))
- + if(GameServer()->IsClientPlayer(i) && m_NetServer.IsClientOnline(i))
- PlayerCount++;
- ClientCount++;
- @@ -1126,7 +1126,7 @@ void CServer::SendServerInfo(const NETADDR *pAddr, int Token)
- p.AddString(ClientClan(i), MAX_CLAN_LENGTH); // client clan
- str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Country); p.AddString(aBuf, 6); // client country
- str_format(aBuf, sizeof(aBuf), "%d", m_aClients[i].m_Score); p.AddString(aBuf, 6); // client score
- - str_format(aBuf, sizeof(aBuf), "%d", GameServer()->IsClientPlayer(i)?1:0); p.AddString(aBuf, 2); // is player?
- + str_format(aBuf, sizeof(aBuf), "%d", (GameServer()->IsClientPlayer(i) && m_NetServer.IsClientOnline(i)) ? 1 : 0); p.AddString(aBuf, 2); // is player?
- }
- }
- diff --git src/engine/shared/network.h src/engine/shared/network.h
- index b934563c..66a9219e 100644
- --- src/engine/shared/network.h
- +++ src/engine/shared/network.h
- @@ -264,6 +264,8 @@ class CNetServer
- void *m_UserPtr;
- CNetRecvUnpacker m_RecvUnpacker;
- +
- + int FindReusableSlot();
- public:
- int SetCallbacks(NETFUNC_NEWCLIENT pfnNewClient, NETFUNC_DELCLIENT pfnDelClient, void *pUser);
- @@ -272,6 +274,9 @@ public:
- bool Open(NETADDR BindAddr, class CNetBan *pNetBan, int MaxClients, int MaxClientsPerIP, int Flags);
- int Close();
- + bool IsClientOnline(int ClientID)
- + { return m_aSlots[ClientID].m_Connection.State() == NET_CONNSTATE_ONLINE; }
- +
- //
- int Recv(CNetChunk *pChunk);
- int Send(CNetChunk *pChunk);
- diff --git src/engine/shared/network_server.cpp src/engine/shared/network_server.cpp
- index 537048cb..d9ec177d 100644
- --- src/engine/shared/network_server.cpp
- +++ src/engine/shared/network_server.cpp
- @@ -1,7 +1,11 @@
- /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
- /* If you are missing that file, acquire a complete release at teeworlds.com. */
- +#include <vector>
- +#include <cstdlib>
- +
- #include <base/system.h>
- +
- #include <engine/console.h>
- #include "netban.h"
- @@ -88,9 +92,45 @@ int CNetServer::Update()
- return 0;
- }
- +#include <iostream>
- +
- +int CNetServer::FindReusableSlot()
- +{
- +
- + // Slots that are completely empty
- + std::vector<int> Empty;
- + // Slots that can be reused if necessary
- + std::vector<int> Reusable;
- +
- + for (int i = 0; i < MaxClients(); ++i)
- + {
- + CNetConnection& Con = m_aSlots[i].m_Connection;
- + std::cerr << Con.State();
- + if (Con.State() == NET_CONNSTATE_OFFLINE)
- + {
- + Empty.push_back(i);
- + }
- + else if (
- + Con.State() == NET_CONNSTATE_ERROR
- + || Con.State() == NET_CONNSTATE_CONNECT
- + || Con.State() == NET_CONNSTATE_PENDING
- + ) {
- + Reusable.push_back(i);
- + }
- + }
- + std::cerr << std::endl;
- +
- + if (!Empty.empty())
- + return Empty[0];
- + if (!Reusable.empty())
- + return Reusable[std::rand() % Reusable.size()];
- + return -1;
- +}
- +
- /*
- TODO: chopp up this function into smaller working parts
- */
- +
- int CNetServer::Recv(CNetChunk *pChunk)
- {
- while(1)
- @@ -171,24 +211,29 @@ int CNetServer::Recv(CNetChunk *pChunk)
- }
- }
- }
- -
- - for(int i = 0; i < MaxClients(); i++)
- - {
- - if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_OFFLINE)
- - {
- - Found = true;
- - m_aSlots[i].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr);
- - if(m_pfnNewClient)
- - m_pfnNewClient(i, m_UserPtr);
- - break;
- - }
- +
- + int Slot = FindReusableSlot();
- + const char FullMsg[] = "This server is full";
- +
- + if(Slot < 0)
- + {
- + std::cerr << "No slot found!" << std::endl;
- + CNetBase::SendControlMsg(
- + m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE,
- + FullMsg, sizeof(FullMsg)
- + );
- }
- -
- - if(!Found)
- + else
- {
- - const char FullMsg[] = "This server is full";
- - CNetBase::SendControlMsg(m_Socket, &Addr, 0, NET_CTRLMSG_CLOSE, FullMsg, sizeof(FullMsg));
- + std::cerr << "Overwriting slot!" << std::endl;
- + if (m_aSlots[Slot].m_Connection.State() != NET_CONNSTATE_OFFLINE)
- + Drop(Slot, FullMsg);
- +
- + m_aSlots[Slot].m_Connection.Feed(&m_RecvUnpacker.m_Data, &Addr);
- + if (m_pfnNewClient)
- + m_pfnNewClient(Slot, m_UserPtr);
- }
- +
- }
- }
- else
Add Comment
Please, Sign In to add comment