Changeset 23125 for branches/version-2_13-dev/data/module/Net/Socket.php
- Timestamp:
- 2013/08/26 12:42:34 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13-dev/data/module/Net/Socket.php
r20998 r23125 5 5 * PHP Version 4 6 6 * 7 * Copyright (c) 1997-20 03 The PHP Group7 * Copyright (c) 1997-2013 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$27 26 * @link http://pear.php.net/packages/Net_Socket 28 27 */ … … 78 77 79 78 /** 80 * Number of seconds to wait on socket connections before assuming79 * Number of seconds to wait on socket operations before assuming 81 80 * there's no more data. Defaults to no timeout. 82 * @var integer $timeout83 */ 84 var $timeout = false;81 * @var integer|float $timeout 82 */ 83 var $timeout = null; 85 84 86 85 /** … … 101 100 * already connected, it disconnects and connects again. 102 101 * 103 * @param string $addr IP address or host name .102 * @param string $addr IP address or host name (may be with protocol prefix). 104 103 * @param integer $port TCP port number. 105 104 * @param boolean $persistent (optional) Whether the connection is 106 105 * persistent (kept open between requests 107 106 * by the web server). 108 * @param integer $timeout (optional) How long to wait for data.107 * @param integer $timeout (optional) Connection socket timeout. 109 108 * @param array $options See options for stream_context_create. 110 109 * 111 110 * @access public 112 111 * 113 * @return boolean |PEAR_Error True on success or a PEAR_Error on failure.112 * @return boolean|PEAR_Error True on success or a PEAR_Error on failure. 114 113 */ 115 114 function connect($addr, $port = 0, $persistent = null, … … 123 122 if (!$addr) { 124 123 return $this->raiseError('$addr cannot be empty'); 125 } elseif (strspn($addr, '.0123456789') == strlen($addr) || 126 strstr($addr, '/') !== false) { 124 } else if (strspn($addr, ':.0123456789') == strlen($addr)) { 125 $this->addr = strpos($addr, ':') !== false ? '['.$addr.']' : $addr; 126 } else { 127 127 $this->addr = $addr; 128 } else {129 $this->addr = @gethostbyname($addr);130 128 } 131 129 … … 134 132 if ($persistent !== null) { 135 133 $this->persistent = $persistent; 136 }137 138 if ($timeout !== null) {139 $this->timeout = $timeout;140 134 } 141 135 … … 146 140 $old_track_errors = @ini_set('track_errors', 1); 147 141 142 if ($timeout <= 0) { 143 $timeout = @ini_get('default_socket_timeout'); 144 } 145 148 146 if ($options && function_exists('stream_context_create')) { 149 if ($this->timeout) {150 $timeout = $this->timeout;151 } else {152 $timeout = 0;153 }154 147 $context = stream_context_create($options); 155 148 … … 170 163 } 171 164 } else { 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 } 165 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout); 178 166 } 179 167 … … 188 176 @ini_set('track_errors', $old_track_errors); 189 177 $this->fp = $fp; 190 178 $this->setTimeout(); 191 179 return $this->setBlocking($this->blocking); 192 180 } … … 259 247 * 260 248 * @param integer $seconds Seconds. 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); 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 } 273 274 } 274 275 … … 316 317 } 317 318 318 return s ocket_get_status($this->fp);319 return stream_get_meta_data($this->fp); 319 320 } 320 321 … … 322 323 * Get a specified line of data 323 324 * 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. 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. 329 333 */ 330 334 function gets($size = null) … … 371 375 * @access public 372 376 * @return mixed If the socket is not connected, returns an instance of 373 * PEAR_Error 374 * If the write succeeds, returns the number of bytes written 377 * PEAR_Error. 378 * If the write succeeds, returns the number of bytes written. 375 379 * If the write fails, returns false. 380 * If the socket times out, returns an instance of PEAR_Error. 376 381 */ 377 382 function write($data, $blocksize = null) … … 382 387 383 388 if (is_null($blocksize) && !OS_WINDOWS) { 384 return @fwrite($this->fp, $data); 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; 385 405 } else { 386 406 if (is_null($blocksize)) { … … 392 412 while ($pos < $size) { 393 413 $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); 414 415 // Check for timeout or lost connection 394 416 if (!$written) { 417 $meta_data = $this->getStatus(); 418 419 if (!is_array($meta_data)) { 420 return $meta_data; // PEAR_Error 421 } 422 423 if (!empty($meta_data['timed_out'])) { 424 return $this->raiseError('timed out'); 425 } 426 395 427 return $written; 396 428 } 429 397 430 $pos += $written; 398 431 } … … 408 441 * 409 442 * @access public 410 * @return mixed f puts result, or an error443 * @return mixed fwrite() result, or PEAR_Error when not connected 411 444 */ 412 445 function writeLine($data)
Note: See TracChangeset
for help on using the changeset viewer.