About Store Forum Documentation Contact



Post Reply 
Network Multi-Thread
Author Message
Lancer Offline
Member

Post: #1
Network Multi-Thread
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;
}
(This post was last modified: 03-08-2020 01:47 PM by Lancer.)
03-08-2020 01:36 PM
Find all posts by this user Quote this message in a reply
Lancer Offline
Member

Post: #2
RE: Network Multi-Thread
Another question,

is it possible to create a function "TYPE lockedPop()" for MemcThreadSafe class
03-08-2020 02:38 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #3
RE: Network Multi-Thread
(03-08-2020 02:38 PM)Lancer Wrote:  Another question,

is it possible to create a function "TYPE lockedPop()" for MemcThreadSafe class

https://github.com/Esenthel/EsenthelEngi...0139910a34
03-08-2020 03:27 PM
Find all posts by this user Quote this message in a reply
GokUsama Offline
Member

Post: #4
RE: Network Multi-Thread
Thanks that helps! but regarding his second question:
_server is private, so I can not access it. Would it be possible to make it protected?

Could you also provide answer for that?
03-08-2020 04:19 PM
Find all posts by this user Quote this message in a reply
Lancer Offline
Member

Post: #5
RE: Network Multi-Thread
Hi,

is there a way to fix this issue?

I have
EE::MemcThreadSafe<File> m_memPackets;

But whenever I do
File& packet = m_memPackets.lockedElm(i);
or
File& packet = m_memPackets.lockedPop(i);

I get the issue
Error C2248 'EE::File::operator =': cannot access private member declared in class 'EE::File' LoginServer P:\Engine\EsenthelEngine\Engine\H\Memory\Mem Continuous Thread Safe.h 53
03-09-2020 03:11 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: Network Multi-Thread
File& packet = m_memPackets.lockedElm(i);
this works fine, please check again

File& packet = m_memPackets.lockedPop(i);
https://github.com/Esenthel/EsenthelEngi...c666f340c1
please use 'lockedSwapPop'

Code:
MemcThreadSafe<File> m;
File &f=m.lockedElm(0);
File temp; m.lockedSwapPop(temp);
03-10-2020 05:57 AM
Find all posts by this user Quote this message in a reply
Lancer Offline
Member

Post: #7
RE: Network Multi-Thread
Hi,

yes, sorry, the function works fine.

Could you check the MemcThreadSafe.add function ?

Code:
m_memPackets.add(connection.data);
does not work and gives the above error.
03-10-2020 12:34 PM
Find all posts by this user Quote this message in a reply
Lancer Offline
Member

Post: #8
RE: Network Multi-Thread
Whenever I try to do
Code:
EE::File fil;
if (EE::Connection::data.copy(fil) == false)
{
     LogN("Not copy success");
}
then it returns false.
Any way I can copy the connection.data to another File ?
(This post was last modified: 03-11-2020 02:41 AM by Lancer.)
03-11-2020 02:35 AM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #9
RE: Network Multi-Thread
Did you open new file for writing? like writeMem()?
03-11-2020 03:16 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #10
RE: Network Multi-Thread
I can write this code for you if you'd become a supporter:
https://www.esenthel.com/?id=store&cat=1
03-11-2020 05:32 AM
Find all posts by this user Quote this message in a reply
Lancer Offline
Member

Post: #11
RE: Network Multi-Thread
(03-11-2020 05:32 AM)Esenthel Wrote:  I can write this code for you if you'd become a supporter:
https://www.esenthel.com/?id=store&cat=1

Thanks.grin
03-11-2020 12:02 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #12
RE: Network Multi-Thread
Please update to latest source from GitHub, and then try using something like this:
Code:
MemcThreadSafe<File> m;
Connection c;
//..
m.lockedSwapAdd(c.data); c.reinitData();

Your 'File.copy' was another way to do it, however as Houge pointed out, you didn't open it for writing using 'writeMem', and that would be slower than doing the method above.
The method above just swaps the 'data' from connection into the container, and then reinitializes connection 'data'.
03-11-2020 12:09 PM
Find all posts by this user Quote this message in a reply
Lancer Offline
Member

Post: #13
RE: Network Multi-Thread
Thanks.
It works fine.
03-13-2020 04:22 PM
Find all posts by this user Quote this message in a reply
Post Reply