Skip to content

Commit

Permalink
整理代码目录结构, 增加http,tcp协议逻辑目录
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy918 committed Jun 11, 2018
1 parent 7ed2836 commit d8bda66
Show file tree
Hide file tree
Showing 41 changed files with 357,955 additions and 9 deletions.
Empty file modified Dockerfile
100644 → 100755
Empty file.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

## 三,特性

* 对websocket协议进行封装,异步处理任务
* 实现前后端二进制封包解包,采用的是msgpack扩展,msgpack对数据进行了压缩,并实现粘包处理
* 对websocket,http,tcp协议进行封装,异步处理任务
* 实现前后端二进制封包解包,采用的是msgpack扩展,msgpack对数据进行了压缩,并实现粘包处理, 服务器加入了protobuf支付,tcp有测试例子
* 数据采用固定包头,包头4个字节存包体长度,包体前2个字节分别为cmd(主命令字),scmd(子命令字),后面为包体内容
* 采用策略模式解耦游戏中的每个协议逻辑
* 实现定义游戏开发cmd(主命令字)和scmd(子命令字)定义,cmd和scmd主要用来路由到游戏协议逻辑处理
Expand All @@ -36,7 +36,8 @@
* php 5.6
* php扩展:
* swoole 1.9以上版本
* msgpack
* msgpack
* protobuf


## 五,开始使用
Expand All @@ -49,6 +50,7 @@
./core 框架服务器核心代码
./lib 公共类库代码
./app 游戏协议逻辑处理代码
./conf 相关配置目录
./log 日志目录
```
Expand All @@ -57,9 +59,9 @@
* 2,进入bin目录,启动服务器 :

```
./poker start 启动服务
./poker stop 关闭服务
./poker reload 服务重载, 优雅载入, 不需要启动服务, 可以载入所修改的逻辑
./game start 启动服务
./game stop 关闭服务
./game reload 服务重载, 优雅载入, 不需要启动服务, 可以载入所修改的逻辑
```

Expand Down
83 changes: 83 additions & 0 deletions app/GameServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
namespace Game\App;

use Game\Core\BaseServer;
use Game\Core\GameConst;
use Game\Core\Dispatch;
use Game\Core\Packet;

/**
* websocket服务器
*/
class GameServer extends BaseServer {
/**
* 是否开启http服务监听
* @var bool
*/
protected $is_open_http = true;

/**
* 是否开启websocket监听
* @var bool
*/
protected $is_open_tcp = true;

/**
* 全局db对象
*/
protected $db = null;

/**
*
* 全局redis对象
*/
protected $redis = null;

/**
* 附件服务器初始化,例如:such as swoole atomic table or buffer 可以放置swoole的计数器,table等
*/
protected function init($serv){

}

/**
* WorkerStart时候可以调用, //require_once() 你要加载的处理方法函数等 what's you want load (such as framework init)
* 比如需要动态加载的东西,可以做到无缝重启逻辑
*/
protected function initReload($server, $worker_id) {
//自动加载, 可以通过reload更新
spl_autoload_register(function($classname) {
if(file_exists(__DIR__.'/../lib/' . $classname . '.php')) {
require_once __DIR__.'/../lib/' . $classname . '.php';
}
if(file_exists(__DIR__.'/../app/' . $classname . '.php')) {
require_once __DIR__ . '/../app/' . $classname . '.php';
}
});
}



public function initDB() {
//初始化db
//$this->db = new PdoDB(Config::getDbConf());
//$this->db->connect();
$conf = array(
'host' => '192.168.1.85',
'port' => 3308,
'user' => 'web',
'password' => 'aG98asg!($',
'database' => 'accounts_mj'
);
$this->db = new MysqlPool($conf);
$this->db = $this->db->get();
}

public function initRedis() {
//初始化redis
$redis_conf = Config::getRedisConf();
$this->redis = new \Redis();
$this->redis->connect($redis_conf['host'], $redis_conf['port']);
}
}

18 changes: 18 additions & 0 deletions app/http/HttpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Game\App\http;

use Game\Core\AStrategy;

/**
* http测试逻辑
*/

class HttpTest extends AStrategy {
/**
* 执行方法
*/
public function exec() {
//处理扣金币逻辑,暂时不处理原封不动发回去
return 'http test is ok!';
}
}
30 changes: 30 additions & 0 deletions app/tcp/TcpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Game\App\tcp;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\Lib\protobuf\Person;

/**
* 测试tcp逻辑处理
*/

class TcpTest extends AStrategy {
/**
* 执行方法
*/
public function exec() {
//解开protobuff数据,并输出
$data = $this->_params['data'];
$obj = new Person();
$obj->parseFromString($data);
echo $obj->getName() ."\n";
echo $obj->getEmail() ."\n";
echo $obj->getMoney() ."\n";
echo $obj->getId() . "\n";

//原样返回protobuf数据
$data = Packet::packEncode($data, 1, 1, 'protobuf');
return $data;
}
}
23 changes: 23 additions & 0 deletions app/websock/CancelCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;

/**
* 取消翻倍
*/

class CancelCard extends AStrategy {
/**
* 执行方法
*/
public function exec() {
//处理扣金币逻辑,暂时不处理原封不动发回去
$data = Packet::packFormat('OK', 0, $this->_params['data']);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::CANCEL_CARD_RESP);
return $data;
}
}
23 changes: 23 additions & 0 deletions app/websock/ChatMsg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;

/**
* 聊天消息回复
*/

class ChatMsg extends AStrategy {
/**
* 执行方法
*/
public function exec() {
//原封不动发回去
$data = Packet::packFormat('OK', 0, $this->_params['data']);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::CHAT_MSG_RESP);
return $data;
}
}
43 changes: 43 additions & 0 deletions app/websock/GetCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\Lib\JokerPoker;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;

/**
* 获取卡牌信息
*/

class GetCard extends AStrategy {
/**
* 执行方法
*/
public function exec() {
$data = JokerPoker::getFiveCard();
$data = Packet::packFormat('OK', 0, $data);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::GET_CARD_RESP);
// $this->testDB();
// $this->testRedis();
return $data;
}

/**
* 测试db,根据自己情况使用
*/
public function testDB() {
$sql = 'Call sp_account_get_by_uid(1000006)';
$sql_data = $this->_params['db']->queryAll($sql, '');
var_dump(__CLASS__.'=======>',$sql_data);
}

/**
* 测试redis,,根据自己情况使用
*/
public function testRedis() {
$res = $this->_params['redis']->set('test', 'aaaaaaaaceshithis 这是一个测试程序');
var_dump(__CLASS__.'=======>redis',$res);
}
}
25 changes: 25 additions & 0 deletions app/websock/GetSingerCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\Lib\JokerPoker;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;

/**
* 翻倍处理
*/

class GetSingerCard extends AStrategy {
/**
* 执行方法
*/
public function exec() {
$card = JokerPoker::getOneCard();
$data = array('card'=>$card);
$data = Packet::packFormat('OK', 0, $data);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::GET_SINGER_CARD_RESP);
return $data;
}
}
29 changes: 29 additions & 0 deletions app/websock/IsDouble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\Lib\JokerPoker;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;


/**
* 翻倍处理
*/

class IsDouble extends AStrategy {
/**
* 执行方法
*/
public function exec() {
$card = isset($this->_params['data']['card']) ? $this->_params['data']['card'] : 2; ;//明牌
$pos = isset($this->_params['data']['pos']) && ($this->_params['data']['pos'] < 4) ? intval($this->_params['data']['pos']) : 2;//我选中的位置 0123
$res = JokerPoker::getIsDoubleCard($card, $pos);
$res['bean'] = 0;
$res['bet'] = 0;
$data = Packet::packFormat('OK', 0, $res);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::IS_DOUBLE_RESP);
return $data;
}
}
23 changes: 23 additions & 0 deletions app/websock/SendCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;

/**
* 发牌信息
*/

class SendCard extends AStrategy {
/**
* 执行方法
*/
public function exec() {
//处理扣金币逻辑,暂时不处理原封不动发回去
$data = Packet::packFormat('OK', 0, $this->_params['data']);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::SEND_CARD_RESP);
return $data;
}
}
26 changes: 26 additions & 0 deletions app/websock/TurnCard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Game\App\websock;

use Game\Core\AStrategy;
use Game\Core\Packet;
use Game\Lib\JokerPoker;
use Game\conf\MainCmd;
use Game\conf\SubCmdSys;

/**
* 翻牌处理
*/

class TurnCard extends AStrategy {
/**
* 执行方法
*/
public function exec() {
$card = isset($this->_params['data']['card']) ? $this->_params['data']['card'] : array();
$card = JokerPoker::getFiveCard($card);
$res = JokerPoker::getCardType($card);
$data = Packet::packFormat('OK', 0, $res);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmdSys::TURN_CARD_RESP);
return $data;
}
}
Loading

0 comments on commit d8bda66

Please sign in to comment.