Beispiel #1
0
function send_data()
{
    global $count, $max_send_num, $send_interval;
    global $url, $port;
    $pid = posix_getpid();
    while (1) {
        if ($max_send_num < $count) {
            $count = 1;
            sleep($send_interval);
            continue;
            //exit(0);
        }
        $client = new swoole_client(SWOOLE_TCP, SWOOLE_SOCK_ASYNC);
        $ret = $client->connect($url, $port, 0.5, 1);
        $client->send("HELLO WORLD\n");
        $clients[$client->sock] = $client;
        while (!empty($clients)) {
            $write = $error = array();
            $read = array_values($clients);
            $n = swoole_client_select($read, $write, $error, 0.6);
            if ($n > 0) {
                foreach ($read as $index => $c) {
                    echo "Recv #{$c->sock}: " . $c->recv() . "\n";
                    unset($clients[$c->sock]);
                }
            }
        }
        /*
                $fp = @stream_socket_client("tcp://{$url}:{$port}", $errno, $errstr, 30);
                if (!$fp) {
           exit(1);
                }
        
                $rand = random();
                $data = time() . "-{$rand}-{$pid}-{$count}";
                fwrite($fp, $data . "\r\n");
                lg("c: $data");
                fgets($fp, 1024);
                fclose($fp);
        
                $count++;
        */
        usleep(5000);
    }
}
Beispiel #2
0
<?php

$clients = array();
for ($i = 0; $i < 20; $i++) {
    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
    //同步阻塞
    $ret = $client->connect('127.0.0.1', 9501, 0.5, 0);
    if (!$ret) {
        echo "Connect Server fail.errCode=" . $client->errCode;
    } else {
        $client->send("HELLO WORLD\n");
        $clients[$client->sock] = $client;
    }
}
while (!empty($clients)) {
    $write = $error = array();
    $read = array_values($clients);
    $n = swoole_client_select($read, $write, $error, 0.6);
    if ($n > 0) {
        foreach ($read as $index => $c) {
            echo "Recv #{$c->sock}: " . $c->recv() . "\n";
            unset($clients[$c->sock]);
        }
    }
}
Beispiel #3
0
 protected function recvWaitForSwoole($timeout)
 {
     $st = microtime(true);
     $success_num = 0;
     while (true) {
         $write = $error = $read = array();
         if (empty($this->waitList)) {
             break;
         }
         foreach ($this->waitList as $obj) {
             if ($obj->socket !== null) {
                 $read[] = $obj->socket;
             }
         }
         if (empty($read)) {
             break;
         }
         $n = swoole_client_select($read, $write, $error, $timeout);
         if ($n > 0) {
             //可读
             foreach ($read as $sock) {
                 $id = $sock->sock;
                 /**
                  * @var $retObj SOA_Result
                  */
                 $retObj = $this->waitList[$id];
                 $data = $retObj->socket->recv();
                 //socket被关闭了
                 if (empty($data)) {
                     $retObj->code = SOA_Result::ERR_CLOSED;
                     unset($this->waitList[$id], $retObj->socket);
                     continue;
                 } else {
                     $header = unpack(SOAServer::HEADER_STRUCT, substr($data, 0, SOAServer::HEADER_SIZE));
                     //串号不一致,丢弃结果
                     if ($header['serid'] != $retObj->requestId) {
                         trigger_error(__CLASS__ . " requestId[{$retObj->requestId}]!=responseId[{$header['serid']}]", E_USER_WARNING);
                         continue;
                     }
                     //成功处理
                     $this->finish(SOAServer::decode(substr($data, SOAServer::HEADER_SIZE), $header['type']), $retObj);
                     $success_num++;
                 }
             }
         }
         //发生超时
         if (microtime(true) - $st > $timeout) {
             foreach ($this->waitList as $obj) {
                 $obj->code = $obj->socket->isConnected() ? SOA_Result::ERR_TIMEOUT : SOA_Result::ERR_CONNECT;
                 //执行after钩子函数
                 $this->afterRequest($obj);
             }
             //清空当前列表
             $this->waitList = array();
             return $success_num;
         }
     }
     //未发生任何超时
     $this->waitList = array();
     $this->requestIndex = 0;
     return $success_num;
 }