游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2382|回复: 0

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

[复制链接]

29

主题

38

帖子

384

积分

中级会员

Rank: 3Rank: 3

积分
384
QQ
发表于 2017-2-23 16:16:48 | 显示全部楼层 |阅读模式
管理服务器端数据

本讲主要讲述,管理服务器端数据的操作过程。
在这里,连接服务器端的所有客户端被叫做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++开发服务器实现多人游戏(七)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-24 23:56

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表