• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            Getting Online with Multiplayer Gaming(12)

             

            Processing Game Messages

            Now that the game messages have made their way into the message queue, the next
            step is to remove the messages at each frame and process them. To keep things
            running quickly, only 64 messages at a time are processed (as defined by the
            MESSAGE_PER_FRAME macro in the server source code).

            Message processing takes place within the cApp::process_queued_msg function:

            void cApp::process_queue_msg()
            {
                
            long count = 0;

                
            // pull out messages to process
                while(count != MESSAGES_PER_FRAME && m_msg_head != m_msg_tail)
                {
                    
            // get pointer to 'tail' message
                    EnterCriticalSection(&m_msg_cs);
                    sMsg* msg = &m_msgs[m_msg_tail];
                    LeaveCriticalSection(&m_msg_cs);

                    
            // process a single message based on type
                    switch(msg->header.type)
                    {
                    
            case MSG_SEND_PLAYER_INFO:
                        send_player_info(msg, msg->header.player_id);
                        
            break;

                    
            case MSG_CREATE_PLAYER:
                        add_player(msg);
                        
            break;

                    
            case MSG_DESTROY_PLAYER:
                        remove_player(msg);
                        
            break;

                    
            case MSG_STATE_CHANGE:
                        player_state_change(msg);
                        
            break;
                    }

                    count++;    
            // increase processed message count

                    // goto next message in list
                    EnterCriticalSection(&m_msg_cs);
                    m_msg_tail = (m_msg_tail + 1) % MAX_MESSAGES;
                    LeaveCriticalSection(&m_msg_cs);
                }
            }

            As process_queue_msg iterates through the next 64 messages, it calls upon a separate
            set of functions to handle the various game messages. Those message-handling
            functions are described in the following sections.

             

            cApp::add_player

            Let’s face it—your game is going to be cool, and before long you’ll have players joining
            the game left and right. When a player joins the game (or at least tries to join), a player
            message is added to the queue, and when that message is processed, the add_player function
            is called to find room for the player. If no room exists, that player is disconnected.

            bool cApp::add_player(const sMsg* msg)
            {
                DPNID player_id = msg->header.player_id;
                DWORD size = 0;
                DPN_PLAYER_INFO* player_info = NULL;

                
            // get the player information
                HRESULT hr = m_server.get_server()->GetClientInfo(player_id, player_info, &size, 0);
                
            if(FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
                    
            return false;

                
            // allocate player data buffer and try again
                player_info = (DPN_PLAYER_INFO*) new byte[size];
                
            if(player_info == NULL)
                    
            return false;

                ZeroMemory(player_info, size);

                player_info->dwSize = 
            sizeof(DPN_PLAYER_INFO);

                
            if(FAILED(m_server.get_server()->GetClientInfo(player_id, player_info, &size, 0)))
                {
                    delete[] player_info;
                    
            return false;
                }
                
                
            // make sure not already in list
                for(long i = 0; i < MAX_PLAYERS; i++)
                {
                    
            if(m_players[i].player_id == player_id && m_players[i].connected)
                    {
                        delete[] player_info;
                        m_server.disconnect_player(player_id);
                        
            return false;
                    }
                }

                
            // search for an empty slot to put player
                for(long i = 0; i < MAX_PLAYERS; i++)
                {
                    
            if(! m_players[i].connected)
                    {
                        m_players[i].connected  = 
            true;
                        m_players[i].player_id  = player_id;
                        m_players[i].x_pos      = 0.0f;
                        m_players[i].y_pos      = 0.0f;
                        m_players[i].z_pos      = 0.0f;
                        m_players[i].direction  = 0.0f;
                        m_players[i].speed      = 512.0f;
                        m_players[i].last_state = STATE_IDLE;
                        m_players[i].latency    = 0;

                        wcstombs(m_players[i].name, player_info->pwszName, 
            sizeof(m_players[i].name));

                        
            // send add player information to all players in area

                        sCreatePlayerMsg create_msg;

                        create_msg.header.type      = MSG_CREATE_PLAYER;
                        create_msg.header.size      = 
            sizeof(sCreatePlayerMsg);
                        create_msg.header.player_id = player_id;
                        create_msg.x_pos            = m_players[i].x_pos;
                        create_msg.y_pos            = m_players[i].y_pos;
                        create_msg.z_pos            = m_players[i].z_pos;
                        create_msg.direction        = m_players[i].direction;

                        send_network_msg(&create_msg, DPNSEND_NOLOOPBACK, ALL_CLIENT_PLAYERS);

                        m_connected_player_num++;
                        list_players();
                        delete[] player_info;

                        
            return true;
                    }   
                }

                delete[] player_info;
                m_server.disconnect_player(player_id);

                
            return false;
            }

            ///////////////////////////////////////////////////////////////////////////////////////

            void cApp::list_players()
            {
                
            // clear player list
                reset_listbox(m_controls[CONTROL_PLAYER_LIST]);

                
            // count alll players and add names to list
                for(long i = 0; i < MAX_PLAYERS; i++)
                {
                    
            if(m_players[i].connected)
                        add_string_to_listbox(m_controls[CONTROL_PLAYER_LIST], m_players[i].name);
                }

                
            // display player count
                if(m_connected_player_num == 0)
                    SetWindowText(m_controls[CONTROL_PLAYER_NUM], "No Connected Players");
                
            else
                {
                    
            char text[256];
                    sprintf(text, "%lu players connected", m_connected_player_num);

                    SetWindowText(m_controls[CONTROL_PLAYER_NUM], text);
                }
            }
             

            posted on 2007-12-18 22:00 lovedday 閱讀(187) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久久久国产精品嫩草影院| 色综合久久久久综合99| 亚洲国产成人精品无码久久久久久综合| 久久综合狠狠综合久久| 久久久无码精品亚洲日韩蜜臀浪潮| 久久久久一本毛久久久| 久久有码中文字幕| 香蕉99久久国产综合精品宅男自| 日韩AV毛片精品久久久| 久久婷婷国产剧情内射白浆| 麻豆av久久av盛宴av| 久久国产色av免费看| 久久99久久99精品免视看动漫| 99久久国产综合精品麻豆| 久久se精品一区二区| 久久高潮一级毛片免费| 久久精品成人欧美大片| 成人久久综合网| 久久久精品久久久久久| 久久无码国产专区精品| 久久精品水蜜桃av综合天堂| 人人狠狠综合久久亚洲婷婷 | 久久精品亚洲乱码伦伦中文| 久久国产福利免费| 综合久久国产九一剧情麻豆| 久久婷婷五月综合色奶水99啪| 欧美激情精品久久久久| 久久一区二区免费播放| 久久久久亚洲AV成人片| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 亚洲午夜久久久| 久久久久高潮毛片免费全部播放| 久久精品国产精品青草app| 久久免费视频6| 国产美女久久精品香蕉69| 久久久久无码精品| 潮喷大喷水系列无码久久精品| 久久综合精品国产一区二区三区| 久久久精品国产sm调教网站| 精品久久国产一区二区三区香蕉 | 亚洲国产精品综合久久网络|