遊戲伺服器程序

2021-10-08 04:11:12 字數 2767 閱讀 4499

遊戲伺服器中的程序就是前文遊戲伺服器架構中所述的各類伺服器程序了,這類程序一般採用單例設計模式,裡面會包含乙個main函式,是程序主程式的入口。在main函式中會做一些初始化操作,比如初始化日誌,初始化一些全域性引數等,然後再去執行init函式,主要的初始化工作其實會在這個init函式中執行。遊戲伺服器程序各個類設計繼承層次比較多,一種設計方案如下所示:

class service

public:

static service *instance() //連線池大小

virtual const int get_pool_state() const //連線池狀態

virtual ~netservice() {

_instance = null;

protected:

service(const string &name) : service(name) {

_net_instance = this;

_net_name = name;

_tcp_server = null;

bool init();

bool  callback();

void final(); 

private:

static netservice *_net_instance;

string _net_name; //伺服器程序名稱

tcpserver *_tcp_server;//tcpserver類主要封裝了伺服器監聽模組,用於建立乙個伺服器物件,等待客戶端的連線

netservice *netservice::_net_instance = null;

netservice這個類主要用於偵聽連線的。

class subnetservice : public netservice

public:

static subnetservice *subnetserviceinstance() {

return _sub_net_instance ;

virtual ~subnetservice () {

_sub_net_instance = null;

vitual bool parse_super_cmd() = 0;

bool send_cmd_to_super_cmd();

const int get_server_id() const { //伺服器編號

return _server_id;

const int get_server_type() const { //伺服器型別

return _server_type;

protected:

subnetservice(const string &name, int type) : _name(name),_server_type(type) {

_sub_net_instance = this;

bool init();

void final();

int _server_id;

int _server_type;

char _str_name[100];

char _str_ip[100]; //內網位址

int _port; //內網埠

int _ext_str_ip[100]; //外網位址

int _ext_port; //外網埠

private:

static subnetservice *_sub_net_instance ;

int _super_port; //伺服器管理器埠

char _super_ip[100]; //伺服器管理器位址

superclient *_super_client; //伺服器管理器客戶端例項

subnetservice *subnetservice ::_sub_net_instance = null;

這個類主要用於與伺服器管理器之間建立連線。

class processservice : public subnetservice //processservice代表各類伺服器程序

public:

static processservice &processserviceinstance() {

if (null == _process_instance)

_process_instance = new processservice();

return *_process_instance ;

virtual ~processservice () {

_process_instance = null;

vitual bool parse_super_cmd() = 0;

bool send_cmd_to_super_cmd();

//各類指向資料庫連線池例項的指標

private:

static processservice *_process_instance ;

tcptaskpool *_task_pool; //tcp連線池

bool init();

void final();

void new_tcp_task();

processservice () : subnetservice("各類伺服器程序", type) {

_task_pool= null;

processservice *processservice ::_process_instance = null;

這個類就是文章一開始所述的遊戲伺服器了,它往上繼承層次有3層。

mysql 做遊戲伺服器配置 遊戲伺服器部署

bin bash 小菜鳥 掛機 版本 1.0 遊戲伺服器部署 基礎環境 mysql資料庫svn客戶端需部署才能執行此指令碼 if uid 0 then echo game server install else exit 1 firead p please create the storage ga...

mysql遊戲伺服器快取 遊戲伺服器快取策略

1 什麼是快取 在資料庫與伺服器邏輯之間加入的資料層 2 作用 減少資料庫操作 伺服器使用mysql作為資料庫,mysql每秒鐘併發數量有限,所以我們要減少mysql的操作。3 erlang的快取 erlang 在記憶體中可用 程序字典 gen state ets 儲存變數,理論上三種方式都可以作為...

遊戲伺服器架構

登陸伺服器判斷賬戶合法性,如果合法的話,把session資訊寫入memcache,閘道器伺服器收到玩家連線請求後,在memcache裡查詢是否合法玩家,防止非法連線。閘道器伺服器要管理玩家連線,需要高併發,可以開多個 scene mgr純粹的 訊息功能 資料庫伺服器純粹的查詢修改資料功能,如果成為瓶...