Commit cd7304ae by BANice

bintohex的协议对粘包进行处理,当缓存区中的数据长度大于等于第一个头中的length字段,返回lenght字段,小于则继续接受

parent 1ed38de1
...@@ -5,6 +5,12 @@ GATEWAY_COUNT=1 ...@@ -5,6 +5,12 @@ GATEWAY_COUNT=1
GATEWAY_NAME=cdz GATEWAY_NAME=cdz
REGISTER_PORT=1239 REGISTER_PORT=1239
REGISTER_IP=127.0.0.1 REGISTER_IP=127.0.0.1
#REGISTER_IP=cdzregister.default.svc.cluster.local
REDIS_HOST=127.0.0.1 REDIS_HOST=127.0.0.1
REDIS_PORT=6379 REDIS_PORT=6379
START_FILES=start_businessworker START_FILES=start_businessworker
\ No newline at end of file DB_USE=cdz
DB_NAME=cdzpay
DB_HOST=127.0.0.1
DB_PORT=6379
DB_PASSWORD=senthink123
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" serverName="cdz">
<serverData>
<paths name="cdz">
<serverdata>
<mappings>
<mapping deploy="/cdzbusiness" local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="WebServers">
<option name="servers">
<webServer id="2c45e911-5d40-4ae3-a0f2-38159dc8dcb3" name="cdz" url="http://hub.senthink.com">
<fileTransfer anonymous="true" host="hub.senthink.com" port="21">
<advancedOptions>
<advancedOptions compatibilityMode="true" dataProtectionLevel="Private" passiveMode="true" />
</advancedOptions>
<option name="port" value="21" />
</fileTransfer>
</webServer>
</option>
</component>
</project>
\ No newline at end of file
...@@ -39,6 +39,9 @@ class Controller ...@@ -39,6 +39,9 @@ class Controller
protected $message; protected $message;
/**
* @var Request
*/
protected $request; protected $request;
/** /**
...@@ -57,6 +60,9 @@ class Controller ...@@ -57,6 +60,9 @@ class Controller
protected $logRecords = []; protected $logRecords = [];
/**
* @var array
*/
protected $defaultHeader = [ protected $defaultHeader = [
'preamble' => 2, 'preamble' => 2,
...@@ -102,6 +108,12 @@ class Controller ...@@ -102,6 +108,12 @@ class Controller
*/ */
protected $response; protected $response;
/**
* @var int
*/
protected $plc = 0;
/** /**
* *
* Controller constructor. * Controller constructor.
...@@ -130,6 +142,9 @@ class Controller ...@@ -130,6 +142,9 @@ class Controller
} }
/**
* todo 一次性写入日志
*/
public function __destruct() public function __destruct()
{ {
if (count($this->logRecords) != 0) { if (count($this->logRecords) != 0) {
...@@ -157,12 +172,16 @@ class Controller ...@@ -157,12 +172,16 @@ class Controller
$parameters[$key] = substr($data, $start, $length); $parameters[$key] = substr($data, $start, $length);
if (property_exists($this, $key)) { if (property_exists($this, $key)) {
$this->$key = $parameters[$key]; $this->$key = $parameters[$key];
} }
$start += $length; $start += $length;
$this->plc +=$length;
} }
return $parameters; return $parameters;
...@@ -274,8 +293,12 @@ class Controller ...@@ -274,8 +293,12 @@ class Controller
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
//记录日志
if (in_array($name, ['debug', 'error', 'warn', 'alert'])) { if (in_array($name, ['debug', 'error', 'warn', 'alert'])) {
if (count($arguments) === 2) { if (count($arguments) === 2) {
$logRecord = new LogRecord($name, $arguments[0]); $logRecord = new LogRecord($name, $arguments[0]);
} else { } else {
...@@ -301,4 +324,22 @@ class Controller ...@@ -301,4 +324,22 @@ class Controller
return $this->request->get($key); return $this->request->get($key);
} }
/**
* @return int
*/
public function getPlc(): int
{
return $this->plc;
}
/**
* @param int $plc
*/
public function setPlc(int $plc): void
{
$this->plc = $plc;
}
} }
\ No newline at end of file
...@@ -19,10 +19,19 @@ class AccessController extends Controller ...@@ -19,10 +19,19 @@ class AccessController extends Controller
'02' => [ '02' => [
'mac' => 6 'mac' => 6
] ],
'03' => [
'start_num' => 1,
'count' => 1,
'total_count' => 1
],
]; ];
/**
* 网关请求入网
* command 01
*/
public function access() public function access()
{ {
if($_SESSION['certified'] === true) if($_SESSION['certified'] === true)
...@@ -34,10 +43,60 @@ class AccessController extends Controller ...@@ -34,10 +43,60 @@ class AccessController extends Controller
Gateway::closeClient($this->clientId); Gateway::closeClient($this->clientId);
} }
} }
public function syncPort()
{
$startLogicNum = $this->get('start_num');
$count = $this->get('count');
$totalCount = $this->get('total_count');
$portMacsState = substr($this->message,$this->plc);
var_dump($portMacsState);
}
}
}
\ No newline at end of file
...@@ -38,8 +38,6 @@ class Events ...@@ -38,8 +38,6 @@ class Events
static::$redis->pconnect(Env::get('REDIS_HOST'), Env::get('REDIS_PORT'), 0); static::$redis->pconnect(Env::get('REDIS_HOST'), Env::get('REDIS_PORT'), 0);
//static::$redis->pconnect('redis',Env::get('REDIS_PORT'),0);
if (Env::has('REDIS_PASSWORD')) { if (Env::has('REDIS_PASSWORD')) {
var_dump(Env::get('REDIS_PASSWORD')); var_dump(Env::get('REDIS_PASSWORD'));
...@@ -49,7 +47,7 @@ class Events ...@@ -49,7 +47,7 @@ class Events
static::$redis->select(4); static::$redis->select(4);
// static::$db = new Connection(Env::get('DB_HOST'), Env::get('DB_PORT'), Env::get('DB_USERNAME'), Env::get('DB_PASSWORD'), Env::get('DB_DATABASE')); static::$db = new Connection(Env::get('DB_HOST'), Env::get('DB_PORT'), Env::get('DB_USER'), Env::get('DB_PASSWORD'), Env::get('DB_NAME'));
} }
...@@ -85,6 +83,9 @@ class Events ...@@ -85,6 +83,9 @@ class Events
$controllerName = APP.$controllerName; $controllerName = APP.$controllerName;
//去除crc字节
$message = substr($message,0,-2);
$controller = new $controllerName($clientId,$message,$command); $controller = new $controllerName($clientId,$message,$command);
$controller->$method(); $controller->$method();
......
<?php
/**
* Created by PhpStorm.
* User: suhangyang
* Date: 2017/12/2
* Time: 下午12:57
*/
namespace Application;
//use Applications\messagePush\Events;
use Workerman\MySQL\Connection;
class Model
{
/**
* @var Connection
*/
protected $db;
protected $table;
public function __construct()
{
$this->db = \Events::getDb();
}
public function getColumn($column,$where)
{
return $this->db->single("SELECT $column FROM $this->table WHERE $where");
}
public function getColumns(Array $columns,$where)
{
$columns = implode(',',$columns);
return $this->db->row("SELECT $columns FROM $this->table WHERE $where");
}
public function getRow($where)
{
return $this->db->row("SELECT * FROM $this->table WHERE $where");
}
public function insertRow(Array $data)
{
return $this->db->insert($this->table)->cols($data)->query();
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/5/20
* Time: 下午3:10
*/
namespace Application\Model;
use Application\Model;
class GatewayModel extends Model
{
protected $table = 'gateway';
}
\ No newline at end of file
...@@ -13,5 +13,4 @@ class RedisKey ...@@ -13,5 +13,4 @@ class RedisKey
{ {
} }
\ No newline at end of file
...@@ -23,6 +23,7 @@ class Api ...@@ -23,6 +23,7 @@ class Api
static::add('02','AccessController@access'); static::add('02','AccessController@access');
static::add('03','AccessController@syncPort');
} }
......
...@@ -25,10 +25,14 @@ class Bintohex ...@@ -25,10 +25,14 @@ class Bintohex
$data = $data[1]; $data = $data[1];
var_dump(['message' => $data]);
$packageLength = strlen($data); $packageLength = strlen($data);
$packageLength/=2; $packageLength/=2;
echo 'length' .$packageLength.PHP_EOL;
//小于最小的头长度继续等待 //小于最小的头长度继续等待
if($packageLength < self::HEAD_LENGTH) if($packageLength < self::HEAD_LENGTH)
{ {
...@@ -46,16 +50,17 @@ class Bintohex ...@@ -46,16 +50,17 @@ class Bintohex
$hexCount = hexdec(substr($data,8,4)); $hexCount = hexdec(substr($data,8,4));
if($hexCount == $packageLength) if($packageLength >= $hexCount)
{ {
return $hexCount; return $hexCount;
}else{ }else{
$tcpConnection->close(); return 0;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment