Ignore:
Timestamp:
2013/08/28 13:55:43 (11 years ago)
Author:
m_uehara
Message:

#2348 r23140 をマージ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_13_0/data/module/Net/Socket.php

    r23126 r23143  
    55 * PHP Version 4 
    66 * 
    7  * Copyright (c) 1997-2013 The PHP Group 
     7 * Copyright (c) 1997-2003 The PHP Group 
    88 * 
    99 * This source file is subject to version 2.0 of the PHP license, 
     
    2424 * @copyright 1997-2003 The PHP Group 
    2525 * @license   http://www.php.net/license/2_02.txt PHP 2.02 
     26 * @version   CVS: $Id$ 
    2627 * @link      http://pear.php.net/packages/Net_Socket 
    2728 */ 
     
    7778 
    7879    /** 
    79      * Number of seconds to wait on socket operations before assuming 
     80     * Number of seconds to wait on socket connections before assuming 
    8081     * there's no more data. Defaults to no timeout. 
    81      * @var integer|float $timeout 
    82      */ 
    83     var $timeout = null; 
     82     * @var integer $timeout 
     83     */ 
     84    var $timeout = false; 
    8485 
    8586    /** 
     
    100101     * already connected, it disconnects and connects again. 
    101102     * 
    102      * @param string  $addr       IP address or host name (may be with protocol prefix). 
     103     * @param string  $addr       IP address or host name. 
    103104     * @param integer $port       TCP port number. 
    104105     * @param boolean $persistent (optional) Whether the connection is 
    105106     *                            persistent (kept open between requests 
    106107     *                            by the web server). 
    107      * @param integer $timeout    (optional) Connection socket timeout. 
     108     * @param integer $timeout    (optional) How long to wait for data. 
    108109     * @param array   $options    See options for stream_context_create. 
    109110     * 
    110111     * @access public 
    111112     * 
    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. 
    113114     */ 
    114115    function connect($addr, $port = 0, $persistent = null, 
     
    122123        if (!$addr) { 
    123124            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; 
    126128        } else { 
    127             $this->addr = $addr; 
     129            $this->addr = @gethostbyname($addr); 
    128130        } 
    129131 
     
    132134        if ($persistent !== null) { 
    133135            $this->persistent = $persistent; 
     136        } 
     137 
     138        if ($timeout !== null) { 
     139            $this->timeout = $timeout; 
    134140        } 
    135141 
     
    140146        $old_track_errors = @ini_set('track_errors', 1); 
    141147 
    142         if ($timeout <= 0) { 
    143             $timeout = @ini_get('default_socket_timeout'); 
    144         } 
    145  
    146148        if ($options && function_exists('stream_context_create')) { 
     149            if ($this->timeout) { 
     150                $timeout = $this->timeout; 
     151            } else { 
     152                $timeout = 0; 
     153            } 
    147154            $context = stream_context_create($options); 
    148155 
     
    163170            } 
    164171        } 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            } 
    166178        } 
    167179 
     
    176188        @ini_set('track_errors', $old_track_errors); 
    177189        $this->fp = $fp; 
    178         $this->setTimeout(); 
     190 
    179191        return $this->setBlocking($this->blocking); 
    180192    } 
     
    247259     * 
    248260     * @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); 
    274273    } 
    275274 
     
    317316        } 
    318317 
    319         return stream_get_meta_data($this->fp); 
     318        return socket_get_status($this->fp); 
    320319    } 
    321320 
     
    323322     * Get a specified line of data 
    324323     * 
    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. 
    333329     */ 
    334330    function gets($size = null) 
     
    375371     * @access public 
    376372     * @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 
    379375     *               If the write fails, returns false. 
    380      *               If the socket times out, returns an instance of PEAR_Error. 
    381376     */ 
    382377    function write($data, $blocksize = null) 
     
    387382 
    388383        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); 
    405385        } else { 
    406386            if (is_null($blocksize)) { 
     
    412392            while ($pos < $size) { 
    413393                $written = @fwrite($this->fp, substr($data, $pos, $blocksize)); 
    414  
    415                 // Check for timeout or lost connection 
    416394                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  
    427395                    return $written; 
    428396                } 
    429  
    430397                $pos += $written; 
    431398            } 
     
    441408     * 
    442409     * @access public 
    443      * @return mixed fwrite() result, or PEAR_Error when not connected 
     410     * @return mixed fputs result, or an error 
    444411     */ 
    445412    function writeLine($data) 
Note: See TracChangeset for help on using the changeset viewer.