Skip to content

Commit

Permalink
增加通过配置文件配置服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy918 committed Jun 13, 2018
1 parent 06902dc commit f1d141d
Show file tree
Hide file tree
Showing 5 changed files with 1,176 additions and 33 deletions.
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# swoole-game

* 基于Swoole扩展开发游戏服务器框架
* 基于Swoole扩展开游戏服务器框架

## 一,概述
### 一,概述

* 该框架主要用于开发游戏服务器,简化游戏前后端开发,框架主要实现了前后端,封包解包,协议解析,压缩,粘包,路由等功能,代码示例为h5游戏。
* 框架比较简单, 几乎不依赖外部源码库, 方便阅读和使用。
* 基本时间框架功能, 框架组件还不完善, 自己用到什么组件可以方便集成进来。

## 二,示例图
### 二,示例图

![游戏demo1](images/demo1.jpg)
![游戏demo2](images/demo2.jpg)
Expand All @@ -16,7 +18,7 @@



## 三,特性
### 三,特性

* 对websocket,http,tcp协议进行封装,异步处理任务
* 实现前后端二进制封包解包,采用的是msgpack扩展,msgpack对数据进行了压缩,并实现粘包处理, 服务器加入了protobuf支付,tcp有测试例子
Expand All @@ -28,34 +30,31 @@
* 可以方便的把JokerPoker范例去除,只使用框架功能定制开发开发自己的游戏功能


备注:注意此范例数字都是默认值, 未用到存储, 运行不需要数据库,数据库配置只是测试操作数据库用
> 备注:注意此范例数字都是默认值, 未用到存储, 运行不需要数据库,数据库配置只是测试操作数据库用

## 四,环境依赖,需要支持高版本swoole, 需要修改一下代码
### 四,环境依赖,需要支持高版本swoole, 环境可以自行编译, 为了方便大家使用, 可以使用根目录提供的Dockerfile文件构建环境

* php 5.6
* php扩展:
* php 7.1及以上
* swoole 1.9以上版本
* msgpack
* protobuf
* msgpack
* protobuf 选择使用


## 五,开始使用
### 五,开始使用

* 1,目录说明:

```
./bin 服务启动目录
./client 客户端交互测试工具
./core 框架服务器核心代码
./lib 公共类库代码
./app 游戏协议逻辑处理代码
./conf 相关配置目录
./log 日志目录
./client 客户端交互测试工具
```

* 2,进入bin目录,启动服务器 :

```
Expand All @@ -76,6 +75,7 @@ php run.php

```
http://192.168.7.197:9503/HttpTest?aaa=bbb&ccc=ddd
http://192.168.7.197:9501/HttpTest?aaa=bbb&ccc=ddd
```

Expand All @@ -87,18 +87,15 @@ php tcp_client.php
```



* 3 ,H5游戏客户端代码由于公司限制,暂不开放, 但是提供了一个客户端交互测试工具,直接把client目录放入web服务器, 修改客服端配置文件配置websocket链接就能运行。


## 六,联系方式
### 六,联系方式

* qq:251413215

## 七,备注
### 七,备注

* 根目录增加docker运行环境, 可以直接直接执行下面的命令,创建镜像php_swoole,环境增加php-protobuf支持
* 根目录增加docker运行环境(Dockerfile), 可以直接直接执行下面的命令,创建镜像php_swoole, 环境增加php-protobuf支持

```
docker build -t php_swoole .
Expand Down
6 changes: 4 additions & 2 deletions bin/run.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
date_default_timezone_set('Asia/Shanghai');
header("Content-type: text/html; charset=utf-8");
header("Content-type: text/html; charset=utf-8");

use Game\Conf\Config;
use \Game\App\GameServer;

require __DIR__ .'/../vendor/autoload.php';

GameServer::getInstance()->initServer()->start();
$config = Config::getPortConf();
GameServer::getInstance()->initServer($config)->start();
76 changes: 76 additions & 0 deletions conf/Config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,85 @@
<?php
namespace Game\Conf;

use Game\Core\GameConst;
/**
* 系统配置文件, 可以通过增加方法的形式载入
*/
class Config {
/**
* 监听服务器配置,可以通过配置来设置参数
* @return mixed
*/
public static function getPortConf() {
$config = array(
array(
'port'=>array(
'socket_type' => GameConst::GM_PROTOCOL_WEBSOCK,
'socket_name' => GameConst::GM_SERVER_IP,
'socket_port' => GameConst::GM_PROTOCOL_WEBSOCK_PORT,
),
'set'=>array(
'dispatch_mode' => 3,
'open_length_check' => 1,
'package_length_type' => 'N',
'package_length_offset' => 0,
'package_body_offset' => 4,

'package_max_length' => 2097152, // 1024 * 1024 * 2,
'buffer_output_size' => 3145728, //1024 * 1024 * 3,
'pipe_buffer_size' => 33554432, // 1024 * 1024 * 32,

'heartbeat_check_interval' => 30,
'heartbeat_idle_time' => 60,

// 'open_cpu_affinity' => 1,

// 'reactor_num' => 32,//建议设置为CPU核数 x 2 新版会自动设置 cpu个数
'max_conn'=>2000,
'worker_num' => 2,
'task_worker_num' => 4,//生产环境请加大,建议1000

'max_request' => 0, //必须设置为0,否则会导致并发任务超时,don't change this number
'task_max_request' => 2000,

// 'daemonize'=>1,
// 'log_level' => 2, //swoole 日志级别 Info
'backlog' => 3000,
'log_file' => '../log/sw_server.log',//swoole 系统日志,任何代码内echo都会在这里输出
// 'task_tmpdir' => '/dev/shm/swtask/',//task 投递内容过长时,会临时保存在这里,请将tmp设置使用内存

// 'document_root' => '/data/web/test/myswoole/poker/client',
// 'enable_static_handler' => true,
)
),
array(
'port'=>array(
'socket_type' => GameConst::GM_PROTOCOL_TCP,
'socket_name' => GameConst::GM_SERVER_IP,
'socket_port' => GameConst::GM_PROTOCOL_TCP_PORT,
),
'set'=>array(

),
),
array(
'port'=>array(
'socket_type' => GameConst::GM_PROTOCOL_HTTP,
'socket_name' => GameConst::GM_SERVER_IP,
'socket_port' => GameConst::GM_PROTOCOL_HTTP_PORT,
),
'set'=>array(

),
),
);
return $config;
}

/**
* 获取db配置
* @return array
*/
public static function getDbConf() {
//db配置,需要用时可以启用
$db = array(
Expand Down
102 changes: 91 additions & 11 deletions core/BaseServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,45 @@ abstract class BaseServer {
*/
protected $is_open_http = false;

/**
* ws服务器ip
* @var string
*/
protected $ws_ip = GameConst::GM_SERVER_IP;

/**
* ws服务器端口
* @var int
*/
protected $ws_port = GameConst::GM_PROTOCOL_WEBSOCK_PORT;

/**
* http服务器ip
* @var string
*/
protected $http_ip = GameConst::GM_SERVER_IP;
/**
* http服务器端口
* @var int
*/
protected $http_port = GameConst::GM_PROTOCOL_HTTP_PORT;

/**
* tcp服务器ip
* @var string
*/
protected $tcp_ip = GameConst::GM_SERVER_IP;

/**
* tcp服务器端口
* @var int
*/
protected $tcp_port = GameConst::GM_PROTOCOL_TCP_PORT;

/**
* 服务器默认配置
* ws服务器默认配置
*/
protected $config = array(
protected $ws_config = array(
'dispatch_mode' => 3,
'open_length_check' => 1,
'package_length_type' => 'N',
Expand Down Expand Up @@ -104,7 +139,9 @@ public static function getInstance() {
* @return null
*/
public function setTcpConf($config = array()) {
$this->tcp_config = $config;
if(!emtpy($config)) {
$this->tcp_config = $config;
}
return self::$_instance;
}

Expand All @@ -114,7 +151,9 @@ public function setTcpConf($config = array()) {
* @return null
*/
public function setHttpConf($config = array()) {
$this->http_config = $config;
if(!empty($config)) {
$this->http_config = $config;
}
return self::$_instance;
}

Expand All @@ -124,26 +163,33 @@ public function setHttpConf($config = array()) {
* @return null
*/
public function setWebsockConf($config = array()) {
$this->config = $config;
if(!empty($config)) {
$this->config = $config;
}
return self::$_instance;
}

/**
* 初始化服务器
* 初始化服务器,需要支持ssl,自行修改一下
*/
public function initServer() {
public function initServer($config = array()) {
$this->initConfig($config);
//开启websocket服务器
$this->server = new \Swoole\Websocket\Server(GameConst::GM_SERVER_IP, GameConst::GM_PROTOCOL_WEBSOCK_PORT);
$this->server->set($this->config);
$this->server = new \Swoole\Websocket\Server($this->ws_ip, $this->ws_port);
$this->server->set($this->ws_config);
//如果http端口有设置, 将开启http协议
if($this->is_open_http) {
//http server
$httpserver = $this->server->listen(GameConst::GM_SERVER_IP, GameConst::GM_PROTOCOL_HTTP_PORT, SWOOLE_SOCK_TCP);
$httpserver = $this->server->listen($this->http_ip, $this->http_port, SWOOLE_SOCK_TCP);
if(!empty($this->http_config)) {
$httpserver->set($this->http_config);
}
}

//如果tcp端口有设置, 将开启tcp协议
if($this->is_open_tcp) {
//tcp server
$tcpserver = $this->server->listen(GameConst::GM_SERVER_IP, GameConst::GM_PROTOCOL_TCP_PORT, SWOOLE_SOCK_TCP);
$tcpserver = $this->server->listen($this->tcp_ip, $this->tcp_port, SWOOLE_SOCK_TCP);
$tcpserver->on('Receive', array($this, 'onReceive'));
$tcpserver->set($this->tcp_config);
}
Expand All @@ -166,6 +212,40 @@ public function initServer() {
return self::$_instance;
}

/**
* 如果服务器有配置端口, 走配置端口
* @param $config
*/
protected function initConfig($config = array()) {
//服务器设置
if(!empty($config)) {
//设置服务器参数
foreach($config as $k=>$v) {
if(isset($v['port']['socket_type']) && $v['port']['socket_type'] == GameConst::GM_PROTOCOL_WEBSOCK) {
//设置websocket
if(isset($v['port']['socket_name'])) $this->ws_ip = $v['port']['socket_name'];
if(isset($v['port']['socket_port'])) $this->ws_port = $v['port']['socket_port'];
//设置配置
if(isset($v['set'])) $this->ws_config = $v['set'];
} elseif(isset($v['port']['socket_type']) && $v['port']['socket_type'] == GameConst::GM_PROTOCOL_HTTP) {
//设置http
if(isset($v['port']['socket_name'])) $this->http_ip = $v['port']['socket_name'];
if(isset($v['port']['socket_port'])) $this->http_port = $v['port']['socket_port'];
//设置配置
if(isset($v['set'])) $this->http_config = $v['set'];
$this->is_open_http = true;
} elseif(isset($v['port']['socket_type']) && $v['port']['socket_type'] == GameConst::GM_PROTOCOL_TCP) {
//设置tcp
if(isset($v['port']['socket_name'])) $this->tcp_ip = $v['port']['socket_name'];
if(isset($v['port']['socket_port'])) $this->tcp_port = $v['port']['socket_port'];
//设置配置
if(isset($v['set'])) $this->tcp_config = $v['set'];
$this->is_open_tcp = true;
}
}
}
}

/**
* 附件服务器初始化,例如:such as swoole atomic table or buffer 可以放置swoole的计数器,table等
*/
Expand Down
Loading

0 comments on commit f1d141d

Please sign in to comment.