Changeset 23143 for branches/version-2_13_0/data/module/Net/Socket.php
- Timestamp:
- 2013/08/28 13:55:43 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13_0/data/module/Net/Socket.php
r23126 r23143 5 5 * PHP Version 4 6 6 * 7 * Copyright (c) 1997-20 13 The PHP Group7 * Copyright (c) 1997-2003 The PHP Group 8 8 * 9 9 * This source file is subject to version 2.0 of the PHP license, … … 24 24 * @copyright 1997-2003 The PHP Group 25 25 * @license http://www.php.net/license/2_02.txt PHP 2.02 26 * @version CVS: $Id$ 26 27 * @link http://pear.php.net/packages/Net_Socket 27 28 */ … … 77 78 78 79 /** 79 * Number of seconds to wait on socket operations before assuming80 * Number of seconds to wait on socket connections before assuming 80 81 * there's no more data. Defaults to no timeout. 81 * @var integer |float$timeout82 */ 83 var $timeout = null;82 * @var integer $timeout 83 */ 84 var $timeout = false; 84 85 85 86 /** … … 100 101 * already connected, it disconnects and connects again. 101 102 * 102 * @param string $addr IP address or host name (may be with protocol prefix).103 * @param string $addr IP address or host name. 103 104 * @param integer $port TCP port number. 104 105 * @param boolean $persistent (optional) Whether the connection is 105 106 * persistent (kept open between requests 106 107 * by the web server). 107 * @param integer $timeout (optional) Connection socket timeout.108 * @param integer $timeout (optional) How long to wait for data. 108 109 * @param array $options See options for stream_context_create. 109 110 * 110 111 * @access public 111 112 * 112 * @return boolean |PEAR_Error True on success or a PEAR_Error on failure.113 * @return boolean | PEAR_Error True on success or a PEAR_Error on failure. 113 114 */ 114 115 function connect($addr, $port = 0, $persistent = null, … … 122 123 if (!$addr) { 123 124 return $this->raiseError('$addr cannot be empty'); 124 } else if (strspn($addr, ':.0123456789') == strlen($addr)) { 125 $this->addr = strpos($addr, ':') !== false ? '['.$addr.']' : $addr; 125 } elseif (strspn($addr, '.0123456789') == strlen($addr) || 126 strstr($addr, '/') !== false) { 127 $this->addr = $addr; 126 128 } else { 127 $this->addr = $addr;129 $this->addr = @gethostbyname($addr); 128 130 } 129 131 … … 132 134 if ($persistent !== null) { 133 135 $this->persistent = $persistent; 136 } 137 138 if ($timeout !== null) { 139 $this->timeout = $timeout; 134 140 } 135 141 … … 140 146 $old_track_errors = @ini_set('track_errors', 1); 141 147 142 if ($timeout <= 0) {143 $timeout = @ini_get('default_socket_timeout');144 }145 146 148 if ($options && function_exists('stream_context_create')) { 149 if ($this->timeout) { 150 $timeout = $this->timeout; 151 } else { 152 $timeout = 0; 153 } 147 154 $context = stream_context_create($options); 148 155 … … 163 170 } 164 171 } else { 165 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout); 172 if ($this->timeout) { 173 $fp = @$openfunc($this->addr, $this->port, $errno, 174 $errstr, $this->timeout); 175 } else { 176 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr); 177 } 166 178 } 167 179 … … 176 188 @ini_set('track_errors', $old_track_errors); 177 189 $this->fp = $fp; 178 $this->setTimeout(); 190 179 191 return $this->setBlocking($this->blocking); 180 192 } … … 247 259 * 248 260 * @param integer $seconds Seconds. 249 * @param integer $microseconds Microseconds, optional. 250 * 251 * @access public 252 * @return mixed True on success or false on failure or 253 * a PEAR_Error instance when not connected 254 */ 255 function setTimeout($seconds = null, $microseconds = null) 256 { 257 if (!is_resource($this->fp)) { 258 return $this->raiseError('not connected'); 259 } 260 261 if ($seconds === null && $microseconds === null) { 262 $seconds = (int) $this->timeout; 263 $microseconds = (int) (($this->timeout - $seconds) * 1000000); 264 } else { 265 $this->timeout = $seconds + $microseconds/1000000; 266 } 267 268 if ($this->timeout > 0) { 269 return stream_set_timeout($this->fp, (int) $seconds, (int) $microseconds); 270 } 271 else { 272 return false; 273 } 261 * @param integer $microseconds Microseconds. 262 * 263 * @access public 264 * @return mixed true on success or a PEAR_Error instance otherwise 265 */ 266 function setTimeout($seconds, $microseconds) 267 { 268 if (!is_resource($this->fp)) { 269 return $this->raiseError('not connected'); 270 } 271 272 return socket_set_timeout($this->fp, $seconds, $microseconds); 274 273 } 275 274 … … 317 316 } 318 317 319 return s tream_get_meta_data($this->fp);318 return socket_get_status($this->fp); 320 319 } 321 320 … … 323 322 * Get a specified line of data 324 323 * 325 * @param int $size Reading ends when size - 1 bytes have been read, 326 * or a newline or an EOF (whichever comes first). 327 * If no size is specified, it will keep reading from 328 * the stream until it reaches the end of the line. 329 * 330 * @access public 331 * @return mixed $size bytes of data from the socket, or a PEAR_Error if 332 * not connected. If an error occurs, FALSE is returned. 324 * @param int $size ?? 325 * 326 * @access public 327 * @return $size bytes of data from the socket, or a PEAR_Error if 328 * not connected. 333 329 */ 334 330 function gets($size = null) … … 375 371 * @access public 376 372 * @return mixed If the socket is not connected, returns an instance of 377 * PEAR_Error .378 * If the write succeeds, returns the number of bytes written .373 * PEAR_Error 374 * If the write succeeds, returns the number of bytes written 379 375 * If the write fails, returns false. 380 * If the socket times out, returns an instance of PEAR_Error.381 376 */ 382 377 function write($data, $blocksize = null) … … 387 382 388 383 if (is_null($blocksize) && !OS_WINDOWS) { 389 $written = @fwrite($this->fp, $data); 390 391 // Check for timeout or lost connection 392 if (!$written) { 393 $meta_data = $this->getStatus(); 394 395 if (!is_array($meta_data)) { 396 return $meta_data; // PEAR_Error 397 } 398 399 if (!empty($meta_data['timed_out'])) { 400 return $this->raiseError('timed out'); 401 } 402 } 403 404 return $written; 384 return @fwrite($this->fp, $data); 405 385 } else { 406 386 if (is_null($blocksize)) { … … 412 392 while ($pos < $size) { 413 393 $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); 414 415 // Check for timeout or lost connection416 394 if (!$written) { 417 $meta_data = $this->getStatus();418 419 if (!is_array($meta_data)) {420 return $meta_data; // PEAR_Error421 }422 423 if (!empty($meta_data['timed_out'])) {424 return $this->raiseError('timed out');425 }426 427 395 return $written; 428 396 } 429 430 397 $pos += $written; 431 398 } … … 441 408 * 442 409 * @access public 443 * @return mixed f write() result, or PEAR_Error when not connected410 * @return mixed fputs result, or an error 444 411 */ 445 412 function writeLine($data)
Note: See TracChangeset
for help on using the changeset viewer.