/web/thrift/lib/transport/TSocket.php
https://github.com/lenn0x/Milo-Tracing-Framework · PHP · 313 lines · 145 code · 31 blank · 137 comment · 27 complexity · 0f2ef2e24edd2f89c7d36438eacbb875 MD5 · raw file
- <?php
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- * @package thrift.transport
- */
- /**
- * Sockets implementation of the TTransport interface.
- *
- * @package thrift.transport
- */
- class TSocket extends TTransport {
- /**
- * Handle to PHP socket
- *
- * @var resource
- */
- private $handle_ = null;
- /**
- * Remote hostname
- *
- * @var string
- */
- protected $host_ = 'localhost';
- /**
- * Remote port
- *
- * @var int
- */
- protected $port_ = '9090';
- /**
- * Send timeout in milliseconds
- *
- * @var int
- */
- private $sendTimeout_ = 100;
- /**
- * Recv timeout in milliseconds
- *
- * @var int
- */
- private $recvTimeout_ = 750;
- /**
- * Is send timeout set?
- *
- * @var bool
- */
- private $sendTimeoutSet_ = FALSE;
- /**
- * Persistent socket or plain?
- *
- * @var bool
- */
- private $persist_ = FALSE;
- /**
- * Debugging on?
- *
- * @var bool
- */
- protected $debug_ = FALSE;
- /**
- * Debug handler
- *
- * @var mixed
- */
- protected $debugHandler_ = null;
- /**
- * Socket constructor
- *
- * @param string $host Remote hostname
- * @param int $port Remote port
- * @param bool $persist Whether to use a persistent socket
- * @param string $debugHandler Function to call for error logging
- */
- public function __construct($host='localhost',
- $port=9090,
- $persist=FALSE,
- $debugHandler=null) {
- $this->host_ = $host;
- $this->port_ = $port;
- $this->persist_ = $persist;
- $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log';
- }
- /**
- * Sets the send timeout.
- *
- * @param int $timeout Timeout in milliseconds.
- */
- public function setSendTimeout($timeout) {
- $this->sendTimeout_ = $timeout;
- }
- /**
- * Sets the receive timeout.
- *
- * @param int $timeout Timeout in milliseconds.
- */
- public function setRecvTimeout($timeout) {
- $this->recvTimeout_ = $timeout;
- }
- /**
- * Sets debugging output on or off
- *
- * @param bool $debug
- */
- public function setDebug($debug) {
- $this->debug_ = $debug;
- }
- /**
- * Get the host that this socket is connected to
- *
- * @return string host
- */
- public function getHost() {
- return $this->host_;
- }
- /**
- * Get the remote port that this socket is connected to
- *
- * @return int port
- */
- public function getPort() {
- return $this->port_;
- }
- /**
- * Tests whether this is open
- *
- * @return bool true if the socket is open
- */
- public function isOpen() {
- return is_resource($this->handle_);
- }
- /**
- * Connects the socket.
- */
- public function open() {
- if ($this->persist_) {
- $this->handle_ = @pfsockopen($this->host_,
- $this->port_,
- $errno,
- $errstr,
- $this->sendTimeout_/1000.0);
- } else {
- $this->handle_ = @fsockopen($this->host_,
- $this->port_,
- $errno,
- $errstr,
- $this->sendTimeout_/1000.0);
- }
- // Connect failed?
- if ($this->handle_ === FALSE) {
- $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])';
- if ($this->debug_) {
- call_user_func($this->debugHandler_, $error);
- }
- throw new TException($error);
- }
- stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000);
- $this->sendTimeoutSet_ = TRUE;
- }
- /**
- * Closes the socket.
- */
- public function close() {
- if (!$this->persist_) {
- @fclose($this->handle_);
- $this->handle_ = null;
- }
- }
- /**
- * Uses stream get contents to do the reading
- *
- * @param int $len How many bytes
- * @return string Binary data
- */
- public function readAll($len) {
- // This call does not obey stream_set_timeout values!
- // $buf = @stream_get_contents($this->handle_, $len);
- $pre = null;
- while (TRUE) {
- $read = array($this->handle_);
- $write = NULL;
- $except = NULL;
- $nc = @stream_select($read, $write, $except, 0, $this->recvTimeout_*1000);
- if ($nc === false || $nc == 0) {
- throw new TException('TSocket: timed out reading '.$len.' bytes from '.
- $this->host_.':'.$this->port_);
- } elseif ($nc > 0) {
- $buf = @stream_socket_recvfrom($this->handle_, $len);
- if ($buf === FALSE || $buf === '') {
- throw new TException('TSocket: Could not read '.$len.' bytes from '.
- $this->host_.':'.$this->port_);
- }
- if (($sz = mb_strlen($buf,'8bit')) < $len) {
- $pre .= $buf;
- $len -= $sz;
- } else {
- return $pre.$buf;
- }
- }
- }
- }
- /**
- * Read from the socket
- *
- * @param int $len How many bytes
- * @return string Binary data
- */
- public function read($len) {
- if (!is_resource($this->handle_)) {
- throw new TException('Invalid handle, could not read from socket');
- }
- $read = array($this->handle_);
- $write = NULL;
- $except = NULL;
- $nc = @stream_select($read, $write, $except, 0, $this->recvTimeout_*1000);
- if ($nc === false || $nc == 0) {
- throw new TException('TSocket: timed out reading '.$len.' bytes from '.
- $this->host_.':'.$this->port_);
- } elseif ($nc > 0) {
- $data = @stream_socket_recvfrom($this->handle_, $len);
- if ($data === FALSE || $data === '') {
- throw new TException('TSocket: Could not read '.$len.' bytes from '.
- $this->host_.':'.$this->port_);
- }
- }
- return $data;
- }
- /**
- * Write to the socket.
- *
- * @param string $buf The data to write
- */
- public function write($buf) {
- if (!is_resource($this->handle_)) {
- throw new TException('Invalid handle, could not write to socket');
- }
- if (!$this->sendTimeoutSet_) {
- stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000);
- $this->sendTimeoutSet_ = TRUE;
- }
- while (mb_strlen($buf,'8bit') > 0) {
- $got = @fwrite($this->handle_, $buf);
- if ($got === 0 || $got === FALSE) {
- $md = stream_get_meta_data($this->handle_);
- if ($md['timed_out']) {
- throw new TException('TSocket: timed out writing '.mb_strlen($buf,'8bit').' bytes from '.
- $this->host_.':'.$this->port_);
- } else {
- throw new TException('TSocket: Could not write '.mb_strlen($buf,'8bit').' bytes '.
- $this->host_.':'.$this->port_);
- }
- }
- $buf = mb_substr($buf, $got, mb_strlen($buf, '8bit'), '8bit');
- }
- }
- /**
- * Flush output to the socket.
- */
- public function flush() {
- $ret = fflush($this->handle_);
- if ($ret === FALSE) {
- throw new TException('TSocket: Could not flush: '.
- $this->host_.':'.$this->port_);
- }
- }
- }
- ?>