• <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>
            OldJiang.com

            浩毛的博客

            OldJiang.com
            posts - 14, comments - 81, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            The Sun Game Server Architecture

                 Sun‘s Game Server technology logically is divided vertically into 3 layers: Communications, Simulation Logic, and
            Object Store.

                 The Object Store layer contains the game states for all games running in the Game Server. It is a highly efficient(tenths of a millisecond per operation), scalable, and fault-tolerant transactional database layer that provides deadlock proof access to the simulation objects, which can either be locked (a write-lock) or peeked (a nonrepeatable read).

                 The Simulation Logic layer is responsible for executing the actual game code. Here, tasks are created based on incoming events which, in turn, check objects out of the Object Store as needed. When a task is completed, the object is updated and returned to the Object Store.

                 The Communications layer organizes player communication into channels of grouped communicators. It manages routing of data packets between the players and the Simulation Logic servers, and between the players themselves. It also is responsible for translation to and from other forms of networking (e.g., HTTP communications to and from cell phones).

            posted @ 2010-06-08 14:28 浩毛 閱讀(1953) | 評(píng)論 (0)編輯 收藏

             

            1、 游戲世界由很多個(gè)游戲?qū)ο蠼M成(游戲角色、物品、NPC、技能等);

             

            2、 一個(gè)游戲?qū)ο蟮挠行?shù)據(jù)主要存放在客戶端、游戲服務(wù)器和持久性數(shù)據(jù)庫中;

             

            3、 游戲?qū)ο蟮奶幚砜蓜澐譃榕c位置有關(guān)的和與位置無關(guān)的,如公會(huì)處理、物品處理等主要行為可以看作是與位置無關(guān)的處理,而NPCAI)、戰(zhàn)斗、移動(dòng)這類的主要行為可以看成是與位置有關(guān)的。

             

            4、 從客戶端的角度來看,游戲行為可分為四類動(dòng)作:

            a)         來自服務(wù)器端的動(dòng)作,如另外一個(gè)玩家跳起來。

            b)        本地動(dòng)作。僅僅發(fā)生在本地客戶端的動(dòng)作,不需要與服務(wù)器端或其他客戶端通訊。

            c)         先執(zhí)行后驗(yàn)證的可撤銷的動(dòng)作。客戶端先執(zhí)行,再提交服務(wù)器端驗(yàn)證,驗(yàn)證不成功通知客戶端將執(zhí)行的動(dòng)作撤銷。比如玩家控制的游戲角色執(zhí)行移動(dòng)處理。

            d)        嚴(yán)格服務(wù)器端驗(yàn)證的動(dòng)作。客戶端執(zhí)行動(dòng)作前必須經(jīng)過服務(wù)器端驗(yàn)證后才能執(zhí)行。如交易行為、攻擊其他玩家/NPC

             

            5、 客戶端和服務(wù)器,服務(wù)器進(jìn)程之間的相互的通信從邏輯上看就是就是向RemoteObject 發(fā)起的遠(yuǎn)程過程調(diào)用(RPC),RPC主要有兩種類型:

            a)         通知(Notify)。只通知對(duì)方,而不關(guān)心和需要對(duì)方返回結(jié)果。

            b)        請(qǐng)求(Request)。向?qū)Ψ桨l(fā)起請(qǐng)求,對(duì)方處理請(qǐng)求后返回結(jié)果,發(fā)起請(qǐng)求和返回結(jié)果這個(gè)過程可以是同步或異步。游戲服務(wù)器中絕大部分RPC請(qǐng)求都是異步的。

             

            6、 響應(yīng)延遲主要是由于網(wǎng)絡(luò)帶寬和服務(wù)器處理效率引起的。應(yīng)盡可能的通過一些技巧來隱藏和減少玩家的響應(yīng)延遲。但不是所有的最新消息都能立刻發(fā)送出去(或接收處理到),因此,要在服務(wù)器端采用優(yōu)先隊(duì)列來減少重要消息的響應(yīng)時(shí)間。延遲也會(huì)由客戶端產(chǎn)生,如收到消息后的對(duì)消息的處理速度。

             

             

            7、 服務(wù)器負(fù)載,除了升級(jí)硬件設(shè)備外,可以通過一些方式來提高服務(wù)器負(fù)載。

             

            a)         保證足夠的網(wǎng)絡(luò)帶寬。

            b)        分布式運(yùn)算,合理的集群式架構(gòu)。

            c)         游戲策劃從游戲內(nèi)容上避免設(shè)計(jì)高并發(fā),高消耗的游戲行為。

             

             

             

            8、 從服務(wù)器的可伸縮性,穩(wěn)定性和高效率方面來考慮,要試著避免所有事情都在一個(gè)地方處理,盡量讓系統(tǒng)分布式運(yùn)行,但是過多的劃分功能到不同的進(jìn)程/機(jī)器上運(yùn)行,又會(huì)帶來數(shù)據(jù)的大量同步的問題。因此可以將游戲?qū)ο蟮奶幚碇饕獎(jiǎng)澐譃榕c位置無關(guān)和有關(guān)兩種。像公會(huì),玩家信息,物品信息,組隊(duì),拍賣等等這類與位置無關(guān)的但是占用CPU資源較少的處理可以盡可能的放在一個(gè)進(jìn)程中,避免進(jìn)程間對(duì)象同步,而像NPC,尋路,AOI運(yùn)算,戰(zhàn)斗處理等與位置有關(guān)的,處理過程中特別關(guān)心對(duì)象坐標(biāo)位置的、運(yùn)算量特別大的,但是進(jìn)程間對(duì)象同步較少的,都可以單獨(dú)劃分成多個(gè)進(jìn)程。

             

            每類進(jìn)程服務(wù)的功能盡量單一。負(fù)責(zé)路由的就盡量只負(fù)責(zé)網(wǎng)絡(luò)包轉(zhuǎn)發(fā),而不再承擔(dān)其他繁重的任務(wù),負(fù)責(zé)游戲處理的就盡量讓網(wǎng)絡(luò)包流向簡(jiǎn)單。

            posted @ 2010-04-22 16:16 浩毛 閱讀(5601) | 評(píng)論 (6)編輯 收藏

                 摘要:     上一篇內(nèi)存池的實(shí)現(xiàn)其實(shí)更像一個(gè)后備列表的實(shí)現(xiàn)。使用上來說不是很方便,要申請(qǐng)的內(nèi)存塊是一個(gè)BLOCK結(jié)構(gòu)的一個(gè)個(gè)成員,而且每次從系統(tǒng)內(nèi)存堆中申請(qǐng)都是一小塊一小塊,也沒有考慮字節(jié)對(duì)齊。因此讓我們來看看新的一個(gè)內(nèi)存池的實(shí)現(xiàn)吧。    這個(gè)內(nèi)存池是根據(jù)《c++應(yīng)用程序性能優(yōu)化》書里的固定尺寸的內(nèi)存池原理做了一些改動(dòng)用C語言寫的。大家有興趣...  閱讀全文

            posted @ 2009-09-27 14:50 浩毛 閱讀(6371) | 評(píng)論 (5)編輯 收藏

                 都知道頻繁分配內(nèi)存釋放內(nèi)存很耗系統(tǒng)資源,而且容易造成內(nèi)存碎片。因此寫了個(gè)簡(jiǎn)單的內(nèi)存池實(shí)現(xiàn),越簡(jiǎn)單越好,為什么?做復(fù)雜了效率還不如直接malloc。因此這個(gè)內(nèi)存池采用鏈表連接內(nèi)存塊的方式,分配的是固定大小的內(nèi)存塊,從池中取內(nèi)存和歸還內(nèi)存是用的空閑鏈表堆棧操作, 沒有使用線程鎖,如果要線程安全,建議在外部調(diào)用內(nèi)存池的地方加鎖。

                 做過一個(gè)簡(jiǎn)單的測(cè)試,10萬次內(nèi)存池調(diào)用的效率大概比直接分配釋放內(nèi)存提高了30-50%。但是前提是內(nèi)存池不能加鎖(pthread_mutex),加鎖的內(nèi)存池效率和直接分配內(nèi)存的效率差不多,有時(shí)候還要多點(diǎn)點(diǎn)。(測(cè)試的環(huán)境是每次2K,4個(gè)雙核CPU,F(xiàn)REEBSD7)

            代碼實(shí)現(xiàn):
            struct memblock
            {
               
            int              used;
               
            void*            data;
               
            struct memblock* next;
               
            struct memblock* createnext;
            }
            ;


            struct mempool
            {
                
            int            size;//memblock大小
                int            unused;//空閑的memblock大小
                int            datasize;//每次分配的數(shù)據(jù)大小(就是memblock.data)
                struct memblock*    free_linkhead;//空閑memblock鏈表頭
                struct memblock*    create_linkhead;//所有創(chuàng)建的memblock鏈表頭,內(nèi)存池釋放的時(shí)候使用,防止內(nèi)存池釋放的似乎還有memblock未歸還的情況
                
            };
            typedef 
            void (*free_callback)(void*);//釋放回調(diào)函數(shù),釋放membloc.data用,可以簡(jiǎn)單的直接用free函數(shù)

            void    mempool_init(int initialSize,int datasize);//初始化mempool
            void    mempool_dealloc(struct mempool* pool,free_callback callback);//釋放mempool
            void*    mempool_get(struct mempool* pool);//獲取一個(gè)memblock
            void    mempool_release(struct mempool* pool,struct memblock* block);//歸還一個(gè)memblock

            /*********************************
             * mempool
             * *****************************
            */
            //malloc一個(gè)memblock
            static struct memblock* mempool_allocblock( struct mempool* pool );

            //------------------implement--------
            void*
            mempool_init( 
            int initialSize, int datasize )
            {
                
            struct mempool* pool = malloc( sizeofstruct mempool ) );
                pool
            ->unused = 0;
                pool
            ->datasize = datasize;
                pool
            ->free_linkhead = NULL;
              
                
            //預(yù)先初始化initialSize個(gè)內(nèi)存塊
                 pool->create_linkhead = NULL;
                
            int i;
                
            for ( i = 0; i < initialSize; i++ ) {
                    
            struct memblock* block = mempool_allocblock( pool );
                    mempool_release( pool, block );
                }
                
            return ( pool );
            }

            void
            mempool_dealloc( 
            struct mempool* pool, free_callback callback )
            {
                
            struct memblock* block = NULL;
                
            //將所有創(chuàng)建的memblock釋放了
                while ( pool->create_linkhead != NULL ) {
                    block 
            = pool->create_linkhead;
                    pool
            ->create_linkhead = pool->create_linkhead->createnext;
                
            //執(zhí)行free回調(diào)。
                    if ( callback ) {
                        ( 
            *callback )( block->data );
                    }
                    free( block );
                }
                free( pool );
                L_DEBUG( 
            "%s:size(%d),unused(%d)", __func__, pool->size, pool->unused );
            }

            static struct memblock*
            mempool_allocblock( 
            struct mempool* pool )
            {
                
            struct memblock* block = malloc( sizeofstruct memblock ) );
                block
            ->data = malloc( sizeof( pool->datasize ) );
                block
            ->next = NULL;
                block
            ->used = 1;//表示已使用

                
            //加入所有創(chuàng)建的memblock的鏈表頭
                block->createnext = pool->create_linkhead;
                pool
            ->create_linkhead = block;

                pool
            ->size++;
                
            return ( block );
            }

            void
            mempool_release( 
            struct mempool* pool, struct memblock* block )
            {
                
            if ( block == NULL ) {
                    L_WARN( 
            "%s:release a NULL!", __func__ );
                    
            return;
                }
                
            if ( block->used != 1 ) {
                    L_WARN( 
            "%s:used!=1", __func__ );
                    
            return;
                }
                
            //將歸還的內(nèi)存塊放到空閑鏈表頭。
                block->used = 0;//表示空閑
                block->next = pool->free_linkhead;
                pool
            ->free_linkhead = block;
                pool
            ->unused++;//空閑數(shù)+1
            }

            void*
            mempool_get( 
            struct mempool* pool )
            {
               
                
            struct memblock* block = NULL;
                
            if ( pool->free_linkhead ) {
                
            //從空閑鏈表頭取出一個(gè)內(nèi)存塊
                    block = pool->free_linkhead;
                    pool
            ->free_linkhead = pool->free_linkhead->next;
                    block
            ->next = NULL;
                    block
            ->used = 1;//表示已使用
                    pool->unused--;//空閑內(nèi)存塊數(shù)-1
                }
                
            else {
                
            //沒有空閑的內(nèi)存塊,創(chuàng)建一個(gè)
                    block = mempool_allocblock( pool );
                }
                
            return ( block );
            }

            posted @ 2009-09-17 22:34 浩毛 閱讀(7568) | 評(píng)論 (18)編輯 收藏

            僅列出標(biāo)題
            共2頁: 1 2 
            OldJiang.com
            久久国产三级无码一区二区| 一本久久a久久精品vr综合| 国产一级持黄大片99久久| 国产人久久人人人人爽| 国产精品久久成人影院| 日韩欧美亚洲国产精品字幕久久久 | 午夜精品久久久久成人| 亚洲AV无码久久精品成人| 国产精品视频久久| 亚洲国产婷婷香蕉久久久久久| 久久AV无码精品人妻糸列| 久久亚洲国产精品一区二区| 日韩久久无码免费毛片软件 | 久久伊人色| 久久大香香蕉国产| 日韩久久久久中文字幕人妻| 久久美女网站免费| 色偷偷偷久久伊人大杳蕉| 国产精品无码久久久久| 久久久国产乱子伦精品作者| 久久精品视频一| 久久久WWW成人免费毛片| 欧美久久综合性欧美| 久久综合精品国产二区无码| 亚洲人成无码久久电影网站| 精品久久人人妻人人做精品| 亚洲国产成人久久综合碰碰动漫3d | 色青青草原桃花久久综合| 国产精品热久久无码av| 久久99热狠狠色精品一区| 国产精品国色综合久久| 亚洲中文字幕无码久久2020| 欧美午夜精品久久久久久浪潮| 99精品久久久久久久婷婷| 国产成人精品久久亚洲| 国产成人精品久久一区二区三区av | 性欧美大战久久久久久久久| 日本五月天婷久久网站| yy6080久久| 伊人久久综合精品无码AV专区| 一级A毛片免费观看久久精品|