Unity结合C++开发服务器实现多人游戏(六)

来源:互联网 发布:stc单片机烧录器 编辑:程序博客网 时间:2024/05/18 00:45

管理服务器端数据

 

本讲主要讲述,管理服务器端数据的操作过程。

在这里,连接服务器端的所有客户端被叫做remote client,各个客户端的情报进行如下定义,玩家的ID和控制角色以x,y,z坐标输入.

class RemoteClient

{

public:

    std::wstring m_userID;

    float m_x, m_y, m_z;

    float m_vx, m_vy, m_vz;

    float m_angle;

};

在服务器类库中添加"所有客户端目录",使用key值代表各个客户端的HostID.

现在我们的服务器是多线程模式.

因此,从不同客户端接收的信息在另一端被调用,进行此操作的保护需要对critical section或者mutex进行声明

CriticalSection m_critSec;

 

// key: client HostID

unordered_map<int, shared_ptr<RemoteClient> > m_remoteClients;

登陆邀请在服务器端成功的话,创建新的对象remote client添加至以上目录

DEFRMI_Simple_RequestLogin(SimpleServer)

{

    ...

        CriticalSectionLock lock(m_critSec, true);

  

        // already logged in?

        auto it = m_remoteClients.find(remote);

        if (it != m_remoteClients.end())

        {

            m_proxy.NotifyLoginFailed(remote, RmiContext::ReliableSend, L"Already logged in.");

            return true;

        }

  

        // add new player

        // NOTE: we don't care duplicated login for now. We are in tutorial.

        auto newRC = make_shared<RemoteClient>();

        newRC->m_userID = id;

        m_remoteClients[remote] = newRC;

注意:讲座主要针对于游戏服务器开发的介绍,而不是ProudNet的宣传^^;;;

以下进行,客户端离开服务器端,并同时删除客户端.

m_netServer->OnClientLeave = [this](CNetClientInfo* info, ErrorInfo*, const ByteArray&)

{

    cout << "Client " << info->m_HostID << " went out.\n";

 

 

    CriticalSectionLock lock(m_critSec, true);

    // delete player

    m_remoteClients.erase(info->m_HostID);

};


第七篇:Unity结合C++开发服务器实现多人游戏(七)

 

0 0
原创粉丝点击