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

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久AV高清无码| 伊人色综合久久天天人守人婷| 亚洲国产另类久久久精品小说| 亚洲精品蜜桃久久久久久| 久久香蕉国产线看观看精品yw| 日韩亚洲欧美久久久www综合网 | 亚洲国产成人久久笫一页| 亚洲精品无码久久久| 国产精品久久久久久影院| 日本精品久久久久影院日本| 嫩草伊人久久精品少妇AV| 91精品国产91久久| 亚洲精品乱码久久久久久蜜桃图片 | 99热成人精品免费久久| 久久只有这精品99| 国产亚洲成人久久| 99久久免费国产精品热| 久久91精品国产91| 久久97久久97精品免视看秋霞| 久久久久人妻一区精品性色av| 亚洲欧美日韩精品久久亚洲区| 久久亚洲高清观看| www.久久热| 日日噜噜夜夜狠狠久久丁香五月| 天天做夜夜做久久做狠狠| 精品国产综合区久久久久久| 国产精品久久久久久福利漫画| 久久精品人人做人人妻人人玩| 婷婷久久五月天| 日韩久久久久中文字幕人妻| 亚洲国产天堂久久综合网站| 国产精品久久网| 久久亚洲国产精品一区二区| 丁香狠狠色婷婷久久综合| 久久人人爽人人爽人人AV| 亚洲国产欧美国产综合久久| 久久久久亚洲av综合波多野结衣| 久久综合九色综合网站| 久久人人添人人爽添人人片牛牛| 久久久久国产精品嫩草影院| 大香伊人久久精品一区二区|