ARCEmu項目wow私服伺服器網路模組分析(三)

來源:互聯網
上載者:User

看過network代碼的筒子,會發現類定義的時候,經常出現一個Singleton。

為啥要單例啊,這讓我們苦比的中國人情何以堪。

Singleton定義如下:

template < class type > class SERVER_DECL Singleton{public:/// ConstructorSingleton(){/// If you hit this assert, this singleton already exists -- you can't create another one!ASSERT(this->mSingleton == 0);this->mSingleton = static_cast<type*>(this);}/// Destructorvirtual ~Singleton(){this->mSingleton = 0;}ARCEMU_INLINE static type & getSingleton() { ASSERT(mSingleton); return *mSingleton; }ARCEMU_INLINE static type* getSingletonPtr() { return mSingleton; }protected:/// Singleton pointer, must be set to 0 prior to creating the objectstatic type* mSingleton;};

人們常說知道設計模式,但不能濫用設計模式啊。

不過這裡單例用的,恰到好處,不愧是10年磨一劍。

來看看單例的所到之處:

class SERVER_DECL SocketMgr : public Singleton<SocketMgr>{public:SocketMgr();~SocketMgr();ARCEMU_INLINE HANDLE GetCompletionPort() { return m_completionPort; }void SpawnWorkerThreads();void CloseAll();void ShowStatus();void AddSocket(Socket* s){socketLock.Acquire();_sockets.insert(s);++socket_count;socketLock.Release();}void RemoveSocket(Socket* s){socketLock.Acquire();_sockets.erase(s);--socket_count;socketLock.Release();}void ShutdownThreads();long threadcount;private:HANDLE m_completionPort;set<Socket*> _sockets;Mutex socketLock;Arcemu::Threading::AtomicCounter socket_count;};

這些代碼看起來真舒服啊。

設計模式,依據項目環境,而是不因為設計而設計,論語有“過猶不及”。

繼續下篇吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.