• <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 閱讀(182) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久国产精品免费一区二区三区| 久久99精品国产麻豆| 久久伊人色| 亚洲国产另类久久久精品 | 久久综合88熟人妻| 久久精品国产99国产精偷| 亚洲成av人片不卡无码久久 | 国产伊人久久| 精品久久久久久久国产潘金莲| 久久影院综合精品| 中文成人无码精品久久久不卡| 国产99久久精品一区二区| 综合久久给合久久狠狠狠97色 | 久久国产高清字幕中文| 久久伊人亚洲AV无码网站| 久久精品国产亚洲麻豆| 婷婷综合久久中文字幕蜜桃三电影| 久久精品国产亚洲Aⅴ香蕉 | 久久国产成人精品麻豆| 国内精品综合久久久40p| 久久久99精品成人片中文字幕| 久久精品一区二区三区不卡| 亚洲精品乱码久久久久久 | 久久www免费人成看片| 久久99精品国产麻豆婷婷| 久久这里只有精品久久| 精品国产VA久久久久久久冰 | 久久99精品综合国产首页| 久久精品国产亚洲AV大全| 99精品国产综合久久久久五月天| 久久中文字幕无码专区| 久久综合一区二区无码| 久久影院午夜理论片无码| 一本一本久久a久久精品综合麻豆| 久久精品亚洲精品国产欧美| a级毛片无码兔费真人久久| 国产激情久久久久影院小草 | 综合网日日天干夜夜久久| 欧美va久久久噜噜噜久久| 波多野结衣中文字幕久久| 色综合久久综精品|