connect() публичный метод

Do connect.
public connect ( ) : void
Результат void
Пример #1
0
            // 当客户端发来数据时,加密数据,并发给远程服务端
            $connection->onMessage = function ($connection, $data) {
                $connection->opposite->send($connection->encryptor->encrypt($data));
            };
            // 当客户端关闭连接时,关闭远程服务端的连接
            $connection->onClose = function ($connection) {
                $connection->opposite->close();
                $connection->opposite = null;
            };
            // 当客户端连接上有错误时,关闭远程服务端连接
            $connection->onError = function ($connection, $code, $msg) {
                echo "connection err code:{$code} msg:{$msg}\n";
                $connection->close();
                if (isset($connection->opposite)) {
                    $connection->opposite->close();
                }
            };
            // 执行远程连接
            $remote_connection->connect();
            // 改变当前连接的状态为STAGE_STREAM,即开始转发数据流
            $connection->state = STAGE_STREAM;
            //转发首个数据包,包含由客户端封装的目标地址,端口号等信息
            $buffer = substr($buffer, 3);
            $buffer = $connection->encryptor->encrypt($buffer);
            $remote_connection->send($buffer);
    }
};
// 如果不是在根目录启动,则运行runAll方法
if (!defined('GLOBAL_START')) {
    Worker::runAll();
}
Пример #2
0
 /**
  * 检查gateway的通信端口是否都已经连
  * 如果有未连接的端口,则尝试连接
  * @return void
  */
 public function checkGatewayConnections()
 {
     $key = 'GLOBAL_GATEWAY_ADDRESS';
     $addresses_list = Store::instance('gateway')->get($key);
     if (empty($addresses_list)) {
         return;
     }
     foreach ($addresses_list as $addr) {
         if (!isset($this->gatewayConnections[$addr]) && !isset($this->_connectingGatewayAddress[$addr]) && !isset($this->_badGatewayAddress[$addr])) {
             $gateway_connection = new AsyncTcpConnection("GatewayProtocol://{$addr}");
             $gateway_connection->remoteAddress = $addr;
             $gateway_connection->onConnect = array($this, 'onConnectGateway');
             $gateway_connection->onMessage = array($this, 'onGatewayMessage');
             $gateway_connection->onClose = array($this, 'onClose');
             $gateway_connection->onError = array($this, 'onError');
             if (TcpConnection::$defaultMaxSendBufferSize == $gateway_connection->maxSendBufferSize) {
                 $gateway_connection->maxSendBufferSize = 10 * 1024 * 1024;
             }
             $gateway_connection->connect();
             $this->_connectingGatewayAddress[$addr] = 1;
         }
     }
 }
 /**
  * 尝试连接Gateway内部通讯地址
  * @return void
  */
 public function tryToConnectGateway($addr)
 {
     if (!isset($this->gatewayConnections[$addr]) && !isset($this->_connectingGatewayAddresses[$addr]) && isset($this->_gatewayAddresses[$addr])) {
         $gateway_connection = new AsyncTcpConnection("GatewayProtocol://{$addr}");
         $gateway_connection->remoteAddress = $addr;
         $gateway_connection->onConnect = array($this, 'onConnectGateway');
         $gateway_connection->onMessage = array($this, 'onGatewayMessage');
         $gateway_connection->onClose = array($this, 'onGatewayClose');
         $gateway_connection->onError = array($this, 'onGatewayError');
         if (TcpConnection::$defaultMaxSendBufferSize == $gateway_connection->maxSendBufferSize) {
             $gateway_connection->maxSendBufferSize = 50 * 1024 * 1024;
         }
         $gateway_data = GatewayProtocol::$empty;
         $gateway_data['cmd'] = GatewayProtocol::CMD_WORKER_CONNECT;
         $gateway_connection->send($gateway_data);
         $gateway_connection->connect();
         $this->_connectingGatewayAddresses[$addr] = $addr;
     }
     unset($this->_waitingConnectGatewayAddresses[$addr]);
 }