博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RPC-PHP-SDK 设计
阅读量:6351 次
发布时间:2019-06-22

本文共 5931 字,大约阅读时间需要 19 分钟。

Feature

  • 服务端可自定义方法供客户端远程调用

  • 服务端远程调用函数参数(顺序,数量)变化, 不会导致客户端服务端版本不兼容问题

  • 支持多种传输协议 (protocolbuffer, msgpack, json, serialize)

  • 支持多种通讯方式 (阻塞, 非阻塞, SSL阻塞, SSL非阻塞等)

  • 支持自定义传输协议 (引入Rpc\Protocol\Interface接口)

  • 高冷, 一定要高冷, API设计一定要高冷

API

/** * 传输相关 */// 服务端非阻塞socketclass rpc_transport_server_socket{    protected $_port; //通讯端口    final public function __construct(int $port){        $this->_port = $port;    }}// 客户端阻塞socketclass rpc_transport_client_socket{    protected $_host; //通讯地址    protected $_port; //通讯端口    final public function __construct(string $host, int $port) {        $this->_host = $host;        $this->_port = $port;    }}// 客户端非阻塞socketclass rpc_transport_async_client_socket extends rpc_transport_client_socket{    // do something}// 传输层抽象类abstract class rpc_transport{    protected $_timeout; //传输超时, 支持浮点数, 单位:sec    protected $_socket; //传输句柄    abstract public function on(string $event, mixed $callback);}// 服务端传输class rpc_transport_server extends rpc_transport{    final public function __construct(rpc_transport_server_socket $socket, float $timeout){        $this->_socket = $socket;        $this->_timeout = $timeout;    }    public static function factory(int $port, float $timeout){        $this->_socket = new rpc_transport_server_socket($port);        $this->_timeout = $timeout;    }    public function on(string $event, mixed $callback){        SERVER->on(string $event, mixed $callback);    }}// 客户端传输class rpc_transport_client extends rpc_transport{    final public function __construct(rpc_transport_client_socket $socket, float $timeout){        $this->_socket = $socket;        $this->_timeout = $timeout;    }    public static function factory(string $host, int $port, float $timeout){        $this->_socket = new rpc_transport_client_socket($host, $port);        $this->_timeout = $timeout;    }}// 客户端传输class rpc_transport_async_client extends rpc_transport{    final public function __construct(rpc_transport_async_client_socket $socket, float $timeout){        $this->_socket = $socket;        $this->_timeout = $timeout;    }    public static function factory(string $host, int $port, float $timeout){        $this->_socket = new rpc_transport_async_client_socket($host, $port);        $this->_timeout = $timeout;    }    public function on(string $event, mixed $callback){        CLIENT->on(string $event, mixed $callback);    }}/** *  协议相关 *///传输协议工厂类class rpc_protocol{    protected $_transport = null; //传输    protected $_protocol = null; //协议(protocolbuffer, msgpack, json, serialize)    private function __construct($protocol, rpc_transport $transport = NULL){        return self::factory($protocol, $transport);    }    public static function factory($protocol, rpc_transport $transport = NULL){        if(!isset($transport)) {            $this->_transport = $transport;        }        if(class_exists('rpc_protocol_' . $protocol)) {            $this->_protocol = new 'rpc_protocol_' . $protocol;        }        return $this;    }    public function getTransport(){        return $this->_transport;    }    public function setTransport($transport){        $this->_transport = $transport;    }    public function getProtocol(){        return $this->_protocol;    }    public function setProtocol($protocol){        $this->_protocol = $protocol;    }    public function pack($message) {        return $this->_protocol->pack($message);    }    public function unpack($message){        return $this->_protocol->unpack($message);    }}//传输协议接口interface rpc_protocol_interface{    public function pack($message);    public function unpack($message);}//JSON传输协议(打个样)class rpc_protocol_json implements rpc_protocol_interface{    public function pack($message){        ...    }    public function unpack($message){        ...    }}.../** * 服务端 */// 服务端抽象类abstract class rpc_server_service{    public function __call(){        // do something    }}// 服务端class rpc_server{    final public function __construct(rpc_server_interface $server_interface, rpc_protocol $protocol);}/** * 客户端 */// 客户端class rpc_client{    private $_protocol = null;    public function __call(string $method, array $parameters){        // call $server_interface    }    final public function __construct(rpc_protocol $protocol){        $this->_protocol = $protocol;    }}    //客户端回调函数抽象类abstract class rpc_async_client_callback{    private $_response = null;    public function getResult() {         // 返回结果值        return $this->_response;     }     public function onComplete($response){        $this->_response = $response;    }    public function onError($error){        throw new Exception($error);    }}// 非阻塞客户端class rpc_async_client{    private $_protocol = null;    private $_callback = null;    public function __call(string $method, array $parameters){        $callback = array_pop($parameters);        $response = $this->_protocol->getTransport()->receive();        $this->_protocol->getTransport()->on('complete', $callback->onComplete($response));        $this->_protocol->getTransport()->on('error', $callback->onError($error));    }    final public function __construct(rpc_protocol $protocol){        $this->_protocol = $protocol;    }}

Example

// serverclass server_hello extends rpc_server_service {    public function hello($message) {        echo $message;    }}$service = server_hello();$server_transport = new rpc_transport_server(8080, 0.1);$server_transport->on('connect', function(){    echo "Server:Connect.\n";});$server_protocol = new rpc_protocol::factory('json', $server_transport);$server = new rpc_server($server_protocol, $service);$server->serve();// client$client_transport = new rpc_transport_client('127.0.0.1', 8080, 0.1);$client_transport->on('connect', function(){    echo "Client:Connect.\n";});$client_protocol = new rpc_protocol::factory('json', $client_transport);$client = new rpc_client($client_protocol);$client_transport->open();$client->hello('world');$client_transport->close();

转载地址:http://cutla.baihongyu.com/

你可能感兴趣的文章
ts 格式化日期输出
查看>>
Optional
查看>>
sed 命令编辑文本
查看>>
LRUCache 具体解释
查看>>
Activity调用isDestroyed()方法报出,java.lang.NoSuchMethodError
查看>>
使用AFNetworking第三方下载类
查看>>
fhq-treap小结
查看>>
about porting
查看>>
MySQL事务及ACID特性
查看>>
Hadoop_31_MapReduce参数优化
查看>>
linux运维常见英文报错中文翻译(菜鸟必知)
查看>>
[原][osgEarth]添加自由飞行漫游器
查看>>
代码审查 Code Review
查看>>
fastjson如何指定字段不序列化
查看>>
[日常] Go语言圣经--示例: 并发的Echo服务
查看>>
BZOJ1969: [Ahoi2005]LANE 航线规划(LCT)
查看>>
linux内存管理之malloc、vmalloc、kmalloc的区别
查看>>
GreenDao 数据库升级 连接多个DB文件 或者指定不同的model&dao目录
查看>>
M1卡破解(自从学校升级系统之后,还准备在研究下)【转】
查看>>
vue 访问子组件示例 或者子元素
查看>>