1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19:
20: class TSocket extends TTransport {
21:
22: 23: 24: 25: 26:
27: private $handle_ = null;
28:
29: 30: 31: 32: 33:
34: protected $host_ = 'localhost';
35:
36: 37: 38: 39: 40:
41: protected $port_ = '9090';
42:
43: 44: 45: 46: 47:
48: private $sendTimeout_ = 100;
49:
50: 51: 52: 53: 54:
55: private $recvTimeout_ = 750;
56:
57: 58: 59: 60: 61:
62: private $sendTimeoutSet_ = FALSE;
63:
64: 65: 66: 67: 68:
69: private $persist_ = FALSE;
70:
71: 72: 73: 74: 75:
76: protected $debug_ = FALSE;
77:
78: 79: 80: 81: 82:
83: protected $debugHandler_ = null;
84:
85: 86: 87: 88: 89: 90: 91: 92:
93: public function __construct($host='localhost',
94: $port=9090,
95: $persist=FALSE,
96: $debugHandler=null) {
97: $this->host_ = $host;
98: $this->port_ = $port;
99: $this->persist_ = $persist;
100: $this->debugHandler_ = $debugHandler ? $debugHandler : 'error_log';
101: }
102:
103: 104: 105: 106: 107:
108: public function setSendTimeout($timeout) {
109: $this->sendTimeout_ = $timeout;
110: }
111:
112: 113: 114: 115: 116:
117: public function setRecvTimeout($timeout) {
118: $this->recvTimeout_ = $timeout;
119: }
120:
121: 122: 123: 124: 125:
126: public function setDebug($debug) {
127: $this->debug_ = $debug;
128: }
129:
130: 131: 132: 133: 134:
135: public function getHost() {
136: return $this->host_;
137: }
138:
139: 140: 141: 142: 143:
144: public function getPort() {
145: return $this->port_;
146: }
147:
148: 149: 150: 151: 152:
153: public function isOpen() {
154: return is_resource($this->handle_);
155: }
156:
157: 158: 159:
160: public function open() {
161:
162: if ($this->persist_) {
163: $this->handle_ = @pfsockopen($this->host_,
164: $this->port_,
165: $errno,
166: $errstr,
167: $this->sendTimeout_/1000.0);
168: } else {
169: $this->handle_ = @fsockopen($this->host_,
170: $this->port_,
171: $errno,
172: $errstr,
173: $this->sendTimeout_/1000.0);
174: }
175:
176:
177: if ($this->handle_ === FALSE) {
178: $error = 'TSocket: Could not connect to '.$this->host_.':'.$this->port_.' ('.$errstr.' ['.$errno.'])';
179: if ($this->debug_) {
180: call_user_func($this->debugHandler_, $error);
181: }
182: throw new TException($error);
183: }
184:
185: stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000);
186: $this->sendTimeoutSet_ = TRUE;
187: }
188:
189: 190: 191:
192: public function close() {
193: if (!$this->persist_) {
194: @fclose($this->handle_);
195: $this->handle_ = null;
196: }
197: }
198:
199: 200: 201: 202: 203: 204:
205: public function readAll($len) {
206: if ($this->sendTimeoutSet_) {
207: stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000);
208: $this->sendTimeoutSet_ = FALSE;
209: }
210:
211:
212:
213: $pre = null;
214: while (TRUE) {
215: $buf = @fread($this->handle_, $len);
216: if ($buf === FALSE || $buf === '') {
217: $md = stream_get_meta_data($this->handle_);
218: if ($md['timed_out']) {
219: throw new TException('TSocket: timed out reading '.$len.' bytes from '.
220: $this->host_.':'.$this->port_);
221: } else {
222: throw new TException('TSocket: Could not read '.$len.' bytes from '.
223: $this->host_.':'.$this->port_);
224: }
225: } else if (($sz = strlen($buf)) < $len) {
226: $md = stream_get_meta_data($this->handle_);
227: if ($md['timed_out']) {
228: throw new TException('TSocket: timed out reading '.$len.' bytes from '.
229: $this->host_.':'.$this->port_);
230: } else {
231: $pre .= $buf;
232: $len -= $sz;
233: }
234: } else {
235: return $pre.$buf;
236: }
237: }
238: }
239:
240: 241: 242: 243: 244: 245:
246: public function read($len) {
247: if ($this->sendTimeoutSet_) {
248: stream_set_timeout($this->handle_, 0, $this->recvTimeout_*1000);
249: $this->sendTimeoutSet_ = FALSE;
250: }
251: $data = @fread($this->handle_, $len);
252: if ($data === FALSE || $data === '') {
253: $md = stream_get_meta_data($this->handle_);
254: if ($md['timed_out']) {
255: throw new TException('TSocket: timed out reading '.$len.' bytes from '.
256: $this->host_.':'.$this->port_);
257: } else {
258: throw new TException('TSocket: Could not read '.$len.' bytes from '.
259: $this->host_.':'.$this->port_);
260: }
261: }
262: return $data;
263: }
264:
265: 266: 267: 268: 269:
270: public function write($buf) {
271: if (!$this->sendTimeoutSet_) {
272: stream_set_timeout($this->handle_, 0, $this->sendTimeout_*1000);
273: $this->sendTimeoutSet_ = TRUE;
274: }
275: while (strlen($buf) > 0) {
276: $got = @fwrite($this->handle_, $buf);
277: if ($got === 0 || $got === FALSE) {
278: $md = stream_get_meta_data($this->handle_);
279: if ($md['timed_out']) {
280: throw new TException('TSocket: timed out writing '.strlen($buf).' bytes from '.
281: $this->host_.':'.$this->port_);
282: } else {
283: throw new TException('TSocket: Could not write '.strlen($buf).' bytes '.
284: $this->host_.':'.$this->port_);
285: }
286: }
287: $buf = substr($buf, $got);
288: }
289: }
290:
291: 292: 293:
294: public function flush() {
295: $ret = fflush($this->handle_);
296: if ($ret === FALSE) {
297: throw new TException('TSocket: Could not flush: '.
298: $this->host_.':'.$this->port_);
299: }
300: }
301: }
302:
303: ?>
304: