| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php | 
|  | 2 |  | 
|  | 3 | /** | 
|  | 4 | * Sockets implementation of the TTransport interface. | 
|  | 5 | * | 
|  | 6 | * @package thrift.transport | 
|  | 7 | * @author Mark Slee <mcslee@facebook.com> | 
|  | 8 | */ | 
|  | 9 | class TSocket extends TTransport { | 
|  | 10 |  | 
|  | 11 | /** | 
|  | 12 | * Handle to PHP socket | 
|  | 13 | * | 
|  | 14 | * @var resource | 
|  | 15 | */ | 
|  | 16 | private $handle_ = null; | 
|  | 17 |  | 
|  | 18 | /** | 
|  | 19 | * Remote hostname | 
|  | 20 | * | 
|  | 21 | * @var string | 
|  | 22 | */ | 
|  | 23 | private $host_ = 'localhost'; | 
|  | 24 |  | 
|  | 25 | /** | 
|  | 26 | * Remote port | 
|  | 27 | * | 
|  | 28 | * @var int | 
|  | 29 | */ | 
|  | 30 | private $port_ = '9090'; | 
|  | 31 |  | 
|  | 32 | /** | 
|  | 33 | * Persistent socket or plain? | 
|  | 34 | * | 
|  | 35 | * @var bool | 
|  | 36 | */ | 
|  | 37 | private $persist_ = false; | 
|  | 38 |  | 
|  | 39 | /** | 
|  | 40 | * Socket constructor | 
|  | 41 | * | 
|  | 42 | * @param string $host    Remote hostname | 
|  | 43 | * @param int    $port    Remote port | 
|  | 44 | * @param bool   $persist Whether to use a persistent socket | 
|  | 45 | */ | 
|  | 46 | public function __construct($host='localhost', $port=9090, $persist=false) { | 
|  | 47 | $this->host_ = $host; | 
|  | 48 | $this->port_ = $port; | 
|  | 49 | $this->persist_ = $persist; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | /** | 
|  | 53 | * Tests whether this is open | 
|  | 54 | * | 
|  | 55 | * @return bool true if the socket is open | 
|  | 56 | */ | 
|  | 57 | public function isOpen() { | 
|  | 58 | return is_resource($this->handle_); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | /** | 
|  | 62 | * Connects the socket. | 
|  | 63 | */ | 
|  | 64 | public function open() { | 
|  | 65 | if ($this->persist_) { | 
|  | 66 | $this->handle_ = pfsockopen($this->host_, $this->port_); | 
|  | 67 | } else { | 
|  | 68 | $this->handle_ = fsockopen($this->host_, $this->port_); | 
|  | 69 | } | 
|  | 70 | if ($this->handle_ === FALSE) { | 
|  | 71 | throw new Exception('TSocket: Could not connect to '. | 
|  | 72 | $this->host_.':'.$this->port_); | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | /** | 
|  | 77 | * Closes the socket | 
|  | 78 | */ | 
|  | 79 | public function close() { | 
|  | 80 | if (!$this->persist_) { | 
|  | 81 | fclose($this->handle_); | 
|  | 82 | } | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | /** | 
|  | 86 | * Uses stream get contents to do the reading | 
|  | 87 | */ | 
|  | 88 | public function readAll($len) { | 
|  | 89 | return stream_get_contents($this->handle_, $len); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | /** | 
|  | 93 | * Read from the socket | 
|  | 94 | */ | 
|  | 95 | public function read($len) { | 
|  | 96 | $data = fread($this->handle_, 1); | 
|  | 97 | if ($data === FALSE) { | 
|  | 98 | throw new Exception('TSocket: Could not read '.$len.' bytes from '. | 
|  | 99 | $this->host_.':'.$this->port_); | 
|  | 100 | } | 
|  | 101 | return $data; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | /** | 
|  | 105 | * Write to the socket. | 
|  | 106 | */ | 
|  | 107 | public function write($buf) { | 
|  | 108 | while (!empty($buf)) { | 
|  | 109 | $got = fwrite($this->handle_, $buf); | 
|  | 110 | if ($got == false) { | 
|  | 111 | throw new Exception('TSocket: Could not write '.strlen($buf).' bytes '. | 
|  | 112 | $this->host_.':'.$this->port_); | 
|  | 113 | } | 
|  | 114 | $buf = substr($buf, $got); | 
|  | 115 | } | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | /** | 
|  | 119 | * Flush output to the socket. | 
|  | 120 | */ | 
|  | 121 | public function flush() { | 
|  | 122 | fflush($this->handle_); | 
|  | 123 | } | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | ?> |