Hello,
so I decided to make the networking multi-threading.
But I have issues/questions.
I want clients (connections) stored in ThreadSafeMap.
Code:
EE::ThreadSafeMap<SockAddr, Client> m_clients;
Now I have a class
Code:
class CConnectionServer : public EE::ConnectionServer
{
public:
CConnectionServer()
{
}
~CConnectionServer()
{
}
void update();
protected:
EE::ThreadSafeMap<SockAddr, TYPE> m_clients;
};
my update function
Code:
// Call this continuously to accept new clients and process existing ones
for (; _server.wait(0); )
{
Connection connection;
if (connection.serverAcceptClient(_server))
{
Client& client = *m_clients.get(connection.address());
Swap(client.connection, connection);
client.create(T);
}
}
m_clients.lock(); // lock map -- To avoid main thread accessing
// update clients
FREP(m_clients.elms())
{
if (!m_clients.lockedData(i).update())
{
m_clients.lockedRemove(i);
}
}
// now when all clients have multiple data chunks queued in their 'connection' ready to be sent, send them all together
FREP(m_clients.elms())
{
m_clients.lockedData(i).connection.flush();
}
// update traffic
flt d = Time.curTime() - m_traffic.last_time;
if (d >= 5) // 5 seconds passed since last traffic measurement, so calculate new traffic averages
{
m_traffic.Update(d, bytesSentTotal(), bytesReceivedTotal());
}
// set max connections peak
MAX(m_traffic.connections_peak, m_clients.elms());
m_clients.unlock(); // unlock
Now Question 1:
_server is private, so I can not access it. Would it be possible to make it protected?
Question 2:
Is the above code fine? The "update" func is called on the network thread,
in main thread I will call following func to find a connection
Code:
CGameClient* CGameClientManager::GetClient(ACCOUNTID accountId)
{
m_clients.lock();
FREP(m_clients.elms())
{
CGameClient* pClnt = &m_clients.lockedData(i);
if (pClnt && pClnt->GetAccountID() == accountId)
{
m_clients.unlock();
return pClnt;
}
}
m_clients.unlock();
return null;
}