7

Feature

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

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

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

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

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

  • 高冷, 一定要高冷, 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.\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();

丁靖
619 声望121 粉丝

好好学习, 天天编码.