资讯专栏INFORMATION COLUMN

RPC-PHP-SDK 设计

anyway / 411人阅读

摘要:服务端可自定义方法供客户端远程调用服务端远程调用函数参数顺序数量变化不会导致客户端服务端版本不兼容问题支持多种传输协议支持多种通讯方式阻塞非阻塞阻塞非阻塞等支持自定义传输协议引入接口高冷一定要高冷设计一定要高冷传输相关服务端非阻塞通讯端口

Feature

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

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

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

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

支持自定义传输协议 (引入RpcProtocolInterface接口)

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

API
/**
 * 传输相关
 */
// 服务端非阻塞socket
class rpc_transport_server_socket
{
    protected $_port; //通讯端口
    final public function __construct(int $port){
        $this->_port = $port;
    }
}
// 客户端阻塞socket
class rpc_transport_client_socket
{
    protected $_host; //通讯地址
    protected $_port; //通讯端口
    final public function __construct(string $host, int $port) {
        $this->_host = $host;
        $this->_port = $port;
    }
}
// 客户端非阻塞socket
class 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
// server
class 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.
";
});
$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.
";
});
$client_protocol = new rpc_protocol::factory("json", $client_transport);
$client = new rpc_client($client_protocol);

$client_transport->open();
$client->hello("world");
$client_transport->close();

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/21119.html

相关文章

  • 被误读的设计模式

    摘要:可以说,如果问题是我们的敌人,代码是我们的剑,设计模式就是高手心中的剑谱。中级选手,在编程的时候知道何时该用什么设计模式,而什么时候不该用。设计模式被用来简化设计,让设计更优雅。其中最具有普遍性的方案往往就是我们的设计模式的内容。 showImg(https://segmentfault.com/img/remote/1460000019100076?w=800&h=440); 目录概...

    William_Sang 评论0 收藏0
  • PHP设计模式(七):设计模式分类

    摘要:原文地址设计模式七设计模式分类根据目的和范围,设计模式可以分为五类。按照目的分为创建设计模式,结构设计模式,以及行为设计模式。与类的设计模式不同,对象设计模式主要用于运行期对象的状态改变动态行为变更等。 原文地址:PHP设计模式(七):设计模式分类 Introduction 根据目的和范围,设计模式可以分为五类。按照目的分为:创建设计模式,结构设计模式,以及行为设计模式。按照范围分为:...

    tracymac7 评论0 收藏0
  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    Dionysus_go 评论0 收藏0
  • php设计模式

    摘要:我们今天也来做一个万能遥控器设计模式适配器模式将一个类的接口转换成客户希望的另外一个接口。今天要介绍的仍然是创建型设计模式的一种建造者模式。设计模式的理论知识固然重要,但 计算机程序的思维逻辑 (54) - 剖析 Collections - 设计模式 上节我们提到,类 Collections 中大概有两类功能,第一类是对容器接口对象进行操作,第二类是返回一个容器接口对象,上节我们介绍了...

    vspiders 评论0 收藏0

发表评论

0条评论

anyway

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<