• <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>

            天行健 君子當(dāng)自強(qiáng)而不息

            Getting Online with Multiplayer Gaming(11)

             

            The Message Queue

            The server never deals directly with incoming messages; instead, the server pulls
            messages from the queue. If a message needs to be processed, it must be inserted
            into the queue. Using a queue ensures that the server never gets bogged down with
            processing incoming network data.

            The queue is just an array of sMsg structures that is allocated when the application
            class is initialized. I set a limit of 1,024 messages to be allocated for the server, but you
            can change that amount just by altering the MAX_MESSAGE macro in the source code.

            To track messages being added and removed from the queue, use two variables—
            m_msg_head and m_msg_tail. Check out Figure 19.12 to see how the queue uses those
            two variables to track which messages are to be inserted or removed.

            Whenever a message needs to be added to the message queue, a special function is
            called. That function is cApp::queue_msg, and it takes a single argument: the
            sMsg structure to add to the queue.

            Remember the incoming message functions of cApp (covered in the section “DirectPlay
            Messages to Game Messages”)? Those functions built a message structure and added
            the message to the queue via queue_msg. Look at the queue_msg code to see what’s
            going on:

            bool cApp::queue_msg(const void* msg)
            {
                
            const sMsgHeader* header = (const sMsgHeader*) msg;

                
            // return if no room left in queue
                if((m_msg_head + 1) % MAX_MESSAGES == m_msg_tail)
                    
            return false;

                
            // stuff message into queue
                if(header->size <= sizeof(sMsg))
                {
                    EnterCriticalSection(&m_msg_cs);

                    memcpy(&m_msgs[m_msg_head], msg, header->size);

                    
            // goto next empty message (flip around if at end)
                    m_msg_head++;

                    
            if(m_msg_head >= MAX_MESSAGES)
                        m_msg_head = 0;

                    LeaveCriticalSection(&m_msg_cs);
                }

                
            return true;
            }

            As you can see, queue_msg merely copies the supplied sMsg structure into the
            next available element in the message array (pointed to by m_msg_head). Two things
            you haven’t seen are the EnterCriticalSection and LeaveCriticalSection functions.
            Windows uses these two functions to restrict the application’s access to memory
            (using the EnterCriticalSection function), only allowing a single process to modify
            that memory. Once you finish modifying the memory, you need to inform
            Windows by calling LeaveCriticalSection.

            Although this may not make sense at first, think about it like this—the network
            component (a process) is running in the background at the same time as the application
            (another process). If the network component is adding messages to the
            array while the application is trying to remove messages or modify the messages,
            the program data can become corrupt. Critical sections ensure that only one
            process gets sole access to data for a short time.

            posted on 2007-12-18 21:25 lovedday 閱讀(167) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(lèi)(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評(píng)論

            四虎国产永久免费久久| 久久无码专区国产精品发布 | 亚洲精品国产第一综合99久久| 久久久久亚洲精品中文字幕| 久久亚洲日韩看片无码| 久久香蕉国产线看观看乱码| 久久国内免费视频| 久久久久夜夜夜精品国产| 中文字幕精品无码久久久久久3D日动漫 | 亚洲欧美日韩久久精品| a高清免费毛片久久| 大香伊人久久精品一区二区| 国产 亚洲 欧美 另类 久久 | 香蕉99久久国产综合精品宅男自| 性欧美丰满熟妇XXXX性久久久| 欧美久久综合性欧美| 一本色道久久88精品综合| 精品国产青草久久久久福利| 亚洲精品美女久久久久99| 国产69精品久久久久观看软件| 久久青青草原综合伊人| 97r久久精品国产99国产精| 久久久久久伊人高潮影院| 亚洲精品NV久久久久久久久久 | 人妻精品久久无码区| 久久无码专区国产精品发布| 婷婷久久综合九色综合绿巨人 | 国产精品久久精品| 久久精品www人人爽人人| 精品国产青草久久久久福利| 久久国产亚洲精品| 久久亚洲国产精品成人AV秋霞| 久久久久亚洲AV成人网| 久久99精品久久久久久水蜜桃| 丰满少妇人妻久久久久久4| 亚洲欧美日韩精品久久| 久久九色综合九色99伊人| 久久国产精品免费一区| 亚洲精品WWW久久久久久| 久久精品中文字幕一区| 久久天堂AV综合合色蜜桃网 |