|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of workerman. |
| 4 | + * |
| 5 | + * Licensed under The MIT License |
| 6 | + * For full copyright and license information, please see the MIT-LICENSE.txt |
| 7 | + * Redistributions of files must retain the above copyright notice. |
| 8 | + * |
| 9 | + * @author walkor<walkor@workerman.net> |
| 10 | + * @copyright walkor<walkor@workerman.net> |
| 11 | + * @link http://www.workerman.net/ |
| 12 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 13 | + */ |
| 14 | +namespace Workerman\Connection; |
| 15 | + |
| 16 | +use Workerman\Events\EventInterface; |
| 17 | +use Workerman\Worker; |
| 18 | +use Exception; |
| 19 | + |
| 20 | +/** |
| 21 | + * AsyncTcpConnection. |
| 22 | + */ |
| 23 | +class AsyncTcpConnection extends TcpConnection |
| 24 | +{ |
| 25 | + /** |
| 26 | + * Emitted when socket connection is successfully established. |
| 27 | + * |
| 28 | + * @var callback |
| 29 | + */ |
| 30 | + public $onConnect = null; |
| 31 | + |
| 32 | + /** |
| 33 | + * Status. |
| 34 | + * |
| 35 | + * @var int |
| 36 | + */ |
| 37 | + protected $_status = self::STATUS_CONNECTING; |
| 38 | + |
| 39 | + /** |
| 40 | + * Remote host. |
| 41 | + * |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $_remoteHost = ''; |
| 45 | + |
| 46 | + /** |
| 47 | + * Construct. |
| 48 | + * |
| 49 | + * @param string $remote_address |
| 50 | + * @throws Exception |
| 51 | + */ |
| 52 | + public function __construct($remote_address) |
| 53 | + { |
| 54 | + list($scheme, $address) = explode(':', $remote_address, 2); |
| 55 | + if ($scheme != 'tcp') { |
| 56 | + // Get application layer protocol. |
| 57 | + $scheme = ucfirst($scheme); |
| 58 | + $this->protocol = '\\Protocols\\' . $scheme; |
| 59 | + if (!class_exists($this->protocol)) { |
| 60 | + $this->protocol = '\\Workerman\\Protocols\\' . $scheme; |
| 61 | + if (!class_exists($this->protocol)) { |
| 62 | + throw new Exception("class \\Protocols\\$scheme not exist"); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + $this->_remoteAddress = substr($address, 2); |
| 67 | + $this->_remoteHost = substr($this->_remoteAddress, 0, strrpos($this->_remoteAddress, ':')); |
| 68 | + $this->id = self::$_idRecorder++; |
| 69 | + // For statistics. |
| 70 | + self::$statistics['connection_count']++; |
| 71 | + $this->maxSendBufferSize = self::$defaultMaxSendBufferSize; |
| 72 | + } |
| 73 | + |
| 74 | + public function connect() |
| 75 | + { |
| 76 | + // Open socket connection asynchronously. |
| 77 | + $this->_socket = stream_socket_client("tcp://{$this->_remoteAddress}", $errno, $errstr, 0, |
| 78 | + STREAM_CLIENT_ASYNC_CONNECT); |
| 79 | + // If failed attempt to emit onError callback. |
| 80 | + if (!$this->_socket) { |
| 81 | + $this->_status = self::STATUS_CLOSED; |
| 82 | + $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr); |
| 83 | + return; |
| 84 | + } |
| 85 | + // Add socket to global event loop waiting connection is successfully established or faild. |
| 86 | + Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection')); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get remote address. |
| 91 | + * |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + public function getRemoteHost() |
| 95 | + { |
| 96 | + return $this->_remoteHost; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Try to emit onError callback. |
| 101 | + * |
| 102 | + * @param int $code |
| 103 | + * @param string $msg |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + protected function emitError($code, $msg) |
| 107 | + { |
| 108 | + if ($this->onError) { |
| 109 | + try { |
| 110 | + call_user_func($this->onError, $this, $code, $msg); |
| 111 | + } catch (\Exception $e) { |
| 112 | + echo $e; |
| 113 | + exit(250); |
| 114 | + } catch (\Error $e) { |
| 115 | + echo $e; |
| 116 | + exit(250); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Check connection is successfully established or faild. |
| 123 | + * |
| 124 | + * @param resource $socket |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + public function checkConnection($socket) |
| 128 | + { |
| 129 | + // Check socket state. |
| 130 | + if (stream_socket_get_name($socket, true)) { |
| 131 | + // Remove write listener. |
| 132 | + Worker::$globalEvent->del($socket, EventInterface::EV_WRITE); |
| 133 | + // Nonblocking. |
| 134 | + stream_set_blocking($socket, 0); |
| 135 | + // Try to open keepalive for tcp and disable Nagle algorithm. |
| 136 | + if (function_exists('socket_import_stream')) { |
| 137 | + $raw_socket = socket_import_stream($socket); |
| 138 | + socket_set_option($raw_socket, SOL_SOCKET, SO_KEEPALIVE, 1); |
| 139 | + socket_set_option($raw_socket, SOL_TCP, TCP_NODELAY, 1); |
| 140 | + } |
| 141 | + // Register a listener waiting read event. |
| 142 | + Worker::$globalEvent->add($socket, EventInterface::EV_READ, array($this, 'baseRead')); |
| 143 | + // There are some data waiting to send. |
| 144 | + if ($this->_sendBuffer) { |
| 145 | + Worker::$globalEvent->add($socket, EventInterface::EV_WRITE, array($this, 'baseWrite')); |
| 146 | + } |
| 147 | + $this->_status = self::STATUS_ESTABLISH; |
| 148 | + $this->_remoteAddress = stream_socket_get_name($socket, true); |
| 149 | + // Try to emit onConnect callback. |
| 150 | + if ($this->onConnect) { |
| 151 | + try { |
| 152 | + call_user_func($this->onConnect, $this); |
| 153 | + } catch (\Exception $e) { |
| 154 | + echo $e; |
| 155 | + exit(250); |
| 156 | + } catch (\Error $e) { |
| 157 | + echo $e; |
| 158 | + exit(250); |
| 159 | + } |
| 160 | + } |
| 161 | + } else { |
| 162 | + // Connection failed. |
| 163 | + $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect fail'); |
| 164 | + $this->destroy(); |
| 165 | + $this->onConnect = null; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments