Commit 3bf2ebc1 by banice

添加发送文件

parent 3dbe344c
File added
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/10
* Time: 下午4:32
*/
namespace Application\controller\transfer;
class SendController
{
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/10
* Time: 下午4:25
*/
namespace Lib;
class Hash
{
/**
* @param $fileName
* @return string
*/
public static function hashFile($fileName)
{
return md5_file($fileName);
}
/**
* @param string $hex
* @return string
*/
public static function hashHex(string $hex)
{
return md5(hex2bin($hex));
}
public static function test()
{
echo md5_file(__DIR__.'/../HT60xx.bin');
}
}
<?php
namespace Src\request;
class Request
{
/**
* 数据帧依照map解析后的数据
*
* @var array
*/
private $parameters = [];
public function __construct(array $parameters = [])
{
$this->parameters = $parameters;
}
/**
* 获取解析后的值
*
* @param [string] $key
* @return string|null
*/
public function get($key)
{
return isset($this->parameters[$key]) ? $this->parameters[$key] : null;
}
}
\ No newline at end of file
<?php
namespace Src\response;
use Application\controller\Controller;
use Src\request\Request;
use Lib\Crc;
use Lib\Gateway;
class Response
{
/**
* 协议头
*/
const PREAMBLE = 'ffaa';
/**
* 请求实例
*
* @var Request
*/
private $request;
/**
* 请求是否加密
*
* @var string
*/
private $headerFlag = '04';
/**
* @var string
*/
private $headerCtrl = '';
/**
* 请求数据长度
*
* @var string
*/
private $length = '';
/**
* 随机字符串
* @var string
*/
private $nonce = '';
/**
* 数据体
*
* @var string
*/
private $body = '';
/**
* 响应消息
*
* @var string
*/
private $responseMessage = '';
/**
* @var string
*/
private $clientId ='';
public function __construct(string $clientId, string $headerFlag)
{
$this->clientId = $clientId;
$this->headerFlag = $headerFlag;
}
public function send(string $command, string $body)
{
$this->body = $body;
$this->headerCtrl = $command;
$this->nonce = sprintf('%02x', dechex(rand(0, 255)));
$this->length = sprintf('%04x', (8 + strlen($body) / 2));
Gateway::sendToClient($this->clientId, $this->organize());
}
private function organize()
{
if ($this->headerFlag === '05') {
//todo 加密
}
$exceptCrc = self::PREAMBLE . $this->headerFlag . $this->headerCtrl . $this->length . $this->nonce . $this->body;
$crc = substr(sprintf('%08x', Crc::crc($exceptCrc)), 0, 2);
return $this->responseMessage = $exceptCrc . $crc;
}
/**
* @return string
*/
public function getResponseMessage(): string
{
return $this->responseMessage;
}
/**
* @param string $responseMessage
*/
public function setResponseMessage(string $responseMessage): void
{
$this->responseMessage = $responseMessage;
}
}
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