Tcp.php 2.7 KB
Newer Older
lzc828's avatar
lzc828 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/10/22
 * Time: 15:48
 */

namespace One\Swoole\Client;

use One\ConfigTrait;
use One\Protocol\ProtocolAbstract;
use One\Swoole\Pools;
use Swoole\Coroutine\Client;

class Tcp
{
    use ConfigTrait, Pools;

    private $key = '';

    private $config = [];

lzc828's avatar
lzc828 已提交
24 25
    private $max_retry_count = 1;

lzc828's avatar
lzc828 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    /**
     * @var ProtocolAbstract
     */
    private $protocol = null;

    public function __construct($key = 'default')
    {
        $this->setConnection($key);
    }

    public function setConnection($key)
    {
        $this->key    = $key;
        $this->config = self::$conf[$key];
        if (isset($this->config['pack_protocol'])) {
            $this->protocol = $this->config['pack_protocol'];
        }
lzc828's avatar
lzc828 已提交
43
        $this->max_retry_count = $this->config['max_connect_count'] + 3;
lzc828's avatar
lzc828 已提交
44 45 46 47 48 49 50 51 52
        return $this;
    }

    private function createRes()
    {
        $client = new \Swoole\Coroutine\Client(SWOOLE_TCP);
        if (isset($this->config['set'])) {
            $client->set($this->config['set']);
        }
lzc828's avatar
lzc828 已提交
53
        $r = $client->connect($this->config['ip'], $this->config['port'], $this->config['time_out']);
lzc828's avatar
lzc828 已提交
54 55 56 57 58 59 60
        if ($r) {
            return $client;
        } else {
            throw new \Exception('连接失败 tcp://' . $this->config['ip'] . ':' . $this->config['port']);
        }
    }

lzc828's avatar
lzc828 已提交
61
    public function call($data, $time_out = 3.0)
lzc828's avatar
lzc828 已提交
62
    {
lzc828's avatar
lzc828 已提交
63 64 65 66
        $cli = $this->pop();
        if ($this->protocol !== null) {
            $data = $this->protocol::encode($data);
        }
67 68
        $r     = @$cli->send($data);
        $retry = 0;
lzc828's avatar
lzc828 已提交
69
        if ($r === false) {
70 71 72 73 74 75 76 77 78 79
            retry:{
                do {
                    $cli->close();
                    self::$connect_count--;
                    $cli = $this->pop();
                    $r   = @$cli->send($data);
                    $retry++;
                    echo "retry\n";
                } while ($retry < $this->max_retry_count && $r === false);
            }
lzc828's avatar
lzc828 已提交
80
        }
lzc828's avatar
lzc828 已提交
81
        $ret = $cli->recv($time_out);
82 83 84
        if ($ret === '' && $retry === 0) {
            goto retry;
        }
lzc828's avatar
lzc828 已提交
85 86 87
        if ($this->protocol !== null) {
            $ret = $this->protocol::decode($ret);
        }
lzc828's avatar
lzc828 已提交
88
        $this->push($cli);
lzc828's avatar
lzc828 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        return $ret;

    }


    public function recv($time_out = 3.0)
    {
        $rs  = $this->pop();
        $ret = $rs->recv($time_out);
        if ($this->protocol !== null) {
            $ret = $this->protocol::decode($ret);
        }
        $this->push($rs);
        return $ret;
    }

    public function send($data)
    {
        $rs = $this->pop();
        if ($this->protocol !== null) {
            $data = $this->protocol::encode($data);
        }
        $ret = $rs->send($data);
        $this->push($rs);
        return $ret;
    }

}