0

我正在为基于 IOCP 的在线游戏编写服务器,处理游戏消息的核心代码如下所示:

CMessage ret;
int now_roomnum = recv_msg->para1;
int now_playernum = recv_msg->para2;
/*if(true)
{
    cout<<"Received Game Message: "<<endl;
    cout<<"type2 = "<<recv_msg->type2;
    cout<<" player_num = "<<now_playernum<<" msg= "<<recv_msg->msg<<endl;

    cout<<endl;
}*/

if(recv_msg->type2 == MSG_GAME_OPERATION)
{
    ret.type1 = MSG_GAME;
    ret.type2 = MSG_GAME_OPERATION;

    while(game_host[now_roomnum].Ready(now_playernum) == true)
    {
        ;
    }
    //cout<<"Entered from "<<now_playernum<<endl;

    game_host[now_roomnum].SetMessage(now_playernum, recv_msg->msg);
    game_host[now_roomnum].SetReady(now_playernum, true);
    game_host[now_roomnum].SetUsed(now_playernum, false);

    while(true)
    {
        bool tmp = game_host[now_roomnum].AllReady();
        if(tmp == true)
            break;
    }

    //cout<<"AllReady from"<<now_playernum<<endl;

    string all_msg = game_host[now_roomnum].GetAllMessage();
    game_host[now_roomnum].SetUsed(now_playernum, true);

    while(!game_host[now_roomnum].AllUsed())
    {
        ;
    }

    //cout<<"AllUsed from "<<now_playernum<<endl;

    EnterCriticalSection(&cs);
    game_host[now_roomnum].ClearReady();
    LeaveCriticalSection(&cs);

    strcpy_s(ret.msg, all_msg.c_str());

    //cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;

}

return ret;

现在,问题是:在PC上,当所有cout评论都像上面一样时,游戏会立即冻结;但是当我取消评论时,服务器运行良好。

更重要的是,当我在笔记本电脑上运行服务器时,无论我是否评论,一切都很好cout。我的笔记本电脑和 PC 之间的主要区别在于我的笔记本电脑的操作系统是 Windows 8.1,而 PC 是 Windows 7。

我完全糊涂了。如果有人能告诉我该怎么做,那将有很大帮助。谢谢!

4

2 回答 2

0

看起来像一个多线程问题。

顺便说一句,我看到你使用了一个关键部分,ClearReady但在测试AllReady. 该调用也应该被包装(或者,更好的是,编写一个LockedAllReady使用锁的)。

于 2015-05-14T06:25:27.907 回答
0
//cout<<"Return msg "<<now_playernum<<": "<<ret.msg<<endl;

你说的 ret.msg 是什么意思?如果 msg 是方法,你必须做 ret.msg(); ,它是一个领域吗?

如果你有这个好,那么就像他们上面说的可能是时间问题,试着在没有的情况下做 coutret.msg看看会发生什么,然后你就知道问题出在哪里了。

于 2015-05-14T07:46:02.543 回答